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.io.IOException;
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   * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
28   * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
29   * @version $Id: VMSFTPEntryParserTest.java 1407341 2012-11-09 01:31:00Z ggregory $
30   */
31  public class VMSFTPEntryParserTest extends FTPParseTestFramework
32  {
33      private static final String[] badsamples =
34      {
35  
36          "1-JUN.LIS;2              9/9           JUN-2-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
37          "1-JUN.LIS;2              a/9           2-JUN-98 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
38          "DATA.DIR; 1              1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (,RWED,RWED,RE)",
39          "120196.TXT;1           118/126        14-APR-1997 12:45:27 PM  [GROUP,OWNER]    (RWED,,RWED,RE)",
40          "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP-1,OWNER]    (RWED,RWED,RWED,RE)",
41          "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED2,RWED,RWED,RE)",
42          "AA.;2                  152/153        13-FED-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
43          "Directory USER1:[TEMP]\r\n\r\n",
44          "\r\nTotal 14 files"
45      };
46  
47      private static final String[] goodsamples =
48      {
49          "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
50          "1-JUN.LIS;3              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
51          "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
52          "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [TRANSLATED]     (,RWED,RWED,RE)",
53          "120196.TXT;1           118/126        14-APR-1997 12:45:27  [GROUP,OWNER]    (RWED,,RWED,RE)",
54          "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
55          "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
56          "AA.;2                  152/153        13-FEB-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
57          "UCX$REXECD_STARTUP.LOG;1098\r\n                         4/15         24-FEB-2003 13:17:24  [POSTWARE,LP]    (RWED,RWED,RE,)",
58          "UNARCHIVE.COM;1          2/15          7-JUL-1997 16:37:45  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
59          "UNXMERGE.COM;15          1/15         20-AUG-1996 13:59:50  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
60          "UNXTEMP.COM;7            1/15         15-AUG-1996 14:10:38  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
61          "UNZIP_AND_ATTACH_FILES.COM;12\r\n                        14/15         24-JUL-2002 14:35:40  [TRANSLATED]    (RWE,RWE,RWE,RE)",
62          "UNZIP_AND_ATTACH_FILES.SAV;1\r\n                        14/15         17-JAN-2002 11:13:53  [POSTWARE,LP]    (RWE,RWED,RWE,RE)",
63          "FREEWARE40.DIR;1        27/36         16-FEB-1999 10:01:46  [AP_HTTPD,APACHE$WWW                               (RWE,RWE,RE,RE)",
64          "1-JUN.LIS;1              9/9           2-jun-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
65      };
66  
67      private static final String fullListing = "Directory USER1:[TEMP]\r\n\r\n"+
68      "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)\r\n"+
69      "2-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
70      "3-JUN.LIS;1              9/9           3-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
71      "3-JUN.LIS;4              9/9           7-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
72      "3-JUN.LIS;2              9/9           4-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
73      "3-JUN.LIS;3              9/9           6-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
74      "\r\nTotal 6 files";
75  
76      /**
77       * @see junit.framework.TestCase#TestCase(String)
78       */
79      public VMSFTPEntryParserTest(String name)
80      {
81          super(name);
82      }
83  
84      /**
85       * Test the parsing of the whole list.
86       * @throws IOException
87       */
88      public void testWholeListParse() throws IOException
89      {
90          VMSFTPEntryParser parser = new VMSFTPEntryParser();
91          parser.configure(null);
92          FTPListParseEngine engine = new FTPListParseEngine(parser);
93          engine.readServerList(
94                  new ByteArrayInputStream(fullListing.getBytes()), null); // use default encoding
95          FTPFile[] files = engine.getFiles();
96          assertEquals(6, files.length);
97          assertFileInListing(files, "2-JUN.LIS");
98          assertFileInListing(files, "3-JUN.LIS");
99          assertFileInListing(files, "1-JUN.LIS");
100         assertFileNotInListing(files, "1-JUN.LIS;1");
101 
102     }
103 
104     /**
105      * Test the parsing of the whole list.
106      * @throws IOException
107      */
108     public void testWholeListParseWithVersioning() throws IOException
109     {
110 
111         VMSFTPEntryParser parser = new VMSVersioningFTPEntryParser();
112         parser.configure(null);
113         FTPListParseEngine engine = new FTPListParseEngine(parser);
114         engine.readServerList(
115                 new ByteArrayInputStream(fullListing.getBytes()), null); // use default encoding
116         FTPFile[] files = engine.getFiles();
117         assertEquals(3, files.length);
118         assertFileInListing(files, "1-JUN.LIS;1");
119         assertFileInListing(files, "2-JUN.LIS;1");
120         assertFileInListing(files, "3-JUN.LIS;4");
121         assertFileNotInListing(files, "3-JUN.LIS;1");
122         assertFileNotInListing(files, "3-JUN.LIS");
123 
124     }
125 
126     public void assertFileInListing(FTPFile[] listing, String name) {
127         for (FTPFile element : listing)
128         {
129             if (name.equals(element.getName())) {
130                 return;
131             }
132         }
133         fail("File " + name + " not found in supplied listing");
134     }
135     public void assertFileNotInListing(FTPFile[] listing, String name) {
136         for (FTPFile element : listing)
137         {
138             if (name.equals(element.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 }