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.util.Calendar;
20  
21  import org.apache.commons.net.ftp.FTPFile;
22  import org.apache.commons.net.ftp.FTPFileEntryParser;
23  
24  /**
25   * Tests the EnterpriseUnixFTPEntryParser
26   *
27   * @version $Id: EnterpriseUnixFTPEntryParserTest.java 1299238 2012-03-10 17:12:28Z sebb $
28   * @author <a href="mailto:Winston.Ojeda@qg.com">Winston Ojeda</a>
29   */
30  public class EnterpriseUnixFTPEntryParserTest extends FTPParseTestFramework
31  {
32  
33      private static final String[] BADSAMPLES =
34      {
35          "zrwxr-xr-x   2 root     root         4096 Mar  2 15:13 zxbox",
36          "dxrwr-xr-x   2 root     root         4096 Aug 24  2001 zxjdbc",
37          "drwxr-xr-x   2 root     root         4096 Jam  4 00:03 zziplib",
38          "drwxr-xr-x   2 root     99           4096 Feb 23 30:01 zzplayer",
39          "drwxr-xr-x   2 root     root         4096 Aug 36  2001 zztpp",
40          "-rw-r--r--   1 14       staff       80284 Aug 22  zxJDBC-1.2.3.tar.gz",
41          "-rw-r--r--   1 14       staff      119:26 Aug 22  2000 zxJDBC-1.2.3.zip",
42          "-rw-r--r--   1 ftp      no group    83853 Jan 22  2001 zxJDBC-1.2.4.tar.gz",
43          "-rw-r--r--   1ftp       nogroup    126552 Jan 22  2001 zxJDBC-1.2.4.zip",
44          "-rw-r--r--   1 root     root       111325 Apr -7 18:79 zxJDBC-2.0.1b1.tar.gz",
45          "drwxr-xr-x   2 root     root         4096 Mar  2 15:13 zxbox",
46          "drwxr-xr-x 1 usernameftp 512 Jan 29 23:32 prog",
47          "drwxr-xr-x   2 root     root         4096 Aug 24  2001 zxjdbc",
48          "drwxr-xr-x   2 root     root         4096 Jan  4 00:03 zziplib",
49          "drwxr-xr-x   2 root     99           4096 Feb 23  2001 zzplayer",
50          "drwxr-xr-x   2 root     root         4096 Aug  6  2001 zztpp",
51          "-rw-r--r--   1 14       staff       80284 Aug 22  2000 zxJDBC-1.2.3.tar.gz",
52          "-rw-r--r--   1 14       staff      119926 Aug 22  2000 zxJDBC-1.2.3.zip",
53          "-rw-r--r--   1 ftp      nogroup     83853 Jan 22  2001 zxJDBC-1.2.4.tar.gz",
54          "-rw-r--r--   1 ftp      nogroup    126552 Jan 22  2001 zxJDBC-1.2.4.zip",
55          "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
56          "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip"
57      };
58      private static final String[] GOODSAMPLES =
59      {
60          "-C--E-----FTP B QUA1I1      18128       41 Aug 12 13:56 QUADTEST",
61          "-C--E-----FTP A QUA1I1      18128       41 Aug 12 13:56 QUADTEST2"
62      };
63  
64      /**
65       * Creates a new EnterpriseUnixFTPEntryParserTest object.
66       *
67       * @param name Test name.
68       */
69      public EnterpriseUnixFTPEntryParserTest(String name)
70      {
71          super(name);
72      }
73  
74      /**
75       * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
76       */
77      @Override
78      public void testParseFieldsOnDirectory() throws Exception
79      {
80          // Everything is a File for now.
81      }
82  
83      /**
84       * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
85       */
86      @Override
87      public void testParseFieldsOnFile() throws Exception
88      {
89          FTPFile file = getParser().parseFTPEntry("-C--E-----FTP B QUA1I1      18128       5000000000 Aug 12 13:56 QUADTEST");
90          Calendar today  = Calendar.getInstance();
91          int year        = today.get(Calendar.YEAR);
92  
93          assertTrue("Should be a file.",
94                     file.isFile());
95          assertEquals("QUADTEST",
96                       file.getName());
97          assertEquals(5000000000L,
98                       file.getSize());
99          assertEquals("QUA1I1",
100                      file.getUser());
101         assertEquals("18128",
102                      file.getGroup());
103 
104         if (today.get(Calendar.MONTH) < Calendar.AUGUST) {
105             --year;
106         }
107 
108         Calendar timestamp = file.getTimestamp();
109         assertEquals(year, timestamp.get(Calendar.YEAR));
110         assertEquals(Calendar.AUGUST, timestamp.get(Calendar.MONTH));
111         assertEquals(12, timestamp.get(Calendar.DAY_OF_MONTH));
112         assertEquals(13, timestamp.get(Calendar.HOUR_OF_DAY));
113         assertEquals(56, timestamp.get(Calendar.MINUTE));
114         assertEquals(0, timestamp.get(Calendar.SECOND));
115 
116         checkPermisions(file);
117     }
118 
119     /**
120      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getBadListing()
121      */
122     @Override
123     protected String[] getBadListing()
124     {
125 
126         return (BADSAMPLES);
127     }
128 
129     /**
130      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getGoodListing()
131      */
132     @Override
133     protected String[] getGoodListing()
134     {
135 
136         return (GOODSAMPLES);
137     }
138 
139     /**
140      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
141      */
142     @Override
143     protected FTPFileEntryParser getParser()
144     {
145 
146         return (new EnterpriseUnixFTPEntryParser());
147     }
148 
149     /**
150      * Method checkPermisions. Verify that the parser does NOT  set the
151      * permissions.
152      *
153      * @param dir
154      */
155     private void checkPermisions(FTPFile dir)
156     {
157         assertTrue("Owner should not have read permission.",
158                    !dir.hasPermission(FTPFile.USER_ACCESS,
159                                       FTPFile.READ_PERMISSION));
160         assertTrue("Owner should not have write permission.",
161                    !dir.hasPermission(FTPFile.USER_ACCESS,
162                                       FTPFile.WRITE_PERMISSION));
163         assertTrue("Owner should not have execute permission.",
164                    !dir.hasPermission(FTPFile.USER_ACCESS,
165                                       FTPFile.EXECUTE_PERMISSION));
166         assertTrue("Group should not have read permission.",
167                    !dir.hasPermission(FTPFile.GROUP_ACCESS,
168                                       FTPFile.READ_PERMISSION));
169         assertTrue("Group should not have write permission.",
170                    !dir.hasPermission(FTPFile.GROUP_ACCESS,
171                                       FTPFile.WRITE_PERMISSION));
172         assertTrue("Group should not have execute permission.",
173                    !dir.hasPermission(FTPFile.GROUP_ACCESS,
174                                       FTPFile.EXECUTE_PERMISSION));
175         assertTrue("World should not have read permission.",
176                    !dir.hasPermission(FTPFile.WORLD_ACCESS,
177                                       FTPFile.READ_PERMISSION));
178         assertTrue("World should not have write permission.",
179                    !dir.hasPermission(FTPFile.WORLD_ACCESS,
180                                       FTPFile.WRITE_PERMISSION));
181         assertTrue("World should not have execute permission.",
182                    !dir.hasPermission(FTPFile.WORLD_ACCESS,
183                                       FTPFile.EXECUTE_PERMISSION));
184     }
185 }
186 
187 /* Emacs configuration
188  * Local variables:        **
189  * mode:             java  **
190  * c-basic-offset:   4     **
191  * indent-tabs-mode: nil   **
192  * End:                    **
193  */