Esempio n. 1
0
 /** Test of isAbsolute method, of class PathUtils. */
 public void testIsAbsolute() {
   System.out.println("isAbsolute");
   assertTrue(PathUtils.isAbsolute("/absolute"));
   assertTrue(PathUtils.isAbsolute("/any/absolute/paths"));
   assertFalse(PathUtils.isAbsolute("relative path"));
   assertFalse(PathUtils.isAbsolute("../../data"));
   assertFalse(PathUtils.isAbsolute("http://www.seznam.cz"));
 }
Esempio n. 2
0
 /** Test of processResource method, of class PathUtils. */
 public void testProcessResource() {
   System.out.println("processResource");
   // Tests of absolute and remotes
   assertEquals(
       "http://www.seznam.cz", PathUtils.processResource("http://www.seznam.cz", Paths.get("")));
   assertEquals(
       "https://a.b/q/w/e",
       PathUtils.processResource("https://a.b/q/w/e", Paths.get("some/context/file.lesscache")));
   assertEquals("/file", PathUtils.processResource("/file", Paths.get("")));
   assertEquals(
       "/abs/path/file",
       PathUtils.processResource("/abs/path/file", Paths.get("some/context/file.lesscache")));
   // Tests relative
   assertEquals(
       Paths.get("some/context/file.txt").toString(),
       PathUtils.processResource("file.txt", Paths.get("some/context")));
   assertEquals(
       Paths.get("some/context/file.txt").toString(),
       PathUtils.processResource("file.txt", Paths.get("some/context/")));
   assertEquals(
       Paths.get("some/file.txt").toString(),
       PathUtils.processResource("../file.txt", Paths.get("some/context")));
   assertEquals(
       Paths.get("../../file.txt").toString(),
       PathUtils.processResource("../file.txt", Paths.get("../")));
   assertEquals(
       Paths.get("../file.txt").toString(),
       PathUtils.processResource("file.txt", Paths.get("../")));
 }
Esempio n. 3
0
  @SuppressWarnings({"OverlyComplexMethod"})
  private static String removeDotSegments(String path) {

    if (null == path) {
      return null;
    }

    List<String> outputSegments = new LinkedList<>();

    while (path.length() > 0) {
      if (path.startsWith("../")) { // rule 2A
        path = PathUtils.trimLeadingSlashes(path.substring(3));
      } else if (path.startsWith("./")) { // rule 2A
        path = PathUtils.trimLeadingSlashes(path.substring(2));
      } else if (path.startsWith("/./")) { // rule 2B
        path = "/" + PathUtils.trimLeadingSlashes(path.substring(3));
      } else if ("/.".equals(path)) { // rule 2B
        path = "/";
      } else if (path.startsWith("/../")) { // rule 2C
        path = "/" + PathUtils.trimLeadingSlashes(path.substring(4));
        if (!outputSegments.isEmpty()) { // removing last segment if any
          outputSegments.remove(outputSegments.size() - 1);
        }
      } else if ("/..".equals(path)) { // rule 2C
        path = "/";
        if (!outputSegments.isEmpty()) { // removing last segment if any
          outputSegments.remove(outputSegments.size() - 1);
        }
      } else if ("..".equals(path) || ".".equals(path)) { // rule 2D
        path = "";
      } else { // rule E
        int slashStartSearchIndex;
        if (path.startsWith("/")) {
          path = "/" + PathUtils.trimLeadingSlashes(path.substring(1));
          slashStartSearchIndex = 1;
        } else {
          slashStartSearchIndex = 0;
        }
        int segLength = path.indexOf('/', slashStartSearchIndex);
        if (-1 == segLength) {
          segLength = path.length();
        }
        outputSegments.add(path.substring(0, segLength));
        path = path.substring(segLength);
      }
    }

    StringBuffer result = new StringBuffer();
    for (String segment : outputSegments) {
      result.append(segment);
    }

    return result.toString();
  }
  @Test
  public void notExistingBackupDir() throws Exception {
    Path dirToBkp = Files.createTempDirectory("dirToBkp2");

    Path parentdirfile1 = createTempFile(dirToBkp, "FILE_1", ".tmp");
    Path parentdirfile2 = createTempFile(dirToBkp, "FILE_2", ".tmp");

    new BackupHelper(conf).backupFileOrDir(dirToBkp);

    Path bkp = PathUtils.get(conf.getBackupDir(), dirToBkp);
    assertThat(exists(bkp), is(true));
    assertThat(exists(PathUtils.get(bkp, parentdirfile1.getFileName())), is(true));
    assertThat(exists(PathUtils.get(bkp, parentdirfile2.getFileName())), is(true));
  }
  public static void writeNodeContentsToWriter(JSONWriter write, Node node)
      throws RepositoryException, JSONException {
    // Since removal of bigstore we add in jcr:path and jcr:name
    write.key("jcr:path");
    write.value(PathUtils.translateAuthorizablePath(node.getPath()));
    write.key("jcr:name");
    write.value(node.getName());

    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
      Property prop = properties.nextProperty();
      String name = prop.getName();
      write.key(name);
      PropertyDefinition propertyDefinition = prop.getDefinition();
      int propertyType = prop.getType();
      if (PropertyType.BINARY == propertyType) {
        if (propertyDefinition.isMultiple()) {
          write.array();
          for (long l : prop.getLengths()) {
            write.value("binary-length:" + String.valueOf(l));
          }
          write.endArray();
        } else {
          write.value("binary-length:" + String.valueOf(prop.getLength()));
        }
      } else {
        if (propertyDefinition.isMultiple()) {
          Value[] values = prop.getValues();
          write.array();
          for (Value value : values) {
            Object ovalue = stringValue(value);
            if (isUserPath(name, ovalue)) {
              write.value(PathUtils.translateAuthorizablePath(ovalue));
            } else {
              write.value(ovalue);
            }
          }
          write.endArray();
        } else {
          Object value = stringValue(prop.getValue());
          if (isUserPath(name, value)) {
            write.value(PathUtils.translateAuthorizablePath(value));
          } else {
            write.value(value);
          }
        }
      }
    }
  }
Esempio n. 6
0
  @Test
  public void bookFilePathWithSeriesInItWillReturnBookWithSeriesInItsName() throws Exception {
    String author = "Лукьяненко Сергей";
    String bookName = "Лукьяненко 3 Калеки";
    String seriesName = "Геном";
    final String fileName = bookName + ".fb2.zip";
    final String path = PathUtils.createPath("ru", "Л", author, seriesName, fileName);

    mockery.checking(
        new Expectations() {
          {
            allowing(fs).exists(path);
            will(returnValue(true));

            allowing(fs).isDirectory(path);
            will(returnValue(false));

            allowing(fs).getName(path);
            will(returnValue(fileName));
          }
        });

    Book item = (Book) extractor.get(path);
    assertThat(item.name, hasWords(seriesName));
  }
Esempio n. 7
0
  /**
   * Validates the given entry name by removing different slashes that might appear in the begining
   * of the name and any occurences of relative paths like "../", so we can protect from path
   * traversal attacks
   *
   * @param entryName Name of zip entry
   */
  private static String validateEntryName(String entryName) {
    entryName = FilenameUtils.separatorsToUnix(entryName);
    entryName = PathUtils.trimLeadingSlashes(entryName);
    entryName = removeDotSegments(entryName);

    return entryName;
  }
 public static String makeTempFilePath(String cmd) throws UnsupportedEncodingException {
   String dir_path = PathUtils.getTempFolderPath() + File.separator + ParamCommons.SERVICE_PORT;
   File dir = new File(dir_path);
   if (!dir.exists()) {
     dir.mkdirs();
   }
   return dir_path + File.separator + MD5Utils.MD5Hash(cmd, charCode) + ".sh";
 }
Esempio n. 9
0
  @Test
  public void validatePathTest() throws InvalidPathException {
    // check valid paths
    PathUtils.validatePath("/foo/bar");
    PathUtils.validatePath("/foo/bar/");
    PathUtils.validatePath("/foo/./bar/");
    PathUtils.validatePath("/foo/././bar/");
    PathUtils.validatePath("/foo/../bar");
    PathUtils.validatePath("/foo/../bar/");

    // check invalid paths
    LinkedList<String> invalidPaths = new LinkedList<String>();
    invalidPaths.add(null);
    invalidPaths.add("");
    invalidPaths.add(" /");
    invalidPaths.add("/ ");
    for (String invalidPath : invalidPaths) {
      try {
        PathUtils.validatePath(invalidPath);
        Assert.fail("validatePath(" + invalidPath + ") did not fail");
      } catch (InvalidPathException ipe) {
        // this is expected
      }
    }
  }
Esempio n. 10
0
  @Test
  public void getParentTest() throws InvalidPathException {
    // get a parent that is non-root
    Assert.assertEquals("/foo", PathUtils.getParent("/foo/bar"));
    Assert.assertEquals("/foo", PathUtils.getParent("/foo/bar/"));
    Assert.assertEquals("/foo", PathUtils.getParent("/foo/./bar/"));
    Assert.assertEquals("/foo", PathUtils.getParent("/foo/././bar/"));

    // get a parent that is root
    Assert.assertEquals("/", PathUtils.getParent("/foo"));
    Assert.assertEquals("/", PathUtils.getParent("/foo/bar/../"));
    Assert.assertEquals("/", PathUtils.getParent("/foo/../bar/"));

    // get parent of root
    Assert.assertEquals("/", PathUtils.getParent("/"));
    Assert.assertEquals("/", PathUtils.getParent("/foo/bar/../../"));
    Assert.assertEquals("/", PathUtils.getParent("/foo/../bar/../"));
  }
Esempio n. 11
0
 /**
  * Creates the local block path and all the parent directories. Also, sets the appropriate
  * permissions.
  *
  * @param path The path of the block.
  * @throws IOException when fails to create block path and parent directories with appropriate
  *     permissions.
  */
 public static void createBlockPath(String path) throws IOException {
   try {
     createStorageDirPath(PathUtils.getParent(path));
   } catch (InvalidPathException e) {
     throw new IOException("Failed to create block path, get parent path of " + path + "failed");
   } catch (IOException ioe) {
     throw new IOException("Failed to create block path " + path);
   }
 }
  @Test
  public void backupFile() throws IOException {
    Path dirToBkp = Files.createTempDirectory("dirToBkp3");

    Path parentdirfile1 = createTempFile(dirToBkp, "FILE_1", ".tmp");

    new BackupHelper(conf).backupFile(parentdirfile1);
    Path bkp = PathUtils.get(conf.getBackupDir(), parentdirfile1);
    assertThat(exists(bkp), is(true));
  }
Esempio n. 13
0
 /**
  * get archive input stream from File Object
  *
  * @param sourceArchive - archive File
  * @return archive input stream
  * @throws IOException
  */
 private static ArchiveInputStream createArchiveInputStream(File sourceArchive)
     throws IOException {
   String fileName = sourceArchive.getName();
   String extension = PathUtils.getExtension(fileName);
   verifySupportedExtension(extension);
   FileInputStream fis = new FileInputStream(sourceArchive);
   ArchiveInputStream archiveInputStream = returnArchiveInputStream(fis, extension);
   if (archiveInputStream != null) {
     return archiveInputStream;
   }
   throw new IllegalArgumentException("Unsupported archive extension: '" + extension + "'");
 }
Esempio n. 14
0
 private void write(Object part, int id, Vector vector) throws IOException {
   SequenceFile.Writer writer = writers.get(part);
   if (writer == null) {
     Configuration conf = UDFContext.getUDFContext().getJobConf();
     Path file = PathUtils.enter(getStorePath(), String.valueOf(part), "part-" + Env.getPartID());
     writer = IOUtils.forSequenceWrite(conf, file, IntWritable.class, VectorWritable.class);
     writers.put(part, writer);
   }
   keyWritable.set(id);
   valueWritable.set(vector);
   writer.append(keyWritable, valueWritable);
 }
Esempio n. 15
0
 /** Test of isRemote method, of class PathUtils. */
 public void testIsRemote() {
   System.out.println("isRemote");
   assertTrue(PathUtils.isRemote("http://www.seznam.cz"));
   assertTrue(PathUtils.isRemote("https://is.muni.cz/auth/more/data"));
   assertTrue(PathUtils.isRemote("ftp://server"));
   assertFalse(PathUtils.isRemote("relative/path"));
   assertFalse(PathUtils.isRemote("relative"));
   assertFalse(PathUtils.isRemote("/absolute/path"));
   assertFalse(PathUtils.isRemote("./../../data.txt"));
 }
Esempio n. 16
0
  public void remove(Stack stack) {
    Collection<AbsFile> files = this.files.getValue(stack);
    String dir = this.dir.getValue(stack);
    Object host = this.host.getValue(stack);
    VDLFileCache cache = getCache(stack);
    List<Object> rem = new ArrayList<Object>();

    boolean force = this.force.getValue(stack);

    for (AbsFile f : files) {
      File cf = new File(f.getName(), dircat(dir, PathUtils.remoteDirName(f)), host, 0);
      CacheReturn cr = cache.unlockEntry(cf, force);
      rem.addAll(cr.remove);
    }

    cacheFilesToRemove.setValue(stack, rem);
  }
Esempio n. 17
0
 @Test
 public void getPathComponentsNoExceptionTest() throws InvalidPathException {
   Assert.assertArrayEquals(new String[] {""}, PathUtils.getPathComponents("/"));
   Assert.assertArrayEquals(new String[] {"", "bar"}, PathUtils.getPathComponents("/bar"));
   Assert.assertArrayEquals(
       new String[] {"", "foo", "bar"}, PathUtils.getPathComponents("/foo/bar"));
   Assert.assertArrayEquals(
       new String[] {"", "foo", "bar"}, PathUtils.getPathComponents("/foo/bar/"));
   Assert.assertArrayEquals(new String[] {"", "bar"}, PathUtils.getPathComponents("/foo/../bar"));
   Assert.assertArrayEquals(
       new String[] {"", "foo", "bar", "a", "b", "c"},
       PathUtils.getPathComponents("/foo//bar/a/b/c"));
 }
Esempio n. 18
0
 /** Test of relativizeFolders method, of class PathUtils. */
 public void testGetParent() {
   System.out.println("testGetParent");
   Path result = PathUtils.getParent(Paths.get(""));
   assertEquals(Paths.get(""), result);
   result = PathUtils.getParent(Paths.get("import.lesscache"));
   assertEquals(Paths.get(""), result);
   result = PathUtils.getParent(Paths.get("path/import.lesscache"));
   assertEquals(Paths.get("path"), result);
   result = PathUtils.getParent(Paths.get("base/path/import.lesscache"));
   assertEquals(Paths.get("base/path"), result);
   result = PathUtils.getParent(Paths.get("../file.lesscache"));
   assertEquals(Paths.get(".."), result);
   result = PathUtils.getParent(Paths.get("../../file.lesscache"));
   assertEquals(Paths.get("../.."), result);
   result = PathUtils.getParent(Paths.get("../other/file.lesscache"));
   assertEquals(Paths.get("../other"), result);
 }
Esempio n. 19
0
 /**
  * Searches for an entry inside the zip stream by entry path. If there are alternative extensions,
  * will also look for entry with alternative extension. The search stops reading the stream when
  * the entry is found, so calling read on the stream will read the returned entry.
  *
  * <p>The zip input stream doesn't support mark/reset so once this method is used you cannot go
  * back - either the stream was fully read (when entry is not found) or the stream was read until
  * the current entry.
  *
  * @param zis The zip input stream
  * @param entryPath The entry path to search for
  * @param alternativeExtensions List of alternative file extensions to try if the main entry path
  *     is not found.
  * @return The entry if found, null otherwise
  * @throws IOException On failure to read the stream
  */
 public static ZipEntry locateEntry(
     ZipInputStream zis, String entryPath, List<String> alternativeExtensions) throws IOException {
   ZipEntry zipEntry;
   while ((zipEntry = zis.getNextEntry()) != null) {
     String zipEntryName = zipEntry.getName();
     if (zipEntryName.equals(entryPath)) {
       return zipEntry;
     } else if (alternativeExtensions != null) {
       String basePath = PathUtils.stripExtension(entryPath);
       for (String alternativeExtension : alternativeExtensions) {
         String alternativeSourcePath = basePath + "." + alternativeExtension;
         if (zipEntryName.equals(alternativeSourcePath)) {
           return zipEntry;
         }
       }
     }
   }
   return null;
 }
Esempio n. 20
0
  @Test
  public void authorFolderPathReturnsAuthorObjectWithProperName() throws Exception {
    final String author = "Лукьяненко Сергей";
    final String path = PathUtils.createPath("ru", "Л", author);

    mockery.checking(
        new Expectations() {
          {
            allowing(fs).exists(path);
            will(returnValue(true));

            allowing(fs).isDirectory(path);
            will(returnValue(true));

            allowing(fs).getName(path);
            will(returnValue(author));
          }
        });

    Author item = (Author) extractor.get(path);
    assertThat(item, equalTo(new Author(path, author)));
  }
Esempio n. 21
0
  @Test
  public void underscoreFolderPathReturnsFolder() throws Exception {
    final String folderName = "_scifi";
    final String path = PathUtils.createPath("ru", "_", folderName);

    mockery.checking(
        new Expectations() {
          {
            allowing(fs).exists(path);
            will(returnValue(true));

            allowing(fs).isDirectory(path);
            will(returnValue(true));

            allowing(fs).getName(path);
            will(returnValue(folderName));
          }
        });

    Folder item = (Folder) extractor.get(path);
    assertThat(item, equalTo(new Folder(path, folderName)));
  }
Esempio n. 22
0
  @Test
  public void bookFilePathReturnsBookItemWithProperName() throws Exception {
    String author = "Лукьяненко Сергей";
    String bookName = "Геном";
    final String fileName = bookName + ".fb2.zip";
    final String path = PathUtils.createPath("ru", "Л", author, fileName);

    mockery.checking(
        new Expectations() {
          {
            allowing(fs).exists(path);
            will(returnValue(true));

            allowing(fs).isDirectory(path);
            will(returnValue(false));

            allowing(fs).getName(path);
            will(returnValue(fileName));
          }
        });

    Book item = (Book) extractor.get(path);
    assertThat(item, equalTo(new Book(path, bookName)));
  }
 @Test(expected = PathTraversalException.class)
 public void dotDotShouldBeDisallowedIfOutsideSecureDir() throws Exception {
   PathUtils.ensurePathInSecureDir(tmpDir, new File(tmpDir, "../").getPath()); // ${tmpDir}/..
 }
 @Test
 public void dotDotShouldBeAllowedIfInsideSecureDir() throws Exception {
   PathUtils.ensurePathInSecureDir(
       tmpDir, new File(tmpDir, "a/b/../b").getAbsolutePath()); // ${tmpDir}/a/b
 }
Esempio n. 25
0
 public FilePath getParentDirectory() {
   return new FilePath(PathUtils.getDirectoryName(this.asString()));
 }
Esempio n. 26
0
  @Test
  public void cleanPathNoExceptionTest() throws InvalidPathException {
    // test clean path
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo/bar"));

    // test trailing slash
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo/bar/"));

    // test redundant slashes
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo//bar"));
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo//bar//"));
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo///////bar//////"));

    // test dots gets resolved
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo/./bar"));
    Assert.assertEquals("/foo/bar", PathUtils.cleanPath("/foo/././bar"));
    Assert.assertEquals("/foo", PathUtils.cleanPath("/foo/bar/.."));
    Assert.assertEquals("/bar", PathUtils.cleanPath("/foo/../bar"));
    Assert.assertEquals("/", PathUtils.cleanPath("/foo/bar/../.."));

    // the following seems strange
    // TODO(jiri): Instead of returning null, throw InvalidPathException.
    Assert.assertNull(PathUtils.cleanPath("/foo/bar/../../.."));
  }
Esempio n. 27
0
 @Test
 public void getPathComponentsExceptionTest() throws InvalidPathException {
   mException.expect(InvalidPathException.class);
   PathUtils.getPathComponents("/\\   foo / bar");
 }
Esempio n. 28
0
 @Test
 public void uniqPathTest() {
   Assert.assertNotEquals(PathUtils.uniqPath(), PathUtils.uniqPath());
 }
  @Test
  public void backupComplexDir() throws IOException {
    Path dirToBkp = Files.createTempDirectory("dirToBkp4");

    Path subDir1 = Files.createTempDirectory(dirToBkp, "subDir1");
    Path subDir2 = Files.createTempDirectory(dirToBkp, "subDir2");
    Path subDir3 = Files.createTempDirectory(subDir1, "subDir3");

    Path parentdirfile1 = createTempFile(dirToBkp, "FILE_1", ".tmp");
    Path parentdirfile2 = createTempFile(subDir1, "FILE_2", ".tmp");
    Path parentdirfile3 = createTempFile(subDir3, "FILE_3", ".tmp");

    new BackupHelper(conf).backupFileOrDir(dirToBkp);

    Path bkp = PathUtils.get(conf.getBackupDir(), dirToBkp);
    assertThat(exists(bkp), is(true));
    assertThat(exists(PathUtils.get(bkp, parentdirfile1.getFileName())), is(true));
    assertThat(exists(PathUtils.get(bkp, subDir1.getFileName())), is(true));
    assertThat(exists(PathUtils.get(bkp, subDir2.getFileName())), is(true));

    assertThat(
        exists(
            PathUtils.get(PathUtils.get(bkp, subDir1.getFileName()), parentdirfile2.getFileName())),
        is(true));
    assertThat(
        exists(PathUtils.get(PathUtils.get(bkp, subDir1.getFileName()), subDir3.getFileName())),
        is(true));
    assertThat(
        exists(
            PathUtils.get(
                PathUtils.get(PathUtils.get(bkp, subDir1.getFileName()), subDir3.getFileName()),
                parentdirfile3.getFileName())),
        is(true));
  }
Esempio n. 30
0
  @Test
  public void concatPathTest() {
    Assert.assertEquals("/", PathUtils.concatPath("/"));
    Assert.assertEquals("/", PathUtils.concatPath("/", ""));
    Assert.assertEquals("/bar", PathUtils.concatPath("/", "bar"));

    Assert.assertEquals("foo", PathUtils.concatPath("foo"));
    Assert.assertEquals("/foo", PathUtils.concatPath("/foo"));
    Assert.assertEquals("/foo", PathUtils.concatPath("/foo", ""));

    // Join base without trailing "/"
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo", "bar"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo", "bar/"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo", "/bar"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo", "/bar/"));

    // Join base with trailing "/"
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo/", "bar"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo/", "bar/"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo/", "/bar"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo/", "/bar/"));

    // Whitespace must be trimmed.
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo ", "bar  "));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo ", "  bar"));
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo ", "  bar  "));

    // Redundant separator must be trimmed.
    Assert.assertEquals("/foo/bar", PathUtils.concatPath("/foo/", "bar//"));

    // Multiple components to join.
    Assert.assertEquals("/foo/bar/a/b/c", PathUtils.concatPath("/foo", "bar", "a", "b", "c"));
    Assert.assertEquals("/foo/bar/b/c", PathUtils.concatPath("/foo", "bar", "", "b", "c"));

    // Non-string
    Assert.assertEquals("/foo/bar/1", PathUtils.concatPath("/foo", "bar", 1));
    Assert.assertEquals("/foo/bar/2", PathUtils.concatPath("/foo", "bar", 2L));

    // Header
    Assert.assertEquals(
        Constants.HEADER + "host:port/foo/bar",
        PathUtils.concatPath(Constants.HEADER + "host:port", "/foo", "bar"));
  }