public void testImplicitPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-implicit-dirmode.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);

    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);
    archiver.setFileMode(0222);
    archiver.addFile(new File("pom.xml"), "fizz/buzz/pom.xml");
    archiver.setDefaultDirectoryMode(0530);
    archiver.setDirectoryMode(-1); // Not forced mode
    archiver.setFileMode(0111);
    archiver.addFile(new File("pom.xml"), "fazz/bazz/pam.xml");
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("fizz/");
    assertEquals(040641, fizz.getUnixMode());
    ZipArchiveEntry pom = zf.getEntry("fizz/buzz/pom.xml");
    assertEquals(0100222, pom.getUnixMode());

    ZipArchiveEntry fazz = zf.getEntry("fazz/");
    assertEquals(040530, fazz.getUnixMode());
    ZipArchiveEntry pam = zf.getEntry("fazz/bazz/pam.xml");
    assertEquals(0100111, pam.getUnixMode());
  }
  public void testOverddidenPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-overriden-modes.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);
    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);
    archiver.setFileMode(0777);
    archiver.addDirectory(new File("src/test/resources/symlinks/src"));
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("symDir");
    assertTrue(fizz.isUnixSymlink());
    ZipArchiveEntry symR = zf.getEntry("symR");
    assertTrue(symR.isUnixSymlink());
  }
Ejemplo n.º 3
0
  /**
   * Test of compressPathToZip method, of class ZipManager.
   *
   * @throws Exception
   */
  @Test
  public void testCompressPathToZip() throws Exception {
    File path = new File(mavenTargetDirectoryRule.getResourceTestDirFile(), "ZipSample");
    File outfile = new File(tempDir, "testCompressPathToZip.zip");
    ZipUtil.compressPathToZip(path, outfile);
    ZipFile zipFile = new ZipFile(outfile, CharEncoding.UTF_8);
    try {
      Enumeration<? extends ZipEntry> entries = zipFile.getEntries();
      assertThat(zipFile.getEncoding(), is(CharEncoding.UTF_8));
      int nbEntries = 0;
      while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        nbEntries++;
      }
      assertThat(nbEntries, is(5));
      assertThat(zipFile.getEntry("ZipSample/simple.txt"), is(notNullValue()));
      assertThat(zipFile.getEntry("ZipSample/level1/simple.txt"), is(notNullValue()));
      assertThat(zipFile.getEntry("ZipSample/level1/level2b/simple.txt"), is(notNullValue()));
      assertThat(zipFile.getEntry("ZipSample/level1/level2a/simple.txt"), is(notNullValue()));

      ZipEntry accentuatedEntry =
          zipFile.getEntry("ZipSample/level1/level2a/s\u00efmplifi\u00e9.txt");
      if (accentuatedEntry == null) {
        accentuatedEntry =
            zipFile.getEntry(
                "ZipSample/level1/level2a/"
                    + new String("sïmplifié.txt".getBytes("UTF-8"), Charset.defaultCharset()));
      }
      assertThat(accentuatedEntry, is(notNullValue()));
      assertThat(zipFile.getEntry("ZipSample/level1/level2c/"), is(nullValue()));
    } finally {
      zipFile.close();
    }
  }
Ejemplo n.º 4
0
 /**
  * Test of compressStreamToZip method, of class ZipManager.
  *
  * @throws Exception
  */
 @Test
 public void testCompressStreamToZip() throws Exception {
   InputStream inputStream =
       this.getClass().getClassLoader().getResourceAsStream("FrenchScrum.odp");
   String filePathNameToCreate =
       separatorChar + "dir1" + separatorChar + "dir2" + separatorChar + "FrenchScrum.odp";
   File outfile = new File(tempDir, "testCompressStreamToZip.zip");
   ZipUtil.compressStreamToZip(inputStream, filePathNameToCreate, outfile.getPath());
   inputStream.close();
   assertThat(outfile, is(notNullValue()));
   assertThat(outfile.exists(), is(true));
   assertThat(outfile.isFile(), is(true));
   int result = ZipUtil.getNbFiles(outfile);
   assertThat(result, is(1));
   ZipFile zipFile = new ZipFile(outfile);
   assertThat(zipFile.getEntry("/dir1/dir2/FrenchScrum.odp"), is(notNullValue()));
   zipFile.close();
 }
  /* package */ File createAndAddCustomizedAndroidManifestToSelendroidServer()
      throws IOException, ShellCommandException, AndroidSdkException {
    String targetPackageName = applicationUnderTest.getBasePackage();
    File tempdir =
        new File(
            FileUtils.getTempDirectoryPath()
                + File.separatorChar
                + targetPackageName
                + System.currentTimeMillis());

    if (!tempdir.exists()) {
      tempdir.mkdirs();
    }

    File customizedManifest = new File(tempdir, "AndroidManifest.xml");
    log.info(
        "Adding target package '"
            + targetPackageName
            + "' to "
            + customizedManifest.getAbsolutePath());

    // add target package
    InputStream inputStream = getResourceAsStream(selendroidApplicationXmlTemplate);
    if (inputStream == null) {
      throw new SelendroidException("AndroidApplication.xml template file was not found.");
    }
    String content = IOUtils.toString(inputStream, Charset.defaultCharset().displayName());

    // find the first occurance of "package" and appending the targetpackagename to begining
    int i = content.toLowerCase().indexOf("package");
    int cnt = 0;
    for (; i < content.length(); i++) {
      if (content.charAt(i) == '\"') {
        cnt++;
      }
      if (cnt == 2) {
        break;
      }
    }
    content = content.substring(0, i) + "." + targetPackageName + content.substring(i);
    log.info("Final Manifest File:\n" + content);
    content = content.replaceAll(SELENDROID_TEST_APP_PACKAGE, targetPackageName);
    // Seems like this needs to be done
    if (content.contains(ICON)) {
      content = content.replaceAll(ICON, "");
    }

    OutputStream outputStream = new FileOutputStream(customizedManifest);
    IOUtils.write(content, outputStream, Charset.defaultCharset().displayName());
    IOUtils.closeQuietly(inputStream);
    IOUtils.closeQuietly(outputStream);

    // adding the xml to an empty apk
    CommandLine createManifestApk = new CommandLine(AndroidSdk.aapt());

    createManifestApk.addArgument("package", false);
    createManifestApk.addArgument("-M", false);
    createManifestApk.addArgument(customizedManifest.getAbsolutePath(), false);
    createManifestApk.addArgument("-I", false);
    createManifestApk.addArgument(AndroidSdk.androidJar(), false);
    createManifestApk.addArgument("-F", false);
    createManifestApk.addArgument(
        tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk", false);
    createManifestApk.addArgument("-f", false);
    log.info(ShellCommand.exec(createManifestApk, 20000L));

    ZipFile manifestApk =
        new ZipFile(new File(tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk"));
    ZipArchiveEntry binaryManifestXml = manifestApk.getEntry("AndroidManifest.xml");

    File finalSelendroidServerFile = new File(tempdir.getAbsolutePath() + "selendroid-server.apk");
    ZipArchiveOutputStream finalSelendroidServer =
        new ZipArchiveOutputStream(finalSelendroidServerFile);
    finalSelendroidServer.putArchiveEntry(binaryManifestXml);
    IOUtils.copy(manifestApk.getInputStream(binaryManifestXml), finalSelendroidServer);

    ZipFile selendroidPrebuildApk = new ZipFile(selendroidServer.getAbsolutePath());
    Enumeration<ZipArchiveEntry> entries = selendroidPrebuildApk.getEntries();
    for (; entries.hasMoreElements(); ) {
      ZipArchiveEntry dd = entries.nextElement();
      finalSelendroidServer.putArchiveEntry(dd);

      IOUtils.copy(selendroidPrebuildApk.getInputStream(dd), finalSelendroidServer);
    }

    finalSelendroidServer.closeArchiveEntry();
    finalSelendroidServer.close();
    manifestApk.close();
    log.info("file: " + finalSelendroidServerFile.getAbsolutePath());
    return finalSelendroidServerFile;
  }
  public void testForcedFileModes() throws IOException {
    File step1file = new File("target/output/forced-file-mode.zip");
    {
      final ZipArchiver zipArchiver = getZipArchiver(step1file);
      zipArchiver.setFileMode(0077);
      zipArchiver.setDirectoryMode(0007);
      PlexusIoResourceAttributes attrs =
          new SimpleResourceAttributes(123, "fred", 22, "filntstones", 0111);
      PlexusIoResource resource =
          ResourceFactory.createResource(
              new File("src/test/resources/folders/File.txt"), "Test.txt", null, attrs);
      zipArchiver.addResource(resource, "Test2.txt", 0707);
      PlexusIoFileResourceCollection files = new PlexusIoFileResourceCollection();
      files.setBaseDir(new File("src/test/resources/folders"));
      files.setPrefix("sixsixsix/");
      zipArchiver.addResources(files);

      zipArchiver.createArchive();

      ZipFile zf = new ZipFile(step1file);
      fileModeAssert(040007, zf.getEntry("sixsixsix/a/").getUnixMode());
      fileModeAssert(0100077, zf.getEntry("sixsixsix/b/FileInB.txt").getUnixMode());
      fileModeAssert(0100707, zf.getEntry("Test2.txt").getUnixMode());
      zf.close();
    }

    File Step2file = new File("target/output/forced-file-mode-from-zip.zip");
    {
      final ZipArchiver za2 = getZipArchiver(Step2file);
      za2.setFileMode(0666);
      za2.setDirectoryMode(0676);

      PlexusIoZipFileResourceCollection zipSrc = new PlexusIoZipFileResourceCollection();
      zipSrc.setFile(step1file);
      zipSrc.setPrefix("zz/");
      za2.addResources(zipSrc);
      za2.createArchive();
      ZipFile zf = new ZipFile(Step2file);
      fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode());
      fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode());
      zf.close();
    }

    File step3file = new File("target/output/forced-file-mode-from-zip2.zip");
    {
      final ZipArchiver za2 = getZipArchiver(step3file);
      za2.setFileMode(0666);
      za2.setDirectoryMode(0676);

      PlexusArchiverZipFileResourceCollection zipSrc =
          new PlexusArchiverZipFileResourceCollection();
      zipSrc.setFile(step1file);
      zipSrc.setPrefix("zz/");
      za2.addResources(zipSrc);
      za2.createArchive();
      ZipFile zf = new ZipFile(Step2file);
      fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode());
      fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode());
      zf.close();
    }
  }
  public void testCreateArchiveWithDetectedModes() throws Exception {

    String[] executablePaths = {"path/to/executable", "path/to/executable.bat"};

    String[] confPaths = {"path/to/etc/file", "path/to/etc/file2"};

    String[] logPaths = {"path/to/logs/log.txt"};

    int exeMode = 0777;
    int confMode = 0600;
    int logMode = 0640;

    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
      StackTraceElement e = new Throwable().getStackTrace()[0];
      System.out.println(
          "Cannot execute test: " + e.getMethodName() + " on " + System.getProperty("os.name"));
      return;
    }

    File tmpDir = null;
    try {
      tmpDir = File.createTempFile("zip-with-chmod.", ".dir");
      tmpDir.delete();

      tmpDir.mkdirs();

      for (String executablePath : executablePaths) {
        writeFile(tmpDir, executablePath, exeMode);
      }

      for (String confPath : confPaths) {
        writeFile(tmpDir, confPath, confMode);
      }

      for (String logPath : logPaths) {
        writeFile(tmpDir, logPath, logMode);
      }

      {
        Map<String, PlexusIoResourceAttributes> attributesByPath =
            PlexusIoResourceAttributeUtils.getFileAttributesByPath(tmpDir);
        for (String path : executablePaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + exeMode, exeMode, attrs.getOctalMode());
        }

        for (String path : confPaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + confMode,
              confMode,
              attrs.getOctalMode());
        }

        for (String path : logPaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + logMode, logMode, attrs.getOctalMode());
        }
      }

      File zipFile = getTestFile("target/output/zip-with-modes.zip");

      ZipArchiver archiver = getZipArchiver(zipFile);

      archiver.addDirectory(tmpDir);
      archiver.createArchive();

      assertTrue(zipFile.exists());

      File zipFile2 = getTestFile("target/output/zip-with-modes-L2.zip");

      archiver = getZipArchiver();
      archiver.setDestFile(zipFile2);

      archiver.addArchivedFileSet(zipFile);
      archiver.createArchive();

      ZipFile zf = new ZipFile(zipFile2);

      for (String path : executablePaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, mode);
      }

      for (String path : confPaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, mode);
      }

      for (String path : logPaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, mode);
      }
    } finally {
      if (tmpDir != null && tmpDir.exists()) {
        try {
          FileUtils.forceDelete(tmpDir);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }