Advertisement
Guest User

PolicyFile not bijective for some characters

a guest
Mar 26th, 2012
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. package net.java.spnego;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.io.StringReader;
  6. import java.io.StringWriter;
  7.  
  8. import org.junit.Test;
  9.  
  10. import sun.security.provider.PolicyParser;
  11. import sun.security.provider.PolicyParser.PrincipalEntry;
  12.  
  13. /**
  14.  * This is a demo of the current bug in the PolicyParser/StreamTokenizer of the JDK.
  15.  * @author BJB
  16.  */
  17. public class PolicyFileTest {
  18.  
  19.     /**
  20.      * This test will work
  21.      */
  22.     @Test
  23.     public void simpleTest() {
  24.         test("ROLE");
  25.     }
  26.  
  27.     /**
  28.      * This test will work
  29.      */
  30.     @Test
  31.     public void backslashTest() {
  32.         test("ROLE_\00\01");
  33.     }
  34.    
  35.     /**
  36.      * This test will fail as in the current PolicyFile reader
  37.      * neither the writer generates the correct value (double backslash) nor the parser
  38.      * handle the double backslash case
  39.      */
  40.     @Test
  41.     public void doubleBackslashTest() {
  42.         test("ROLE_\\00\\01");
  43.     }
  44.    
  45.    
  46.     @SuppressWarnings("restriction")
  47.     void test(String name) {
  48.         try{
  49.            
  50.         final String theClass = "foo.test.PrincipalClass";
  51.         final String theGroup = name;
  52.        
  53.         final StringWriter s = new StringWriter();
  54.        
  55.         PolicyParser pp = new PolicyParser(true);
  56.         PolicyParser.GrantEntry ge = new PolicyParser.GrantEntry(name, name);
  57.         ge.principals.add(new PolicyParser.PrincipalEntry("foo.test.PrincipalClass",theGroup));
  58.         pp.add(ge);
  59.         pp.write(s);
  60.        
  61.         String policy = s.toString();
  62.         StringReader r = new StringReader(policy);
  63.        
  64.         PolicyParser p2 = new PolicyParser(true);
  65.         p2.read(r);
  66.        
  67.         PolicyParser.GrantEntry g2 = p2.grantElements().nextElement();
  68.         PrincipalEntry principal = g2.principals.element();
  69.        
  70.         assertEquals(theClass, principal.getPrincipalClass());
  71.         assertEquals(theGroup, principal.getPrincipalName());
  72.        
  73.         }catch(Exception e){
  74.             fail("Failing test with "+name+"(len="+name.length()+") because: "+e.getLocalizedMessage());
  75.         }
  76.        
  77.     }
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement