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