You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@river.apache.org by pe...@apache.org on 2010/03/28 14:57:05 UTC
svn commit: r928394 [6/6] - in /incubator/river/jtsk/trunk: ./ qa/
qa/harness/policy/ qa/jtreg/net/jini/jeri/kerberos/UnitTests/
qa/jtreg/net/jini/jeri/transport/multihomed/ qa/jtreg/unittestlib/
qa/src/com/sun/jini/qa/harness/ qa/src/com/sun/jini/qa/r...
Added: incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyEntryTest.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyEntryTest.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyEntryTest.java (added)
+++ incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyEntryTest.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+* @author Alexey V. Varlamov
+* @version $Revision$
+*/
+
+package org.apache.river.security.policy.util;
+
+import java.net.URL;
+import java.security.cert.Certificate;
+import java.security.AllPermission;
+import java.security.CodeSource;
+import java.security.Permission;
+import java.security.Principal;
+import java.security.SecurityPermission;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+
+import junit.framework.TestCase;
+
+
+/**
+ * TODO Put your class description here
+ *
+ */
+
+public class PolicyEntryTest extends TestCase {
+
+ /**
+ * Tests constructor and accessors of PolicyEntry
+ */
+ public void testCtor() {
+ PolicyEntry pe = new PolicyEntry(null, null, null);
+ assertTrue(pe.isVoid());
+ assertNull(pe.getPermissions());
+
+ pe = new PolicyEntry(new CodeSource(null, (Certificate[])null),
+ new ArrayList(), new ArrayList());
+ assertTrue(pe.isVoid());
+ assertNull(pe.getPermissions());
+
+ Collection perms = Arrays.asList(new Permission[] {
+ new SecurityPermission("dsfg"), new AllPermission() });
+ pe = new PolicyEntry(null, null, perms);
+ assertFalse(pe.isVoid());
+ assertEquals(perms, new ArrayList(pe.getPermissions()));
+ }
+
+ /**
+ * Null CodeSource of PolicyEntry implies any CodeSource; non-null
+ * CodeSource should delegate to its own imply() functionality
+ */
+ public void testImpliesCodeSource() throws Exception {
+ CodeSource cs0 = new CodeSource(null, (Certificate[]) null);
+
+ CodeSource cs10 = new CodeSource(new URL("file:"), (Certificate[]) null);
+ CodeSource cs11 = new CodeSource(new URL("file:/"), (Certificate[]) null);
+ CodeSource cs12 = new CodeSource(new URL("file://"), (Certificate[]) null);
+ CodeSource cs13 = new CodeSource(new URL("file:///"), (Certificate[]) null);
+
+ CodeSource cs20 = new CodeSource(new URL("file:*"), (Certificate[]) null);
+ CodeSource cs21 = new CodeSource(new URL("file:/*"), (Certificate[]) null);
+ CodeSource cs22 = new CodeSource(new URL("file://*"), (Certificate[]) null);
+ CodeSource cs23 = new CodeSource(new URL("file:///*"), (Certificate[]) null);
+
+ CodeSource cs30 = new CodeSource(new URL("file:-"), (Certificate[]) null);
+ CodeSource cs31 = new CodeSource(new URL("file:/-"), (Certificate[]) null);
+ CodeSource cs32 = new CodeSource(new URL("file://-"), (Certificate[]) null);
+ CodeSource cs33 = new CodeSource(new URL("file:///-"), (Certificate[]) null);
+
+ PolicyEntry pe0 = new PolicyEntry(null, null, null);
+
+ PolicyEntry pe10 = new PolicyEntry(cs10, null, null);
+ PolicyEntry pe11 = new PolicyEntry(cs11, null, null);
+ PolicyEntry pe12 = new PolicyEntry(cs12, null, null);
+ PolicyEntry pe13 = new PolicyEntry(cs13, null, null);
+
+ PolicyEntry pe20 = new PolicyEntry(cs20, null, null);
+ PolicyEntry pe21 = new PolicyEntry(cs21, null, null);
+ PolicyEntry pe22 = new PolicyEntry(cs22, null, null);
+ PolicyEntry pe23 = new PolicyEntry(cs23, null, null);
+
+ PolicyEntry pe30 = new PolicyEntry(cs30, null, null);
+ PolicyEntry pe31 = new PolicyEntry(cs31, null, null);
+ PolicyEntry pe32 = new PolicyEntry(cs32, null, null);
+ PolicyEntry pe33 = new PolicyEntry(cs33, null, null);
+
+ assertTrue (pe0.impliesCodeSource(null));
+ assertTrue (pe0.impliesCodeSource(cs10));
+ assertTrue (pe0.impliesCodeSource(cs11));
+ assertTrue (pe0.impliesCodeSource(cs12));
+ assertTrue (pe0.impliesCodeSource(cs13));
+ assertTrue (pe0.impliesCodeSource(cs20));
+ assertTrue (pe0.impliesCodeSource(cs21));
+ assertTrue (pe0.impliesCodeSource(cs22));
+ assertTrue (pe0.impliesCodeSource(cs23));
+ assertTrue (pe0.impliesCodeSource(cs30));
+ assertTrue (pe0.impliesCodeSource(cs31));
+ assertTrue (pe0.impliesCodeSource(cs32));
+ assertTrue (pe0.impliesCodeSource(cs33));
+
+ assertFalse(pe10.impliesCodeSource(null));
+ assertTrue (pe10.impliesCodeSource(cs10));
+ assertFalse(pe10.impliesCodeSource(cs11));
+ assertTrue (pe10.impliesCodeSource(cs12));
+ assertFalse(pe10.impliesCodeSource(cs13));
+ assertTrue (pe10.impliesCodeSource(cs20));
+ assertFalse(pe10.impliesCodeSource(cs21));
+ assertFalse(pe10.impliesCodeSource(cs22));
+ assertFalse(pe10.impliesCodeSource(cs23));
+ assertTrue (pe10.impliesCodeSource(cs30));
+ assertFalse(pe10.impliesCodeSource(cs31));
+ assertFalse(pe10.impliesCodeSource(cs32));
+ assertFalse(pe10.impliesCodeSource(cs33));
+
+ assertFalse(pe11.impliesCodeSource(null));
+ assertFalse(pe11.impliesCodeSource(cs10));
+ assertTrue (pe11.impliesCodeSource(cs11));
+ assertFalse(pe11.impliesCodeSource(cs12));
+ assertTrue (pe11.impliesCodeSource(cs13));
+ assertFalse(pe11.impliesCodeSource(cs20));
+ assertFalse(pe11.impliesCodeSource(cs21));
+ assertFalse(pe11.impliesCodeSource(cs22));
+ assertFalse(pe11.impliesCodeSource(cs23));
+ assertFalse(pe11.impliesCodeSource(cs30));
+ assertFalse(pe11.impliesCodeSource(cs31));
+ assertFalse(pe11.impliesCodeSource(cs32));
+ assertFalse(pe11.impliesCodeSource(cs33));
+
+ assertFalse(pe12.impliesCodeSource(null));
+ assertTrue (pe12.impliesCodeSource(cs10));
+ assertFalse(pe12.impliesCodeSource(cs11));
+ assertTrue (pe12.impliesCodeSource(cs12));
+ assertFalse(pe12.impliesCodeSource(cs13));
+ assertTrue (pe12.impliesCodeSource(cs20));
+ assertFalse(pe12.impliesCodeSource(cs21));
+ assertFalse(pe12.impliesCodeSource(cs22));
+ assertFalse(pe12.impliesCodeSource(cs23));
+ assertTrue (pe12.impliesCodeSource(cs30));
+ assertFalse(pe12.impliesCodeSource(cs31));
+ assertFalse(pe12.impliesCodeSource(cs32));
+ assertFalse(pe12.impliesCodeSource(cs33));
+
+ assertFalse(pe13.impliesCodeSource(null));
+ assertFalse(pe13.impliesCodeSource(cs10));
+ assertTrue (pe13.impliesCodeSource(cs11));
+ assertFalse(pe13.impliesCodeSource(cs12));
+ assertTrue (pe13.impliesCodeSource(cs13));
+ assertFalse(pe13.impliesCodeSource(cs20));
+ assertFalse(pe13.impliesCodeSource(cs21));
+ assertFalse(pe13.impliesCodeSource(cs22));
+ assertFalse(pe13.impliesCodeSource(cs23));
+ assertFalse(pe13.impliesCodeSource(cs30));
+ assertFalse(pe13.impliesCodeSource(cs31));
+ assertFalse(pe13.impliesCodeSource(cs32));
+ assertFalse(pe13.impliesCodeSource(cs33));
+
+ assertFalse(pe20.impliesCodeSource(null));
+ assertTrue (pe20.impliesCodeSource(cs10));
+ assertFalse(pe20.impliesCodeSource(cs11));
+ assertTrue (pe20.impliesCodeSource(cs12));
+ assertFalse(pe20.impliesCodeSource(cs13));
+ assertTrue (pe20.impliesCodeSource(cs20));
+ assertFalse(pe20.impliesCodeSource(cs21));
+ assertFalse(pe20.impliesCodeSource(cs22));
+ assertFalse(pe20.impliesCodeSource(cs23));
+ assertTrue (pe20.impliesCodeSource(cs30));
+ assertFalse(pe20.impliesCodeSource(cs31));
+ assertFalse(pe20.impliesCodeSource(cs32));
+ assertFalse(pe20.impliesCodeSource(cs33));
+
+ assertFalse(pe21.impliesCodeSource(null));
+ assertFalse(pe21.impliesCodeSource(cs10));
+ assertTrue (pe21.impliesCodeSource(cs11));
+ assertFalse(pe21.impliesCodeSource(cs12));
+ assertTrue (pe21.impliesCodeSource(cs13));
+ assertFalse(pe21.impliesCodeSource(cs20));
+ assertTrue (pe21.impliesCodeSource(cs21));
+ assertFalse(pe21.impliesCodeSource(cs22));
+ assertTrue (pe21.impliesCodeSource(cs23));
+ assertFalse(pe21.impliesCodeSource(cs30));
+ assertTrue (pe21.impliesCodeSource(cs31));
+ assertFalse(pe21.impliesCodeSource(cs32));
+ assertTrue (pe21.impliesCodeSource(cs33));
+
+ assertFalse(pe22.impliesCodeSource(null));
+ assertFalse(pe22.impliesCodeSource(cs10));
+ // assertFalse(pe22.impliesCodeSource(cs11));
+ assertFalse(pe22.impliesCodeSource(cs12));
+ // assertFalse(pe22.impliesCodeSource(cs13));
+ assertFalse(pe22.impliesCodeSource(cs20));
+ assertFalse(pe22.impliesCodeSource(cs21));
+ assertTrue (pe22.impliesCodeSource(cs22));
+ assertFalse(pe22.impliesCodeSource(cs23));
+ assertFalse(pe22.impliesCodeSource(cs30));
+ assertFalse(pe22.impliesCodeSource(cs31));
+ assertTrue (pe22.impliesCodeSource(cs32));
+ assertFalse(pe22.impliesCodeSource(cs33));
+
+ assertFalse(pe23.impliesCodeSource(null));
+ assertFalse(pe23.impliesCodeSource(cs10));
+ assertTrue (pe23.impliesCodeSource(cs11));
+ assertFalse(pe23.impliesCodeSource(cs12));
+ assertTrue (pe23.impliesCodeSource(cs13));
+ assertFalse(pe23.impliesCodeSource(cs20));
+ assertTrue (pe23.impliesCodeSource(cs21));
+ assertFalse(pe23.impliesCodeSource(cs22));
+ assertTrue (pe23.impliesCodeSource(cs23));
+ assertFalse(pe23.impliesCodeSource(cs30));
+ assertTrue (pe23.impliesCodeSource(cs31));
+ assertFalse(pe23.impliesCodeSource(cs32));
+ assertTrue (pe23.impliesCodeSource(cs33));
+
+ assertFalse(pe30.impliesCodeSource(null));
+ assertTrue (pe30.impliesCodeSource(cs10));
+ assertFalse(pe30.impliesCodeSource(cs11));
+ assertTrue (pe30.impliesCodeSource(cs12));
+ assertFalse(pe30.impliesCodeSource(cs13));
+ assertTrue (pe30.impliesCodeSource(cs20));
+ assertFalse(pe30.impliesCodeSource(cs21));
+ assertFalse(pe30.impliesCodeSource(cs22));
+ assertFalse(pe30.impliesCodeSource(cs23));
+ assertTrue (pe30.impliesCodeSource(cs30));
+ assertFalse(pe30.impliesCodeSource(cs31));
+ assertFalse(pe30.impliesCodeSource(cs32));
+ assertFalse(pe30.impliesCodeSource(cs33));
+
+ assertFalse(pe31.impliesCodeSource(null));
+ assertTrue (pe31.impliesCodeSource(cs10));
+ assertTrue (pe31.impliesCodeSource(cs11));
+ assertTrue (pe31.impliesCodeSource(cs12));
+ assertTrue (pe31.impliesCodeSource(cs13));
+ assertTrue (pe31.impliesCodeSource(cs20));
+ assertTrue (pe31.impliesCodeSource(cs21));
+ assertFalse(pe31.impliesCodeSource(cs22));
+ assertTrue (pe31.impliesCodeSource(cs23));
+ assertTrue (pe31.impliesCodeSource(cs30));
+ assertTrue (pe31.impliesCodeSource(cs31));
+ assertFalse(pe31.impliesCodeSource(cs32));
+ assertTrue (pe31.impliesCodeSource(cs33));
+
+ assertFalse(pe32.impliesCodeSource(null));
+ assertFalse(pe32.impliesCodeSource(cs10));
+ assertFalse(pe32.impliesCodeSource(cs11));
+ assertFalse(pe32.impliesCodeSource(cs12));
+ assertFalse(pe32.impliesCodeSource(cs13));
+ assertFalse(pe32.impliesCodeSource(cs20));
+ assertFalse(pe32.impliesCodeSource(cs21));
+ assertFalse(pe32.impliesCodeSource(cs22));
+ assertFalse(pe32.impliesCodeSource(cs23));
+ assertFalse(pe32.impliesCodeSource(cs30));
+ assertFalse(pe32.impliesCodeSource(cs31));
+ assertTrue (pe32.impliesCodeSource(cs32));
+ assertFalse(pe32.impliesCodeSource(cs33));
+
+ assertFalse(pe33.impliesCodeSource(null));
+ assertTrue (pe33.impliesCodeSource(cs10));
+ assertTrue (pe33.impliesCodeSource(cs11));
+ assertTrue (pe33.impliesCodeSource(cs12));
+ assertTrue (pe33.impliesCodeSource(cs13));
+ assertTrue (pe33.impliesCodeSource(cs20));
+ assertTrue (pe33.impliesCodeSource(cs21));
+ assertFalse(pe33.impliesCodeSource(cs22));
+ assertTrue (pe33.impliesCodeSource(cs23));
+ assertTrue (pe33.impliesCodeSource(cs30));
+ assertTrue (pe33.impliesCodeSource(cs31));
+ assertFalse(pe33.impliesCodeSource(cs32));
+ assertTrue (pe33.impliesCodeSource(cs33));
+ }
+
+ /**
+ * Null or empty set of Principals of PolicyEntry implies any Principals;
+ * otherwise tested set must contain all Principals of PolicyEntry.
+ */
+ public void testImpliesPrincipals() {
+ PolicyEntry pe = new PolicyEntry(null, null, null);
+ Principal[] pp1 = new Principal[] {};
+ Principal[] pp2 = new Principal[] { new UnresolvedPrincipal("a.b.c",
+ "XXX") };
+ Principal[] pp3 = new Principal[] {
+ new UnresolvedPrincipal("a.b.c", "YYY"),
+ new UnresolvedPrincipal("a.b.c", "XXX"),
+ new UnresolvedPrincipal("e.f.g", "ZZZ") };
+
+ assertTrue(pe.impliesPrincipals(null));
+ assertTrue(pe.impliesPrincipals(pp1));
+
+ pe = new PolicyEntry(null, new HashSet(), null);
+ assertTrue(pe.impliesPrincipals(pp3));
+
+ pe = new PolicyEntry(null, Arrays.asList(pp2), null);
+ assertFalse(pe.impliesPrincipals(null));
+ assertFalse(pe.impliesPrincipals(pp1));
+ assertTrue(pe.impliesPrincipals(pp3));
+ }
+}
Propchange: incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyEntryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyUtilsTest.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyUtilsTest.java (added)
+++ incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyUtilsTest.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,330 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+* @author Alexey V. Varlamov
+* @version $Revision$
+*/
+
+package org.apache.river.security.policy.util;
+
+import java.io.File;
+import java.net.URL;
+import java.security.AllPermission;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Security;
+import java.security.SecurityPermission;
+import java.security.UnresolvedPermission;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests for <code>PolicyUtils</code> class fields and methods.
+ *
+ */
+
+public class PolicyUtilsTest extends TestCase {
+
+ /** Tests valid expansion of ${key} entries. */
+ public void testExpand() throws Exception {
+ String[] input = new String[] { "${key.1}", "abcd${key.1}",
+ "a ${key.1} b ${$key$}${key.2}", "$key.1", "${}" };
+ String[] output = new String[] { "value1", "abcdvalue1",
+ "a value1 b ${${${${${${value.2", "$key.1", "" };
+ Properties props = new Properties();
+ props.put("key.1", "value1");
+ props.put("key.2", "value.2");
+ props.put("$key$", "${${${${${${");
+ props.put("", "");
+ for (int i = 0; i < output.length; i++) {
+ assertEquals(output[i], PolicyUtils.expand(input[i], props));
+ }
+ }
+
+ /** Tests ExpansionFailedException for missing keys of ${key} entries. */
+ public void testExpandFailed() throws Exception {
+ try {
+ PolicyUtils.expand("${key.123}", new Properties());
+ fail("Should throw ExpansionFailedException");
+ }
+ catch (PolicyUtils.ExpansionFailedException ok) {}
+ }
+
+ /** Tests valid URL-specific expansion. */
+ public void testExpandURL() throws Exception {
+ String input = "file:/${my.home}" + File.separator + "lib/extensions/";
+ Properties props = new Properties();
+ String q = File.separator + "drl" + File.separator + "tools1.2";
+ props.put("my.home", q);
+ assertEquals("file://drl/tools1.2/lib/extensions/", PolicyUtils.expandURL(input,
+ props));
+ }
+
+ /** Tests valid expansion of ${{protocol:data}} entries. */
+ public void testExpandGeneral() throws Exception {
+ String[] input = new String[] { "${{a:b}}", "a ${{self}}${{a: made}}",
+ "${{}}" };
+ String[] output = new String[] { "b", "a this made", "" };
+ PolicyUtils.GeneralExpansionHandler handler = new PolicyUtils.GeneralExpansionHandler() {
+
+ public String resolve(String protocol, String data) {
+ if ("a".equals(protocol)) {
+ return data;
+ }
+ if ("self".equals(protocol)) {
+ return "this";
+ }
+ if ("".equals(protocol)) {
+ return protocol;
+ }
+ return null;
+ }
+ };
+ for (int i = 0; i < output.length; i++) {
+ assertEquals(output[i], PolicyUtils
+ .expandGeneral(input[i], handler));
+ }
+ }
+
+ /**
+ * Tests ExpansionFailedException for undefined protocol
+ * of ${{protocol:data}} entries.
+ */
+ public void testExpandGeneralFailed() throws Exception {
+ try {
+ PolicyUtils.expandGeneral("${{c}}",
+ new PolicyUtils.GeneralExpansionHandler() {
+
+ public String resolve(String protocol, String data)
+ throws PolicyUtils.ExpansionFailedException {
+ throw new PolicyUtils.ExpansionFailedException("");
+ }
+ });
+ fail("Should throw ExpansionFailedException");
+ }
+ catch (PolicyUtils.ExpansionFailedException ok) {}
+ }
+
+ /**
+ * Tests positive/negative/invalid/missing values of
+ * "policy.expandProperties" security property.
+ */
+ public void testCanExpandProperties() {
+ final String key = "policy.expandProperties";
+ String OLD = Security.getProperty(key);
+ try {
+ Security.setProperty(key, "true");
+ assertTrue(PolicyUtils.canExpandProperties());
+ Security.setProperty(key, "false");
+ assertFalse(PolicyUtils.canExpandProperties());
+ Security.setProperty(key, "");
+ assertTrue(PolicyUtils.canExpandProperties());
+ Security.setProperty(key, "laejhg");
+ assertTrue(PolicyUtils.canExpandProperties());
+ }
+ finally {
+ Security.setProperty(key, OLD);
+ }
+ }
+
+ /**
+ * Tests cases of enabled/disabled system URL.
+ */
+ public void testGetPolicyURLs01() throws Throwable {
+ final String KEY_DYNAMIC = "policy.allowSystemProperty";
+ String OLD_DYNAMIC = Security.getProperty(KEY_DYNAMIC);
+
+ final String KEY = "dsfvdf";
+ Properties arg = new Properties();
+ arg.put(KEY, "http://foo.bar.com");
+ try {
+ Security.setProperty(KEY_DYNAMIC, "true");
+ URL[] result = PolicyUtils.getPolicyURLs(arg, KEY, "");
+ assertNotNull(result);
+ assertEquals(1, result.length);
+ assertEquals(new URL("http://foo.bar.com"), result[0]);
+
+ Security.setProperty(KEY_DYNAMIC, "false");
+ result = PolicyUtils.getPolicyURLs(arg, KEY, "");
+ assertNotNull(result);
+ assertEquals(0, result.length);
+
+ Security.setProperty(KEY_DYNAMIC, "");
+ result = PolicyUtils.getPolicyURLs(arg, KEY, "");
+ assertNotNull(result);
+ assertEquals(1, result.length);
+ assertEquals(new URL("http://foo.bar.com"), result[0]);
+ }
+ finally {
+ Security.setProperty(KEY_DYNAMIC, OLD_DYNAMIC);
+ }
+ }
+
+ /** Tests finding algorithm for numbered locations in security properties. */
+ public void testGetPolicyURLs02() throws Throwable {
+ final String PREFIX = "testGetPolicyURLs02.";
+ String[] OLD = new String[5];
+ for (int i = 0; i < OLD.length; i++) {
+ OLD[i] = Security.getProperty(PREFIX + i);
+ }
+
+ try {
+ Security.setProperty(PREFIX + 0, "http://foo0.bar.com");
+ Security.setProperty(PREFIX + 1, "http://foo1.bar.com");
+ Security.setProperty(PREFIX + 2, "http://foo2.bar.com");
+ Security.setProperty(PREFIX + 4, "http://foo4.bar.com");
+ URL[] result = PolicyUtils.getPolicyURLs(new Properties(), "",
+ PREFIX);
+ assertNotNull(result);
+ assertEquals(2, result.length);
+ assertEquals(new URL("http://foo1.bar.com"), result[0]);
+ assertEquals(new URL("http://foo2.bar.com"), result[1]);
+
+ Security.setProperty(PREFIX + 1, "slkjdfhk/svfv*&^");
+ Security.setProperty(PREFIX + 3, "dlkfjvb3lk5jt");
+ result = PolicyUtils.getPolicyURLs(new Properties(), "", PREFIX);
+ assertNotNull(result);
+ assertEquals(2, result.length);
+ assertEquals(new URL("http://foo2.bar.com"), result[0]);
+ assertEquals(new URL("http://foo4.bar.com"), result[1]);
+ }
+ finally {
+ for (int i = 0; i < OLD.length; i++) {
+ Security
+ .setProperty(PREFIX + i, (OLD[i] == null) ? "" : OLD[i]);
+ }
+ }
+ }
+
+ /**
+ * Tests expansion in system and security URLs.
+ */
+ public void testGetPolicyURLs03() throws Throwable {
+ final String KEY_DYNAMIC = "policy.allowSystemProperty";
+ final String OLD_DYNAMIC = Security.getProperty(KEY_DYNAMIC);
+ final String KEY_EXP = "policy.expandProperties";
+ final String OLD_EXP = Security.getProperty(KEY_EXP);
+ final String PREFIX = "testGetPolicyURLs03.";
+ String[] OLD = new String[5];
+ for (int i = 0; i < OLD.length; i++) {
+ OLD[i] = Security.getProperty(PREFIX + i);
+ }
+
+ final String KEY = "dsfvdf";
+ Properties arg = new Properties();
+ arg.put(KEY, "file://${foo.path}/${foo.name}");
+ arg.put("foo.path", "path");
+ arg.put("foo.name", "name");
+ arg.put("foo", "acme");
+ Security.setProperty(KEY_DYNAMIC, "true");
+ Security.setProperty(KEY_EXP, "true");
+ Security.setProperty(PREFIX + 1, "http://foo0.${foo}.org");
+ Security.setProperty(PREFIX + 2, "http://${bar}.com");
+ Security.setProperty(PREFIX + 3,
+ "http://foo2.bar.com/${foo.path}/${foo.name}");
+ try {
+
+ URL[] result = PolicyUtils.getPolicyURLs(arg, KEY, PREFIX);
+ assertNotNull(result);
+ assertEquals(3, result.length);
+ assertEquals(new URL("http://foo0.acme.org"), result[0]);
+ assertEquals(new URL("http://foo2.bar.com/path/name"), result[1]);
+ assertEquals(new URL("file://path/name"), result[2]);
+
+ //expansion here cannot be switched off
+ Security.setProperty(KEY_EXP, "false");
+ result = PolicyUtils.getPolicyURLs(arg, KEY, PREFIX);
+ assertNotNull(result);
+ assertEquals(3, result.length);
+ assertEquals(new URL("http://foo0.acme.org"), result[0]);
+ assertEquals(new URL("http://foo2.bar.com/path/name"), result[1]);
+ assertEquals(new URL("file://path/name"), result[2]);
+ }
+ finally {
+ Security.setProperty(KEY_DYNAMIC, OLD_DYNAMIC);
+ Security.setProperty(KEY_EXP, OLD_EXP);
+ for (int i = 0; i < OLD.length; i++) {
+ Security
+ .setProperty(PREFIX + i, (OLD[i] == null) ? "" : OLD[i]);
+ }
+ }
+ }
+
+ /** Tests conversion of null, empty and non-empty heterogeneous collections. */
+ public void testToPermissionCollection() {
+ Permission p1 = new SecurityPermission("abc");
+ Permission p2 = new AllPermission();
+ Collection c1 = Arrays.asList(new Permission[] { p1, p2, });
+
+ PermissionCollection pc = PolicyUtils.toPermissionCollection(null);
+ assertNotNull(pc);
+ assertFalse(pc.elements().hasMoreElements());
+
+ pc = PolicyUtils.toPermissionCollection(new HashSet());
+ assertNotNull(pc);
+ assertFalse(pc.elements().hasMoreElements());
+
+ pc = PolicyUtils.toPermissionCollection(c1);
+ assertNotNull(pc);
+ Enumeration en = pc.elements();
+ Collection c2 = new HashSet();
+ c2.add(en.nextElement());
+ c2.add(en.nextElement());
+ assertFalse(en.hasMoreElements());
+ assertTrue(c2.contains(p1));
+ assertTrue(c2.contains(p2));
+ }
+
+ public void testInstantiatePermission() throws Throwable {
+ String name = "abc";
+ Permission expected = new SecurityPermission(name);
+ //test valid input
+ assertEquals(expected, PolicyUtils.instantiatePermission(SecurityPermission.class, name, null));
+ assertEquals(expected, PolicyUtils.instantiatePermission(SecurityPermission.class, name, "4t46"));
+
+ //test invalid class
+ try {
+ PolicyUtils.instantiatePermission(UnresolvedPermission.class, null, null);
+ fail("IllegalArgumentException expected on invalid class argument");
+ } catch (IllegalArgumentException ok) {}
+ }
+
+ /**
+ * Tests various combinations of arrays:
+ * null/empty/containing null/containing real objects.
+ */
+ public void testMatchSubset() {
+ assertTrue(PolicyUtils.matchSubset(null, null));
+ assertTrue(PolicyUtils.matchSubset(new Object[] {}, null));
+ assertTrue(PolicyUtils.matchSubset(new Object[] { null }, null));
+ assertTrue(PolicyUtils.matchSubset(new Object[] {},
+ new Object[] { null }));
+ assertTrue(PolicyUtils.matchSubset(new Object[] { "1", "2" },
+ new Object[] { "3", "2", "1" }));
+ assertTrue(PolicyUtils.matchSubset(new Object[] { "1", null },
+ new Object[] { "3", "2", "1" }));
+ assertFalse(PolicyUtils.matchSubset(new Object[] { "1", null },
+ new Object[] { "3", "2", }));
+ }
+}
Propchange: incubator/river/jtsk/trunk/test/src/org/apache/river/security/policy/util/PolicyUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/tests/support/FakePrincipal.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/tests/support/FakePrincipal.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/tests/support/FakePrincipal.java (added)
+++ incubator/river/jtsk/trunk/test/src/tests/support/FakePrincipal.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+* @author Stepan M. Mishura
+* @version $Revision$
+*/
+
+package tests.support;
+
+import org.apache.river.security.concurrent.*;
+
+public class FakePrincipal extends MyPrincipal {
+ public FakePrincipal(String name) {
+ super(name);
+ }
+}
Propchange: incubator/river/jtsk/trunk/test/src/tests/support/FakePrincipal.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/tests/support/MyPrincipal.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/tests/support/MyPrincipal.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/tests/support/MyPrincipal.java (added)
+++ incubator/river/jtsk/trunk/test/src/tests/support/MyPrincipal.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+* @author Stepan M. Mishura
+* @version $Revision$
+*/
+
+package tests.support;
+
+import java.security.Principal;
+
+public class MyPrincipal implements Principal {
+
+ private String name;
+
+ public MyPrincipal(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
\ No newline at end of file
Propchange: incubator/river/jtsk/trunk/test/src/tests/support/MyPrincipal.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/tests/support/Support_Configuration.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/tests/support/Support_Configuration.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/tests/support/Support_Configuration.java (added)
+++ incubator/river/jtsk/trunk/test/src/tests/support/Support_Configuration.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,475 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.support;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Hashtable;
+
+/**
+ * This class is responsible for providing the dynamic names and addresses for
+ * the java.net classes. There are two directories which need to be placed on an
+ * ftp server and an http server which should accompany this source. The
+ * ftp-files have to be placed on an ftp server and have to be the root of a
+ * user jcltest with password jclpass. The testres files must be available on an
+ * HTTP server and the name and location can be configured below.
+ */
+public class Support_Configuration {
+
+ public static String DomainAddress = "apache.org";
+
+ public static String WebName = "jcltest.";
+
+ public static final String HomeAddress;
+
+ public static String TestResourcesDir = "/testres231";
+
+ public static final String TestResources;
+
+ public static String HomeAddressResponse = "HTTP/1.1 200 OK";
+
+ public static String HomeAddressSoftware = "Jetty(6.0.x)";
+
+ public static String ProxyServerTestHost = "jcltest.apache.org";
+
+ public static String SocksServerTestHost = "jcltest.apache.org";
+
+ public static int SocksServerTestPort = 1080;
+
+ // Need an IP address that does not resolve to a host name
+ public static String UnresolvedIP = "192.168.99.99";
+
+ // the bytes for an address which represents an address which is not
+ // one of the addresses for any of our machines on which tests will run
+ // it is used to verify we get the expected error when we try to bind
+ // to an address that is not one of the machines local addresses
+ public static byte nonLocalAddressBytes[] = { 1, 0, 0, 0 };
+
+ public static String InetTestAddress = "localhost";
+
+ public static String InetTestIP = "127.0.0.1";
+
+ public static String InetTestAddress2 = "localhost";
+
+ public static String InetTestIP2 = "127.0.0.1";
+
+ public static byte[] InetTestCaddr = { 9, 26, -56, -111 };
+
+ public static int InetTestHashcode = 2130706433;
+
+ public static final String HomeAddress6 = "jcltest6.apache.org";
+
+ public static String IPv6GlobalAddressJcl4 = "FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"; // this
+
+ public static String ProxyServerTestHostIPv6 = "jcltest6.apache.org";
+
+ public static String InetTestIP6 = "fe80::20d:60ff:fe24:7410";
+
+ public static String InetTestIP6LO = "::1";
+
+ // ip address that resolves to a host that is not present on the local
+ // network
+ // this allows us to check the timeouts for connect
+ public static String ResolvedNotExistingHost = "9.26.194.72";
+
+ /**
+ * You can compute the hash code with the following code: try { String name =
+ * "whatever.xxx.com";
+ * System.out.println(InetAddress.getByName(name).hashCode()); } catch
+ * (UnknownHostException e) {}
+ */
+
+ /**
+ * An address that resolves to more than one IP address so that the
+ * getAllByName test has something to test.
+ */
+ public static String SpecialInetTestAddress = "jcltestmultiple.apache.org";
+
+ public static int SpecialInetTestAddressNumber = 4;
+
+ /**
+ * InetAlias1 and InetAlias2 must be different host names that resolve to
+ * the same IP address.
+ */
+ public static String InetAlias1 = "alias1.apache.org";
+
+ public static String InetAlias2 = "alias2.apache.org";
+
+ public static String FTPTestAddress = "jcltest:jclpass@localhost";
+
+ public static String URLConnectionLastModifiedString = "Mon, 14 Jun 1999 21:06:22 GMT";
+
+ public static long URLConnectionLastModified = 929394382000L;
+
+ public static long URLConnectionDate = 929106872000L;
+
+ public static boolean RunCommTests = false;
+
+ public static String Port1 = "COM1";
+
+ public static String Port2 = "COM2";
+
+ static Hashtable<String, String> props = null;
+ static {
+ loadProperties();
+ HomeAddress = WebName + DomainAddress;
+ TestResources = HomeAddress + TestResourcesDir;
+ }
+
+ static void loadProperties() {
+ InputStream in = null;
+ Hashtable<String, String> props = new Hashtable<String, String>();
+
+ String iniName = System.getProperty("test.ini.file", "JCLAuto.ini");
+ if (System.getProperty("test.comm") != null) {
+ RunCommTests = true;
+ }
+
+ try {
+ in = new FileInputStream(iniName);
+ } catch (IOException e) {
+ } catch (Exception e) {
+ System.out.println("SupportConfiguration.loadProperties()");
+ System.out.println(e);
+ e.printStackTrace();
+ }
+ if (in == null) {
+ try {
+ Class<?> cl = Class
+ .forName("com.ibm.support.Support_Configuration");
+ in = cl.getResourceAsStream(iniName);
+ } catch (ClassNotFoundException e) {
+ }
+ }
+ try {
+ if (in != null) {
+ load(in, props);
+ }
+ } catch (IOException e) {
+ }
+ if (props.size() == 0) {
+ return;
+ }
+ String value;
+
+ value = props.get("DomainAddress");
+ if (value != null) {
+ DomainAddress = value;
+ }
+
+ value = props.get("WebName");
+ if (value != null) {
+ WebName = value;
+ }
+
+ value = props.get("TestResourcesDir");
+ if (value != null) {
+ TestResourcesDir = value;
+ }
+ value = props.get("HomeAddressResponse");
+ if (value != null) {
+ HomeAddressResponse = value;
+ }
+
+ value = props.get("HomeAddressSoftware");
+ if (value != null) {
+ HomeAddressSoftware = value;
+ }
+
+ value = props.get("ProxyServerTestHost");
+ if (value != null) {
+ ProxyServerTestHost = value;
+ }
+
+ value = props.get("SocksServerTestHost");
+ if (value != null) {
+ SocksServerTestHost = value;
+ }
+
+ value = props.get("SocksServerTestPort");
+ if (value != null) {
+ SocksServerTestPort = Integer.parseInt(value);
+ }
+
+ value = props.get("UnresolvedIP");
+ if (value != null) {
+ UnresolvedIP = value;
+ }
+
+ value = props.get("InetTestAddress");
+ if (value != null) {
+ InetTestAddress = value;
+ }
+
+ value = props.get("InetTestIP");
+ if (value != null) {
+ InetTestIP = value;
+ byte[] addr = new byte[4];
+ int last = 0;
+ try {
+ for (int i = 0; i < 3; i++) {
+ int dot = InetTestIP.indexOf('.', last);
+ addr[i] = (byte) Integer.parseInt(InetTestIP.substring(
+ last, dot));
+ last = dot + 1;
+ }
+ addr[3] = (byte) Integer.parseInt(InetTestIP.substring(last));
+ InetTestCaddr = addr;
+ } catch (RuntimeException e) {
+ System.out.println("Error parsing InetTestIP (" + InetTestIP
+ + ")");
+ System.out.println(e);
+ }
+ }
+
+ value = props.get("NonLocalAddressBytes");
+ if (value != null) {
+ String nonLocalAddressBytesString = value;
+ byte[] addr = new byte[4];
+ int last = 0;
+ try {
+ for (int i = 0; i < 3; i++) {
+ int dot = nonLocalAddressBytesString.indexOf('.', last);
+ addr[i] = (byte) Integer
+ .parseInt(nonLocalAddressBytesString.substring(
+ last, dot));
+ last = dot + 1;
+ }
+ addr[3] = (byte) Integer.parseInt(nonLocalAddressBytesString
+ .substring(last));
+ nonLocalAddressBytes = addr;
+ } catch (RuntimeException e) {
+ System.out.println("Error parsing NonLocalAddressBytes ("
+ + nonLocalAddressBytesString + ")");
+ System.out.println(e);
+ }
+ }
+
+ value = props.get("InetTestAddress2");
+ if (value != null) {
+ InetTestAddress2 = value;
+ }
+
+ value = props.get("InetTestIP2");
+ if (value != null) {
+ InetTestIP2 = value;
+ }
+
+ value = props.get("InetTestHashcode");
+ if (value != null) {
+ InetTestHashcode = Integer.parseInt(value);
+ }
+
+ value = props.get("SpecialInetTestAddress");
+ if (value != null) {
+ SpecialInetTestAddress = value;
+ }
+
+ value = props.get("SpecialInetTestAddressNumber");
+ if (value != null) {
+ SpecialInetTestAddressNumber = Integer.parseInt(value);
+ }
+
+ value = props.get("FTPTestAddress");
+ if (value != null) {
+ FTPTestAddress = value;
+ }
+
+ value = props.get("URLConnectionLastModifiedString");
+ if (value != null) {
+ URLConnectionLastModifiedString = value;
+ }
+
+ value = props.get("URLConnectionLastModified");
+ if (value != null) {
+ URLConnectionLastModified = Long.parseLong(value);
+ }
+
+ value = props.get("URLConnectionDate");
+ if (value != null) {
+ URLConnectionDate = Long.parseLong(value);
+ }
+
+ value = props.get("Port1");
+ if (value != null) {
+ Port1 = value;
+ }
+
+ value = props.get("Port2");
+ if (value != null) {
+ Port2 = value;
+ }
+
+ value = props.get("InetTestIP6");
+ if (value != null) {
+ InetTestIP6 = value;
+ }
+
+ value = props.get("InetTestIP6LO");
+ if (value != null) {
+ InetTestIP6LO = value;
+ }
+
+ value = props.get("ProxyServerTestHostIPv6");
+ if (value != null) {
+ ProxyServerTestHostIPv6 = value;
+ }
+
+ value = props.get("ResolvedNotExistingHost");
+ if (value != null) {
+ ResolvedNotExistingHost = value;
+ }
+
+ value = props.get("InetAlias1");
+ if (value != null) {
+ InetAlias1 = value;
+ }
+
+ value = props.get("InetAlias2");
+ if (value != null) {
+ InetAlias2 = value;
+ }
+
+ value = props.get("IPv6GlobalAddressJcl4");
+ if (value != null) {
+ IPv6GlobalAddressJcl4 = value;
+ }
+
+ }
+
+ static void load(InputStream in, Hashtable<String, String> result) throws IOException {
+ int NONE = 0, SLASH = 1, UNICODE = 2, CONTINUE = 3, DONE = 4, IGNORE = 5;
+ int mode = NONE, unicode = 0, count = 0, nextChar;
+ StringBuffer key = new StringBuffer(), value = new StringBuffer(), buffer = key;
+ boolean firstChar = true;
+
+ while ((nextChar = in.read()) != -1) {
+ if (mode == UNICODE) {
+ int digit = Character.digit((char) nextChar, 16);
+ if (digit >= 0) {
+ unicode = (unicode << 4) + digit;
+ if (++count < 4) {
+ continue;
+ }
+ }
+ mode = NONE;
+ buffer.append((char) unicode);
+ if (nextChar != '\n') {
+ continue;
+ }
+ }
+ if (mode == SLASH) {
+ mode = NONE;
+ switch (nextChar) {
+ case '\r':
+ mode = CONTINUE; // Look for a following \n
+ continue;
+ case '\n':
+ mode = IGNORE; // Ignore whitespace on the next line
+ continue;
+ case 'b':
+ nextChar = '\b';
+ break;
+ case 'f':
+ nextChar = '\f';
+ break;
+ case 'n':
+ nextChar = '\n';
+ break;
+ case 'r':
+ nextChar = '\r';
+ break;
+ case 't':
+ nextChar = '\t';
+ break;
+ case 'u':
+ mode = UNICODE;
+ unicode = count = 0;
+ continue;
+ }
+ } else {
+ switch (nextChar) {
+ case '#':
+ case '!':
+ if (firstChar) {
+ while ((nextChar = in.read()) != -1) {
+ if (nextChar == '\r' || nextChar == '\n') {
+ break;
+ }
+ }
+ continue;
+ }
+ break;
+ case '\n':
+ if (mode == CONTINUE) { // Part of a \r\n sequence
+ mode = IGNORE; // Ignore whitespace on the next line
+ continue;
+ }
+ // fall into the next case
+ case '\r':
+ mode = NONE;
+ firstChar = true;
+ if (key.length() > 0 || buffer == value) {
+ result.put(key.toString(), value.toString());
+ }
+ key.setLength(0);
+ value.setLength(0);
+ buffer = key;
+ continue;
+ case '\\':
+ mode = SLASH;
+ continue;
+ case ':':
+ case '=':
+ if (buffer == key) {
+ buffer = value;
+ continue;
+ }
+ break;
+ }
+ char c = (char) nextChar;
+ if ((c >= 0x1c && c <= 0x20) || (c >= 0x9 && c <= 0xd)) {
+ if (mode == CONTINUE) {
+ mode = IGNORE;
+ }
+ if (buffer.length() == 0 || mode == IGNORE) {
+ continue;
+ }
+ if (buffer == key) {
+ mode = DONE;
+ continue;
+ }
+ }
+ if (mode == IGNORE || mode == CONTINUE) {
+ mode = NONE;
+ }
+ }
+ firstChar = false;
+ if (mode == DONE) {
+ buffer = value;
+ mode = NONE;
+ }
+ buffer.append((char) nextChar);
+ }
+ if (key.length() > 0 || buffer == value) {
+ result.put(key.toString(), value.toString());
+ }
+ }
+
+}
Propchange: incubator/river/jtsk/trunk/test/src/tests/support/Support_Configuration.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/tests/support/Support_Exec.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/tests/support/Support_Exec.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/tests/support/Support_Exec.java (added)
+++ incubator/river/jtsk/trunk/test/src/tests/support/Support_Exec.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.support;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
+import junit.framework.TestCase;
+
+public class Support_Exec extends TestCase {
+
+ /**
+ * This function returns the output of the process as a string
+ */
+ public static String execJava(String[] args, String[] classpath,
+ boolean displayOutput) throws IOException, InterruptedException {
+ Object[] arr =
+ execJavaCommon(args, classpath, null, displayOutput, true);
+
+ return getProcessOutput(arr, displayOutput);
+ }
+
+ /**
+ * This function returns the output of the process as a string
+ */
+ public static String execJava(String[] args, String[] classpath, String[] envp,
+ boolean displayOutput) throws IOException, InterruptedException {
+ Object[] arr =
+ execJavaCommon(args, classpath, envp, displayOutput, false);
+
+ return getProcessOutput(arr, displayOutput);
+ }
+
+ private static String getProcessOutput(Object[] arr, boolean displayOutput)
+ throws IOException, InterruptedException {
+ Process proc = (Process) arr[0];
+ StringBuilder output = new StringBuilder();
+ InputStream in = proc.getInputStream();
+ int result;
+ byte[] bytes = new byte[1024];
+
+ while ((result = in.read(bytes)) != -1) {
+ output.append(new String(bytes, 0, result));
+
+ if (displayOutput) {
+ System.out.write(bytes, 0, result);
+ }
+ }
+
+ in.close();
+ proc.waitFor();
+ checkStderr(arr);
+ proc.destroy();
+
+ return output.toString();
+ }
+
+ public static void checkStderr(Object[] execArgs) {
+ StringBuilder errBuf = (StringBuilder) execArgs[1];
+
+ synchronized (errBuf) {
+ if (errBuf.length() > 0) {
+ fail(errBuf.toString());
+ }
+ }
+ }
+
+ public static Object[] execJava2(String[] args, String[] classpath,
+ boolean displayOutput) throws IOException, InterruptedException {
+ return execJavaCommon(args, classpath, null, displayOutput, true);
+ }
+
+ private static Object[] execJavaCommon(String[] args, String[] classpath,
+ String[] envp, boolean displayOutput, boolean appendToSystemClassPath)
+ throws IOException, InterruptedException {
+ // this function returns the resulting process from the exec
+ ArrayList<String> execArgs = null;
+ StringBuilder classPathString = new StringBuilder();
+ StringBuilder command;
+ String executable;
+ String testVMArgs;
+ StringTokenizer st;
+
+ execArgs = new ArrayList<String>(3 + args.length);
+
+ // construct the name of executable file
+ executable = System.getProperty("java.home");
+ if (!executable.endsWith(File.separator)) {
+ executable += File.separator;
+ }
+ executable += "bin" + File.separator;
+ execArgs.add(executable + "java");
+
+ // add classpath string
+ if (classpath != null) {
+ for (String element : classpath) {
+ classPathString.append(File.pathSeparator);
+ classPathString.append(element);
+ }
+ }
+ if (appendToSystemClassPath) {
+ execArgs.add("-cp");
+ execArgs.add(System.getProperty("java.class.path") +
+ classPathString);
+ } else {
+ if (classpath != null) {
+ execArgs.add("-cp");
+ execArgs.add(classPathString.toString());
+ }
+ }
+
+ // parse hy.test.vmargs if was given
+ testVMArgs = System.getProperty("hy.test.vmargs");
+ if (testVMArgs != null) {
+ st = new StringTokenizer(testVMArgs, " ");
+
+ while (st.hasMoreTokens()) {
+ execArgs.add(st.nextToken());
+ }
+ }
+
+ // add custom args given as parameter
+ for (String arg : args) {
+ execArgs.add(arg);
+ }
+
+ // construct command line string and print it to stdout
+ //if (displayOutput) {
+ command = new StringBuilder(execArgs.get(0));
+ for (int i = 1; i < execArgs.size(); i++) {
+ command.append(" ");
+ command.append(execArgs.get(i));
+ }
+ System.out.println();
+ System.out.println("Exec: " + command.toString());
+ //}
+
+ // execute java process
+ final Process proc = Runtime.getRuntime().exec(
+ execArgs.toArray(new String[execArgs.size()]), envp);
+ final StringBuilder errBuf = new StringBuilder();
+ Thread errThread = new Thread(new Runnable() {
+ public void run() {
+ synchronized (errBuf) {
+ InputStream err;
+ int result;
+ byte[] bytes = new byte[1024];
+
+ synchronized (proc) {
+ proc.notifyAll();
+ }
+
+ err = proc.getErrorStream();
+ try {
+ while ((result = err.read(bytes)) != -1) {
+ System.err.write(bytes, 0, result);
+ errBuf.append(new String(bytes));
+ }
+ err.close();
+ } catch (IOException e) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PrintStream printer = new PrintStream(out);
+
+ e.printStackTrace();
+ e.printStackTrace(printer);
+ printer.close();
+ errBuf.append(new String(out.toByteArray()));
+ }
+ }
+ }
+ });
+
+ synchronized (proc) {
+ errThread.start();
+ // wait for errThread to start
+ int count = 0;
+ boolean isFinished = false;
+ while(!isFinished) {
+ try {
+ proc.wait();
+ isFinished = true;
+ } catch (InterruptedException e) {
+ if(++count == 2) {
+ throw e;
+ }
+ }
+ }
+ if(count > 0) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ return new Object[] { proc, errBuf };
+ }
+
+}
Propchange: incubator/river/jtsk/trunk/test/src/tests/support/Support_Exec.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/tests/support/Support_GetLocal.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/tests/support/Support_GetLocal.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/tests/support/Support_GetLocal.java (added)
+++ incubator/river/jtsk/trunk/test/src/tests/support/Support_GetLocal.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.support;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Hashtable;
+
+
+public class Support_GetLocal {
+
+ static Hashtable<String, File> cache = new Hashtable<String, File>(20);
+
+ public static File getLocalFile(String url) throws IOException,
+ MalformedURLException {
+ url = Support_Resources.RESOURCE_PACKAGE + url;
+ File temp = cache.get(url);
+ if (temp == null) {
+ InputStream in = Support_GetLocal.class.getResourceAsStream(url);
+ temp = File.createTempFile("hyts_local", ".tmp", null);
+ temp.deleteOnExit();
+ FileOutputStream out = new FileOutputStream(temp);
+ int result;
+ byte[] buf = new byte[4096];
+ while ((result = in.read(buf)) != -1) {
+ out.write(buf, 0, result);
+ }
+ in.close();
+ out.close();
+ cache.put(url, temp);
+ }
+ return temp;
+ }
+
+ public static File getExternalLocalFile(String url) throws IOException,
+ MalformedURLException {
+ File temp = cache.get(url);
+ if (temp == null) {
+ InputStream in = new URL(url).openStream();
+ temp = File.createTempFile("hyts_local", ".tmp", null);
+ temp.deleteOnExit();
+ FileOutputStream out = new FileOutputStream(temp);
+ int result;
+ byte[] buf = new byte[4096];
+ while ((result = in.read(buf)) != -1) {
+ out.write(buf, 0, result);
+ }
+ in.close();
+ out.close();
+ cache.put(url, temp);
+ }
+ return temp;
+ }
+
+ static ByteArrayInputStream getStream(String url) throws IOException,
+ MalformedURLException {
+ InputStream in = new URL(url).openStream();
+ ByteArrayOutputStream out = new ByteArrayOutputStream(256);
+ int result;
+ byte[] buf = new byte[256];
+ while ((result = in.read(buf)) != -1) {
+ out.write(buf, 0, result);
+ }
+ return new ByteArrayInputStream(out.toByteArray());
+ }
+
+ public static File createTempFile(String suffix) throws IOException {
+ return File.createTempFile("hyts_", suffix, null);
+ }
+
+ public static JarURLConnection getJarURLConnection() throws IOException {
+ JarURLConnection con1 = null;
+ File file = getLocalFile("hyts_att.jar");
+ URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
+ con1 = (JarURLConnection) fUrl1.openConnection();
+ return con1;
+ }
+}
\ No newline at end of file
Propchange: incubator/river/jtsk/trunk/test/src/tests/support/Support_GetLocal.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/river/jtsk/trunk/test/src/tests/support/Support_Resources.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/test/src/tests/support/Support_Resources.java?rev=928394&view=auto
==============================================================================
--- incubator/river/jtsk/trunk/test/src/tests/support/Support_Resources.java (added)
+++ incubator/river/jtsk/trunk/test/src/tests/support/Support_Resources.java Sun Mar 28 12:57:03 2010
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.support;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import tests.support.Support_Configuration;
+
+public class Support_Resources {
+
+ public static final String RESOURCE_PACKAGE = "/tests/resources/";
+
+ public static final String RESOURCE_PACKAGE_NAME = "tests.resources";
+
+ public static InputStream getStream(String name) {
+ return Support_Resources.class.getResourceAsStream(RESOURCE_PACKAGE
+ + name);
+ }
+
+ public static String getURL(String name) {
+ String folder = null;
+ String fileName = name;
+ File resources = createTempFolder();
+ int index = name.lastIndexOf("/");
+ if (index != -1) {
+ folder = name.substring(0, index);
+ name = name.substring(index + 1);
+ }
+ copyFile(resources, folder, name);
+ URL url = null;
+ String resPath = resources.toString();
+ if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') {
+ resPath = resPath.substring(1);
+ }
+ try {
+ url = new URL("file:/" + resPath + "/" + fileName);
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return url.toString();
+ }
+
+ public static File createTempFolder() {
+
+ File folder = null;
+ try {
+ folder = File.createTempFile("hyts_resources", "", null);
+ folder.delete();
+ folder.mkdirs();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ folder.deleteOnExit();
+ return folder;
+ }
+
+ public static void copyFile(File root, String folder, String file) {
+ File f;
+ if (folder != null) {
+ f = new File(root.toString() + "/" + folder);
+ if (!f.exists()) {
+ f.mkdirs();
+ f.deleteOnExit();
+ }
+ } else {
+ f = root;
+ }
+
+ File dest = new File(f.toString() + "/" + file);
+
+ InputStream in = Support_Resources.getStream(folder == null ? file
+ : folder + "/" + file);
+ try {
+ copyLocalFileto(dest, in);
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public static File createTempFile(String suffix) throws IOException {
+ return File.createTempFile("hyts_", suffix, null);
+ }
+
+ public static void copyLocalFileto(File dest, InputStream in)
+ throws FileNotFoundException, IOException {
+ if (!dest.exists()) {
+ FileOutputStream out = new FileOutputStream(dest);
+ int result;
+ byte[] buf = new byte[4096];
+ while ((result = in.read(buf)) != -1) {
+ out.write(buf, 0, result);
+ }
+ in.close();
+ out.close();
+ dest.deleteOnExit();
+ }
+ }
+
+ public static File getExternalLocalFile(String url) throws IOException,
+ MalformedURLException {
+ File resources = createTempFolder();
+ InputStream in = new URL(url).openStream();
+ File temp = new File(resources.toString() + "/local.tmp");
+ copyLocalFileto(temp, in);
+ return temp;
+ }
+
+ public static String getResourceURL(String resource) {
+ return "http://" + Support_Configuration.TestResources + resource;
+ }
+
+ /**
+ * Util method to load resource files
+ *
+ * @param name - name of resource file
+ * @return - resource input stream
+ */
+ public static InputStream getResourceStream(String name) {
+
+ InputStream is = ClassLoader.getSystemClassLoader()
+ .getResourceAsStream(name);
+
+ if (is == null) {
+ throw new RuntimeException("Failed to load resource: " + name);
+ }
+
+ return is;
+ }
+
+ /**
+ * Util method to get absolute path to resource file
+ *
+ * @param name - name of resource file
+ * @return - path to resource
+ */
+ public static String getAbsoluteResourcePath(String name) {
+
+ URL url = ClassLoader.getSystemClassLoader().getResource(name);
+ if (url == null) {
+ throw new RuntimeException("Failed to load resource: " + name);
+ }
+
+ try {
+ return new File(url.toURI()).getAbsolutePath();
+ } catch (URISyntaxException e) {
+ throw new RuntimeException("Failed to load resource: " + name);
+ }
+ }
+}
Propchange: incubator/river/jtsk/trunk/test/src/tests/support/Support_Resources.java
------------------------------------------------------------------------------
svn:eol-style = native