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.time.Instant;
20  import java.time.Month;
21  import java.time.ZoneId;
22  import java.time.ZonedDateTime;
23  import java.util.Calendar;
24  import java.util.TimeZone;
25  
26  import org.apache.commons.net.ftp.FTPFile;
27  import org.apache.commons.net.ftp.FTPFileEntryParser;
28  
29  /**
30   * Tests the EnterpriseUnixFTPEntryParser
31   */
32  public class EnterpriseUnixFTPEntryParserTest extends AbstractFTPParseTest {
33  
34      private static final String[] BADSAMPLES = { "zrwxr-xr-x   2 root     root         4096 Mar  2 15:13 zxbox",
35              "dxrwr-xr-x   2 root     root         4096 Aug 24  2001 zxjdbc", "drwxr-xr-x   2 root     root         4096 Jam  4 00:03 zziplib",
36              "drwxr-xr-x   2 root     99           4096 Feb 23 30:01 zzplayer", "drwxr-xr-x   2 root     root         4096 Aug 36  2001 zztpp",
37              "-rw-r--r--   1 14       staff       80284 Aug 22  zxJDBC-1.2.3.tar.gz", "-rw-r--r--   1 14       staff      119:26 Aug 22  2000 zxJDBC-1.2.3.zip",
38              "-rw-r--r--   1 ftp      no group    83853 Jan 22  2001 zxJDBC-1.2.4.tar.gz",
39              "-rw-r--r--   1ftp       nogroup    126552 Jan 22  2001 zxJDBC-1.2.4.zip",
40              "-rw-r--r--   1 root     root       111325 Apr -7 18:79 zxJDBC-2.0.1b1.tar.gz", "drwxr-xr-x   2 root     root         4096 Mar  2 15:13 zxbox",
41              "drwxr-xr-x 1 usernameftp 512 Jan 29 23:32 prog", "drwxr-xr-x   2 root     root         4096 Aug 24  2001 zxjdbc",
42              "drwxr-xr-x   2 root     root         4096 Jan  4 00:03 zziplib", "drwxr-xr-x   2 root     99           4096 Feb 23  2001 zzplayer",
43              "drwxr-xr-x   2 root     root         4096 Aug  6  2001 zztpp", "-rw-r--r--   1 14       staff       80284 Aug 22  2000 zxJDBC-1.2.3.tar.gz",
44              "-rw-r--r--   1 14       staff      119926 Aug 22  2000 zxJDBC-1.2.3.zip",
45              "-rw-r--r--   1 ftp      nogroup     83853 Jan 22  2001 zxJDBC-1.2.4.tar.gz",
46              "-rw-r--r--   1 ftp      nogroup    126552 Jan 22  2001 zxJDBC-1.2.4.zip",
47              "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
48              "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip", "drwxr-xr-x   2 root     root         4096 Aug 26  20 zztpp",
49              "drwxr-xr-x   2 root     root         4096 Aug 26  201 zztpp", "drwxr-xr-x   2 root     root         4096 Aug 26  201O zztpp", // OH not zero
50      };
51      private static final String[] GOODSAMPLES = { "-C--E-----FTP B QUA1I1      18128       41 Aug 12 13:56 QUADTEST",
52              "-C--E-----FTP A QUA1I1      18128       41 Aug 12 13:56 QUADTEST2", "-C--E-----FTP A QUA1I1      18128       41 Apr 1 2014 QUADTEST3" };
53  
54      /**
55       * Creates a new EnterpriseUnixFTPEntryParserTest object.
56       *
57       * @param name Test name.
58       */
59      public EnterpriseUnixFTPEntryParserTest(final String name) {
60          super(name);
61      }
62  
63      /**
64       * Method checkPermisions. Verify that the parser does NOT set the permissions.
65       *
66       * @param dir
67       */
68      private void checkPermisions(final FTPFile dir) {
69          assertFalse("Owner should not have read permission.", dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
70          assertFalse("Owner should not have write permission.", dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
71          assertFalse("Owner should not have execute permission.", dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
72          assertFalse("Group should not have read permission.", dir.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
73          assertFalse("Group should not have write permission.", dir.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
74          assertFalse("Group should not have execute permission.", dir.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
75          assertFalse("World should not have read permission.", dir.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
76          assertFalse("World should not have write permission.", dir.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
77          assertFalse("World should not have execute permission.", dir.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
78      }
79  
80      /**
81       * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#getBadListing()
82       */
83      @Override
84      protected String[] getBadListing() {
85          return BADSAMPLES;
86      }
87  
88      /**
89       * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#getGoodListing()
90       */
91      @Override
92      protected String[] getGoodListing() {
93          return GOODSAMPLES;
94      }
95  
96      /**
97       * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#getParser()
98       */
99      @Override
100     protected FTPFileEntryParser getParser() {
101         return new EnterpriseUnixFTPEntryParser();
102     }
103 
104     @Override
105     public void testDefaultPrecision() {
106         testPrecision("-C--E-----FTP B QUA1I1      18128       5000000000 Aug 12 2014 QUADTEST", CalendarUnit.DAY_OF_MONTH);
107     }
108 
109     /**
110      * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#testParseFieldsOnDirectory()
111      */
112     @Override
113     public void testParseFieldsOnDirectory() throws Exception {
114         // Everything is a File for now.
115     }
116 
117     /**
118      * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#testParseFieldsOnFile()
119      */
120     @Override
121     public void testParseFieldsOnFile() throws Exception {
122         // Note: No time zone.
123         final FTPFile ftpFile = getParser().parseFTPEntry("-C--E-----FTP B QUA1I1      18128       5000000000 Aug 12 13:56 QUADTEST");
124         final Calendar today = Calendar.getInstance();
125         int year = today.get(Calendar.YEAR);
126 
127         assertTrue("Should be a file.", ftpFile.isFile());
128         assertEquals("QUADTEST", ftpFile.getName());
129         assertEquals(5000000000L, ftpFile.getSize());
130         assertEquals("QUA1I1", ftpFile.getUser());
131         assertEquals("18128", ftpFile.getGroup());
132 
133         if (today.get(Calendar.MONTH) < Calendar.AUGUST) {
134             --year;
135         }
136 
137         final Calendar timestamp = ftpFile.getTimestamp();
138         assertEquals(year, timestamp.get(Calendar.YEAR));
139         assertEquals(Calendar.AUGUST, timestamp.get(Calendar.MONTH));
140         assertEquals(12, timestamp.get(Calendar.DAY_OF_MONTH));
141         assertEquals(13, timestamp.get(Calendar.HOUR_OF_DAY));
142         assertEquals(56, timestamp.get(Calendar.MINUTE));
143         assertEquals(0, timestamp.get(Calendar.SECOND));
144         // No time zone -> local.
145         final TimeZone timeZone = TimeZone.getDefault();
146         assertEquals(timeZone, timestamp.getTimeZone());
147 
148         checkPermisions(ftpFile);
149 
150         final Instant instant = ftpFile.getTimestampInstant();
151         final ZonedDateTime zDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of(timeZone.getID()));
152         assertEquals(year, zDateTime.getYear());
153         assertEquals(Month.AUGUST, zDateTime.getMonth());
154         assertEquals(12, zDateTime.getDayOfMonth());
155         assertEquals(13, zDateTime.getHour());
156         assertEquals(56, zDateTime.getMinute());
157         assertEquals(0, zDateTime.getSecond());
158     }
159 
160     @Override
161     public void testRecentPrecision() {
162         testPrecision("-C--E-----FTP B QUA1I1      18128       5000000000 Aug 12 13:56 QUADTEST", CalendarUnit.MINUTE);
163     }
164 }