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.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       */
41      public void testParseEntryWithSymlink() {
42  
43          final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
44          config.setDefaultDateFormatStr("yyyy-MM-dd HH:mm");
45  
46          final UnixFTPEntryParser parser = new UnixFTPEntryParser();
47          parser.configure(config);
48  
49          final FTPFile f = parser.parseFTPEntry("lrwxrwxrwx   1 neeme neeme    23 2005-03-02 18:06 macros");
50  
51          assertNotNull("Could not parse entry.", f);
52          assertFalse("Is not a directory.", f.isDirectory());
53          assertTrue("Is a symbolic link", f.isSymbolicLink());
54  
55          assertTrue("Should have user read permission.", f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
56          assertTrue("Should have user write permission.", f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
57          assertTrue("Should have user execute permission.", f.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
58          assertTrue("Should have group read permission.", f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
59          assertTrue("Should have group write permission.", f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
60          assertTrue("Should have group execute permission.", f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
61          assertTrue("Should have world read permission.", f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
62          assertTrue("Should have world write permission.", f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
63          assertTrue("Should have world execute permission.", f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
64  
65          assertEquals(1, f.getHardLinkCount());
66  
67          assertEquals("neeme", f.getUser());
68          assertEquals("neeme", f.getGroup());
69  
70          assertEquals("macros", f.getName());
71          assertEquals(23, f.getSize());
72  
73          final Calendar cal = Calendar.getInstance();
74  
75          cal.set(Calendar.MONTH, Calendar.MARCH);
76          cal.set(Calendar.DAY_OF_MONTH, 2);
77          cal.set(Calendar.HOUR_OF_DAY, 18);
78          cal.set(Calendar.MINUTE, 06);
79          cal.set(Calendar.SECOND, 0);
80          cal.set(Calendar.YEAR, 2005);
81  
82          assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
83  
84      }
85  
86      public void testParseFieldsOnAIX() {
87  
88          // Set a date format for this server type
89          final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
90          config.setDefaultDateFormatStr("dd MMM HH:mm");
91  
92          final UnixFTPEntryParser parser = new UnixFTPEntryParser();
93          parser.configure(config);
94  
95          final FTPFile ftpFile = parser.parseFTPEntry("-rw-r-----   1 ravensm  sca          814 02 Mar 16:27 ZMIR2.m");
96  
97          assertNotNull("Could not parse entry.", ftpFile);
98          assertFalse("Is not a directory.", ftpFile.isDirectory());
99  
100         assertTrue("Should have user read permission.", ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
101         assertTrue("Should have user write permission.", ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
102         assertFalse("Should NOT have user execute permission.", ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
103         assertTrue("Should have group read permission.", ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
104         assertFalse("Should NOT have group write permission.", ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
105         assertFalse("Should NOT have group execute permission.", ftpFile.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
106         assertFalse("Should NOT have world read permission.", ftpFile.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
107         assertFalse("Should NOT have world write permission.", ftpFile.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
108         assertFalse("Should NOT have world execute permission.", ftpFile.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
109 
110         assertEquals(1, ftpFile.getHardLinkCount());
111 
112         assertEquals("ravensm", ftpFile.getUser());
113         assertEquals("sca", ftpFile.getGroup());
114 
115         assertEquals("ZMIR2.m", ftpFile.getName());
116         assertEquals(814, ftpFile.getSize());
117 
118         final Calendar cal = Calendar.getInstance();
119 
120         cal.set(Calendar.MONTH, Calendar.MARCH);
121         cal.set(Calendar.DAY_OF_MONTH, 2);
122         cal.set(Calendar.HOUR_OF_DAY, 16);
123         cal.set(Calendar.MINUTE, 27);
124         cal.set(Calendar.SECOND, 0);
125 
126         // With no year specified, it defaults to 1970
127         // TODO this is probably a bug - it should default to the current year
128         cal.set(Calendar.YEAR, 1970);
129 
130         assertEquals(df.format(cal.getTime()), df.format(ftpFile.getTimestamp().getTime()));
131     }
132 
133 }