Skip to content

Commit

Permalink
Checking whether CIFS parent file is a valid path (Fixes #153)
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Feb 5, 2015
1 parent 4d8df71 commit 2fc4d51
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/com/xebialabs/overthere/cifs/CifsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public String getName() {
@Override
public OverthereFile getParentFile() {
try {
return new CifsFile(getConnection(), new SmbFile(smbFile.getParent(), connection.authentication));
String parent = smbFile.getParent();
if (connection.encoder.isValidUncPath(parent)) {
return new CifsFile(getConnection(), new SmbFile(parent, connection.authentication));
}
return null;
} catch (MalformedURLException exc) {
return null;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/xebialabs/overthere/cifs/PathEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,13 @@ final String fromUncPath(String uncPath) {
return pathMapper.toLocalPath(matcher.group(1));
}

/**
* Check whether the UNC path is valid.
* @param uncPath the UNC path to check.
* @return true if it is a valid UNC path.
*/
final boolean isValidUncPath(String uncPath) {
return UNC_PATH_PATTERN.matcher(uncPath).matches();
}

}
44 changes: 44 additions & 0 deletions src/test/java/com/xebialabs/overthere/cifs/CifsFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.xebialabs.overthere.cifs;

import com.xebialabs.overthere.ConnectionOptions;
import com.xebialabs.overthere.OverthereFile;
import com.xebialabs.overthere.cifs.winrm.CifsWinRmConnection;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static com.xebialabs.overthere.ConnectionOptions.*;
import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS;
import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.*;
import static com.xebialabs.overthere.cifs.CifsConnectionType.WINRM_INTERNAL;
import static com.xebialabs.overthere.util.DefaultAddressPortMapper.INSTANCE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.testng.Assert.*;

public class CifsFileTest {

private ConnectionOptions options;

@BeforeMethod
public void setupOptions() {
options = new ConnectionOptions();
options.set(OPERATING_SYSTEM, WINDOWS);
options.set(CONNECTION_TYPE, WINRM_INTERNAL);
options.set(PASSWORD, "foobar");
options.set(PORT, PORT_DEFAULT_WINRM_HTTP);
options.set(CIFS_PORT, CIFS_PORT_DEFAULT);
options.set(ADDRESS, "localhost");
}


@Test
public void shouldReturnNullForParentFileOfRoot() {
options.set(USERNAME, "[email protected]");
CifsWinRmConnection cifsWinRmConnection = new CifsWinRmConnection(CIFS_PROTOCOL, options, INSTANCE);
OverthereFile file = cifsWinRmConnection.getFile("C:\\");
assertThat(file.getParentFile(), nullValue());
}


}

0 comments on commit 2fc4d51

Please sign in to comment.