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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Calendar;
26  
27  import org.apache.commons.net.ftp.FTPClientConfig;
28  import org.apache.commons.net.ftp.FTPFile;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * This is a simple TestCase that tests entry parsing using the new FTPClientConfig mechanism. The normal FTPClient cannot handle the different date formats in
33   * these entries, however using a configurable format, we can handle it easily.
34   *
35   * The original system presenting this issue was an AIX system - see bug #27437 for details.
36   */
37  class FTPConfigEntryParserTest {
38  
39      private final SimpleDateFormat df = new SimpleDateFormat();
40  
41      /**
42       * This is a new format reported on the mailing lists. Parsing this kind of entry necessitated changing the regex in the parser.
43       */
44      @Test
45      void testParseEntryWithSymlink() {
46  
47          final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
48          config.setDefaultDateFormatStr("yyyy-MM-dd HH:mm");
49  
50          final UnixFTPEntryParser parser = new UnixFTPEntryParser();
51          parser.configure(config);
52  
53          final FTPFile f = parser.parseFTPEntry("lrwxrwxrwx   1 neeme neeme    23 2005-03-02 18:06 macros");
54  
55          assertNotNull(f, "Could not parse entry.");
56          assertFalse(f.isDirectory(), "Is not a directory.");
57          assertTrue(f.isSymbolicLink(), "Is a symbolic link");
58  
59          assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION), "Should have user read permission.");
60          assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION), "Should have user write permission.");
61          assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should have user execute permission.");
62          assertTrue(f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION), "Should have group read permission.");
63          assertTrue(f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION), "Should have group write permission.");
64          assertTrue(f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should have group execute permission.");
65          assertTrue(f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION), "Should have world read permission.");
66          assertTrue(f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION), "Should have world write permission.");
67          assertTrue(f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should have world execute permission.");
68  
69          assertEquals(1, f.getHardLinkCount());
70  
71          assertEquals("neeme", f.getUser());
72          assertEquals("neeme", f.getGroup());
73  
74          assertEquals("macros", f.getName());
75          assertEquals(23, f.getSize());
76  
77          final Calendar cal = Calendar.getInstance();
78  
79          cal.set(Calendar.MONTH, Calendar.MARCH);
80          cal.set(Calendar.DAY_OF_MONTH, 2);
81          cal.set(Calendar.HOUR_OF_DAY, 18);
82          cal.set(Calendar.MINUTE, 06);
83          cal.set(Calendar.SECOND, 0);
84          cal.set(Calendar.YEAR, 2005);
85  
86          assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
87  
88      }
89  
90      @Test
91      void testParseFieldsOnAIX() {
92  
93          // Set a date format for this server type
94          final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
95          config.setDefaultDateFormatStr("dd MMM HH:mm");
96  
97          final UnixFTPEntryParser parser = new UnixFTPEntryParser();
98          parser.configure(config);
99  
100         final FTPFile ftpFile = parser.parseFTPEntry("-rw-r-----   1 ravensm  sca          814 02 Mar 16:27 ZMIR2.m");
101 
102         assertNotNull(ftpFile, "Could not parse entry.");
103         assertFalse(ftpFile.isDirectory(), "Is not a directory.");
104 
105         assertTrue(ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION), "Should have user read permission.");
106         assertTrue(ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION), "Should have user write permission.");
107         assertFalse(ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should NOT have user execute permission.");
108         assertTrue(ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION), "Should have group read permission.");
109         assertFalse(ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION), "Should NOT have group write permission.");
110         assertFalse(ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should NOT have group execute permission.");
111         assertFalse(ftpFile.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION), "Should NOT have world read permission.");
112         assertFalse(ftpFile.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION), "Should NOT have world write permission.");
113         assertFalse(ftpFile.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION), "Should NOT have world execute permission.");
114 
115         assertEquals(1, ftpFile.getHardLinkCount());
116 
117         assertEquals("ravensm", ftpFile.getUser());
118         assertEquals("sca", ftpFile.getGroup());
119 
120         assertEquals("ZMIR2.m", ftpFile.getName());
121         assertEquals(814, ftpFile.getSize());
122 
123         final Calendar cal = Calendar.getInstance();
124 
125         cal.set(Calendar.MONTH, Calendar.MARCH);
126         cal.set(Calendar.DAY_OF_MONTH, 2);
127         cal.set(Calendar.HOUR_OF_DAY, 16);
128         cal.set(Calendar.MINUTE, 27);
129         cal.set(Calendar.SECOND, 0);
130 
131         // With no year specified, it defaults to 1970
132         // TODO this is probably a bug - it should default to the current year
133         cal.set(Calendar.YEAR, 1970);
134 
135         assertEquals(df.format(cal.getTime()), df.format(ftpFile.getTimestamp().getTime()));
136     }
137 
138 }