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.io.IOException;
21  
22  import junit.framework.TestSuite;
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   * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
30   * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
31   * @version $Id: VMSFTPEntryParserTest.java 657169 2008-05-16 19:07:02Z sebb $
32   */
33  public class VMSFTPEntryParserTest extends FTPParseTestFramework
34  {
35      private static final String[] badsamples =
36      {
37  
38          "1-JUN.LIS;2              9/9           JUN-2-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
39          "1-JUN.LIS;2              a/9           2-JUN-98 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
40          "DATA.DIR; 1              1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (,RWED,RWED,RE)",
41          "120196.TXT;1           118/126        14-APR-1997 12:45:27 PM  [GROUP,OWNER]    (RWED,,RWED,RE)",
42          "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP-1,OWNER]    (RWED,RWED,RWED,RE)",
43          "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED2,RWED,RWED,RE)",
44          "AA.;2                  152/153        13-FED-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
45          "Directory USER1:[TEMP]\r\n\r\n",
46          "\r\nTotal 14 files"
47      };
48  
49      private static final String[] goodsamples =
50      {
51          "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
52          "1-JUN.LIS;3              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
53          "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
54          "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [TRANSLATED]     (,RWED,RWED,RE)",
55          "120196.TXT;1           118/126        14-APR-1997 12:45:27  [GROUP,OWNER]    (RWED,,RWED,RE)",
56          "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
57          "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
58          "AA.;2                  152/153        13-FEB-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
59          "UCX$REXECD_STARTUP.LOG;1098\r\n                         4/15         24-FEB-2003 13:17:24  [POSTWARE,LP]    (RWED,RWED,RE,)",
60          "UNARCHIVE.COM;1          2/15          7-JUL-1997 16:37:45  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
61          "UNXMERGE.COM;15          1/15         20-AUG-1996 13:59:50  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
62          "UNXTEMP.COM;7            1/15         15-AUG-1996 14:10:38  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
63          "UNZIP_AND_ATTACH_FILES.COM;12\r\n                        14/15         24-JUL-2002 14:35:40  [TRANSLATED]    (RWE,RWE,RWE,RE)",
64          "UNZIP_AND_ATTACH_FILES.SAV;1\r\n                        14/15         17-JAN-2002 11:13:53  [POSTWARE,LP]    (RWE,RWED,RWE,RE)",
65          "FREEWARE40.DIR;1        27/36         16-FEB-1999 10:01:46  [AP_HTTPD,APACHE$WWW                               (RWE,RWE,RE,RE)",
66          "1-JUN.LIS;1              9/9           2-jun-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
67      };
68  
69      private static final String fullListing = "Directory USER1:[TEMP]\r\n\r\n"+
70      "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)\r\n"+
71      "2-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
72      "3-JUN.LIS;1              9/9           3-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
73      "3-JUN.LIS;4              9/9           7-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
74      "3-JUN.LIS;2              9/9           4-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
75      "3-JUN.LIS;3              9/9           6-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
76      "\r\nTotal 6 files";
77  
78      /**
79       * @see junit.framework.TestCase#TestCase(String)
80       */
81      public VMSFTPEntryParserTest(String name)
82      {
83          super(name);
84      }
85  
86      /**
87       * Test the parsing of the whole list.
88       * @throws IOException
89       */
90      public void testWholeListParse() throws IOException
91      {
92          VMSFTPEntryParser parser = new VMSFTPEntryParser();
93          parser.configure(null);
94          FTPListParseEngine engine = new FTPListParseEngine(parser);
95          engine.readServerList(
96                  new ByteArrayInputStream(fullListing.getBytes()));
97          FTPFile[] files = engine.getFiles();
98          assertEquals(6, files.length);
99          assertFileInListing(files, "2-JUN.LIS");
100         assertFileInListing(files, "3-JUN.LIS");
101         assertFileInListing(files, "1-JUN.LIS");
102         assertFileNotInListing(files, "1-JUN.LIS;1");
103 
104     }
105 
106     /**
107      * Test the parsing of the whole list.
108      * @throws IOException
109      */
110     public void testWholeListParseWithVersioning() throws IOException
111     {
112 
113         VMSFTPEntryParser parser = new VMSVersioningFTPEntryParser();
114         parser.configure(null);
115         FTPListParseEngine engine = new FTPListParseEngine(parser);
116         engine.readServerList(
117                 new ByteArrayInputStream(fullListing.getBytes()));
118         FTPFile[] files = engine.getFiles();
119         assertEquals(3, files.length);
120         assertFileInListing(files, "1-JUN.LIS;1");
121         assertFileInListing(files, "2-JUN.LIS;1");
122         assertFileInListing(files, "3-JUN.LIS;4");
123         assertFileNotInListing(files, "3-JUN.LIS;1");
124         assertFileNotInListing(files, "3-JUN.LIS");
125 
126     }
127 
128     public void assertFileInListing(FTPFile[] listing, String name) {
129         for (int i = 0; i < listing.length; i++) {
130             if (name.equals(listing[i].getName())) {
131                 return;
132             }
133         }
134         fail("File " + name + " not found in supplied listing");
135     }
136     public void assertFileNotInListing(FTPFile[] listing, String name) {
137         for (int i = 0; i < listing.length; i++) {
138             if (name.equals(listing[i].getName())) {
139                 fail("Unexpected File " + name + " found in supplied listing");
140             }
141         }
142     }
143 
144     /**
145      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
146      */
147     @Override
148     public void testParseFieldsOnDirectory() throws Exception
149     {
150 
151         FTPFile dir = getParser().parseFTPEntry("DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)");
152         assertTrue("Should be a directory.",
153                    dir.isDirectory());
154         assertEquals("DATA.DIR",
155                      dir.getName());
156         assertEquals(512,
157                      dir.getSize());
158         assertEquals("Tue Jun 02 07:32:04 1998",
159                      df.format(dir.getTimestamp().getTime()));
160         assertEquals("GROUP",
161                      dir.getGroup());
162         assertEquals("OWNER",
163                      dir.getUser());
164         checkPermisions(dir, 0775);
165 
166 
167         dir = getParser().parseFTPEntry("DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [TRANSLATED]    (RWED,RWED,,RE)");
168         assertTrue("Should be a directory.",
169                            dir.isDirectory());
170         assertEquals("DATA.DIR",
171                              dir.getName());
172         assertEquals(512,
173                              dir.getSize());
174         assertEquals("Tue Jun 02 07:32:04 1998",
175                              df.format(dir.getTimestamp().getTime()));
176         assertEquals(null,
177                      dir.getGroup());
178         assertEquals("TRANSLATED",
179                      dir.getUser());
180         checkPermisions(dir, 0705);
181     }
182 
183     /**
184      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
185      */
186     @Override
187     public void testParseFieldsOnFile() throws Exception
188     {
189         FTPFile file = getParser().parseFTPEntry("1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RW,R)");
190         assertTrue("Should be a file.",
191                    file.isFile());
192         assertEquals("1-JUN.LIS",
193                      file.getName());
194         assertEquals(9 * 512,
195                      file.getSize());
196         assertEquals("Tue Jun 02 07:32:04 1998",
197                      df.format(file.getTimestamp().getTime()));
198         assertEquals("GROUP",
199                      file.getGroup());
200         assertEquals("OWNER",
201                      file.getUser());
202         checkPermisions(file, 0764);
203 
204 
205         file = getParser().parseFTPEntry("1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [TRANSLATED]    (RWED,RD,,)");
206         assertTrue("Should be a file.",
207                    file.isFile());
208         assertEquals("1-JUN.LIS",
209                      file.getName());
210         assertEquals(9 * 512,
211                      file.getSize());
212         assertEquals("Tue Jun 02 07:32:04 1998",
213                      df.format(file.getTimestamp().getTime()));
214         assertEquals(null,
215                      file.getGroup());
216         assertEquals("TRANSLATED",
217                      file.getUser());
218         checkPermisions(file, 0400);
219     }
220 
221     /**
222      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getBadListing()
223      */
224     @Override
225     protected String[] getBadListing()
226     {
227 
228         return (badsamples);
229     }
230 
231     /**
232      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getGoodListing()
233      */
234     @Override
235     protected String[] getGoodListing()
236     {
237 
238         return (goodsamples);
239     }
240 
241     /**
242      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
243      */
244     @Override
245     protected FTPFileEntryParser getParser()
246     {
247         ConfigurableFTPFileEntryParserImpl parser =
248             new VMSFTPEntryParser();
249         parser.configure(null);
250         return parser;
251     }
252     protected FTPFileEntryParser getVersioningParser()
253     {
254         ConfigurableFTPFileEntryParserImpl parser =
255             new VMSVersioningFTPEntryParser();
256         parser.configure(null);
257         return parser;
258     }
259 
260     /**
261      * Method checkPermisions.
262      * Verify that the VMS parser does NOT  set the permissions.
263      * @param dir
264      */
265     private void checkPermisions(FTPFile dir, int octalPerm)
266     {
267         int permMask = 1<<8;
268         assertTrue("Owner should not have read permission.",
269                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.USER_ACCESS,
270                                       FTPFile.READ_PERMISSION));
271         permMask >>= 1;
272         assertTrue("Owner should not have write permission.",
273                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.USER_ACCESS,
274                                       FTPFile.WRITE_PERMISSION));
275         permMask >>= 1;
276         assertTrue("Owner should not have execute permission.",
277                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.USER_ACCESS,
278                                       FTPFile.EXECUTE_PERMISSION));
279         permMask >>= 1;
280         assertTrue("Group should not have read permission.",
281                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.GROUP_ACCESS,
282                                       FTPFile.READ_PERMISSION));
283         permMask >>= 1;
284         assertTrue("Group should not have write permission.",
285                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.GROUP_ACCESS,
286                                       FTPFile.WRITE_PERMISSION));
287         permMask >>= 1;
288         assertTrue("Group should not have execute permission.",
289                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.GROUP_ACCESS,
290                                       FTPFile.EXECUTE_PERMISSION));
291         permMask >>= 1;
292         assertTrue("World should not have read permission.",
293                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.WORLD_ACCESS,
294                                       FTPFile.READ_PERMISSION));
295         permMask >>= 1;
296         assertTrue("World should not have write permission.",
297                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.WORLD_ACCESS,
298                                       FTPFile.WRITE_PERMISSION));
299         permMask >>= 1;
300         assertTrue("World should not have execute permission.",
301                 ((permMask & octalPerm) != 0) == dir.hasPermission(FTPFile.WORLD_ACCESS,
302                                       FTPFile.EXECUTE_PERMISSION));
303     }
304 
305     /**
306      * Method suite.
307      * @return TestSuite
308      */
309     public static TestSuite suite()
310     {
311         return(new TestSuite(VMSFTPEntryParserTest.class));
312     }
313 }