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