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   */
28  public class VMSFTPEntryParserTest extends AbstractFTPParseTest {
29      private static final String[] BAD_SAMPLES = {
30  
31              "1-JUN.LIS;2              9/9           JUN-2-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
32              "1-JUN.LIS;2              a/9           2-JUN-98 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
33              "DATA.DIR; 1              1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (,RWED,RWED,RE)",
34              "120196.TXT;1           118/126        14-APR-1997 12:45:27 PM  [GROUP,OWNER]    (RWED,,RWED,RE)",
35              "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP-1,OWNER]    (RWED,RWED,RWED,RE)",
36              "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED2,RWED,RWED,RE)",
37              "AA.;2                  152/153        13-FED-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)", "Directory USER1:[TEMP]\r\n\r\n",
38              "\r\nTotal 14 files" };
39  
40      // CHECKSTYLE:OFF (long lines)
41      private static final String[] GOOD_SAMPLES = { "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
42              "1-JUN.LIS;3              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
43              "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
44              "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [TRANSLATED]     (,RWED,RWED,RE)",
45              "120196.TXT;1           118/126        14-APR-1997 12:45:27  [GROUP,OWNER]    (RWED,,RWED,RE)",
46              "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
47              "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
48              "AA.;2                  152/153        13-FEB-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
49              "UCX$REXECD_STARTUP.LOG;1098\r\n" + "                         4/15         24-FEB-2003 13:17:24  [POSTWARE,LP]    (RWED,RWED,RE,)",
50              "UNARCHIVE.COM;1          2/15          7-JUL-1997 16:37:45  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
51              "UNXMERGE.COM;15          1/15         20-AUG-1996 13:59:50  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
52              "UNXTEMP.COM;7            1/15         15-AUG-1996 14:10:38  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
53              "UNZIP_AND_ATTACH_FILES.COM;12\r\n" + "                        14/15         24-JUL-2002 14:35:40  [TRANSLATED]    (RWE,RWE,RWE,RE)",
54              "UNZIP_AND_ATTACH_FILES.SAV;1\r\n" + "                        14/15         17-JAN-2002 11:13:53  [POSTWARE,LP]    (RWE,RWED,RWE,RE)",
55              "FREEWARE40.DIR;1        27/36" + "         16-FEB-1999 10:01:46  [AP_HTTPD,APACHE$WWW                               (RWE,RWE,RE,RE)",
56              "1-JUN.LIS;1              9/9           2-jun-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
57              "ALLOCMISS.COM;1            1         15-AUG-1996 14:10:38  [POSTWARE,LP]    (RWE,RWE,RWE,RE)" };
58      // CHECKSTYLE:ON
59  
60      private static final String FULL_LISTING = "Directory USER1:[TEMP]\r\n\r\n"
61              + "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)\r\n"
62              + "2-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"
63              + "3-JUN.LIS;1              9/9           3-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"
64              + "3-JUN.LIS;4              9/9           7-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"
65              + "3-JUN.LIS;2              9/9           4-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"
66              + "3-JUN.LIS;3              9/9           6-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n" + "\r\nTotal 6 files";
67  
68      /**
69       * @see junit.framework.TestCase#TestCase(String)
70       */
71      public VMSFTPEntryParserTest(final String name) {
72          super(name);
73      }
74  
75      public void assertFileInListing(final FTPFile[] listing, final String name) {
76          for (final FTPFile element : listing) {
77              if (name.equals(element.getName())) {
78                  return;
79              }
80          }
81          fail("File " + name + " not found in supplied listing");
82      }
83  
84      public void assertFileNotInListing(final FTPFile[] listing, final String name) {
85          for (final FTPFile element : listing) {
86              if (name.equals(element.getName())) {
87                  fail("Unexpected File " + name + " found in supplied listing");
88              }
89          }
90      }
91  
92      /*
93       * Verify that the VMS parser does NOT set the permissions.
94       */
95      private void checkPermisions(final FTPFile dir, final int octalPerm) {
96          int permMask = 1 << 8;
97          assertEquals("Owner should not have read permission.", (permMask & octalPerm) != 0, dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
98          permMask >>= 1;
99          assertEquals("Owner should not have write permission.", (permMask & octalPerm) != 0, dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
100         permMask >>= 1;
101         assertEquals("Owner should not have execute permission.", (permMask & octalPerm) != 0,
102                 dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
103         permMask >>= 1;
104         assertEquals("Group should not have read permission.", (permMask & octalPerm) != 0, dir.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
105         permMask >>= 1;
106         assertEquals("Group should not have write permission.", (permMask & octalPerm) != 0, dir.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
107         permMask >>= 1;
108         assertEquals("Group should not have execute permission.", (permMask & octalPerm) != 0,
109                 dir.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
110         permMask >>= 1;
111         assertEquals("World should not have read permission.", (permMask & octalPerm) != 0, dir.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
112         permMask >>= 1;
113         assertEquals("World should not have write permission.", (permMask & octalPerm) != 0, dir.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
114         permMask >>= 1;
115         assertEquals("World should not have execute permission.", (permMask & octalPerm) != 0,
116                 dir.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
117     }
118 
119     @Override
120     protected String[] getBadListing() {
121 
122         return BAD_SAMPLES;
123     }
124 
125     @Override
126     protected String[] getGoodListing() {
127 
128         return GOOD_SAMPLES;
129     }
130 
131     @Override
132     protected FTPFileEntryParser getParser() {
133         final ConfigurableFTPFileEntryParserImpl parser = new VMSFTPEntryParser();
134         parser.configure(null);
135         return parser;
136     }
137 
138     protected FTPFileEntryParser getVersioningParser() {
139         final ConfigurableFTPFileEntryParserImpl parser = new VMSVersioningFTPEntryParser();
140         parser.configure(null);
141         return parser;
142     }
143 
144     @Override
145     public void testDefaultPrecision() {
146         testPrecision("1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [TRANSLATED]    (RWED,RD,,)", CalendarUnit.SECOND);
147     }
148 
149     @Override
150     public void testParseFieldsOnDirectory() throws Exception {
151 
152         FTPFile dir = getParser().parseFTPEntry("DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)");
153         assertTrue("Should be a directory.", dir.isDirectory());
154         assertEquals("DATA.DIR", dir.getName());
155         assertEquals(512, dir.getSize());
156         assertEquals("Tue Jun 02 07:32:04 1998", df.format(dir.getTimestamp().getTime()));
157         assertEquals("GROUP", dir.getGroup());
158         assertEquals("OWNER", dir.getUser());
159         checkPermisions(dir, 0775);
160 
161         dir = getParser().parseFTPEntry("DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [TRANSLATED]    (RWED,RWED,,RE)");
162         assertTrue("Should be a directory.", dir.isDirectory());
163         assertEquals("DATA.DIR", dir.getName());
164         assertEquals(512, dir.getSize());
165         assertEquals("Tue Jun 02 07:32:04 1998", df.format(dir.getTimestamp().getTime()));
166         assertNull(dir.getGroup());
167         assertEquals("TRANSLATED", dir.getUser());
168         checkPermisions(dir, 0705);
169     }
170 
171     @Override
172     public void testParseFieldsOnFile() throws Exception {
173         FTPFile file = getParser().parseFTPEntry("1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RW,R)");
174         assertTrue("Should be a file.", file.isFile());
175         assertEquals("1-JUN.LIS", file.getName());
176         assertEquals(9 * 512, file.getSize());
177         assertEquals("Tue Jun 02 07:32:04 1998", df.format(file.getTimestamp().getTime()));
178         assertEquals("GROUP", file.getGroup());
179         assertEquals("OWNER", file.getUser());
180         checkPermisions(file, 0764);
181 
182         file = getParser().parseFTPEntry("1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [TRANSLATED]    (RWED,RD,,)");
183         assertTrue("Should be a file.", file.isFile());
184         assertEquals("1-JUN.LIS", file.getName());
185         assertEquals(9 * 512, file.getSize());
186         assertEquals("Tue Jun 02 07:32:04 1998", df.format(file.getTimestamp().getTime()));
187         assertNull(file.getGroup());
188         assertEquals("TRANSLATED", file.getUser());
189         checkPermisions(file, 0400);
190     }
191 
192     @Override
193     public void testRecentPrecision() {
194         // Not used
195     }
196 
197     public void testWholeListParse() throws IOException {
198         final VMSFTPEntryParser parser = new VMSFTPEntryParser();
199         parser.configure(null);
200         final FTPListParseEngine engine = new FTPListParseEngine(parser);
201         engine.readServerList(new ByteArrayInputStream(FULL_LISTING.getBytes()), null); // use default encoding
202         final FTPFile[] files = engine.getFiles();
203         assertEquals(6, files.length);
204         assertFileInListing(files, "2-JUN.LIS");
205         assertFileInListing(files, "3-JUN.LIS");
206         assertFileInListing(files, "1-JUN.LIS");
207         assertFileNotInListing(files, "1-JUN.LIS;1");
208 
209     }
210 
211     public void testWholeListParseWithVersioning() throws IOException {
212 
213         final VMSFTPEntryParser parser = new VMSVersioningFTPEntryParser();
214         parser.configure(null);
215         final FTPListParseEngine engine = new FTPListParseEngine(parser);
216         engine.readServerList(new ByteArrayInputStream(FULL_LISTING.getBytes()), null); // use default encoding
217         final FTPFile[] files = engine.getFiles();
218         assertEquals(3, files.length);
219         assertFileInListing(files, "1-JUN.LIS;1");
220         assertFileInListing(files, "2-JUN.LIS;1");
221         assertFileInListing(files, "3-JUN.LIS;4");
222         assertFileNotInListing(files, "3-JUN.LIS;1");
223         assertFileNotInListing(files, "3-JUN.LIS");
224 
225     }
226 }