1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.tool;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import org.junit.Test;
28  
29  
30  /**
31   * Tests for {@link UtilsFile}.
32   *
33   * @version $Id: UtilsFileTest.java 4785 2009-12-21 20:10:28Z mosipov $
34   */
35  public class UtilsFileTest {
36  
37  	@Test
38      public void isSingleExtension01() {
39      	boolean condition = UtilsFile.isSingleExtension("hacked.txt");
40      	assertTrue(condition);
41      }
42  
43  	@Test
44      public void isSingleExtension02() {
45      	boolean condition = UtilsFile.isSingleExtension("hacked.php_txt");
46      	assertTrue(condition);
47      }
48  
49  	@Test
50      public void isSingleExtension03() {
51      	boolean condition = UtilsFile.isSingleExtension("hacked.php.txt");
52      	assertFalse(condition);
53      }
54  
55  	@Test
56      public void isSingleExtension04() {
57      	boolean condition = UtilsFile.isSingleExtension("hacked.txt.");
58      	assertFalse(condition);
59      }
60  
61  	@Test
62      public void isSingleExtension05() {
63      	boolean condition = UtilsFile.isSingleExtension("hacked..txt");
64      	assertFalse(condition);
65      }
66  
67  	@Test
68      public void forceSingleExtension01() {
69      	String actual = UtilsFile.forceSingleExtension("hacked.txt");
70      	assertEquals("hacked.txt", actual);
71      }
72  
73  	@Test
74      public void forceSingleExtension02() {
75      	String actual = UtilsFile.forceSingleExtension("hacked.php_txt");
76      	assertEquals("hacked.php_txt", actual);
77      }
78  
79  	@Test
80      public void forceSingleExtension03() {
81      	String actual = UtilsFile.forceSingleExtension("hacked.php.txt");
82      	assertEquals("hacked_php.txt", actual);
83      }
84  
85  	@Test
86      public void forceSingleExtension04() {
87      	String actual = UtilsFile.forceSingleExtension("hacked..txt");
88      	assertEquals("hacked_.txt", actual);
89      }
90  
91  	@Test
92      public void isValidPath00() {
93      	String path = null;
94      	boolean condition = UtilsFile.isValidPath(path);
95      	assertFalse(condition);
96      }
97  	
98  	@Test
99      public void isValidPath01() {
100     	String path = "";
101     	boolean condition = UtilsFile.isValidPath(path);
102     	assertFalse(condition);
103     }
104 
105 	@Test
106     public void isValidPath02() {
107     	String path = "/";
108     	boolean condition = UtilsFile.isValidPath(path);
109     	assertTrue(condition);
110     }
111 
112 	@Test
113     public void isValidPath03() {
114     	String path = "/./";
115     	boolean condition = UtilsFile.isValidPath(path);
116     	assertFalse(condition);
117     }
118 
119 	@Test
120     public void isValidPath04() {
121     	String path = "/newf/..";
122     	boolean condition = UtilsFile.isValidPath(path);
123     	assertFalse(condition);
124     }
125 
126 	@Test
127     public void isValidPath05() {
128     	String path = "/../";
129     	boolean condition = UtilsFile.isValidPath(path);
130     	assertFalse(condition);
131     }
132 
133 	@Test
134     public void isValidPath06() {
135     	String path = "/stuff/../..";
136     	boolean condition = UtilsFile.isValidPath(path);
137     	assertFalse(condition);
138     }
139 
140 	@Test
141     public void isValidPath07() {
142     	String path = "/my/stuff/../";
143     	boolean condition = UtilsFile.isValidPath(path);
144     	assertFalse(condition);
145     }
146 
147 	@Test
148     public void isValidPath08() {
149     	String path = "/my/stuff/";
150     	boolean condition = UtilsFile.isValidPath(path);
151     	assertTrue(condition);
152     }
153 
154 	@Test
155     public void isValidPath09() {
156     	String path = "/my/stuff";
157     	boolean condition = UtilsFile.isValidPath(path);
158     	assertFalse(condition);
159     }
160 
161 	@Test
162     public void isValidPath10() {
163     	String path = "my/stuff/";
164     	boolean condition = UtilsFile.isValidPath(path);
165     	assertFalse(condition);
166     }
167 	
168 	@Test
169     public void isValidPath11() {
170     	String path = "/some/pa\\th/";
171     	boolean condition = UtilsFile.isValidPath(path);
172     	assertFalse(condition);
173     }
174 	
175 	@Test
176     public void isValidPath12() {
177     	String path = "/\\/";
178     	boolean condition = UtilsFile.isValidPath(path);
179     	assertFalse(condition);
180     }
181 	
182 	@Test
183     public void isValidPath13() {
184     	String path = "/.hidden/";
185     	boolean condition = UtilsFile.isValidPath(path);
186     	assertFalse(condition);
187     }
188 
189 	@Test
190     public void sanitizeFolder01() {
191     	assertEquals("a_b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFolderName("a.b|c<d>e:f?g*h<i>"));
192     }
193 	
194 	@Test
195 	public void sanitizeFolder02() {
196 		assertEquals("a_b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFolderName("a.b|c\u007Fd>e:f\u0005g*h<i>"));
197 	}
198 
199 	@Test
200     public void sanitizeFile01() {
201     	assertEquals("b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFileName("b|c<d>e:f?g*h<i>"));
202     }
203 
204 	@Test
205     public void sanitizeFile02() {
206     	assertEquals("name_ext1.ext2", UtilsFile.sanitizeFileName("name.ext1.ext2"));
207     }
208 
209 	@Test
210 	public void sanitizeFile03() {
211 		assertEquals("b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFileName("b|c\u007Fd>e:f\u0005g*h<i>"));
212 	}
213 }