Enhancing NTFS Stream tests

This commit is contained in:
Joakim Erdfelt 2014-07-31 10:09:53 -07:00
parent 971e1f0aaa
commit 6048343fee
3 changed files with 148 additions and 36 deletions

View File

@ -154,7 +154,7 @@ public class FileResource extends Resource
URI alias=new File(can).toURI();
// Have to encode the path as File.toURI does not!
String uri="file://"+URIUtil.encodePath(alias.getPath());
return new URI(uri);
return new URI(uri);
}
}
catch(Exception e)

View File

@ -52,7 +52,31 @@ public class PathResource extends Resource
private final static LinkOption NO_FOLLOW_OPTIONS[] = new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
private final Path path;
private final URI alias;
private final URI uri;
private static final URI toAliasUri(final Path path)
{
Path abs = path;
if (!abs.isAbsolute())
{
abs = path.toAbsolutePath();
}
URI providedUri = abs.toUri();
try
{
URI realUri = abs.toRealPath().toUri();
if (!providedUri.equals(realUri))
{
return realUri;
}
}
catch (IOException e)
{
LOG.ignore(e);
}
return null;
}
public PathResource(File file)
{
@ -61,8 +85,9 @@ public class PathResource extends Resource
public PathResource(Path path)
{
this.path = path;
this.path = path.toAbsolutePath();
this.uri = this.path.toUri();
this.alias = toAliasUri(path);
}
public PathResource(URI uri) throws IOException
@ -96,8 +121,9 @@ public class PathResource extends Resource
throw new IOException("Unable to build Path from: " + uri,e);
}
this.path = path;
this.path = path.toAbsolutePath();
this.uri = path.toUri();
this.alias = toAliasUri(path);
}
public PathResource(URL url) throws IOException, URISyntaxException
@ -109,13 +135,13 @@ public class PathResource extends Resource
public Resource addPath(final String subpath) throws IOException, MalformedURLException
{
String cpath = URIUtil.canonicalPath(subpath);
if ((cpath == null)||(cpath.length()==0))
if ((cpath == null) || (cpath.length() == 0))
throw new MalformedURLException();
if ("/".equals(cpath))
return this;
// subpaths are always under PathResource
// compensate for input subpaths like "/subdir"
// where default java.nio.file behavior would be
@ -279,22 +305,7 @@ public class PathResource extends Resource
@Override
public URI getAlias()
{
if (Files.isSymbolicLink(path))
{
try
{
return path.toRealPath().toUri();
}
catch (IOException e)
{
LOG.debug(e);
return null;
}
}
else
{
return null;
}
return this.alias;
}
@Override

View File

@ -80,7 +80,6 @@ public class FileSystemResourceTest
_class=test;
}
public Resource newResource(URI uri) throws Exception
{
try
@ -147,7 +146,7 @@ public class FileSystemResourceTest
@Override
public void describeMismatch(Object item, Description description)
{
description.appendText("was").appendValue(((Resource)item).getAlias());
description.appendText("was ").appendValue(((Resource)item).getAlias());
}
};
}
@ -159,8 +158,16 @@ public class FileSystemResourceTest
@Override
public boolean matches(Object item)
{
final Resource alias = (Resource)item;
return alias.getAlias().equals(resource.getURI());
final Resource ritem = (Resource)item;
final URI alias = ritem.getAlias();
if (alias == null)
{
return ritem == null;
}
else
{
return alias.equals(resource.getURI());
}
}
@Override
@ -172,7 +179,7 @@ public class FileSystemResourceTest
@Override
public void describeMismatch(Object item, Description description)
{
description.appendText("was").appendValue(((Resource)item).getAlias());
description.appendText("was ").appendValue(((Resource)item).getAlias());
}
};
}
@ -544,28 +551,122 @@ public class FileSystemResourceTest
}
}
/**
* NTFS Alternative Data / File Streams.
* <p>
* See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404(v=vs.85).aspx
*/
@Test
public void testCaseNTParamAlias() throws Exception
public void testNTFSFileStreamAlias() throws Exception
{
File dir = testdir.getDir();
Path path = new File(dir, "file").toPath();
Path path = new File(dir, "testfile").toPath();
Files.createFile(path);
try (Resource base = newResource(testdir.getDir()))
{
Resource resource = base.addPath("file");
Resource resource = base.addPath("testfile");
assertThat("resource.alias", resource, hasNoAlias());
assertThat("resource.uri.alias", newResource(resource.getURI()), hasNoAlias());
assertThat("resource.file.alias", newResource(resource.getFile()), hasNoAlias());
Resource alias = base.addPath("file::$DATA");
if (alias.exists())
try
{
// If it exists, it must be an alias
assertThat("resource.alias", alias, isAliasFor(resource));
assertThat("resource.uri.alias", newResource(alias.getURI()), isAliasFor(resource));
assertThat("resource.file.alias", newResource(alias.getFile()), isAliasFor(resource));
// Attempt to reference same file, but via NTFS simple stream
Resource alias = base.addPath("testfile:stream");
if (alias.exists())
{
// If it exists, it must be an alias
assertThat("resource.alias",alias,isAliasFor(resource));
assertThat("resource.uri.alias",newResource(alias.getURI()),isAliasFor(resource));
assertThat("resource.file.alias",newResource(alias.getFile()),isAliasFor(resource));
}
}
catch (InvalidPathException e)
{
// NTFS filesystem streams are unsupported on some platforms.
assumeNoException(e);
}
}
}
/**
* NTFS Alternative Data / File Streams.
* <p>
* See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404(v=vs.85).aspx
*/
@Test
public void testNTFSFileDataStreamAlias() throws Exception
{
File dir = testdir.getDir();
Path path = new File(dir, "testfile").toPath();
Files.createFile(path);
try (Resource base = newResource(testdir.getDir()))
{
Resource resource = base.addPath("testfile");
assertThat("resource.alias", resource, hasNoAlias());
assertThat("resource.uri.alias", newResource(resource.getURI()), hasNoAlias());
assertThat("resource.file.alias", newResource(resource.getFile()), hasNoAlias());
try
{
// Attempt to reference same file, but via NTFS DATA stream
Resource alias = base.addPath("testfile::$DATA");
if (alias.exists())
{
// If it exists, it must be an alias
assertThat("resource.alias",alias,isAliasFor(resource));
assertThat("resource.uri.alias",newResource(alias.getURI()),isAliasFor(resource));
assertThat("resource.file.alias",newResource(alias.getFile()),isAliasFor(resource));
}
}
catch (InvalidPathException e)
{
// NTFS filesystem streams are unsupported on some platforms.
assumeNoException(e);
}
}
}
/**
* NTFS Alternative Data / File Streams.
* <p>
* See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404(v=vs.85).aspx
*/
@Test
public void testNTFSFileEncodedDataStreamAlias() throws Exception
{
File dir = testdir.getDir();
Path path = new File(dir, "testfile").toPath();
Files.createFile(path);
try (Resource base = newResource(testdir.getDir()))
{
Resource resource = base.addPath("testfile");
assertThat("resource.alias", resource, hasNoAlias());
assertThat("resource.uri.alias", newResource(resource.getURI()), hasNoAlias());
assertThat("resource.file.alias", newResource(resource.getFile()), hasNoAlias());
try
{
// Attempt to reference same file, but via NTFS DATA stream
Resource alias = base.addPath("testfile::%24DATA");
if (alias.exists())
{
// If it exists, it must be an alias
assertThat("resource.alias",alias,isAliasFor(resource));
assertThat("resource.uri.alias",newResource(alias.getURI()),isAliasFor(resource));
assertThat("resource.file.alias",newResource(alias.getFile()),isAliasFor(resource));
}
}
catch (InvalidPathException e)
{
// NTFS filesystem streams are unsupported on some platforms.
assumeNoException(e);
}
}
}