示例#1
0
 /** @tests java.lang.Package#isSealed(java.net.URL) */
 public void test_isSealedLjava_net_URL() throws Exception {
   Package p = getTestPackage("hyts_c.jar", "p.C");
   assertFalse(
       "Package isSealed returns wrong boolean (1)",
       p.isSealed(new URL("file:/" + resPath + "/")));
   assertTrue(
       "Package isSealed returns wrong boolean (2)",
       p.isSealed(new URL("file:/" + resPath + "/Package/hyts_c.jar")));
 }
 /**
  * Helper method called to check whether it is acceptable to create a new class in package that
  * given class is part of. This is used to prevent certain class of failures, related to access
  * limitations: for example, we can not add classes in sealed packages, or core Java packages
  * (java.*).
  *
  * @since 2.2.1
  */
 public static boolean canAddClassInPackageOf(Class<?> cls) {
   final Package beanPackage = cls.getPackage();
   if (beanPackage != null) {
     // 01-May-2013, tatu: How about "javax."?
     if (beanPackage.isSealed() || beanPackage.getName().startsWith("java.")) {
       return false;
     }
   }
   return true;
 }
 /*
  * Retrieve the package using the specified package name.
  * If non-null, verify the package using the specified code
  * source and manifest.
  */
 private Package _getAndVerifyPackage(String pkgname, Manifest man, URL url) {
   Package pkg = getPackage(pkgname);
   if (pkg != null) {
     // Package found, so check package sealing.
     if (pkg.isSealed()) {
       // Verify that code source URL is the same.
       if (!pkg.isSealed(url)) {
         throw new SecurityException("sealing violation: package " + pkgname + " is sealed");
       }
     } else {
       // Make sure we are not attempting to seal the package
       // at this code source URL.
       if ((man != null) && _isSealed(pkgname, man)) {
         throw new SecurityException(
             "sealing violation: can't seal package " + pkgname + ": already loaded");
       }
     }
   }
   return pkg;
 }
示例#4
0
 private Class<?> defineClass(final String name, final ClassSpec classSpec) {
   // Ensure that the package is loaded
   final int lastIdx = name.lastIndexOf('.');
   if (lastIdx != -1) {
     // there's a package name; get the Package for it
     final String packageName = name.substring(0, lastIdx);
     final Package pkg = getPackage(packageName);
     if (pkg != null) {
       // Package is defined already
       if (pkg.isSealed() && !pkg.isSealed(classSpec.getCodeSource().getLocation())) {
         // use the same message as the JDK
         throw new SecurityException("sealing violation: package " + packageName + " is sealed");
       }
     } else {
       final PackageSpec spec;
       try {
         spec = getModule().getLocalPackageSpec(name);
         definePackage(packageName, spec);
       } catch (IOException e) {
         definePackage(packageName, null);
       }
     }
   }
   final Class<?> newClass;
   try {
     final byte[] bytes = classSpec.getBytes();
     newClass = defineClass(name, bytes, 0, bytes.length, classSpec.getCodeSource());
   } catch (Error e) {
     if (debugDefines) System.err.println("Failed to define class '" + name + "': " + e);
     throw e;
   } catch (RuntimeException e) {
     if (debugDefines) System.err.println("Failed to define class '" + name + "': " + e);
     throw e;
   }
   final AssertionSetting setting = classSpec.getAssertionSetting();
   if (setting != AssertionSetting.INHERIT) {
     setClassAssertionStatus(name, setting == AssertionSetting.ENABLED);
   }
   return newClass;
 }
示例#5
0
 /** @tests java.lang.Package#isSealed() */
 public void test_isSealed() throws Exception {
   Package p = getTestPackage("hyts_pq.jar", "p.q.C");
   assertTrue("Package isSealed returns wrong boolean", p.isSealed());
 }
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "definePackage",
      args = {
        java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.net.URL.class
      })
  public void test_definePackage() {

    PackageClassLoader pcl = new PackageClassLoader(getClass().getClassLoader());

    String[] packageProperties = {
      "test.package", "title", "1.0", "Vendor", "Title", "1.1", "implementation vendor"
    };

    URL url = null;
    try {
      url = new URL("file:");
    } catch (MalformedURLException e) {
      fail("MalformedURLException was thrown.");
    }
    pcl.definePackage(
        packageProperties[0],
        packageProperties[1],
        packageProperties[2],
        packageProperties[3],
        packageProperties[4],
        packageProperties[5],
        packageProperties[6],
        url);

    Package pack = pcl.getPackage(packageProperties[0]);
    assertEquals(packageProperties[1], pack.getSpecificationTitle());
    assertEquals(packageProperties[2], pack.getSpecificationVersion());
    assertEquals(packageProperties[3], pack.getSpecificationVendor());
    assertEquals(packageProperties[4], pack.getImplementationTitle());
    assertEquals(packageProperties[5], pack.getImplementationVersion());
    assertEquals(packageProperties[6], pack.getImplementationVendor());
    assertTrue(pack.isSealed(url));
    assertTrue(pack.isSealed());

    try {
      pcl.definePackage(
          packageProperties[0],
          packageProperties[1],
          packageProperties[2],
          packageProperties[3],
          packageProperties[4],
          packageProperties[5],
          packageProperties[6],
          null);
      fail("IllegalArgumentException was not thrown.");
    } catch (IllegalArgumentException iae) {
      // expected
    }

    pcl.definePackage("test.package.test", null, null, null, null, null, null, null);
    pack = pcl.getPackage("test.package.test");
    assertNull(pack.getSpecificationTitle());
    assertNull(pack.getSpecificationVersion());
    assertNull(pack.getSpecificationVendor());
    assertNull(pack.getImplementationTitle());
    assertNull(pack.getImplementationVersion());
    assertNull(pack.getImplementationVendor());
    assertFalse(pack.isSealed());
  }
  private void definePackage(String className, URL jarUrl, Manifest manifest) {
    int packageEnd = className.lastIndexOf('.');
    if (packageEnd < 0) {
      return;
    }

    String packageName = className.substring(0, packageEnd);
    String packagePath = packageName.replace('.', '/') + "/";

    Attributes packageAttributes = null;
    Attributes mainAttributes = null;
    if (manifest != null) {
      packageAttributes = manifest.getAttributes(packagePath);
      mainAttributes = manifest.getMainAttributes();
    }
    Package pkg = getPackage(packageName);
    if (pkg != null) {
      if (pkg.isSealed()) {
        if (!pkg.isSealed(jarUrl)) {
          throw new SecurityException(
              "Package was already sealed with another URL: package="
                  + packageName
                  + ", url="
                  + jarUrl);
        }
      } else {
        if (isSealed(packageAttributes, mainAttributes)) {
          throw new SecurityException(
              "Package was already been loaded and not sealed: package="
                  + packageName
                  + ", url="
                  + jarUrl);
        }
      }
    } else {
      String specTitle =
          getAttribute(Attributes.Name.SPECIFICATION_TITLE, packageAttributes, mainAttributes);
      String specVendor =
          getAttribute(Attributes.Name.SPECIFICATION_VENDOR, packageAttributes, mainAttributes);
      String specVersion =
          getAttribute(Attributes.Name.SPECIFICATION_VERSION, packageAttributes, mainAttributes);
      String implTitle =
          getAttribute(Attributes.Name.IMPLEMENTATION_TITLE, packageAttributes, mainAttributes);
      String implVendor =
          getAttribute(Attributes.Name.IMPLEMENTATION_VENDOR, packageAttributes, mainAttributes);
      String implVersion =
          getAttribute(Attributes.Name.IMPLEMENTATION_VERSION, packageAttributes, mainAttributes);

      URL sealBase = null;
      if (isSealed(packageAttributes, mainAttributes)) {
        sealBase = jarUrl;
      }

      definePackage(
          packageName,
          specTitle,
          specVersion,
          specVendor,
          implTitle,
          implVersion,
          implVendor,
          sealBase);
    }
  }