Skip to content
Snippets Groups Projects
Commit 9464e29d authored by akwizgran's avatar akwizgran
Browse files

Unit tests for the reader portion of RemovableDrivePlugin.

parent f6df3337
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,8 @@ abstract class FilePlugin implements BatchTransportPlugin { ...@@ -28,7 +28,8 @@ abstract class FilePlugin implements BatchTransportPlugin {
protected Map<ContactId, Map<String, String>> remoteProperties = null; protected Map<ContactId, Map<String, String>> remoteProperties = null;
protected Map<String, String> config = null; protected Map<String, String> config = null;
protected BatchTransportCallback callback = null; protected BatchTransportCallback callback = null;
private boolean started = false;
private volatile boolean started = false;
protected abstract File chooseOutputDirectory(); protected abstract File chooseOutputDirectory();
protected abstract void writerFinished(File f); protected abstract void writerFinished(File f);
...@@ -117,6 +118,7 @@ abstract class FilePlugin implements BatchTransportPlugin { ...@@ -117,6 +118,7 @@ abstract class FilePlugin implements BatchTransportPlugin {
} }
protected void createReaderFromFile(File f) { protected void createReaderFromFile(File f) {
if(!started) throw new IllegalStateException();
if(!isPossibleConnectionFilename(f.getName())) return; if(!isPossibleConnectionFilename(f.getName())) return;
if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return; if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return;
try { try {
...@@ -128,6 +130,11 @@ abstract class FilePlugin implements BatchTransportPlugin { ...@@ -128,6 +130,11 @@ abstract class FilePlugin implements BatchTransportPlugin {
if(read == -1) break; if(read == -1) break;
offset += read; offset += read;
} }
if(offset < iv.length) {
// The file was truncated
in.close();
return;
}
ContactId c = recogniser.acceptConnection(iv); ContactId c = recogniser.acceptConnection(iv);
if(c == null) { if(c == null) {
// Nobody there // Nobody there
......
...@@ -11,6 +11,6 @@ interface RemovableDriveMonitor { ...@@ -11,6 +11,6 @@ interface RemovableDriveMonitor {
interface Callback { interface Callback {
void driveInserted(File f); void driveInserted(File root);
} }
} }
package net.sf.briar.plugins.file; package net.sf.briar.plugins.file;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
...@@ -10,7 +11,9 @@ import junit.framework.TestCase; ...@@ -10,7 +11,9 @@ import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.transport.ConnectionRecogniser; import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.TransportConstants;
import net.sf.briar.api.transport.batch.BatchTransportCallback; import net.sf.briar.api.transport.batch.BatchTransportCallback;
import net.sf.briar.api.transport.batch.BatchTransportReader;
import net.sf.briar.api.transport.batch.BatchTransportWriter; import net.sf.briar.api.transport.batch.BatchTransportWriter;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback; import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
...@@ -39,6 +42,7 @@ public class RemovableDrivePluginTest extends TestCase { ...@@ -39,6 +42,7 @@ public class RemovableDrivePluginTest extends TestCase {
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class); context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor); finder, monitor);
...@@ -292,6 +296,154 @@ public class RemovableDrivePluginTest extends TestCase { ...@@ -292,6 +296,154 @@ public class RemovableDrivePluginTest extends TestCase {
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@Test
public void testEmptyDriveIsIgnored() throws Exception {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
plugin.driveInserted(testDir);
context.assertIsSatisfied();
}
@Test
public void testFilenames() {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefgh_dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefgh.rat"));
assertTrue(plugin.isPossibleConnectionFilename("abcdefgh.dat"));
assertTrue(plugin.isPossibleConnectionFilename("ABCDEFGH.DAT"));
context.assertIsSatisfied();
}
@Test
public void testSmallFileIsIgnored() throws Exception {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);
out.write(new byte[TransportConstants.MIN_CONNECTION_LENGTH - 1]);
out.flush();
out.close();
assertEquals(TransportConstants.MIN_CONNECTION_LENGTH - 1, f.length());
plugin.driveInserted(testDir);
context.assertIsSatisfied();
}
@Test
public void testIvIsChecked() throws Exception {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(recogniser).acceptConnection(with(any(byte[].class)));
will(returnValue(null));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);
out.write(new byte[TransportConstants.MIN_CONNECTION_LENGTH]);
out.flush();
out.close();
assertEquals(TransportConstants.MIN_CONNECTION_LENGTH, f.length());
plugin.driveInserted(testDir);
context.assertIsSatisfied();
}
@Test
public void testReaderIsCreated() throws Exception {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(recogniser).acceptConnection(with(any(byte[].class)));
will(returnValue(contactId));
oneOf(callback).readerCreated(with(contactId),
with(any(byte[].class)),
with(any(BatchTransportReader.class)));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);
out.write(new byte[TransportConstants.MIN_CONNECTION_LENGTH]);
out.flush();
out.close();
assertEquals(TransportConstants.MIN_CONNECTION_LENGTH, f.length());
plugin.driveInserted(testDir);
context.assertIsSatisfied();
}
@After @After
public void tearDown() { public void tearDown() {
TestUtils.deleteTestDirectory(testDir); TestUtils.deleteTestDirectory(testDir);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment