1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.ftp.parser;
18
19 import java.io.ByteArrayInputStream;
20 import java.nio.charset.Charset;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Calendar;
23
24 import org.apache.commons.net.ftp.FTPFile;
25 import org.apache.commons.net.ftp.FTPFileEntryParser;
26 import org.apache.commons.net.ftp.FTPListParseEngine;
27
28
29
30 public class NTFTPEntryParserTest extends CompositeFTPParseTestFramework {
31
32 private static final String[][] goodsamples = { {
33 "05-26-95 10:57AM 143712 $LDR$", "05-20-97 03:31PM 681 .bash_history",
34 "12-05-96 05:03PM <DIR> absoft2", "11-14-97 04:21PM 953 AUDITOR3.INI",
35 "05-22-97 08:08AM 828 AUTOEXEC.BAK", "01-22-98 01:52PM 795 AUTOEXEC.BAT",
36 "05-13-97 01:46PM 828 AUTOEXEC.DOS", "12-03-96 06:38AM 403 AUTOTOOL.LOG",
37 "12-03-96 06:38AM <DIR> 123xyz", "01-20-97 03:48PM <DIR> bin", "05-26-1995 10:57AM 143712 $LDR$",
38
39 "12-05-96 17:03 <DIR> absoft2", "05-22-97 08:08 828 AUTOEXEC.BAK",
40 "01-01-98 05:00 <DIR> Network", "01-01-98 05:00 <DIR> StorageCard", "09-13-10 20:08 <DIR> Recycled",
41 "09-06-06 19:00 69 desktop.ini", "09-13-10 13:08 23 Control Panel.lnk",
42 "09-13-10 13:08 <DIR> My Documents", "09-13-10 13:08 <DIR> Program Files",
43 "09-13-10 13:08 <DIR> Temp", "09-13-10 13:08 <DIR> Windows", },
44 {
45 "-rw-r--r-- 1 root root 111325 Apr 27 2001 zxJDBC-2.0.1b1.tar.gz",
46 "-rw-r--r-- 1 root root 190144 Apr 27 2001 zxJDBC-2.0.1b1.zip",
47 "-rwxr-xr-x 2 500 500 166 Nov 2 2001 73131-testtes1.afp",
48 "-rw-r--r-- 1 500 500 166 Nov 9 2001 73131-testtes1.AFP",
49 "drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata", } };
50
51 private static final String[][] badsamples = { {
52 "20-05-97 03:31PM 681 .bash_history", " 0 DIR 05-19-97 12:56 local",
53 " 0 DIR 05-12-97 16:52 Maintenance Desktop", },
54 {
55 "drwxr-xr-x 2 root 99 4096Feb 23 30:01 zzplayer", } };
56
57 private static final String directoryBeginningWithNumber = "12-03-96 06:38AM <DIR> 123xyz";
58
59
60 private static final byte[] listFilesByteTrace = { 48, 57, 45, 48, 52, 45, 49, 51, 32, 32, 48, 53, 58, 53, 49, 80, 77, 32, 32, 32, 32, 32, 32, 32, 60, 68,
61 73, 82, 62, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 97, 115, 112, 110, 101, 116, 95, 99, 108, 105, 101, 110, 116, 13, 10,
62 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 53, 52, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
63 50, 32, 65, 95, 113, 117, 105, 99, 107, 95, 98, 114, 111, 119, 110, 95, 102, 111, 120, 95, 106, 117, 109, 112, 115, 95, 111, 118, 101, 114, 95, 116,
64 104, 101, 95, 108, 97, 122, 121, 95, 100, 111, 103, 13, 10,
65 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 49, 55, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
66 51, 32, 120, -127, -123, 121, 13, 10,
67 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 49, 58, 52, 57, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
68 52, 32, -126, -28, -126, -83, -119, -51, -126, -52, -105, -84, -126, -22, -126, -51, -112, -30, -126, -90, -126, -72, -126, -75, -126, -60, -127,
69 65, -126, -75, -126, -87, -126, -32, -126, -32, -126, -58, -126, -52, -112, -123, -126, -55, -126, -96, -126, -25, -126, -72, 46, 116, 120, 116, 13,
70 10,
71 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 52, 54, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
72 53, 32, -125, 76, -125, -125, -125, 98, -125, 86, -125, 116, -125, -115, -127, 91, -116, 118, -114, 90, -113, -111, 13, 10,
73 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 52, 54, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
74 54, 32, -125, 76, -125, -125, -125, 98, -125, 86, -125, -123, -125, 116, -125, -115, -127, 91, -116, 118, -114, 90, -113, -111, 13, 10,
75 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 49, 58, 52, 57, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
76 55, 32, -114, 79, -116, -38, -126, -52, -105, -25, 46, 116, 120, 116, 13, 10,
77 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 49, 58, 52, 57, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
78 56, 32, -111, -66, -116, -10, -106, 93, 46, 116, 120, 116, 13, 10,
79 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 53, 52, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
80 57, 32, -113, -84, -106, -20, -106, -123, -114, 113, 13, 10,
81 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 49, 58, 52, 57, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
82 48, 32, -119, -28, -109, 99, -118, -108, -114, -82, -119, -17, -114, -48, -120, -8, -112, -123, -108, 95, -117, -58, 46, 80, 68, 70, 13, 10,
83 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 49, 49, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
84 49, 32, -112, -124, -99, -56, 46, 116, 120, 116, 13, 10,
85 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 52, 51, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
86 50, 32, -117, -76, -116, -123, 13, 10,
87 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 49, 50, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
88 51, 32, -114, -107, -111, -123, -108, 94, -104, 82, 13, 10,
89 48, 55, 45, 48, 51, 45, 49, 51, 32, 32, 48, 50, 58, 51, 53, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
90 52, 32, -112, -123, -117, -101, -126, -52, -116, -16, -126, -19, -126, -24, 46, 116, 120, 116, 13, 10,
91 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 50, 58, 49, 50, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
92 53, 32, -114, -123, -117, -101, -112, -20, 13, 10,
93 48, 55, 45, 49, 55, 45, 49, 51, 32, 32, 48, 49, 58, 52, 57, 80, 77, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49,
94 54, 32, -107, -94, -112, -123, -106, 126, -126, -55, -107, -44, -126, -25, -126, -72, 46, 116, 120, 116, 13, 10
95 };
96
97 private static final int LISTFILE_COUNT = 16;
98
99
100
101
102 public NTFTPEntryParserTest(final String name) {
103 super(name);
104 }
105
106 @Override
107 protected void doAdditionalGoodTests(final String test, final FTPFile f) {
108 if (test.contains("<DIR>")) {
109 assertEquals("directory.type", FTPFile.DIRECTORY_TYPE, f.getType());
110 }
111 }
112
113
114
115
116 @Override
117 protected String[][] getBadListings() {
118 return badsamples;
119 }
120
121
122
123
124 @Override
125 protected String[][] getGoodListings() {
126 return goodsamples;
127 }
128
129
130
131
132 @Override
133 protected FTPFileEntryParser getParser() {
134 return new CompositeFileEntryParser(new FTPFileEntryParser[] { new NTFTPEntryParser(), new UnixFTPEntryParser()
135
136 });
137 }
138
139 @Override
140 public void testDefaultPrecision() {
141 testPrecision("05-26-1995 10:57AM 143712 $LDR$", CalendarUnit.MINUTE);
142 testPrecision("05-22-97 08:08 828 AUTOEXEC.BAK", CalendarUnit.MINUTE);
143 }
144
145
146
147
148 public void testDirectoryBeginningWithNumber() {
149 final FTPFile f = getParser().parseFTPEntry(directoryBeginningWithNumber);
150 assertEquals("name", "123xyz", f.getName());
151 }
152
153 public void testDirectoryBeginningWithNumberFollowedBySpaces() {
154 FTPFile f = getParser().parseFTPEntry("12-03-96 06:38AM <DIR> 123 xyz");
155 assertEquals("name", "123 xyz", f.getName());
156 f = getParser().parseFTPEntry("12-03-96 06:38AM <DIR> 123 abc xyz");
157 assertNotNull(f);
158 assertEquals("name", "123 abc xyz", f.getName());
159 }
160
161
162
163
164 public void testGroupNameWithSpaces() {
165 final FTPFile f = getParser().parseFTPEntry("drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata");
166 assertNotNull(f);
167 assertEquals("maxm", f.getUser());
168 assertEquals("Domain Users", f.getGroup());
169 }
170
171 public void testNET339() {
172 final FTPFile file = getParser().parseFTPEntry("05-22-97 12:08 5000000000 10 years and under");
173 assertNotNull("Could not parse entry", file);
174 assertEquals("10 years and under", file.getName());
175 assertEquals(5000000000L, file.getSize());
176 Calendar timestamp = file.getTimestamp();
177 assertNotNull("Could not parse time", timestamp);
178 assertEquals("Thu May 22 12:08:00 1997", df.format(timestamp.getTime()));
179
180 final FTPFile dir = getParser().parseFTPEntry("12-03-96 06:38 <DIR> 10 years and under");
181 assertNotNull("Could not parse entry", dir);
182 assertEquals("10 years and under", dir.getName());
183 timestamp = dir.getTimestamp();
184 assertNotNull("Could not parse time", timestamp);
185 assertEquals("Tue Dec 03 06:38:00 1996", df.format(timestamp.getTime()));
186 }
187
188 public void testNET516() throws Exception {
189 final int utf = testNET516(StandardCharsets.UTF_8);
190 assertEquals(LISTFILE_COUNT, utf);
191 final int ascii = testNET516(StandardCharsets.US_ASCII);
192 assertEquals(LISTFILE_COUNT, ascii);
193 final int iso8859_1 = testNET516(StandardCharsets.ISO_8859_1);
194 assertEquals(LISTFILE_COUNT, iso8859_1);
195 }
196
197 private int testNET516(final Charset charset) throws Exception {
198 final FTPFileEntryParser parser = new NTFTPEntryParser();
199 final FTPListParseEngine engine = new FTPListParseEngine(parser);
200 engine.readServerList(new ByteArrayInputStream(listFilesByteTrace), charset.name());
201 final FTPFile[] ftpfiles = engine.getFiles();
202 return ftpfiles.length;
203 }
204
205
206
207
208 @Override
209 public void testParseFieldsOnDirectory() throws Exception {
210 FTPFile dir = getParser().parseFTPEntry("12-05-96 05:03PM <DIR> absoft2");
211 assertNotNull("Could not parse entry.", dir);
212 assertEquals("Thu Dec 05 17:03:00 1996", df.format(dir.getTimestamp().getTime()));
213 assertTrue("Should have been a directory.", dir.isDirectory());
214 assertEquals("absoft2", dir.getName());
215 assertEquals(0, dir.getSize());
216
217 dir = getParser().parseFTPEntry("12-03-96 06:38AM <DIR> 123456");
218 assertNotNull("Could not parse entry.", dir);
219 assertTrue("Should have been a directory.", dir.isDirectory());
220 assertEquals("123456", dir.getName());
221 assertEquals(0, dir.getSize());
222
223 }
224
225
226
227
228 @Override
229 public void testParseFieldsOnFile() throws Exception {
230 FTPFile f = getParser().parseFTPEntry("05-22-97 12:08AM 5000000000 AUTOEXEC.BAK");
231 assertNotNull("Could not parse entry.", f);
232 assertEquals("Thu May 22 00:08:00 1997", df.format(f.getTimestamp().getTime()));
233 assertTrue("Should have been a file.", f.isFile());
234 assertEquals("AUTOEXEC.BAK", f.getName());
235 assertEquals(5000000000L, f.getSize());
236
237
238
239
240 f = getParser().parseFTPEntry("-rw-rw-r-- 1 mqm mqm 17707 Mar 12 3:33 killmq.sh.log");
241 assertNotNull("Could not parse entry.", f);
242 final Calendar cal = Calendar.getInstance();
243 cal.setTime(f.getTimestamp().getTime());
244 assertEquals("hour", 3, cal.get(Calendar.HOUR));
245 assertTrue("Should have been a file.", f.isFile());
246 assertEquals(17707, f.getSize());
247 }
248
249 public void testParseLeadingDigits() {
250 final FTPFile file = getParser().parseFTPEntry("05-22-97 12:08AM 5000000000 10 years and under");
251 assertNotNull("Could not parse entry", file);
252 assertEquals("10 years and under", file.getName());
253 assertEquals(5000000000L, file.getSize());
254 Calendar timestamp = file.getTimestamp();
255 assertNotNull("Could not parse time", timestamp);
256 assertEquals("Thu May 22 00:08:00 1997", df.format(timestamp.getTime()));
257
258 final FTPFile dir = getParser().parseFTPEntry("12-03-96 06:38PM <DIR> 10 years and under");
259 assertNotNull("Could not parse entry", dir);
260 assertEquals("10 years and under", dir.getName());
261 timestamp = dir.getTimestamp();
262 assertNotNull("Could not parse time", timestamp);
263 assertEquals("Tue Dec 03 18:38:00 1996", df.format(timestamp.getTime()));
264 }
265
266 @Override
267 public void testRecentPrecision() {
268
269 }
270 }