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.util.Calendar;
020    
021    import junit.framework.TestSuite;
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: NTFTPEntryParserTest.java 629276 2008-02-19 23:31:25Z rwinston $
028     */
029    public class NTFTPEntryParserTest extends CompositeFTPParseTestFramework
030    {
031    
032        private static final String [][] goodsamples = {
033        {
034                "05-26-95  10:57AM               143712 $LDR$",
035                "05-20-97  03:31PM                  681 .bash_history",
036                "12-05-96  05:03PM       <DIR>          absoft2",
037                "11-14-97  04:21PM                  953 AUDITOR3.INI",
038                "05-22-97  08:08AM                  828 AUTOEXEC.BAK",
039                "01-22-98  01:52PM                  795 AUTOEXEC.BAT",
040                "05-13-97  01:46PM                  828 AUTOEXEC.DOS",
041                "12-03-96  06:38AM                  403 AUTOTOOL.LOG",
042                "12-03-96  06:38AM       <DIR>          123xyz",
043                "01-20-97  03:48PM       <DIR>          bin",
044                "05-26-1995  10:57AM               143712 $LDR$",
045        },
046        {
047                "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
048                "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip",
049                "-rwxr-xr-x   2 500      500           166 Nov  2  2001 73131-testtes1.afp",
050                "-rw-r--r--   1 500      500           166 Nov  9  2001 73131-testtes1.AFP",
051                "drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata",
052            }
053        };
054    
055        private static final String[][] badsamples =
056            {
057                {
058                    "20-05-97  03:31PM                  681 .bash_history",
059                    "drwxr-xr-x   2 root     99           4096 Feb 23 30:01 zzplayer",
060                    "12-05-96  17:03         <DIR>          absoft2",
061                    "05-22-97  08:08                    828 AUTOEXEC.BAK",
062                    "     0           DIR   05-19-97   12:56  local",
063                    "     0           DIR   05-12-97   16:52  Maintenance Desktop",
064                },
065                {
066                    "20-05-97  03:31PM                  681 .bash_history",
067                    "drwxr-xr-x   2 root     99           4096Feb 23 30:01 zzplayer",
068                    "12-05-96  17:03         <DIR>          absoft2",
069                    "05-22-97  08:08                    828 AUTOEXEC.BAK",
070                    "     0           DIR   05-19-97   12:56  local",
071                    "     0           DIR   05-12-97   16:52  Maintenance Desktop",
072                }
073                };
074    
075        private static final String directoryBeginningWithNumber =
076            "12-03-96  06:38AM       <DIR>          123xyz";
077    
078    
079        /**
080         * @see junit.framework.TestCase#TestCase(String)
081         */
082        public NTFTPEntryParserTest (String name)
083        {
084            super(name);
085        }
086    
087        /**
088         * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
089         */
090        @Override
091        protected String[][] getGoodListings()
092        {
093            return goodsamples;
094        }
095    
096        /**
097         * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
098         */
099        @Override
100        protected String[][] getBadListings()
101        {
102            return badsamples;
103        }
104    
105        /**
106         * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
107         */
108        @Override
109        protected FTPFileEntryParser getParser()
110        {
111           return new CompositeFileEntryParser(new FTPFileEntryParser[]
112            {
113                new NTFTPEntryParser(),
114                new UnixFTPEntryParser()
115    
116            });
117        }
118    
119        /**
120         * Method suite.
121         *
122         * @return TestSuite
123         */
124        public static TestSuite suite()
125        {
126            return(new TestSuite(NTFTPEntryParserTest.class));
127        }
128    
129        /**
130         * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
131         */
132        @Override
133        public void testParseFieldsOnDirectory() throws Exception
134        {
135            FTPFile dir = getParser().parseFTPEntry("12-05-96  05:03PM       <DIR>          absoft2");
136            assertNotNull("Could not parse entry.", dir);
137            assertEquals("Thu Dec 05 17:03:00 1996",
138                         df.format(dir.getTimestamp().getTime()));
139            assertTrue("Should have been a directory.",
140                       dir.isDirectory());
141            assertEquals("absoft2", dir.getName());
142            assertEquals(0, dir.getSize());
143    
144            dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123456");
145            assertNotNull("Could not parse entry.", dir);
146            assertTrue("Should have been a directory.",
147                    dir.isDirectory());
148            assertEquals("123456", dir.getName());
149            assertEquals(0, dir.getSize());
150    
151        }
152        
153        public void testParseLeadingDigits() {
154                FTPFile file = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 10 years and under");
155                assertNotNull("Could not parse entry", file);
156                assertEquals("10 years and under", file.getName());
157                assertEquals(5000000000L, file.getSize());
158                
159                FTPFile dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>           10 years and under");
160                assertNotNull("Could not parse entry", dir);
161                assertEquals("10 years and under", dir.getName());
162        }
163    
164        /**
165         * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
166         */
167        @Override
168        public void testParseFieldsOnFile() throws Exception
169        {
170            FTPFile f = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 AUTOEXEC.BAK");
171            assertNotNull("Could not parse entry.", f);
172            assertEquals("Thu May 22 00:08:00 1997",
173                         df.format(f.getTimestamp().getTime()));
174            assertTrue("Should have been a file.",
175                       f.isFile());
176            assertEquals("AUTOEXEC.BAK", f.getName());
177            assertEquals(5000000000L, f.getSize());
178    
179            // test an NT-unix style listing that does NOT have a leading zero
180            // on the hour.
181    
182            f = getParser().parseFTPEntry(
183                    "-rw-rw-r--   1 mqm        mqm          17707 Mar 12  3:33 killmq.sh.log");
184            assertNotNull("Could not parse entry.", f);
185            Calendar cal = Calendar.getInstance();
186            cal.setTime(f.getTimestamp().getTime());
187            assertEquals("hour", 3, cal.get(Calendar.HOUR));
188            assertTrue("Should have been a file.",
189                    f.isFile());
190            assertEquals(17707, f.getSize());
191        }
192    
193    
194        @Override
195        protected void doAdditionalGoodTests(String test, FTPFile f)
196        {
197            if (test.indexOf("<DIR>") >= 0)
198            {
199                        assertEquals("directory.type",
200                                FTPFile.DIRECTORY_TYPE, f.getType());
201            }
202        }
203    
204        /**
205         * test condition reported as bug 20259.
206         * directory with name beginning with a numeric character
207         * was not parsing correctly
208         *
209         * @throws Exception
210         */
211        public void testDirectoryBeginningWithNumber() throws Exception
212        {
213            FTPFile f = getParser().parseFTPEntry(directoryBeginningWithNumber);
214            assertEquals("name", "123xyz", f.getName());
215        }
216        
217        public void testDirectoryBeginningWithNumberFollowedBySpaces() throws Exception
218        {
219            FTPFile f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 xyz");
220            assertEquals("name", "123 xyz", f.getName());
221            f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 abc xyz");
222            assertNotNull(f);
223            assertEquals("name", "123 abc xyz", f.getName());
224        }
225        
226        /**
227         * Test that group names with embedded spaces can be handled correctly
228         *
229         */
230        public void testGroupNameWithSpaces() {
231            FTPFile f = getParser().parseFTPEntry("drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata");
232            assertNotNull(f);
233            assertEquals("maxm", f.getUser());
234            assertEquals("Domain Users", f.getGroup());
235        }
236        
237    }