Debian Codegraph @

brut.apktool/apktool-lib/src/test/java/brut/androlib/util/UnknownDirectoryTraversalTest.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.util;
18
19 import brut.androlib.BaseTest;
20 import brut.androlib.TestUtils;
21 import brut.common.BrutException;
22 import brut.common.InvalidUnknownFileException;
23 import brut.common.RootUnknownFileException;
24 import brut.common.TraversalUnknownFileException;
25 import brut.directory.ExtFile;
26 import brut.util.BrutIO;
27 import brut.util.OS;
28 import brut.util.OSDetection;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32
33 import java.io.File;
34 import java.io.IOException;
35
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
38
39 public class UnknownDirectoryTraversalTest extends BaseTest {
40
41 @BeforeClass
42 public static void beforeClass() throws Exception {
43 sTmpDir = new ExtFile(OS.createTempDirectory());
44 TestUtils.copyResourceDir(UnknownDirectoryTraversalTest.class, "util/traversal", sTmpDir);
45 }
46
47 @AfterClass
48 public static void afterClass() throws BrutException {
49 OS.rmdir(sTmpDir);
50 }
51
52 @Test
53 public void validFileTest() throws IOException, BrutException {
54 String validFilename = BrutIO.sanitizeUnknownFile(sTmpDir, "file");
55 assertEquals(validFilename, "file");
56
57 File validFile = new File(sTmpDir, validFilename);
58 assertTrue(validFile.isFile());
59 }
60
61 @Test(expected = TraversalUnknownFileException.class)
62 public void invalidBackwardFileTest() throws IOException, BrutException {
63 BrutIO.sanitizeUnknownFile(sTmpDir, "../file");
64 }
65
66 @Test(expected = RootUnknownFileException.class)
67 public void invalidRootFileTest() throws IOException, BrutException {
68 String rootLocation = OSDetection.isWindows() ? "C:/" : File.separator;
69 BrutIO.sanitizeUnknownFile(sTmpDir, rootLocation + "file");
70 }
71
72 @Test(expected = InvalidUnknownFileException.class)
73 public void noFilePassedTest() throws IOException, BrutException {
74 BrutIO.sanitizeUnknownFile(sTmpDir, "");
75 }
76
77 @Test(expected = TraversalUnknownFileException.class)
78 public void invalidBackwardPathOnWindows() throws IOException, BrutException {
79 String invalidPath;
80 if (! OSDetection.isWindows()) {
81 invalidPath = "../../app";
82 } else {
83 invalidPath = "..\\..\\app.exe";
84 }
85
86 BrutIO.sanitizeUnknownFile(sTmpDir, invalidPath);
87 }
88
89 @Test
90 public void validDirectoryFileTest() throws IOException, BrutException {
91 String validFilename = BrutIO.sanitizeUnknownFile(sTmpDir, "dir" + File.separator + "file");
92 assertEquals("dir" + File.separator + "file", validFilename);
93 }
94 }