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.util.Calendar;
20  
21  import junit.framework.TestSuite;
22  import org.apache.commons.net.ftp.FTPFile;
23  import org.apache.commons.net.ftp.FTPFileEntryParser;
24  
25  /**
26   * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
27   * @version $Id: NTFTPEntryParserTest.java 629276 2008-02-19 23:31:25Z rwinston $
28   */
29  public class NTFTPEntryParserTest extends CompositeFTPParseTestFramework
30  {
31  
32      private static final String [][] goodsamples = {
33      {
34              "05-26-95  10:57AM               143712 $LDR$",
35              "05-20-97  03:31PM                  681 .bash_history",
36              "12-05-96  05:03PM       <DIR>          absoft2",
37              "11-14-97  04:21PM                  953 AUDITOR3.INI",
38              "05-22-97  08:08AM                  828 AUTOEXEC.BAK",
39              "01-22-98  01:52PM                  795 AUTOEXEC.BAT",
40              "05-13-97  01:46PM                  828 AUTOEXEC.DOS",
41              "12-03-96  06:38AM                  403 AUTOTOOL.LOG",
42              "12-03-96  06:38AM       <DIR>          123xyz",
43              "01-20-97  03:48PM       <DIR>          bin",
44              "05-26-1995  10:57AM               143712 $LDR$",
45      },
46      {
47              "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
48              "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip",
49              "-rwxr-xr-x   2 500      500           166 Nov  2  2001 73131-testtes1.afp",
50              "-rw-r--r--   1 500      500           166 Nov  9  2001 73131-testtes1.AFP",
51              "drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata",
52          }
53      };
54  
55      private static final String[][] badsamples =
56          {
57              {
58                  "20-05-97  03:31PM                  681 .bash_history",
59                  "drwxr-xr-x   2 root     99           4096 Feb 23 30:01 zzplayer",
60                  "12-05-96  17:03         <DIR>          absoft2",
61                  "05-22-97  08:08                    828 AUTOEXEC.BAK",
62                  "     0           DIR   05-19-97   12:56  local",
63                  "     0           DIR   05-12-97   16:52  Maintenance Desktop",
64              },
65              {
66                  "20-05-97  03:31PM                  681 .bash_history",
67                  "drwxr-xr-x   2 root     99           4096Feb 23 30:01 zzplayer",
68                  "12-05-96  17:03         <DIR>          absoft2",
69                  "05-22-97  08:08                    828 AUTOEXEC.BAK",
70                  "     0           DIR   05-19-97   12:56  local",
71                  "     0           DIR   05-12-97   16:52  Maintenance Desktop",
72              }
73              };
74  
75      private static final String directoryBeginningWithNumber =
76          "12-03-96  06:38AM       <DIR>          123xyz";
77  
78  
79      /**
80       * @see junit.framework.TestCase#TestCase(String)
81       */
82      public NTFTPEntryParserTest (String name)
83      {
84          super(name);
85      }
86  
87      /**
88       * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
89       */
90      @Override
91      protected String[][] getGoodListings()
92      {
93          return goodsamples;
94      }
95  
96      /**
97       * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
98       */
99      @Override
100     protected String[][] getBadListings()
101     {
102         return badsamples;
103     }
104 
105     /**
106      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
107      */
108     @Override
109     protected FTPFileEntryParser getParser()
110     {
111        return new CompositeFileEntryParser(new FTPFileEntryParser[]
112         {
113             new NTFTPEntryParser(),
114             new UnixFTPEntryParser()
115 
116         });
117     }
118 
119     /**
120      * Method suite.
121      *
122      * @return TestSuite
123      */
124     public static TestSuite suite()
125     {
126         return(new TestSuite(NTFTPEntryParserTest.class));
127     }
128 
129     /**
130      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
131      */
132     @Override
133     public void testParseFieldsOnDirectory() throws Exception
134     {
135         FTPFile dir = getParser().parseFTPEntry("12-05-96  05:03PM       <DIR>          absoft2");
136         assertNotNull("Could not parse entry.", dir);
137         assertEquals("Thu Dec 05 17:03:00 1996",
138                      df.format(dir.getTimestamp().getTime()));
139         assertTrue("Should have been a directory.",
140                    dir.isDirectory());
141         assertEquals("absoft2", dir.getName());
142         assertEquals(0, dir.getSize());
143 
144         dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123456");
145         assertNotNull("Could not parse entry.", dir);
146         assertTrue("Should have been a directory.",
147                 dir.isDirectory());
148         assertEquals("123456", dir.getName());
149         assertEquals(0, dir.getSize());
150 
151     }
152     
153     public void testParseLeadingDigits() {
154             FTPFile file = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 10 years and under");
155             assertNotNull("Could not parse entry", file);
156             assertEquals("10 years and under", file.getName());
157             assertEquals(5000000000L, file.getSize());
158             
159             FTPFile dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>           10 years and under");
160             assertNotNull("Could not parse entry", dir);
161             assertEquals("10 years and under", dir.getName());
162     }
163 
164     /**
165      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
166      */
167     @Override
168     public void testParseFieldsOnFile() throws Exception
169     {
170         FTPFile f = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 AUTOEXEC.BAK");
171         assertNotNull("Could not parse entry.", f);
172         assertEquals("Thu May 22 00:08:00 1997",
173                      df.format(f.getTimestamp().getTime()));
174         assertTrue("Should have been a file.",
175                    f.isFile());
176         assertEquals("AUTOEXEC.BAK", f.getName());
177         assertEquals(5000000000L, f.getSize());
178 
179         // test an NT-unix style listing that does NOT have a leading zero
180         // on the hour.
181 
182         f = getParser().parseFTPEntry(
183                 "-rw-rw-r--   1 mqm        mqm          17707 Mar 12  3:33 killmq.sh.log");
184         assertNotNull("Could not parse entry.", f);
185         Calendar cal = Calendar.getInstance();
186         cal.setTime(f.getTimestamp().getTime());
187         assertEquals("hour", 3, cal.get(Calendar.HOUR));
188         assertTrue("Should have been a file.",
189                 f.isFile());
190         assertEquals(17707, f.getSize());
191     }
192 
193 
194     @Override
195     protected void doAdditionalGoodTests(String test, FTPFile f)
196     {
197         if (test.indexOf("<DIR>") >= 0)
198         {
199                     assertEquals("directory.type",
200                             FTPFile.DIRECTORY_TYPE, f.getType());
201         }
202     }
203 
204     /**
205      * test condition reported as bug 20259.
206      * directory with name beginning with a numeric character
207      * was not parsing correctly
208      *
209      * @throws Exception
210      */
211     public void testDirectoryBeginningWithNumber() throws Exception
212     {
213         FTPFile f = getParser().parseFTPEntry(directoryBeginningWithNumber);
214         assertEquals("name", "123xyz", f.getName());
215     }
216     
217     public void testDirectoryBeginningWithNumberFollowedBySpaces() throws Exception
218     {
219         FTPFile f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 xyz");
220         assertEquals("name", "123 xyz", f.getName());
221         f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 abc xyz");
222         assertNotNull(f);
223         assertEquals("name", "123 abc xyz", f.getName());
224     }
225     
226     /**
227      * Test that group names with embedded spaces can be handled correctly
228      *
229      */
230     public void testGroupNameWithSpaces() {
231         FTPFile f = getParser().parseFTPEntry("drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata");
232         assertNotNull(f);
233         assertEquals("maxm", f.getUser());
234         assertEquals("Domain Users", f.getGroup());
235     }
236     
237 }