1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.net.ftp.parser;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.Calendar;
20  import java.util.Date;
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$
35   */
36  public class FTPConfigEntryParserTest extends TestCase {
37  	
38  	private 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  		Date refDate = new Date();
86  		
87  		cal.set(Calendar.MONTH, Calendar.MARCH);
88  		cal.set(Calendar.DATE, 2);
89  		cal.set(Calendar.HOUR_OF_DAY, 16);
90  		cal.set(Calendar.MINUTE, 27);
91  		cal.set(Calendar.SECOND, 0);
92  		
93  		// With no year specified, it defaults to 1970
94  		// TODO this is probably a bug - it should default to the current year
95  		cal.set(Calendar.YEAR, 1970);
96  		
97  		assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp()
98  				.getTime()));
99  	}
100 	
101 	/**
102 	 * This is a new format reported on the mailing lists. Parsing this kind of 
103 	 * entry necessitated changing the regex in the parser.
104 	 *
105 	 */
106 	public void testParseEntryWithSymlink() {
107 		
108 		FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
109 		config.setDefaultDateFormatStr("yyyy-MM-dd HH:mm");
110 
111 		UnixFTPEntryParser parser = new UnixFTPEntryParser();
112 		parser.configure(config);
113 
114 		FTPFile f = parser.parseFTPEntry("lrwxrwxrwx   1 neeme neeme    23 2005-03-02 18:06 macros");
115 
116 		assertNotNull("Could not parse entry.", f);
117 		assertFalse("Is not a directory.", f.isDirectory());
118 		assertTrue("Is a symbolic link", f.isSymbolicLink());
119 
120 		assertTrue("Should have user read permission.", f.hasPermission(
121 				FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
122 		assertTrue("Should have user write permission.", f.hasPermission(
123 				FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
124 		assertTrue("Should have user execute permission.", f
125 				.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
126 		assertTrue("Should have group read permission.", f.hasPermission(
127 				FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
128 		assertTrue("Should have group write permission.", f
129 				.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
130 		assertTrue("Should have group execute permission.",
131 				f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
132 		assertTrue("Should have world read permission.", f.hasPermission(
133 				FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
134 		assertTrue("Should have world write permission.", f
135 				.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
136 		assertTrue("Should have world execute permission.",
137 				f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
138 
139 		assertEquals(1, f.getHardLinkCount());
140 
141 		assertEquals("neeme", f.getUser());
142 		assertEquals("neeme", f.getGroup());
143 
144 		assertEquals("macros", f.getName());
145 		assertEquals(23, f.getSize());
146 
147 		Calendar cal = Calendar.getInstance();
148 		
149 		Date refDate = new Date();
150 		
151 		cal.set(Calendar.MONTH, Calendar.MARCH);
152 		cal.set(Calendar.DATE, 2);
153 		cal.set(Calendar.HOUR_OF_DAY, 18);
154 		cal.set(Calendar.MINUTE, 06);
155 		cal.set(Calendar.SECOND, 0);
156 		cal.set(Calendar.YEAR, 2005);
157 			
158 		assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp()
159 				.getTime()));
160 		
161 	}
162 
163 }