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