package com.limegroup.gnutella.malware;
import java.io.File;
import junit.framework.Test;
import org.limewire.core.settings.FilterSettings;
import org.limewire.gnutella.tests.LimeTestCase;
import org.limewire.util.TestUtils;
import com.limegroup.gnutella.library.FileManagerTestUtils;
public class FileExtensionCheckerTest extends LimeTestCase {
// The ASF test file is in the public domain:
// http://www.archive.org/details/DovKaplanKolNidreKolNidrewma
private File asf =
TestUtils.getResourceFile("com/limegroup/gnutella/resources/Kol_Nidre.wma");
// The EXE test file is MIT-licensed:
// http://www.chiark.greenend.org.uk/~sgtatham/putty/licence.html
private File exe =
TestUtils.getResourceFile("com/limegroup/gnutella/resources/putty.exe");
// The TXT test file start with the letters MZ, which match the first two
// bytes of EXE files
private File txt =
TestUtils.getResourceFile("com/limegroup/gnutella/resources/MZoooooo.txt");
private FileExtensionChecker checker;
public FileExtensionCheckerTest(String name) {
super(name);
}
public static Test suite() {
return buildTestSuite(FileExtensionCheckerTest.class);
}
@Override
protected void setUp() throws Exception {
checker = new FileExtensionChecker();
}
public void testAllowsAsfNamedAsf() throws Exception {
File f = FileManagerTestUtils.createPartialCopy(asf, ".asf", _scratchDir, 1024);
assertFalse(checker.isDangerous(f));
}
public void testDoesNotAllowAsfNamedFoo() throws Exception {
File f = FileManagerTestUtils.createPartialCopy(asf, ".foo", _scratchDir, 1024);
assertTrue(checker.isDangerous(f));
}
public void testDoesNotAllowFullMagic() throws Exception {
File f = FileManagerTestUtils.createPartialCopy(asf, ".foo", _scratchDir, 16);
assertTrue(checker.isDangerous(f));
}
public void testAllowsPartialMagic() throws Exception {
File f = FileManagerTestUtils.createPartialCopy(asf, ".foo", _scratchDir, 15);
assertFalse(checker.isDangerous(f));
}
public void testAllowsExeNamedScr() throws Exception {
File f = FileManagerTestUtils.createPartialCopy(exe, ".scr", _scratchDir, 1024);
assertFalse(checker.isDangerous(f));
}
public void testDoesNotAllowExeNamedFoo() throws Exception {
File f = FileManagerTestUtils.createPartialCopy(exe, ".foo", _scratchDir, 1024);
assertTrue(checker.isDangerous(f));
}
public void testAllowsFirstTwoBytesMatchingExe() throws Exception {
assertFalse(checker.isDangerous(txt));
}
public void testIgnoresEmptySetting() throws Exception {
FilterSettings.DANGEROUS_FILE_TYPES.set("");
checker = new FileExtensionChecker();
assertEquals(0, checker.getNumKnownTypes());
}
public void testIgnoresBrokenSetting() throws Exception {
String setting = FilterSettings.DANGEROUS_FILE_TYPES.get();
assertGreaterThan(1, setting.length());
setting = setting.substring(1); // Oops
FilterSettings.DANGEROUS_FILE_TYPES.set(setting);
checker = new FileExtensionChecker();
assertEquals(0, checker.getNumKnownTypes());
}
}