public void testReadPermission() throws SAXException, IOException, ParserConfigurationException {
    final InputStream inputStream = this.getClass().getResourceAsStream("policy-def-test.xml");
    final FilePermissionStore permStore = new FilePermissionStore(inputStream);
    final Permissions perms = permStore.loadPermissions(new SimplePrincipal("christian"));
    assertTrue(perms != null);
    final Enumeration<Permission> enumPerms = perms.elements();
    Permission p;
    int count = 0;

    while (enumPerms.hasMoreElements()) {
      p = enumPerms.nextElement();
      count++;
      if (p instanceof FilePermission) {
        final FilePermission fp = (FilePermission) p;
        assertEquals("*.tmp", fp.getName());
        assertTrue(fp.getActions().equals("read"));
      } else {
        if (p instanceof TestcasePermission) {
          final TestcasePermission tcp = (TestcasePermission) p;
          assertEquals("testPerm", tcp.getName());
        } else {
          assertTrue("unknown permission " + p, false);
        }
      }
    }
    assertTrue(count == 2);
  }
Beispiel #2
0
 @SuppressWarnings("unchecked")
 public static void main(String[] args)
     throws ClassNotFoundException, NoSuchMethodException, IllegalArgumentException,
         InstantiationException, IllegalAccessException, InvocationTargetException {
   Class c = Class.forName("java.io.FilePermission");
   Class[] paramTypes = new Class[] {"".getClass(), "".getClass()};
   Constructor<FilePermission> constr = c.getConstructor(paramTypes);
   Object[] params = new String[] {"log.txt", "read"};
   FilePermission fp = constr.newInstance(params);
   fp.toString();
 }
Beispiel #3
0
  /**
   * Checks if this ExecPermission object "implies" the specified permission.
   *
   * <p>More specifically, this method returns true if:
   *
   * <p>
   *
   * <ul>
   *   <li><i>p</i> is an instanceof ExecPermission,
   *       <p>and
   *   <li><i>p</i>'s pathname is implied by this object's pathname. For example, "/tmp/*" implies
   *       "/tmp/foo", since "/tmp/*" encompasses the "/tmp" directory and all files in that
   *       directory, including the one named "foo".
   * </ul>
   *
   * @param p the permission to check against.
   * @return true if the specified permission is implied by this object, false if not.
   */
  public boolean implies(Permission p) {
    if (!(p instanceof ExecPermission)) return false;

    ExecPermission that = (ExecPermission) p;

    return fp.implies(that.fp);
  }
Beispiel #4
0
  /**
   * Checks two ExecPermission objects for equality. Checks that <i>obj</i>'s class is the same as
   * this object's class and has the same name as this object.
   *
   * <p>
   *
   * @param obj the object we are testing for equality with this object.
   * @return true if <i>obj</i> is an ExecPermission, and has the same pathname as this
   *     ExecPermission object, false otherwise.
   */
  public boolean equals(Object obj) {
    if (obj == this) return true;

    if (!(obj instanceof ExecPermission)) return false;

    ExecPermission that = (ExecPermission) obj;

    return fp.equals(that.fp);
  }
  /* ------------------------------------------------------------ */
  @BeforeClass
  public static void setUp() throws Exception {
    if (data != null) return;

    File file = new File(__userDir);
    file = new File(file.getCanonicalPath());
    URI uri = file.toURI();
    __userURL = uri.toURL();

    __userURL =
        new URL(__userURL.toString() + "src/test/resources/org/eclipse/jetty/util/resource/");
    FilePermission perm = (FilePermission) __userURL.openConnection().getPermission();
    __userDir = new File(perm.getName()).getCanonicalPath() + File.separatorChar;
    __relDir =
        "src/test/resources/org/eclipse/jetty/util/resource/".replace('/', File.separatorChar);

    tmpFile = File.createTempFile("test", null).getCanonicalFile();
    tmpFile.deleteOnExit();

    data = new Data[50];
    int i = 0;

    data[i++] = new Data(tmpFile.toString(), EXISTS, !DIR);

    int rt = i;
    data[i++] = new Data(__userURL, EXISTS, DIR);
    data[i++] = new Data(__userDir, EXISTS, DIR);
    data[i++] = new Data(__relDir, EXISTS, DIR);
    data[i++] = new Data(__userURL + "resource.txt", EXISTS, !DIR);
    data[i++] = new Data(__userDir + "resource.txt", EXISTS, !DIR);
    data[i++] = new Data(__relDir + "resource.txt", EXISTS, !DIR);
    data[i++] = new Data(__userURL + "NoName.txt", !EXISTS, !DIR);
    data[i++] = new Data(__userDir + "NoName.txt", !EXISTS, !DIR);
    data[i++] = new Data(__relDir + "NoName.txt", !EXISTS, !DIR);

    data[i++] = new Data(data[rt], "resource.txt", EXISTS, !DIR);
    data[i++] = new Data(data[rt], "/resource.txt", EXISTS, !DIR);
    data[i++] = new Data(data[rt], "NoName.txt", !EXISTS, !DIR);
    data[i++] = new Data(data[rt], "/NoName.txt", !EXISTS, !DIR);

    int td = i;
    data[i++] = new Data(data[rt], "TestData", EXISTS, DIR);
    data[i++] = new Data(data[rt], "TestData/", EXISTS, DIR);
    data[i++] = new Data(data[td], "alphabet.txt", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    data[i++] = new Data("jar:file:/somejar.jar!/content/", !EXISTS, DIR);
    data[i++] = new Data("jar:file:/somejar.jar!/", !EXISTS, DIR);

    int tj = i;
    data[i++] = new Data("jar:" + __userURL + "TestData/test.zip!/", EXISTS, DIR);
    data[i++] = new Data(data[tj], "Unkown", !EXISTS, !DIR);
    data[i++] = new Data(data[tj], "/Unkown/", !EXISTS, DIR);

    data[i++] = new Data(data[tj], "subdir", EXISTS, DIR);
    data[i++] = new Data(data[tj], "/subdir/", EXISTS, DIR);
    data[i++] = new Data(data[tj], "alphabet", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    data[i++] = new Data(data[tj], "/subdir/alphabet", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    Resource base = Resource.newResource(__userDir);
    Resource dir0 = base.addPath("TestData");
    assertTrue(dir0.isDirectory());
    assertTrue(dir0.toString().endsWith("/"));
    assertTrue(dir0.getAlias() == null);
    Resource dir1 = base.addPath("TestData/");
    assertTrue(dir1.isDirectory());
    assertTrue(dir1.toString().endsWith("/"));
    assertTrue(dir1.getAlias() == null);
  }