SecurityManagerPolicy extends Policy { private static String toString(CodeSource codeSource) { if (codeSource != null && codeSource.getLocation() != null) { return codeSource.getLocation().toString(); } else { return "<null>"; } }  @Override String getName(); @Override FactMetaData getRootFactMetaData(); boolean implies(CodeSource codeSource, Permission permission); void logViolation(CodeSource codeSource, Permission permission); void writeJavaPolicy(Writer writer); void parseJavaPolicy(Reader reader);  }
@Test public void testCollapsePerms() throws IOException { JSONObject jsonPolicy = new JSONObject() .put("file:/tmp/foo.jar", new JSONObject() .put("java.io.FilePermission", new JSONObject() .put("/tmp/foo/bar/baz.txt", new JSONObject().put("read", new JSONObject())) .put("/tmp/foo/bar/bing.txt", new JSONObject().put("read", new JSONObject())) .put("/tmp/foo/blarg.txt", new JSONObject().put("read", new JSONObject())) .put("/tmp/foo2/bar/baz.txt", new JSONObject().put("read,write", new JSONObject())) .put("/tmp/foo2/bar/blarg.txt", new JSONObject().put("read,write", new JSONObject())) .put("/tmp/foo4/bar/baz.txt", new JSONObject().put("read", new JSONObject())) .put("/tmp/foo4/bar/bing.txt", new JSONObject().put("read", new JSONObject())) .put("/tmp/foo4/bar2/baz.txt", new JSONObject().put("read", new JSONObject())) .put("/tmp/foo4/bar2/bing.txt", new JSONObject().put("read", new JSONObject())) ) ); SecurityManagerPolicy policy = createPolicy(); policy.parsePolicy(new StringReader(jsonPolicy.toString())); policy.collapsePolicy(); StringWriter writer = new StringWriter(); policy.writePolicy(writer); jsonPolicy = new JSONObject(writer.toString()); JSONObject fileGrants = jsonPolicy.getJSONObject("file:/tmp/foo.jar").getJSONObject("java.io.FilePermission"); assertEquals(fileGrants.keySet().size(), 4); assertTrue(fileGrants.keySet().contains("/tmp/foo/bar/*")); assertEquals(fileGrants.getJSONObject("/tmp/foo/bar/*").keySet().size(), 1); assertTrue(fileGrants.getJSONObject("/tmp/foo/bar/*").keySet().contains("read")); assertTrue(fileGrants.keySet().contains("/tmp/foo/blarg.txt")); assertEquals(fileGrants.getJSONObject("/tmp/foo/blarg.txt").keySet().size(), 1); assertTrue(fileGrants.getJSONObject("/tmp/foo/blarg.txt").keySet().contains("read")); assertTrue(fileGrants.keySet().contains("/tmp/foo2/bar/*")); assertEquals(fileGrants.getJSONObject("/tmp/foo2/bar/*").keySet().size(), 1); assertTrue(fileGrants.getJSONObject("/tmp/foo2/bar/*").keySet().contains("read,write")); assertTrue(fileGrants.keySet().contains("/tmp/foo4/-")); assertEquals(fileGrants.getJSONObject("/tmp/foo4/-").keySet().size(), 1); assertTrue(fileGrants.getJSONObject("/tmp/foo4/-").keySet().contains("read")); }