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