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