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