001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.net.ftp.parser;
018
019 import java.text.SimpleDateFormat;
020 import java.util.Calendar;
021
022 import junit.framework.TestCase;
023
024 import org.apache.commons.net.ftp.FTPClientConfig;
025 import org.apache.commons.net.ftp.FTPFile;
026
027 /**
028 * This is a simple TestCase that tests entry parsing using the new FTPClientConfig
029 * mechanism. The normal FTPClient cannot handle the different date formats in these
030 * entries, however using a configurable format, we can handle it easily.
031 *
032 * The original system presenting this issue was an AIX system - see bug #27437 for details.
033 *
034 * @version $Id$
035 */
036 public class FTPConfigEntryParserTest extends TestCase {
037
038 private SimpleDateFormat df = new SimpleDateFormat();
039
040 public void testParseFieldsOnAIX() {
041
042 // Set a date format for this server type
043 FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
044 config.setDefaultDateFormatStr("dd MMM HH:mm");
045
046 UnixFTPEntryParser parser = new UnixFTPEntryParser();
047 parser.configure(config);
048
049 FTPFile f = parser.parseFTPEntry("-rw-r----- 1 ravensm sca 814 02 Mar 16:27 ZMIR2.m");
050
051 assertNotNull("Could not parse entry.", f);
052 assertFalse("Is not a directory.", f.isDirectory());
053
054 assertTrue("Should have user read permission.", f.hasPermission(
055 FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
056 assertTrue("Should have user write permission.", f.hasPermission(
057 FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
058 assertFalse("Should NOT have user execute permission.", f
059 .hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
060 assertTrue("Should have group read permission.", f.hasPermission(
061 FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
062 assertFalse("Should NOT have group write permission.", f
063 .hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
064 assertFalse("Should NOT have group execute permission.",
065 f.hasPermission(FTPFile.GROUP_ACCESS,
066 FTPFile.EXECUTE_PERMISSION));
067 assertFalse("Should NOT have world read permission.", f.hasPermission(
068 FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
069 assertFalse("Should NOT have world write permission.", f
070 .hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
071 assertFalse("Should NOT have world execute permission.",
072 f.hasPermission(FTPFile.WORLD_ACCESS,
073 FTPFile.EXECUTE_PERMISSION));
074
075 assertEquals(1, f.getHardLinkCount());
076
077 assertEquals("ravensm", f.getUser());
078 assertEquals("sca", f.getGroup());
079
080 assertEquals("ZMIR2.m", f.getName());
081 assertEquals(814, f.getSize());
082
083 Calendar cal = Calendar.getInstance();
084
085 cal.set(Calendar.MONTH, Calendar.MARCH);
086 cal.set(Calendar.DATE, 2);
087 cal.set(Calendar.HOUR_OF_DAY, 16);
088 cal.set(Calendar.MINUTE, 27);
089 cal.set(Calendar.SECOND, 0);
090
091 // With no year specified, it defaults to 1970
092 // TODO this is probably a bug - it should default to the current year
093 cal.set(Calendar.YEAR, 1970);
094
095 assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp()
096 .getTime()));
097 }
098
099 /**
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 }