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