001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Aleksei Y. Semenov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests;
022:
023: import java.security.Identity;
024: import java.security.IdentityScope;
025: import java.security.KeyManagementException;
026: import java.util.Enumeration;
027:
028: import org.apache.harmony.security.SystemScope;
029: import org.apache.harmony.security.tests.support.IdentityScopeStub;
030: import org.apache.harmony.security.tests.support.PublicKeyStub;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * Unit test for class org.apache.harmony.security.SystemScope
036: *
037: */
038:
039: public class SystemScopeTest extends TestCase {
040:
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(SystemScopeTest.class);
043: }
044:
045: IdentityScope ss = null;
046: final static boolean mode = true;
047:
048: /*
049: * @see TestCase#setUp()
050: */
051: protected void setUp() throws Exception {
052: super .setUp();
053: if (mode)
054: ss = new SystemScope("SystemScope");
055: else {
056: ss = IdentityScope.getSystemScope();
057: Enumeration e = ss.identities();
058: while (e.hasMoreElements())
059: ss.removeIdentity((Identity) e.nextElement());
060: }
061: }
062:
063: /**
064: * Constructor for SystemScopeTest.
065: * @param arg0
066: */
067: public SystemScopeTest(String arg0) {
068: super (arg0);
069: }
070:
071: /**
072: * verify SystemScope.size() returns number of Identities
073: */
074:
075: public void testSize() throws Exception {
076: //SystemScope ss = new SystemScope("SystemScope");
077: assertEquals(0, ss.size());
078: ss.addIdentity(new IdentityScopeStub("aaa"));
079: assertEquals(1, ss.size());
080: ss.addIdentity(new IdentityScopeStub("bbb"));
081: assertEquals(2, ss.size());
082: }
083:
084: /*
085: * verify getIdentity(String) returns requested identity or null if not found
086: */
087: public void testGetIdentityString() throws Exception {
088: //SystemScope ss = new SystemScope("SystemScope");
089: assertNull(ss.getIdentity("aaa"));
090:
091: java.security.Identity aaa = new IdentityScopeStub("aaa");
092: ss.addIdentity(aaa);
093: assertSame(aaa, ss.getIdentity(aaa.getName()));
094: assertNull(ss.getIdentity("bbb"));
095:
096: }
097:
098: /*
099: * verify getIdentity(PublicKey) returns requested identity or null if not found
100: */
101: public void testGetIdentityPublicKey() throws Exception {
102: //SystemScope ss = new SystemScope("SystemScope");
103: java.security.PublicKey kkk = new PublicKeyStub("kkk", "fff",
104: null);
105: assertNull(ss.getIdentity(kkk));
106: java.security.Identity aaa = new IdentityScopeStub("aaa");
107: aaa.setPublicKey(kkk);
108: ss.addIdentity(aaa);
109:
110: assertSame(aaa, ss.getIdentity(kkk));
111: }
112:
113: /**
114: * verify that only one identity with given name or public key can be added
115: * i.e. KeyManagementException is thrown
116: */
117: public void testAddIdentity() throws Exception {
118: // SystemScope ss = new SystemScope("SystemScope");
119: java.security.PublicKey kkk = new PublicKeyStub("kkk", "fff",
120: null);
121: java.security.Identity aaa = new IdentityScopeStub("aaa");
122: aaa.setPublicKey(kkk);
123: ss.addIdentity(aaa);
124:
125: java.security.Identity bbb = new IdentityScopeStub("aaa");
126: try {
127: ss.addIdentity(bbb);
128: fail("KeyManagementException should be thrown for already used name");
129: } catch (KeyManagementException ok) {
130: }
131:
132: java.security.Identity ccc = new IdentityScopeStub("ccc");
133: ccc.setPublicKey(kkk);
134: try {
135: ss.addIdentity(ccc);
136: fail("KeyManagementException should be thrown for already used key");
137: } catch (KeyManagementException ok) {
138: }
139:
140: }
141:
142: /**
143: * verify that identities are removed
144: * @throws Exception
145: */
146:
147: public void testRemoveIdentity() throws Exception {
148: // SystemScope ss = new SystemScope("SystemScope");
149: java.security.Identity aaa = new IdentityScopeStub("aaa");
150: ss.addIdentity(aaa);
151: assertEquals(1, ss.size());
152: ss.removeIdentity(aaa);
153: assertEquals(0, ss.size());
154: }
155:
156: public void testIdentities() throws Exception {
157: // SystemScope ss = new SystemScope("SystemScope");
158: java.security.Identity aaa = new IdentityScopeStub("aaa");
159: java.security.Identity bbb = new IdentityScopeStub("bbb");
160: ss.addIdentity(aaa);
161: ss.addIdentity(bbb);
162:
163: boolean hasAaa = false, hasBbb = false;
164: Enumeration e = ss.identities();
165: while (e.hasMoreElements()) {
166: Object i = e.nextElement();
167: if (!hasAaa)
168: hasAaa = (i == aaa);
169: if (!hasBbb)
170: hasBbb = (i == bbb);
171: }
172: assertTrue(hasAaa && hasBbb);
173: }
174:
175: /*
176: * Class under test for void SystemScope()
177: */
178: public void testSystemScope() {
179: }
180:
181: /*
182: * Class under test for void SystemScope(String)
183: */
184: public void testSystemScopeString() {
185: SystemScope ss = new SystemScope("SystemScope");
186: assertEquals("SystemScope", ss.getName());
187: }
188:
189: /*
190: * Class under test for void SystemScope(String, IdentityScope)
191: */
192: public void testSystemScopeStringIdentityScope() throws Exception {
193: SystemScope ss = new SystemScope("SystemScope");
194: SystemScope nested = new SystemScope("NestedScope", ss);
195: assertEquals("NestedScope", nested.getName());
196: assertSame(ss, nested.getScope());
197: }
198:
199: }
|