View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://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 org.apache.commons.net.ftp.parser;
18  
19  import java.io.ByteArrayInputStream;
20  import java.util.Calendar;
21  
22  import org.apache.commons.net.ftp.FTPFile;
23  import org.apache.commons.net.ftp.FTPFileEntryParser;
24  import org.apache.commons.net.ftp.FTPListParseEngine;
25  
26  /**
27   */
28  public class NTFTPEntryParserTest extends CompositeFTPParseTestFramework {
29  
30      private static final String[][] goodsamples = { { // DOS-style tests
31              "05-26-95  10:57AM               143712 $LDR$", "05-20-97  03:31PM                  681 .bash_history",
32              "12-05-96  05:03PM       <DIR>          absoft2", "11-14-97  04:21PM                  953 AUDITOR3.INI",
33              "05-22-97  08:08AM                  828 AUTOEXEC.BAK", "01-22-98  01:52PM                  795 AUTOEXEC.BAT",
34              "05-13-97  01:46PM                  828 AUTOEXEC.DOS", "12-03-96  06:38AM                  403 AUTOTOOL.LOG",
35              "12-03-96  06:38AM       <DIR>          123xyz", "01-20-97  03:48PM       <DIR>          bin", "05-26-1995  10:57AM               143712 $LDR$",
36              // 24hr clock as used on Windows_CE
37              "12-05-96  17:03         <DIR>          absoft2", "05-22-97  08:08                    828 AUTOEXEC.BAK",
38              "01-01-98  05:00       <DIR>          Network", "01-01-98  05:00       <DIR>          StorageCard", "09-13-10  20:08       <DIR>          Recycled",
39              "09-06-06  19:00                   69 desktop.ini", "09-13-10  13:08                   23 Control Panel.lnk",
40              "09-13-10  13:08       <DIR>          My Documents", "09-13-10  13:08       <DIR>          Program Files",
41              "09-13-10  13:08       <DIR>          Temp", "09-13-10  13:08       <DIR>          Windows", },
42              { // Unix-style tests
43                      "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
44                      "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip",
45                      "-rwxr-xr-x   2 500      500           166 Nov  2  2001 73131-testtes1.afp",
46                      "-rw-r--r--   1 500      500           166 Nov  9  2001 73131-testtes1.AFP",
47                      "drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata", } };
48  
49      private static final String[][] badsamples = { { // DOS-style tests
50              "20-05-97  03:31PM                  681 .bash_history", "     0           DIR   05-19-97   12:56  local",
51              "     0           DIR   05-12-97   16:52  Maintenance Desktop", },
52              { // Unix-style tests
53                      "drwxr-xr-x   2 root     99           4096Feb 23 30:01 zzplayer", } };
54  
55      private static final String directoryBeginningWithNumber = "12-03-96  06:38AM       <DIR>          123xyz";
56  
57      // byte -123 when read using ISO-8859-1 encoding becomes 0X85 line terminator
58      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,
59              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, // 1
60              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,
61              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,
62              104, 101, 95, 108, 97, 122, 121, 95, 100, 111, 103, 13, 10, // 2
63              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,
64              51, 32, 120, -127, -123, 121, 13, 10, // 3
65              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,
66              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,
67              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,
68              10, // 4
69              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,
70              53, 32, -125, 76, -125, -125, -125, 98, -125, 86, -125, 116, -125, -115, -127, 91, -116, 118, -114, 90, -113, -111, 13, 10, // 5
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              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, // 6
73              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,
74              55, 32, -114, 79, -116, -38, -126, -52, -105, -25, 46, 116, 120, 116, 13, 10, // 7
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              56, 32, -111, -66, -116, -10, -106, 93, 46, 116, 120, 116, 13, 10, // 8
77              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,
78              57, 32, -113, -84, -106, -20, -106, -123, -114, 113, 13, 10, // 9
79              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,
80              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, // 10
81              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,
82              49, 32, -112, -124, -99, -56, 46, 116, 120, 116, 13, 10, // 11
83              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,
84              50, 32, -117, -76, -116, -123, 13, 10, // 12
85              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,
86              51, 32, -114, -107, -111, -123, -108, 94, -104, 82, 13, 10, // 13
87              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,
88              52, 32, -112, -123, -117, -101, -126, -52, -116, -16, -126, -19, -126, -24, 46, 116, 120, 116, 13, 10, // 14
89              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,
90              53, 32, -114, -123, -117, -101, -112, -20, 13, 10, // 15
91              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,
92              54, 32, -107, -94, -112, -123, -106, 126, -126, -55, -107, -44, -126, -25, -126, -72, 46, 116, 120, 116, 13, 10 // 16
93      };
94  
95      private static final int LISTFILE_COUNT = 16;
96  
97      /**
98       * @see junit.framework.TestCase#TestCase(String)
99       */
100     public NTFTPEntryParserTest(final String name) {
101         super(name);
102     }
103 
104     @Override
105     protected void doAdditionalGoodTests(final String test, final FTPFile f) {
106         if (test.indexOf("<DIR>") >= 0) {
107             assertEquals("directory.type", FTPFile.DIRECTORY_TYPE, f.getType());
108         }
109     }
110 
111     /**
112      * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
113      */
114     @Override
115     protected String[][] getBadListings() {
116         return badsamples;
117     }
118 
119     /**
120      * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
121      */
122     @Override
123     protected String[][] getGoodListings() {
124         return goodsamples;
125     }
126 
127     /**
128      * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#getParser()
129      */
130     @Override
131     protected FTPFileEntryParser getParser() {
132         return new CompositeFileEntryParser(new FTPFileEntryParser[] { new NTFTPEntryParser(), new UnixFTPEntryParser()
133 
134         });
135     }
136 
137     @Override
138     public void testDefaultPrecision() {
139         testPrecision("05-26-1995  10:57AM               143712 $LDR$", CalendarUnit.MINUTE);
140         testPrecision("05-22-97  08:08                    828 AUTOEXEC.BAK", CalendarUnit.MINUTE);
141     }
142 
143     /*
144      * test condition reported as bug 20259 - now NET-106. directory with name beginning with a numeric character was not parsing correctly
145      */
146     public void testDirectoryBeginningWithNumber() {
147         final FTPFile f = getParser().parseFTPEntry(directoryBeginningWithNumber);
148         assertEquals("name", "123xyz", f.getName());
149     }
150 
151     public void testDirectoryBeginningWithNumberFollowedBySpaces() {
152         FTPFile f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 xyz");
153         assertEquals("name", "123 xyz", f.getName());
154         f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 abc xyz");
155         assertNotNull(f);
156         assertEquals("name", "123 abc xyz", f.getName());
157     }
158 
159     /*
160      * Test that group names with embedded spaces can be handled correctly
161      *
162      */
163     public void testGroupNameWithSpaces() {
164         final FTPFile f = getParser().parseFTPEntry("drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata");
165         assertNotNull(f);
166         assertEquals("maxm", f.getUser());
167         assertEquals("Domain Users", f.getGroup());
168     }
169 
170     public void testNET339() {
171         final FTPFile file = getParser().parseFTPEntry("05-22-97  12:08                  5000000000 10 years and under");
172         assertNotNull("Could not parse entry", file);
173         assertEquals("10 years and under", file.getName());
174         assertEquals(5000000000L, file.getSize());
175         Calendar timestamp = file.getTimestamp();
176         assertNotNull("Could not parse time", timestamp);
177         assertEquals("Thu May 22 12:08:00 1997", df.format(timestamp.getTime()));
178 
179         final FTPFile dir = getParser().parseFTPEntry("12-03-96  06:38       <DIR>           10 years and under");
180         assertNotNull("Could not parse entry", dir);
181         assertEquals("10 years and under", dir.getName());
182         timestamp = dir.getTimestamp();
183         assertNotNull("Could not parse time", timestamp);
184         assertEquals("Tue Dec 03 06:38:00 1996", df.format(timestamp.getTime()));
185     }
186 
187     public void testNET516() throws Exception { // problem where part of a multi-byte char gets converted to 0x85 = line term
188         final int utf = testNET516("UTF-8");
189         assertEquals(LISTFILE_COUNT, utf);
190         final int ascii = testNET516("ASCII");
191         assertEquals(LISTFILE_COUNT, ascii);
192         final int iso8859_1 = testNET516("ISO-8859-1");
193         assertEquals(LISTFILE_COUNT, iso8859_1);
194     }
195 
196     private int testNET516(final String charset) throws Exception {
197         final FTPFileEntryParser parser = new NTFTPEntryParser();
198         final FTPListParseEngine engine = new FTPListParseEngine(parser);
199         engine.readServerList(new ByteArrayInputStream(listFilesByteTrace), charset);
200         final FTPFile[] ftpfiles = engine.getFiles();
201         return ftpfiles.length;
202     }
203 
204     /**
205      * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#testParseFieldsOnDirectory()
206      */
207     @Override
208     public void testParseFieldsOnDirectory() throws Exception {
209         FTPFile dir = getParser().parseFTPEntry("12-05-96  05:03PM       <DIR>          absoft2");
210         assertNotNull("Could not parse entry.", dir);
211         assertEquals("Thu Dec 05 17:03:00 1996", df.format(dir.getTimestamp().getTime()));
212         assertTrue("Should have been a directory.", dir.isDirectory());
213         assertEquals("absoft2", dir.getName());
214         assertEquals(0, dir.getSize());
215 
216         dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123456");
217         assertNotNull("Could not parse entry.", dir);
218         assertTrue("Should have been a directory.", dir.isDirectory());
219         assertEquals("123456", dir.getName());
220         assertEquals(0, dir.getSize());
221 
222     }
223 
224     /**
225      * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#testParseFieldsOnFile()
226      */
227     @Override
228     public void testParseFieldsOnFile() throws Exception {
229         FTPFile f = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 AUTOEXEC.BAK");
230         assertNotNull("Could not parse entry.", f);
231         assertEquals("Thu May 22 00:08:00 1997", df.format(f.getTimestamp().getTime()));
232         assertTrue("Should have been a file.", f.isFile());
233         assertEquals("AUTOEXEC.BAK", f.getName());
234         assertEquals(5000000000L, f.getSize());
235 
236         // test an NT-unix style listing that does NOT have a leading zero
237         // on the hour.
238 
239         f = getParser().parseFTPEntry("-rw-rw-r--   1 mqm        mqm          17707 Mar 12  3:33 killmq.sh.log");
240         assertNotNull("Could not parse entry.", f);
241         final Calendar cal = Calendar.getInstance();
242         cal.setTime(f.getTimestamp().getTime());
243         assertEquals("hour", 3, cal.get(Calendar.HOUR));
244         assertTrue("Should have been a file.", f.isFile());
245         assertEquals(17707, f.getSize());
246     }
247 
248     public void testParseLeadingDigits() {
249         final FTPFile file = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 10 years and under");
250         assertNotNull("Could not parse entry", file);
251         assertEquals("10 years and under", file.getName());
252         assertEquals(5000000000L, file.getSize());
253         Calendar timestamp = file.getTimestamp();
254         assertNotNull("Could not parse time", timestamp);
255         assertEquals("Thu May 22 00:08:00 1997", df.format(timestamp.getTime()));
256 
257         final FTPFile dir = getParser().parseFTPEntry("12-03-96  06:38PM       <DIR>           10 years and under");
258         assertNotNull("Could not parse entry", dir);
259         assertEquals("10 years and under", dir.getName());
260         timestamp = dir.getTimestamp();
261         assertNotNull("Could not parse time", timestamp);
262         assertEquals("Tue Dec 03 18:38:00 1996", df.format(timestamp.getTime()));
263     }
264 
265     @Override
266     public void testRecentPrecision() {
267         // Not used
268     }
269 }