001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp.parser;
017:
018: import java.util.Calendar;
019:
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.net.ftp.FTPFile;
023: import org.apache.commons.net.ftp.FTPFileEntryParser;
024:
025: /**
026: * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
027: * @version $Id: UnixFTPEntryParserTest.java 165675 2005-05-02 20:09:55Z rwinston $
028: */
029: public class UnixFTPEntryParserTest extends FTPParseTestFramework {
030:
031: private static final String[] badsamples = {
032: "zrwxr-xr-x 2 root root 4096 Mar 2 15:13 zxbox",
033: "dxrwr-xr-x 2 root root 4096 Aug 24 2001 zxjdbc",
034: "drwxr-xr-x 2 root root 4096 Jam 4 00:03 zziplib",
035: "drwxr-xr-x 2 root 99 4096 Feb 23 30:01 zzplayer",
036: "drwxr-xr-x 2 root root 4096 Aug 36 2001 zztpp",
037: "-rw-r--r-- 1 14 staff 80284 Aug 22 zxJDBC-1.2.3.tar.gz",
038: "-rw-r--r-- 1 14 staff 119:26 Aug 22 2000 zxJDBC-1.2.3.zip",
039: "-rw-r--r-- 1 ftp no group 83853 Jan 22 2001 zxJDBC-1.2.4.tar.gz",
040: "-rw-r--r-- 1ftp nogroup 126552 Jan 22 2001 zxJDBC-1.2.4.zip",
041: "-rw-r--r-- 1 root root 190144 2001-04-27 zxJDBC-2.0.1b1.zip",
042: "-rw-r--r-- 1 root root 111325 Apr -7 18:79 zxJDBC-2.0.1b1.tar.gz" };
043:
044: private static final String[] goodsamples = {
045: "-rw-r--r-- 1 500 500 21 Aug 8 14:14 JB3-TES1.gz",
046: "-rwxr-xr-x 2 root root 4096 Mar 2 15:13 zxbox",
047: "drwxr-xr-x 2 root root 4096 Aug 24 2001 zxjdbc",
048: "drwxr-xr-x 2 root root 4096 Jan 4 00:03 zziplib",
049: "drwxr-xr-x 2 root 99 4096 Feb 23 2001 zzplayer",
050: "drwxr-xr-x 2 root root 4096 Aug 6 2001 zztpp",
051: "drwxr-xr-x 1 usernameftp 512 Jan 29 23:32 prog",
052: "lrw-r--r-- 1 14 14 80284 Aug 22 2000 zxJDBC-1.2.3.tar.gz",
053: "frw-r--r-- 1 14 staff 119926 Aug 22 2000 zxJDBC-1.2.3.zip",
054: "crw-r--r-- 1 ftp nogroup 83853 Jan 22 2001 zxJDBC-1.2.4.tar.gz",
055: "brw-r--r-- 1 ftp nogroup 126552 Jan 22 2001 zxJDBC-1.2.4.zip",
056: "-rw-r--r-- 1 root root 111325 Apr 27 2001 zxJDBC-2.0.1b1.tar.gz",
057: "-rw-r--r-- 1 root root 190144 Apr 27 2001 zxJDBC-2.0.1b1.zip",
058: "-rwxr-xr-x 2 500 500 166 Nov 2 2001 73131-testtes1.afp",
059: "-rw-r--r-- 1 500 500 166 Nov 9 2001 73131-testtes1.AFP",
060: "-rw-r--r-- 1 500 500 166 Nov 12 2001 73131-testtes2.afp",
061: "-rw-r--r-- 1 500 500 166 Nov 12 2001 73131-testtes2.AFP",
062: "-rw-r--r-- 1 500 500 2040000 Aug 5 07:35 testRemoteUPCopyNIX",
063: "-rw-r--r-- 1 500 500 2040000 Aug 5 07:31 testRemoteUPDCopyNIX",
064: "-rw-r--r-- 1 500 500 2040000 Aug 5 07:31 testRemoteUPVCopyNIX",
065: "-rw-r--r-T 1 500 500 0 Mar 25 08:20 testSticky",
066: "-rwxr-xr-t 1 500 500 0 Mar 25 08:21 testStickyExec",
067: "-rwSr-Sr-- 1 500 500 0 Mar 25 08:22 testSuid",
068: "-rwsr-sr-- 1 500 500 0 Mar 25 08:23 testSuidExec",
069: "-rwsr-sr-- 1 500 500 0 Mar 25 0:23 testSuidExec2",
070: "drwxrwx---+ 23 500 500 0 Jan 10 13:09 testACL",
071: "-rw-r--r-- 1 1 3518644 May 25 12:12 std",
072: "lrwxrwxrwx 1 neeme neeme 23 Mar 2 18:06 macros -> ./../../global/macros/."
073:
074: };
075:
076: /**
077: * @see junit.framework.TestCase#TestCase(String)
078: */
079: public UnixFTPEntryParserTest(String name) {
080: super (name);
081: }
082:
083: /**
084: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getBadListing()
085: */
086: protected String[] getBadListing() {
087: return (badsamples);
088: }
089:
090: /**
091: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getGoodListing()
092: */
093: protected String[] getGoodListing() {
094: return (goodsamples);
095: }
096:
097: /**
098: */
099: public void testNumericDateFormat() {
100: String testNumericDF = "-rw-r----- 1 neeme neeme 346 2005-04-08 11:22 services.vsp";
101: String testNumericDF2 = "lrwxrwxrwx 1 neeme neeme 23 2005-03-02 18:06 macros -> ./../../global/macros/.";
102:
103: UnixFTPEntryParser parser = new UnixFTPEntryParser(
104: UnixFTPEntryParser.NUMERIC_DATE_CONFIG);
105:
106: FTPFile f = parser.parseFTPEntry(testNumericDF);
107: assertNotNull("Failed to parse " + testNumericDF, f);
108:
109: Calendar cal = Calendar.getInstance();
110: cal.clear();
111: cal.set(Calendar.YEAR, 2005);
112: cal.set(Calendar.MONTH, Calendar.APRIL);
113:
114: cal.set(Calendar.DATE, 8);
115: cal.set(Calendar.HOUR_OF_DAY, 11);
116: cal.set(Calendar.MINUTE, 22);
117: assertEquals(cal.getTime(), f.getTimestamp().getTime());
118:
119: FTPFile f2 = parser.parseFTPEntry(testNumericDF2);
120: assertNotNull("Failed to parse " + testNumericDF2, f2);
121: assertEquals("symbolic link", "./../../global/macros/.", f2
122: .getLink());
123:
124: }
125:
126: /**
127: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
128: */
129: protected FTPFileEntryParser getParser() {
130: return (new UnixFTPEntryParser());
131: }
132:
133: /**
134: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
135: */
136: public void testParseFieldsOnDirectory() throws Exception {
137: FTPFile f = getParser()
138: .parseFTPEntry(
139: "drwxr-xr-x 2 user group 4096 Mar 2 15:13 zxbox");
140: assertNotNull("Could not parse entry.", f);
141: assertTrue("Should have been a directory.", f.isDirectory());
142: checkPermissions(f);
143: assertEquals(2, f.getHardLinkCount());
144: assertEquals("user", f.getUser());
145: assertEquals("group", f.getGroup());
146: assertEquals("zxbox", f.getName());
147: assertEquals(4096, f.getSize());
148:
149: Calendar cal = Calendar.getInstance();
150: cal.set(Calendar.MONTH, Calendar.MARCH);
151:
152: cal.set(Calendar.DATE, 1);
153: cal.set(Calendar.HOUR_OF_DAY, 0);
154: cal.set(Calendar.MINUTE, 0);
155: cal.set(Calendar.SECOND, 0);
156: if (f.getTimestamp().getTime().before(cal.getTime())) {
157: cal.add(Calendar.YEAR, -1);
158: }
159: cal.set(Calendar.DATE, 2);
160: cal.set(Calendar.HOUR_OF_DAY, 15);
161: cal.set(Calendar.MINUTE, 13);
162:
163: assertEquals(df.format(cal.getTime()), df.format(f
164: .getTimestamp().getTime()));
165: }
166:
167: /**
168: * Method checkPermissions.
169: * Verify that the persmissions were properly set.
170: * @param f
171: */
172: private void checkPermissions(FTPFile f) {
173: assertTrue("Should have user read permission.", f
174: .hasPermission(FTPFile.USER_ACCESS,
175: FTPFile.READ_PERMISSION));
176: assertTrue("Should have user write permission.", f
177: .hasPermission(FTPFile.USER_ACCESS,
178: FTPFile.WRITE_PERMISSION));
179: assertTrue("Should have user execute permission.", f
180: .hasPermission(FTPFile.USER_ACCESS,
181: FTPFile.EXECUTE_PERMISSION));
182: assertTrue("Should have group read permission.", f
183: .hasPermission(FTPFile.GROUP_ACCESS,
184: FTPFile.READ_PERMISSION));
185: assertTrue("Should NOT have group write permission.", !f
186: .hasPermission(FTPFile.GROUP_ACCESS,
187: FTPFile.WRITE_PERMISSION));
188: assertTrue("Should have group execute permission.", f
189: .hasPermission(FTPFile.GROUP_ACCESS,
190: FTPFile.EXECUTE_PERMISSION));
191: assertTrue("Should have world read permission.", f
192: .hasPermission(FTPFile.WORLD_ACCESS,
193: FTPFile.READ_PERMISSION));
194: assertTrue("Should NOT have world write permission.", !f
195: .hasPermission(FTPFile.WORLD_ACCESS,
196: FTPFile.WRITE_PERMISSION));
197: assertTrue("Should have world execute permission.", f
198: .hasPermission(FTPFile.WORLD_ACCESS,
199: FTPFile.EXECUTE_PERMISSION));
200: }
201:
202: /**
203: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
204: */
205: public void testParseFieldsOnFile() throws Exception {
206: FTPFile f = getParser()
207: .parseFTPEntry(
208: "-rwxr-xr-x 2 user group 5000000000 Mar 2 15:13 zxbox");
209: assertNotNull("Could not parse entry.", f);
210: assertTrue("Should have been a file.", f.isFile());
211: checkPermissions(f);
212: assertEquals(2, f.getHardLinkCount());
213: assertEquals("user", f.getUser());
214: assertEquals("group", f.getGroup());
215: assertEquals("zxbox", f.getName());
216: assertEquals(5000000000L, f.getSize());
217:
218: Calendar cal = Calendar.getInstance();
219: cal.set(Calendar.MONTH, Calendar.MARCH);
220:
221: cal.set(Calendar.DATE, 1);
222: cal.set(Calendar.HOUR_OF_DAY, 0);
223: cal.set(Calendar.MINUTE, 0);
224: cal.set(Calendar.SECOND, 0);
225: if (f.getTimestamp().getTime().before(cal.getTime())) {
226: cal.add(Calendar.YEAR, -1);
227: }
228: cal.set(Calendar.DATE, 2);
229: cal.set(Calendar.HOUR_OF_DAY, 15);
230: cal.set(Calendar.MINUTE, 13);
231: assertEquals(df.format(cal.getTime()), df.format(f
232: .getTimestamp().getTime()));
233: }
234:
235: /**
236: * Method suite.
237: * @return TestSuite
238: */
239: public static TestSuite suite() {
240: return (new TestSuite(UnixFTPEntryParserTest.class));
241: }
242:
243: /*
244: * @param test
245: * @param f
246: */
247: protected void doAdditionalGoodTests(String test, FTPFile f) {
248: String link = f.getLink();
249: if (null != link) {
250: int linklen = link.length();
251: if (linklen > 0) {
252: assertEquals(link, test.substring(test.length()
253: - linklen));
254: assertEquals(f.getType(), FTPFile.SYMBOLIC_LINK_TYPE);
255: }
256: }
257: int type = f.getType();
258: switch (test.charAt(0)) {
259: case 'd':
260: assertEquals("Type of " + test, type,
261: FTPFile.DIRECTORY_TYPE);
262: break;
263: case 'l':
264: assertEquals("Type of " + test, type,
265: FTPFile.SYMBOLIC_LINK_TYPE);
266: break;
267: case 'b':
268: case 'c':
269: assertEquals(0, f.getHardLinkCount());
270: case 'f':
271: case '-':
272: assertEquals("Type of " + test, type, FTPFile.FILE_TYPE);
273: break;
274: default:
275: assertEquals("Type of " + test, type, FTPFile.UNKNOWN_TYPE);
276: }
277:
278: for (int access = FTPFile.USER_ACCESS; access <= FTPFile.WORLD_ACCESS; access++) {
279: for (int perm = FTPFile.READ_PERMISSION; perm <= FTPFile.EXECUTE_PERMISSION; perm++) {
280: int pos = 3 * access + perm + 1;
281: char permchar = test.charAt(pos);
282: assertEquals("Permission " + test.substring(1, 10), f
283: .hasPermission(access, perm), permchar != '-'
284: && !Character.isUpperCase(permchar));
285: }
286: }
287:
288: }
289: }
|