package net.java.spnego; import static org.junit.Assert.*; import java.io.StringReader; import java.io.StringWriter; import org.junit.Test; import sun.security.provider.PolicyParser; import sun.security.provider.PolicyParser.PrincipalEntry; /** * This is a demo of the current bug in the PolicyParser/StreamTokenizer of the JDK. * @author JB BUGEAUD */ public class PolicyFileTest { /** * This test will work */ @Test public void simpleTest() { test("ROLE"); } /** * This test will work */ @Test public void backslashTest() { test("ROLE_\00\01"); } /** * This test will fail as in the current PolicyFile reader * neither the writer generates the correct value (double backslash) nor the parser * handle the double backslash case */ @Test public void doubleBackslashTest() { test("ROLE_\\00\\01"); } /** * Fail for the same reason */ @Test public void tripleBackslashTest() { test("ROLE_\\\00\\\01"); } /** * Fail for the same reason */ @Test public void quadrupleBackslashTest() { test("ROLE_\\\\00\\\\01"); } @SuppressWarnings("restriction") void test(String name) { try{ final String theClass = "foo.test.PrincipalClass"; final String theGroup = name; final StringWriter s = new StringWriter(); PolicyParser pp = new PolicyParser(true); PolicyParser.GrantEntry ge = new PolicyParser.GrantEntry(name, name); ge.principals.add(new PolicyParser.PrincipalEntry("foo.test.PrincipalClass",theGroup)); pp.add(ge); pp.write(s); String policy = s.toString(); StringReader r = new StringReader(policy); PolicyParser p2 = new PolicyParser(true); p2.read(r); PolicyParser.GrantEntry g2 = p2.grantElements().nextElement(); PrincipalEntry principal = g2.principals.element(); assertEquals(theClass, principal.getPrincipalClass()); assertEquals(theGroup, principal.getPrincipalName()); }catch(Exception e){ fail("Failing test with "+name+"(len="+name.length()+") because: "+e.getLocalizedMessage()); } } }