Author: sebb Date: Wed Dec 10 01:57:43 2014 New Revision: 1644305 URL: http://svn.apache.org/r1644305 Log: NET-563 MLSxEntryParser needs test cases; parsing is too lax
Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSxEntryParserTest.java Modified: commons/proper/net/trunk/src/changes/changes.xml commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java Modified: commons/proper/net/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1644305&r1=1644304&r2=1644305&view=diff ============================================================================== --- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original) +++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Wed Dec 10 01:57:43 2014 @@ -68,6 +68,9 @@ This is mainly a bug-fix release. See fu IMAPExportMbox (example app) allows IMAP folders to be exported into an mbox file. This is the inverse of the IMAPImportMbox example added previously "> + <action issue="NET-563" type="fix" dev="sebb"> + MLSxEntryParser needs test cases; parsing is too lax + </action> <action issue="NET-561" type="fix" dev="sebb"> FTPFile.toFormattedString prints user and group in wrong order </action> Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java?rev=1644305&r1=1644304&r2=1644305&view=diff ============================================================================== --- commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java (original) +++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java Wed Dec 10 01:57:43 2014 @@ -93,25 +93,43 @@ public class MLSxEntryParser extends FTP // @Override public FTPFile parseFTPEntry(String entry) { + if (entry.startsWith(" ")) {// leading space means no facts are present + if (entry.length() > 1) { // is there a path name? + FTPFile file = new FTPFile(); + file.setRawListing(entry); + file.setName(entry.substring(1)); + return file; + } else { + return null; // Invalid - no pathname + } + + } String parts[] = entry.split(" ",2); // Path may contain space - if (parts.length != 2) { + if (parts.length != 2 || parts[1].length() == 0) { + return null; // no space found or no file name + } + final String factList = parts[0]; + if (!factList.endsWith(";")) { return null; } FTPFile file = new FTPFile(); file.setRawListing(entry); file.setName(parts[1]); - String[] facts = parts[0].split(";"); + String[] facts = factList.split(";"); boolean hasUnixMode = parts[0].toLowerCase(Locale.ENGLISH).contains("unix.mode="); for(String fact : facts) { - String []factparts = fact.split("="); + String []factparts = fact.split("=", -1); // Don't drop empty values // Sample missing permission // drwx------ 2 mirror mirror 4096 Mar 13 2010 subversion // modify=20100313224553;perm=;type=dir;unique=811U282598;UNIX.group=500;UNIX.mode=0700;UNIX.owner=500; subversion if (factparts.length != 2) { - continue; // nothing to do here + return null; // invalid - there was no "=" sign } String factname = factparts[0].toLowerCase(Locale.ENGLISH); String factvalue = factparts[1]; + if (factvalue.length() == 0) { + continue; // nothing to see here + } String valueLowerCase = factvalue.toLowerCase(Locale.ENGLISH); if ("size".equals(factname)) { file.setSize(Long.parseLong(factvalue)); @@ -132,10 +150,10 @@ public class MLSxEntryParser extends FTP GregorianCalendar gc = new GregorianCalendar(GMT); try { gc.setTime(sdf.parse(factvalue)); + file.setTimestamp(gc); } catch (ParseException e) { // TODO ?? } - file.setTimestamp(gc); } else if ("type".equals(factname)) { Integer intType = TYPE_TO_INT.get(valueLowerCase); Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSxEntryParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSxEntryParserTest.java?rev=1644305&view=auto ============================================================================== --- commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSxEntryParserTest.java (added) +++ commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSxEntryParserTest.java Wed Dec 10 01:57:43 2014 @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.net.ftp.parser; + +import org.apache.commons.net.ftp.FTPFile; +import org.apache.commons.net.ftp.FTPFileEntryParser; + +/** + * @author <a href="mailto:sco...@apache.org">Steve Cohen</a> + * @version $Id: UnixFTPEntryParserTest.java 1643407 2014-12-05 19:32:00Z sebb $ + */ +public class MLSxEntryParserTest extends FTPParseTestFramework { + + private static final String[] badsamples = { + "Type=cdir;Modify=20141022065101;UNIX.mode=0775;/no/space", // no space between facts and name + "Type=cdir;Modify=20141022065103;UNIX.mode=0775;", // no name or space + "/no/leading/space", + "", //empty + "Type=cdir;Modify=20141022065102;UNIX.mode=0775; ", // no name + "Type=dir;Size; missing =size", + "Type=dir missing-semicolon", + "Type= missing value and semicolon", + " ", // no path + }; + + private static final String[] goodsamples = { + "Type=cdir;Modify=20141022065102;UNIX.mode=0775; /commons/net", + "Type=pdir;Modify=20141205180002;UNIX.mode=0775; /commons", + "Type=file;Size=431;Modify=20130303210732;UNIX.mode=0664; HEADER.html", + "Type=file;Size=1880;Modify=20130611172748;UNIX.mode=0664; README.html", + "Type=file;Size=2364;Modify=20130611170131;UNIX.mode=0664; RELEASE-NOTES.txt", + "Type=dir;Modify=20141022065102;UNIX.mode=0775; binaries", + "Type=dir;Modify=20141022065102.999;UNIX.mode=0775; source", + " /no/facts", // no facts + "Type=; /empty/fact", + "Size=; /empty/size", + " Type=cdir;Modify=20141022065102;UNIX.mode=0775; /leading/space", // leading space before facts => it's a file name! + " ", // pathname of space + }; + + public MLSxEntryParserTest(String name) { + super(name); + } + + @Override + protected String[] getBadListing() { + return (badsamples); + } + + @Override + protected String[] getGoodListing() { + return (goodsamples); + } + + + @Override + protected FTPFileEntryParser getParser() { + return (MLSxEntryParser.getInstance()); + } + + /** + * Check if FTPFile entry parsing failed; i.e. if entry is null. + * We override parent check, as a null timestamp is not acceptable + * for these tests. + * + * @param f FTPFile entry - may be null + * @return null if f is null + */ + @Override + protected FTPFile nullFileOrNullDate(FTPFile f) { + return f; + } + +// @Override + public void testParseFieldsOnFile() throws Exception { + } + +// @Override + public void testParseFieldsOnDirectory() throws Exception { + } +}