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 Alexey V. Varlamov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.fortress;
022:
023: import java.io.File;
024: import java.io.FileWriter;
025: import java.net.URL;
026: import java.security.CodeSource;
027: import java.security.Principal;
028: import java.security.SecurityPermission;
029: import java.security.cert.Certificate;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import org.apache.harmony.security.PolicyEntry;
034: import org.apache.harmony.security.fortress.DefaultPolicyParser;
035: import junit.framework.TestCase;
036:
037: /**
038: * Tests for DefaultPolicyParser
039: *
040: */
041:
042: public class DefaultPolicyParserTest extends TestCase {
043:
044: public static void main(String[] args) {
045: junit.textui.TestRunner.run(DefaultPolicyParserTest.class);
046: }
047:
048: /**
049: * Tests parsing of a sample policy from temporary file, validates returned
050: * PolicyEntries.
051: */
052: public void testParse() throws Exception {
053: File tmp = File.createTempFile("policy", null);
054: try {
055: FileWriter out = new FileWriter(tmp);
056: out
057: .write("grant{}KeyStore \"url2\", \"type2\" "
058: + "GRANT signedby \"duke,Li\", codebase\"\", principal a.b.c \"guest\" "
059: + "{permission XXX \"YYY\", SignedBy \"ZZZ\" \n \t };;;"
060: + "GRANT codebase\"http://a.b.c/-\", principal * * "
061: + "{permission java.security.SecurityPermission \"YYY\";}"
062: + "GRANT {permission java.security.SecurityPermission \"ZZZ\";}"
063: + "GRANT {permission java.security.UnresolvedPermission \"NONE\";}");
064: out.flush();
065: out.close();
066:
067: DefaultPolicyParser parser = new DefaultPolicyParser();
068: Collection entries = parser
069: .parse(tmp.toURI().toURL(), null);
070: assertEquals(2, entries.size());
071: for (Iterator iter = entries.iterator(); iter.hasNext();) {
072: PolicyEntry element = (PolicyEntry) iter.next();
073: if (element.getPermissions().contains(
074: new SecurityPermission("ZZZ"))) {
075: assertTrue(element
076: .impliesCodeSource(new CodeSource(null,
077: (Certificate[]) null)));
078: assertTrue(element.impliesPrincipals(null));
079: } else if (element.getPermissions().contains(
080: new SecurityPermission("YYY"))) {
081: assertFalse(element.impliesCodeSource(null));
082: assertFalse(element.impliesPrincipals(null));
083: assertTrue(element
084: .impliesCodeSource(new CodeSource(new URL(
085: "http://a.b.c/-"),
086: (Certificate[]) null)));
087: assertTrue(element
088: .impliesPrincipals(new Principal[] { new FakePrincipal(
089: "qqq") }));
090: } else {
091: fail("Extra entry parsed");
092: }
093: }
094: } finally {
095: tmp.delete();
096: }
097: }
098: }
099:
100: class FakePrincipal implements Principal {
101:
102: private String name;
103:
104: public FakePrincipal(String name) {
105: this .name = name;
106: }
107:
108: public String getName() {
109: return name;
110: }
111: }
|