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