View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.phoenix.common;
19  
20  import java.net.InetAddress;
21  import java.net.UnknownHostException;
22  import java.security.AccessControlException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import junit.framework.TestCase;
26  import net.jini.export.ServerContext;
27  import net.jini.io.context.ClientHost;
28  import org.apache.river.resource.ServiceConfigurationError;
29  
30  public class LocalAccessTest extends TestCase {
31  
32      public void testNoServerContext() {
33          try {
34              LocalAccess.check();
35          } catch(AccessControlException e) {
36              fail("unexpected: " + e);
37          } catch(ServiceConfigurationError e){
38  	    // Ignore
39  	}
40      }
41  
42      public void testNoClientHost() {
43          ServerContext.doWithServerContext(new Runnable() {
44              public void run() {
45                  try {
46                      LocalAccess.check();
47                  }
48                  catch(AccessControlException e) {
49                      fail("unexpected: " + e);
50                  }
51              }
52          }, new ArrayList(0));
53          
54      }
55  
56      public void testLocalClientHost() throws UnknownHostException {
57          Collection col = new ArrayList(1);
58          col.add(new ClientHost() {
59              public InetAddress getClientHost() {
60                  try {
61                      return InetAddress.getLocalHost();
62                  }
63                  catch(UnknownHostException e) {
64                      return null;
65                  }
66              }
67          });
68          ServerContext.doWithServerContext(new Runnable() {
69              public void run() {
70                  try {
71                      LocalAccess.check();
72                  }
73                  catch(AccessControlException e) {
74                      fail("unexpected: " + e);
75                  }
76              }
77          }, col);
78      }
79  
80      public void testLoopBackClientHost() {
81          Collection col = new ArrayList(1);
82          col.add(new ClientHost() {
83              public InetAddress getClientHost() {
84                  try {
85                      return InetAddress.getByName("127.0.1.1");
86                  }
87                  catch(UnknownHostException e) {
88                      return null;
89                  }
90              }
91          });
92          ServerContext.doWithServerContext(new Runnable() {
93              public void run() {
94                  try {
95                      LocalAccess.check();
96                  }
97                  catch(AccessControlException e) {
98                      fail("unexpected: " + e);
99                  }
100             }
101         }, col);
102         
103     }
104 
105     public void testRemoteClientHost() {
106         Collection col = new ArrayList(1);
107         col.add(new ClientHost() {
108             public InetAddress getClientHost() {
109                 try {
110                     return InetAddress.getByName("www.apache.org");
111                 }
112                 catch(UnknownHostException e) {
113                     return null;
114                 }
115             }
116         });
117         ServerContext.doWithServerContext(new Runnable() {
118             public void run() {
119                 try {
120                     LocalAccess.check();
121                 }
122                 catch(AccessControlException e) {
123                     return;
124                 }
125                 fail("expected AccessControlException not thrown");
126             }
127         }, col);
128         
129     }
130 
131 }