001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.net.ftp.parser;
018    
019    import java.io.ByteArrayInputStream;
020    import java.io.IOException;
021    
022    import junit.framework.TestSuite;
023    
024    import org.apache.commons.net.ftp.FTPFile;
025    import org.apache.commons.net.ftp.FTPFileEntryParser;
026    import org.apache.commons.net.ftp.FTPListParseEngine;
027    
028    /**
029     * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
030     * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
031     * @version $Id: VMSFTPEntryParserTest.java 657169 2008-05-16 19:07:02Z sebb $
032     */
033    public class VMSFTPEntryParserTest extends FTPParseTestFramework
034    {
035        private static final String[] badsamples =
036        {
037    
038            "1-JUN.LIS;2              9/9           JUN-2-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
039            "1-JUN.LIS;2              a/9           2-JUN-98 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
040            "DATA.DIR; 1              1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (,RWED,RWED,RE)",
041            "120196.TXT;1           118/126        14-APR-1997 12:45:27 PM  [GROUP,OWNER]    (RWED,,RWED,RE)",
042            "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP-1,OWNER]    (RWED,RWED,RWED,RE)",
043            "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED2,RWED,RWED,RE)",
044            "AA.;2                  152/153        13-FED-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
045            "Directory USER1:[TEMP]\r\n\r\n",
046            "\r\nTotal 14 files"
047        };
048    
049        private static final String[] goodsamples =
050        {
051            "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
052            "1-JUN.LIS;3              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
053            "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)",
054            "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [TRANSLATED]     (,RWED,RWED,RE)",
055            "120196.TXT;1           118/126        14-APR-1997 12:45:27  [GROUP,OWNER]    (RWED,,RWED,RE)",
056            "30CHARBAR.TXT;1         11/18          2-JUN-1998 08:38:42  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
057            "A.;2                    18/18          1-JUL-1998 08:43:20  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
058            "AA.;2                  152/153        13-FEB-1997 08:13:43  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
059            "UCX$REXECD_STARTUP.LOG;1098\r\n                         4/15         24-FEB-2003 13:17:24  [POSTWARE,LP]    (RWED,RWED,RE,)",
060            "UNARCHIVE.COM;1          2/15          7-JUL-1997 16:37:45  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
061            "UNXMERGE.COM;15          1/15         20-AUG-1996 13:59:50  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
062            "UNXTEMP.COM;7            1/15         15-AUG-1996 14:10:38  [POSTWARE,LP]    (RWE,RWE,RWE,RE)",
063            "UNZIP_AND_ATTACH_FILES.COM;12\r\n                        14/15         24-JUL-2002 14:35:40  [TRANSLATED]    (RWE,RWE,RWE,RE)",
064            "UNZIP_AND_ATTACH_FILES.SAV;1\r\n                        14/15         17-JAN-2002 11:13:53  [POSTWARE,LP]    (RWE,RWED,RWE,RE)",
065            "FREEWARE40.DIR;1        27/36         16-FEB-1999 10:01:46  [AP_HTTPD,APACHE$WWW                               (RWE,RWE,RE,RE)",
066            "1-JUN.LIS;1              9/9           2-jun-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
067        };
068    
069        private static final String fullListing = "Directory USER1:[TEMP]\r\n\r\n"+
070        "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)\r\n"+
071        "2-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
072        "3-JUN.LIS;1              9/9           3-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
073        "3-JUN.LIS;4              9/9           7-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
074        "3-JUN.LIS;2              9/9           4-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
075        "3-JUN.LIS;3              9/9           6-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,)\r\n"+
076        "\r\nTotal 6 files";
077    
078        /**
079         * @see junit.framework.TestCase#TestCase(String)
080         */
081        public VMSFTPEntryParserTest(String name)
082        {
083            super(name);
084        }
085    
086        /**
087         * Test the parsing of the whole list.
088         * @throws IOException
089         */
090        public void testWholeListParse() throws IOException
091        {
092            VMSFTPEntryParser parser = new VMSFTPEntryParser();
093            parser.configure(null);
094            FTPListParseEngine engine = new FTPListParseEngine(parser);
095            engine.readServerList(
096                    new ByteArrayInputStream(fullListing.getBytes()));
097            FTPFile[] files = engine.getFiles();
098            assertEquals(6, files.length);
099            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    }