mirror of
https://github.com/angryip/ipscan.git
synced 2025-10-26 11:18:17 +00:00
starting to merge several separate projects back into one: no more separate unit tests and swt
git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@126 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
parent
91013d1446
commit
1e41e1e660
@ -1,48 +0,0 @@
|
||||
package net.azib.ipscan;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import net.azib.ipscan.feeders.FeederException;
|
||||
|
||||
public class MainTest {
|
||||
|
||||
@Test
|
||||
public void getLocalizedMessage() {
|
||||
// unknown exception
|
||||
final boolean wasStackTraceLogged[] = {false};
|
||||
Throwable e = new Exception("hello, test!");
|
||||
Main.LOG.setUseParentHandlers(false);
|
||||
Main.LOG.addHandler(new Handler() {
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
public void flush() {
|
||||
}
|
||||
public void publish(LogRecord record) {
|
||||
wasStackTraceLogged[0] = true;
|
||||
}
|
||||
});
|
||||
assertEquals(e.toString(), Main.getLocalizedMessage(e));
|
||||
assertTrue(wasStackTraceLogged[0]);
|
||||
|
||||
// localized exception
|
||||
assertEquals(Labels.getLabel("exception.FeederException.range.greaterThan"),
|
||||
Main.getLocalizedMessage(new FeederException("range.greaterThan")));
|
||||
|
||||
// message-less localized exception
|
||||
assertEquals(Labels.getLabel("exception.OutOfMemoryError"),
|
||||
Main.getLocalizedMessage(new OutOfMemoryError()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classShortName() {
|
||||
assertEquals("String", Main.getClassShortName(String.class));
|
||||
assertEquals("MainTest", Main.getClassShortName(MainTest.class));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.config;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author anton
|
||||
*/
|
||||
public class ConfigTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void globalSetUp() throws Exception {
|
||||
Config.initialize();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize() {
|
||||
assertNotNull(Config.getPreferences());
|
||||
assertNotNull(Config.getGlobal());
|
||||
}
|
||||
}
|
||||
@ -1,64 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import org.eclipse.swt.graphics.Rectangle;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* DimensionsConfigTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class DimensionsConfigTest {
|
||||
|
||||
private Preferences preferences;
|
||||
private DimensionsConfig config;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
preferences = Preferences.userRoot().node("ipscan-test");
|
||||
preferences.clear();
|
||||
config = new DimensionsConfig(preferences);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
preferences.removeNode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWindowDimensions() throws Exception {
|
||||
config.windowHeight = 1;
|
||||
config.windowWidth = 2;
|
||||
config.windowTop = 3;
|
||||
config.windowLeft = 4;
|
||||
assertEquals(new Rectangle(4, 3, 2, 1), config.getWindowBounds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void store() throws Exception {
|
||||
config.setWindowBounds(new Rectangle(11, 22, 33, 44), false);
|
||||
config.store();
|
||||
assertEquals(11, preferences.getInt("windowLeft", 0));
|
||||
|
||||
config.setWindowBounds(new Rectangle(77, 22, 33, 44), true);
|
||||
config.store();
|
||||
assertEquals(11, preferences.getInt("windowLeft", 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void columnWidths() throws Exception {
|
||||
config.setColumnWidth("ABC", 35);
|
||||
assertEquals(35, config.getColumnWidth("ABC"));
|
||||
assertEquals(35, preferences.getInt("columnWidth.ABC", 0));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,138 +0,0 @@
|
||||
package net.azib.ipscan.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LabelsTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Labels should initialize themselves on first load
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReinitialize() {
|
||||
Labels.initialize(new Locale("en"));
|
||||
Object oldInternalInstance = Labels.getInstance();
|
||||
Labels.initialize(new Locale("en"));
|
||||
assertTrue(oldInternalInstance == Labels.getInstance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize() {
|
||||
Labels.initialize(new Locale("en"));
|
||||
Object oldInternalInstance = Labels.getInstance();
|
||||
Labels.initialize(new Locale("ee"));
|
||||
assertFalse(oldInternalInstance == Labels.getInstance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleLabel() {
|
||||
assertEquals("&File", Labels.getLabel("menu.file"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInexistentLabel() {
|
||||
try {
|
||||
Labels.getLabel("abra-cadabra");
|
||||
fail();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// exception is good
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImageAsStream() throws IOException {
|
||||
InputStream stream = Labels.getInstance().getImageAsStream("button.start.img");
|
||||
// Now check the first bytes of GIF image header
|
||||
stream.read();
|
||||
assertEquals((int)'P', stream.read());
|
||||
assertEquals((int)'N', stream.read());
|
||||
assertEquals((int)'G', stream.read());
|
||||
stream.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This test recursively processes all source files and tries
|
||||
* to resolve every label it finds.
|
||||
*/
|
||||
@Test
|
||||
public void testAllLabels() throws IOException {
|
||||
File srcDir = new File("../ipscan/src");
|
||||
recurseAndTestLabels(srcDir);
|
||||
}
|
||||
|
||||
private void recurseAndTestLabels(File dir) throws IOException {
|
||||
String files[] = dir.list();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
File file = new File(dir, files[i]);
|
||||
if (file.isDirectory()) {
|
||||
recurseAndTestLabels(file);
|
||||
}
|
||||
else
|
||||
if (file.getName().endsWith(".java")) {
|
||||
findAndTestLabels(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void findAndTestLabels(File file) throws IOException {
|
||||
// TODO: tune these regexps
|
||||
final Pattern LABELS_REGEX = Pattern.compile("Label.{1,60}\"([a-z]\\w+?\\.[a-z][\\w.]+?)\"");
|
||||
final Pattern EXCEPTION_REGEX = Pattern.compile("new\\s+?(\\w+?Exception)\\(\"([\\w.]+?)\"");
|
||||
|
||||
BufferedReader fileReader = new BufferedReader(new FileReader(file));
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String fileLine;
|
||||
while ((fileLine = fileReader.readLine()) != null) {
|
||||
sb.append(fileLine);
|
||||
}
|
||||
fileReader.close();
|
||||
String fileContent = sb.toString();
|
||||
|
||||
String key = null;
|
||||
// String value = null;
|
||||
try {
|
||||
// System.out.println(file.getPath());
|
||||
|
||||
Matcher matcher = LABELS_REGEX.matcher(fileContent);
|
||||
while (matcher.find()) {
|
||||
// try to load the label
|
||||
key = matcher.group(1);
|
||||
// value =
|
||||
Labels.getLabel(key);
|
||||
// System.out.println(key + "=" + value);
|
||||
}
|
||||
|
||||
matcher = EXCEPTION_REGEX.matcher(fileContent);
|
||||
while (matcher.find()) {
|
||||
// try to load the label
|
||||
key = "exception." + matcher.group(1) + "." + matcher.group(2);
|
||||
// value =
|
||||
Labels.getLabel(key);
|
||||
// System.out.println(key + "=" + value);
|
||||
}
|
||||
}
|
||||
catch (MissingResourceException e) {
|
||||
throw new AssertionFailedError("Label not found: " + key + ", in file: " + file.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.config;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class LoggerFactoryTest extends TestCase {
|
||||
|
||||
private static Logger staticLogger = LoggerFactory.getLogger();
|
||||
|
||||
public void testAutomaticNameInitialization() {
|
||||
assertEquals(getClass().getName(), staticLogger.getName());
|
||||
assertEquals(getClass().getName(), LoggerFactory.getLogger().getName());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,93 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* NamedListConfigTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class NamedListConfigTest {
|
||||
|
||||
private static final String PREFERENCE_NAME = "blah";
|
||||
private Preferences preferences;
|
||||
private NamedListConfig config;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
preferences = Preferences.userRoot().node("ipscan-test");
|
||||
preferences.node(PREFERENCE_NAME).clear();
|
||||
config = new NamedListConfig(preferences, PREFERENCE_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
preferences.removeNode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
config.add("Mega favorite", "aaa:xxx");
|
||||
assertEquals("aaa:xxx", config.get("Mega favorite"));
|
||||
assertEquals(1, config.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoad() throws Exception {
|
||||
preferences.put(PREFERENCE_NAME, "aa###aaa###bb###bbb###cc###ccc");
|
||||
NamedListConfig config = new NamedListConfig(preferences, PREFERENCE_NAME);
|
||||
|
||||
assertEquals("aaa", config.get("aa"));
|
||||
assertEquals("bbb", config.get("bb"));
|
||||
assertEquals("ccc", config.get("cc"));
|
||||
assertEquals(3, config.size());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testOrder() throws Exception {
|
||||
preferences.put(PREFERENCE_NAME, "aa###aaa###bb###bbb###cc###ccc");
|
||||
NamedListConfig config = new NamedListConfig(preferences, PREFERENCE_NAME);
|
||||
|
||||
Iterator namesIterator = config.iterateNames();
|
||||
assertEquals("aa", namesIterator.next());
|
||||
assertEquals("bb", namesIterator.next());
|
||||
assertEquals("cc", namesIterator.next());
|
||||
assertFalse(namesIterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStore() throws Exception {
|
||||
config.add("x", "y");
|
||||
config.add("Buga muga x,1,2,3,4,5", "opopo op : , . l ; - # | @@");
|
||||
config.add("127.0.0.1", "192.168.2.25");
|
||||
config.store();
|
||||
|
||||
assertEquals("x###y###Buga muga x,1,2,3,4,5###opopo op : , . l ; - # | @@###127.0.0.1###192.168.2.25", preferences.get(PREFERENCE_NAME, ""));
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testUpdate() {
|
||||
config.add("z", "zzz");
|
||||
config.add("y", "yyy");
|
||||
config.add("x", "xxx");
|
||||
|
||||
config.update(new String[] {"x", "z"});
|
||||
|
||||
Iterator i = config.iterateNames();
|
||||
assertEquals("xxx", config.get((String)i.next()));
|
||||
assertEquals("zzz", config.get((String)i.next()));
|
||||
assertFalse(i.hasNext());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,128 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* OpenersConfigTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class OpenersConfigTest {
|
||||
|
||||
private static final String PREFERENCE_NAME = "openers";
|
||||
private Preferences preferences;
|
||||
private OpenersConfig config;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
preferences = Preferences.userRoot().node("ipscan-test");
|
||||
preferences.clear();
|
||||
config = new OpenersConfig(preferences);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
preferences.removeNode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultValues() throws Exception {
|
||||
assertTrue(config.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNoStrings() {
|
||||
try {
|
||||
config.add("aa", "b");
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
// clear default values
|
||||
config.update(new String[] {});
|
||||
|
||||
config.add("Mega favorite", new OpenersConfig.Opener("a@@@0@@@c"));
|
||||
assertEquals("a", config.getOpener("Mega favorite").execString);
|
||||
assertEquals(false, config.getOpener("Mega favorite").inTerminal);
|
||||
assertEquals("c", config.getOpener("Mega favorite").workingDir.getName());
|
||||
assertEquals(1, config.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenerDeserialize() {
|
||||
OpenersConfig.Opener o = new OpenersConfig.Opener("uu@@uu@@@1@@@");
|
||||
assertEquals("uu@@uu", o.execString);
|
||||
assertEquals(true, o.inTerminal);
|
||||
assertEquals(null, o.workingDir);
|
||||
|
||||
o = new OpenersConfig.Opener("c:\\program files\\mega app\\app.exe@@@0@@@c:\\windoze system");
|
||||
assertEquals("c:\\program files\\mega app\\app.exe", o.execString);
|
||||
assertEquals(false, o.inTerminal);
|
||||
assertEquals("c:\\windoze system", o.workingDir.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoad() throws Exception {
|
||||
preferences.put(PREFERENCE_NAME, "aa###aaa@@@1@@@###bb###bbb@@@1@@@");
|
||||
OpenersConfig config = new OpenersConfig(preferences);
|
||||
|
||||
assertEquals("aaa", config.getOpener("aa").execString);
|
||||
assertEquals("bbb", config.getOpener("bb").execString);
|
||||
assertEquals(2, config.size());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testOrder() throws Exception {
|
||||
preferences.put(PREFERENCE_NAME, "aa###aaa@@@1@@@###bb###bbb@@@1@@@");
|
||||
OpenersConfig config = new OpenersConfig(preferences);
|
||||
|
||||
Iterator namesIterator = config.iterateNames();
|
||||
assertEquals("aa", namesIterator.next());
|
||||
assertEquals("bb", namesIterator.next());
|
||||
assertFalse(namesIterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStore() throws Exception {
|
||||
// clear default values
|
||||
config.update(new String[] {});
|
||||
|
||||
config.add("x", new OpenersConfig.Opener("aa", true, null));
|
||||
config.add("x y z", new OpenersConfig.Opener("a a", true, new File("zzz z")));
|
||||
config.store();
|
||||
|
||||
assertEquals("x###aa@@@1@@@###x y z###a a@@@1@@@zzz z", preferences.get(PREFERENCE_NAME, ""));
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testUpdate() {
|
||||
config.add("x", new OpenersConfig.Opener("aa", true, null));
|
||||
config.add("y", new OpenersConfig.Opener("bb", false, null));
|
||||
config.add("z", new OpenersConfig.Opener("ccc", false, null));
|
||||
|
||||
config.update(new String[] {"x", "z"});
|
||||
|
||||
Iterator i = config.iterateNames();
|
||||
assertEquals("x", i.next());
|
||||
assertEquals("z", i.next());
|
||||
assertFalse(i.hasNext());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,101 +0,0 @@
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class InetAddressUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testStartRangeByNetmask() throws UnknownHostException {
|
||||
assertEquals("127.0.1.64", InetAddressUtils.startRangeByNetmask(
|
||||
InetAddress.getByName("127.0.1.92"),
|
||||
InetAddress.getByName("255.255.255.192")).getHostAddress());
|
||||
assertEquals("127.0.0.15", InetAddressUtils.startRangeByNetmask(
|
||||
InetAddress.getByName("127.0.0.15"),
|
||||
InetAddress.getByName("255.255.255.255")).getHostAddress());
|
||||
assertEquals("192.10.0.0", InetAddressUtils.startRangeByNetmask(
|
||||
InetAddress.getByName("192.10.11.13"),
|
||||
InetAddress.getByName("255.255.0.0")).getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEndRangeByNetmask() throws UnknownHostException {
|
||||
assertEquals("127.0.1.127", InetAddressUtils.endRangeByNetmask(
|
||||
InetAddress.getByName("127.0.1.92"),
|
||||
InetAddress.getByName("255.255.255.192")).getHostAddress());
|
||||
assertEquals("127.0.0.15", InetAddressUtils.endRangeByNetmask(
|
||||
InetAddress.getByName("127.0.0.15"),
|
||||
InetAddress.getByName("255.255.255.255")).getHostAddress());
|
||||
assertEquals("192.10.255.255", InetAddressUtils.endRangeByNetmask(
|
||||
InetAddress.getByName("192.10.11.13"),
|
||||
InetAddress.getByName("255.255.0.0")).getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncrement() throws UnknownHostException {
|
||||
assertEquals("127.0.0.2", InetAddressUtils.increment(InetAddress.getByName("127.0.0.1")).getHostAddress());
|
||||
assertEquals("128.0.0.0", InetAddressUtils.increment(InetAddress.getByName("127.255.255.255")).getHostAddress());
|
||||
assertEquals("0.0.0.0", InetAddressUtils.increment(InetAddress.getByName("255.255.255.255")).getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreaterThan() throws UnknownHostException {
|
||||
assertTrue(InetAddressUtils.greaterThan(InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.0")));
|
||||
assertTrue(InetAddressUtils.greaterThan(InetAddress.getByName("129.0.0.1"), InetAddress.getByName("128.0.0.0")));
|
||||
assertTrue(InetAddressUtils.greaterThan(InetAddress.getByName("255.0.0.0"), InetAddress.getByName("254.255.255.255")));
|
||||
assertFalse(InetAddressUtils.greaterThan(InetAddress.getByName("0.0.0.0"), InetAddress.getByName("255.255.255.255")));
|
||||
assertFalse(InetAddressUtils.greaterThan(InetAddress.getByName("0.0.0.0"), InetAddress.getByName("0.0.0.0")));
|
||||
assertFalse(InetAddressUtils.greaterThan(InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.5.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNetmask() throws UnknownHostException {
|
||||
assertEquals("255.255.255.255", InetAddressUtils.parseNetmask("255.255.255.255").getHostAddress());
|
||||
assertEquals("255.255.255.255", InetAddressUtils.parseNetmask("255...255").getHostAddress());
|
||||
assertEquals("255.0.255.255", InetAddressUtils.parseNetmask("255.0..255").getHostAddress());
|
||||
assertEquals("255.255.255.192", InetAddressUtils.parseNetmask("255...192").getHostAddress());
|
||||
assertEquals("255.0.255.0", InetAddressUtils.parseNetmask("255.0..0").getHostAddress());
|
||||
assertEquals("0.0.0.0", InetAddressUtils.parseNetmask("0.0.0.0").getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaskPrototypeBytes() throws UnknownHostException {
|
||||
byte[] bytes = InetAddress.getByName("32.23.34.254").getAddress();
|
||||
InetAddressUtils.maskPrototypeAddressBytes(bytes, InetAddress.getByName("255.0.0.255").getAddress(), InetAddress.getByName("29.1.2.255").getAddress());
|
||||
assertEquals("29.23.34.255", InetAddress.getByAddress(bytes).getHostAddress());
|
||||
|
||||
bytes = InetAddress.getByName("250.250.250.250").getAddress();
|
||||
InetAddressUtils.maskPrototypeAddressBytes(bytes, InetAddress.getByName("0.0.0.0").getAddress(), InetAddress.getByName("29.1.2.255").getAddress());
|
||||
assertEquals("250.250.250.250", InetAddress.getByAddress(bytes).getHostAddress());
|
||||
|
||||
bytes = InetAddress.getByName("250.250.250.250").getAddress();
|
||||
InetAddressUtils.maskPrototypeAddressBytes(bytes, InetAddress.getByName("255.255.255.255").getAddress(), InetAddress.getByName("29.128.127.73").getAddress());
|
||||
assertEquals("29.128.127.73", InetAddress.getByAddress(bytes).getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsLikelyBroadcast() throws UnknownHostException {
|
||||
assertTrue(InetAddressUtils.isLikelyBroadcast(InetAddress.getByName("127.0.2.0")));
|
||||
assertTrue(InetAddressUtils.isLikelyBroadcast(InetAddress.getByName("127.6.32.255")));
|
||||
assertFalse(InetAddressUtils.isLikelyBroadcast(InetAddress.getByName("127.4.5.6")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAddressByName() throws Exception {
|
||||
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
|
||||
ifaces.nextElement();
|
||||
if (ifaces.hasMoreElements()) {
|
||||
// this test depends on the network configuraton of the system
|
||||
// so we run it only if there are more than 1 network interface in the system (which is a loopback interface)
|
||||
assertFalse(InetAddress.getByName(InetAddressUtils.getAddressByName(InetAddress.getLocalHost().getHostName())).isLoopbackAddress());
|
||||
assertFalse(InetAddress.getByName(InetAddressUtils.getAddressByName("localhost")).isLoopbackAddress());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,57 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import net.azib.ipscan.core.values.IntegerWithUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* IntegerWithUnitTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class IntegerWithUnitTest {
|
||||
|
||||
@Test
|
||||
public void testIntValue() throws Exception {
|
||||
assertEquals(0, new IntegerWithUnit(0, "a").intValue());
|
||||
assertEquals(-1, new IntegerWithUnit(-1, "a").intValue());
|
||||
assertEquals(Integer.MAX_VALUE, new IntegerWithUnit(Integer.MAX_VALUE, "a").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
assertEquals("151" + Labels.getLabel("fetcher.value.ms"), new IntegerWithUnit(151, "fetcher.value.ms").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
assertTrue(new IntegerWithUnit(666, null).equals(new IntegerWithUnit(666, null)));
|
||||
assertTrue(new IntegerWithUnit(42, "a").equals(new IntegerWithUnit(42, "b")));
|
||||
assertFalse(new IntegerWithUnit(0, null).equals(null));
|
||||
assertFalse(new IntegerWithUnit(42, "a").equals(new IntegerWithUnit(43, "a")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() throws Exception {
|
||||
assertEquals(3, new IntegerWithUnit(3, null).hashCode());
|
||||
assertEquals(-31, new IntegerWithUnit(-31, null).hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareTo() throws Exception {
|
||||
assertTrue(Comparable.class.isAssignableFrom(IntegerWithUnit.class));
|
||||
assertEquals(0, new IntegerWithUnit(1, null).compareTo(new IntegerWithUnit(1, null)));
|
||||
assertEquals(1, new IntegerWithUnit(123456789, null).compareTo(new IntegerWithUnit(123456, null)));
|
||||
assertEquals(-1, new IntegerWithUnit(12, null).compareTo(new IntegerWithUnit(123456, null)));
|
||||
assertEquals(1, new IntegerWithUnit(12, null).compareTo(null));
|
||||
IntegerWithUnit instance = new IntegerWithUnit(211082, null);
|
||||
assertEquals(0, instance.compareTo(instance));
|
||||
}
|
||||
}
|
||||
@ -1,48 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* PortIteratorTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class PortIteratorTest {
|
||||
|
||||
@Test
|
||||
public void testBasic() {
|
||||
assertEquals("1 2 3 5 7 ", iterateToString(new PortIterator("1,2,3,5,7")));
|
||||
assertEquals("27 1 65535 ", iterateToString(new PortIterator("27, 1;65535")));
|
||||
assertEquals("16 ", iterateToString(new PortIterator("16")));
|
||||
assertEquals("", iterateToString(new PortIterator("")));
|
||||
assertEquals("12 ", iterateToString(new PortIterator(" 12")));
|
||||
assertEquals("12 ", iterateToString(new PortIterator("12, ")));
|
||||
// TODO assertEquals("", iterateToString(new PortIterator("65536")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRange() {
|
||||
assertEquals("1 2 3 ", iterateToString(new PortIterator("1-3")));
|
||||
assertEquals("65530 65531 65532 65533 65534 65535 ", iterateToString(new PortIterator("65530-65535")));
|
||||
assertEquals("100 13 14 17 18 19 20 ", iterateToString(new PortIterator("100,13-14,17-20")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopy() {
|
||||
assertNotNull(new PortIterator("1").copy());
|
||||
}
|
||||
|
||||
private static String iterateToString(PortIterator iterator) {
|
||||
StringBuffer sb = new StringBuffer(64);
|
||||
while (iterator.hasNext()) {
|
||||
sb.append(iterator.next()).append(' ');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,148 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.azib.ipscan.config.Config;
|
||||
import net.azib.ipscan.core.values.NotAvailableValue;
|
||||
import net.azib.ipscan.core.values.NotScannedValue;
|
||||
import net.azib.ipscan.fetchers.Fetcher;
|
||||
import net.azib.ipscan.fetchers.FetcherRegistry;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ScannerTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class ScannerTest {
|
||||
|
||||
private Set<Class<? extends Fetcher>> initCalled = new HashSet<Class<? extends Fetcher>>();
|
||||
private Set<Class<? extends Fetcher>> cleanupCalled = new HashSet<Class<? extends Fetcher>>();
|
||||
private Scanner scanner;
|
||||
private FetcherRegistry fetcherRegistry;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Config.initialize();
|
||||
|
||||
fetcherRegistry = createMock(FetcherRegistry.class);
|
||||
expect(fetcherRegistry.getSelectedFetchers()).andReturn(
|
||||
Arrays.asList(new Fetcher[] {new FakeFetcher(), new AnotherFakeFetcher(), new AbortingFetcher(), new FailingFetcher()})
|
||||
);
|
||||
replay(fetcherRegistry);
|
||||
|
||||
scanner = new Scanner(fetcherRegistry);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
verify(fetcherRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScan() throws Exception {
|
||||
// scan the local host
|
||||
ScanningResult scanningResult = new ScanningResult(InetAddress.getLocalHost(), 4);
|
||||
scanner.scan(InetAddress.getLocalHost(), scanningResult);
|
||||
|
||||
assertEquals(ScanningSubject.RESULT_TYPE_ALIVE, scanningResult.getType());
|
||||
assertEquals(InetAddress.getLocalHost(), scanningResult.getAddress());
|
||||
assertEquals(4, scanningResult.getValues().size());
|
||||
assertEquals("blah", scanningResult.getValues().get(0));
|
||||
assertEquals(NotAvailableValue.INSTANCE, scanningResult.getValues().get(1));
|
||||
assertEquals("666 ms", scanningResult.getValues().get(2));
|
||||
assertEquals(NotScannedValue.INSTANCE, scanningResult.getValues().get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit() throws Exception {
|
||||
scanner.init();
|
||||
|
||||
assertTrue(initCalled.contains(FakeFetcher.class));
|
||||
assertTrue(initCalled.contains(AnotherFakeFetcher.class));
|
||||
assertTrue(initCalled.contains(AbortingFetcher.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCleanup() throws Exception {
|
||||
scanner.cleanup();
|
||||
|
||||
assertTrue(cleanupCalled.contains(FakeFetcher.class));
|
||||
assertTrue(cleanupCalled.contains(AnotherFakeFetcher.class));
|
||||
assertTrue(cleanupCalled.contains(AbortingFetcher.class));
|
||||
}
|
||||
|
||||
private class FakeFetcher implements Fetcher {
|
||||
public String getLabel() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object scan(ScanningSubject subject) {
|
||||
try {
|
||||
// check that the IP is correct
|
||||
assertEquals(InetAddress.getLocalHost(), subject.getIPAddress());
|
||||
|
||||
// set the result type to check after scanning
|
||||
subject.setResultType(ScanningSubject.RESULT_TYPE_ALIVE);
|
||||
|
||||
// try to set parameter here and read from another Fetcher
|
||||
subject.setParameter("megaParam", new Long(211082));
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
fail();
|
||||
}
|
||||
return "blah";
|
||||
}
|
||||
|
||||
public void init() {
|
||||
initCalled.add(getClass());
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
cleanupCalled.add(getClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class AnotherFakeFetcher extends FakeFetcher {
|
||||
public Object scan(ScanningSubject subject) {
|
||||
// the parameter was set by FakeFetcher
|
||||
assertEquals(new Long(211082), subject.getParameter("megaParam"));
|
||||
// try null as a return value
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class AbortingFetcher extends FakeFetcher {
|
||||
public Object scan(ScanningSubject subject) {
|
||||
subject.abortScanning();
|
||||
return "666 ms";
|
||||
}
|
||||
}
|
||||
|
||||
private class FailingFetcher extends FakeFetcher {
|
||||
public Object scan(ScanningSubject subject) {
|
||||
fail("This fetcher should not be reached");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,189 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import net.azib.ipscan.core.values.NotScannedValue;
|
||||
import net.azib.ipscan.fetchers.Fetcher;
|
||||
import net.azib.ipscan.fetchers.FetcherRegistry;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ScanningResultListTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class ScanningResultListTest {
|
||||
|
||||
private List<Fetcher> fetchers = new ArrayList<Fetcher>(Arrays.asList(new Fetcher[] {new DummyFetcher("fetcher.ip"), new DummyFetcher("fetcher.ping"), new DummyFetcher("fetcher.hostname"), new DummyFetcher("fetcher.ping.ttl")}));
|
||||
|
||||
private FetcherRegistry fetcherRegistry;
|
||||
private ScanningResultList scanningResults;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcherRegistry = createMock(FetcherRegistry.class);
|
||||
expect(fetcherRegistry.getSelectedFetchers())
|
||||
.andReturn(fetchers).anyTimes();
|
||||
replay(fetcherRegistry);
|
||||
|
||||
scanningResults = new ScanningResultList(fetcherRegistry);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
verify(fetcherRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() throws Exception {
|
||||
int index = scanningResults.add(InetAddress.getByName("10.0.0.5"));
|
||||
assertEquals("10.0.0.5", scanningResults.getResult(index).getAddress().getHostAddress());
|
||||
assertEquals("10.0.0.5", scanningResults.getResult(index).getValues().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() throws Exception {
|
||||
assertFalse(scanningResults.iterator().hasNext());
|
||||
scanningResults.add(InetAddress.getLocalHost());
|
||||
Iterator<?> i = scanningResults.iterator();
|
||||
assertTrue(i.hasNext());
|
||||
assertTrue(i.next() instanceof ScanningResult);
|
||||
assertFalse(i.hasNext());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testClear() throws Exception {
|
||||
fetcherRegistry.getSelectedFetchers().clear();
|
||||
fetcherRegistry.getSelectedFetchers().add(new DummyFetcher("hello"));
|
||||
scanningResults.add(InetAddress.getLocalHost());
|
||||
scanningResults.clear();
|
||||
|
||||
assertFalse("Results must be empty", scanningResults.iterator().hasNext());
|
||||
assertEquals("Cached Fetchers must be re-initilized", 1, scanningResults.getFetchers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCachedFetchers() throws Exception {
|
||||
fetcherRegistry.getSelectedFetchers().clear();
|
||||
assertEquals("Fetchers should be cached from the last scan", 4, scanningResults.getFetchers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() throws Exception {
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.1"));
|
||||
int i2 = scanningResults.add(InetAddress.getByName("127.9.9.2"));
|
||||
int i3 = scanningResults.add(InetAddress.getByName("127.9.9.3"));
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.4"));
|
||||
scanningResults.remove(new int[] {i2,i3});
|
||||
|
||||
Iterator<?> i = scanningResults.iterator();
|
||||
assertTrue(i.hasNext());
|
||||
assertEquals(InetAddress.getByName("127.9.9.1"), ((ScanningResult)i.next()).getAddress());
|
||||
assertTrue(i.hasNext());
|
||||
assertEquals(InetAddress.getByName("127.9.9.4"), ((ScanningResult)i.next()).getAddress());
|
||||
assertFalse(i.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSort() throws Exception {
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.1"));
|
||||
scanningResults.getResult(0).setValue(1, "x");
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.2"));
|
||||
scanningResults.getResult(1).setValue(1, "a");
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.3"));
|
||||
scanningResults.getResult(2).setValue(1, "z");
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.4"));
|
||||
scanningResults.getResult(3).setValue(1, "m");
|
||||
|
||||
scanningResults.sort(1);
|
||||
|
||||
Iterator<?> i = scanningResults.iterator();
|
||||
assertEquals(InetAddress.getByName("127.9.9.2"), ((ScanningResult)i.next()).getAddress());
|
||||
assertEquals(InetAddress.getByName("127.9.9.4"), ((ScanningResult)i.next()).getAddress());
|
||||
assertEquals(InetAddress.getByName("127.9.9.1"), ((ScanningResult)i.next()).getAddress());
|
||||
assertEquals(InetAddress.getByName("127.9.9.3"), ((ScanningResult)i.next()).getAddress());
|
||||
assertFalse(i.hasNext());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testGetResultsAsString() throws Exception {
|
||||
List<Fetcher> fetchers = scanningResults.getFetchers();
|
||||
int index = scanningResults.add(InetAddress.getByName("172.28.43.55"));
|
||||
ScanningResult result = scanningResults.getResult(index);
|
||||
result.setValue(1, "123");
|
||||
result.setValue(2, "xxxxx");
|
||||
result.setValue(3, null);
|
||||
|
||||
String s = scanningResults.getResultsAsString(index);
|
||||
String ln = System.getProperty("line.separator");
|
||||
assertTrue(s.endsWith(ln));
|
||||
assertTrue(s.indexOf(Labels.getLabel(fetchers.get(0).getLabel()) + ":\t172.28.43.55" + ln) >= 0);
|
||||
assertTrue(s.indexOf(Labels.getLabel(fetchers.get(1).getLabel()) + ":\t123" + ln) >= 0);
|
||||
assertTrue(s.indexOf(Labels.getLabel(fetchers.get(2).getLabel()) + ":\txxxxx" + ln) >= 0);
|
||||
assertTrue(s.indexOf(Labels.getLabel(fetchers.get(3).getLabel()) + ":\t" + ln) >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindText() throws Exception {
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.1"));
|
||||
scanningResults.getResult(0).setValue(1, NotScannedValue.INSTANCE);
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.2"));
|
||||
scanningResults.getResult(1).setValue(1, new Long(123456789L));
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.3"));
|
||||
scanningResults.getResult(2).setValue(1, "zzzz");
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.4"));
|
||||
scanningResults.getResult(3).setValue(1, "mmmmm");
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.5"));
|
||||
scanningResults.getResult(4).setValue(1, null);
|
||||
scanningResults.add(InetAddress.getByName("127.9.9.6"));
|
||||
scanningResults.getResult(5).setValue(1, InetAddress.getByName("127.0.0.1"));
|
||||
|
||||
assertEquals(-1, scanningResults.findText("sometext", 0));
|
||||
assertEquals(1, scanningResults.findText("345", 0));
|
||||
assertEquals(-1, scanningResults.findText("345", 2));
|
||||
assertEquals(3, scanningResults.findText("m", 2));
|
||||
assertEquals(5, scanningResults.findText("0.0.", 2));
|
||||
}
|
||||
|
||||
private static class DummyFetcher implements Fetcher {
|
||||
private String label;
|
||||
|
||||
public DummyFetcher(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public Object scan(ScanningSubject subject) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core.net;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* SharedPingerTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class ICMPSharedPingerTest {
|
||||
|
||||
@Test @Ignore("this test works only under root")
|
||||
public void testPing() throws Exception {
|
||||
Pinger pinger = new ICMPSharedPinger(1000);
|
||||
PingResult result = pinger.ping(InetAddress.getLocalHost(), 3);
|
||||
assertTrue(result.getAverageTime() >= 0);
|
||||
assertTrue(result.getAverageTime() < 50);
|
||||
assertTrue(result.getTTL() >= 0);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core.net;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import net.azib.ipscan.config.Labels;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* PingerRegistryImplTest
|
||||
*
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class PingerRegistryImplTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
System.setProperty("java.library.path", "../swt/lib");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRegisteredNames() throws Exception {
|
||||
String[] names = new PingerRegistryImpl().getRegisteredNames();
|
||||
assertNotNull(names);
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
assertNotNull(Labels.getLabel(names[i]));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatePinger() throws Exception {
|
||||
PingerRegistry registry = new PingerRegistryImpl();
|
||||
String[] names = registry.getRegisteredNames();
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
try {
|
||||
Pinger pinger = registry.createPinger(names[i], 0);
|
||||
pinger.close();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
// ignore IOExceptions in case RawSockets cannot be initialized
|
||||
// under current conditions
|
||||
assertTrue(e.getMessage().startsWith("Unable to create pinger"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core.values;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import net.azib.ipscan.config.Config;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* NotAvailableValueTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class NotAvailableValueTest {
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
assertEquals(NotAvailableValue.INSTANCE, NotAvailableValue.INSTANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
Config.initialize();
|
||||
assertEquals(Config.getGlobal().notAvailableText, NotAvailableValue.INSTANCE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareTo() throws Exception {
|
||||
assertTrue(Comparable.class.isAssignableFrom(NotAvailableValue.class));
|
||||
assertEquals(0, NotAvailableValue.INSTANCE.compareTo(NotAvailableValue.INSTANCE));
|
||||
assertEquals(-1, NotAvailableValue.INSTANCE.compareTo("Hello"));
|
||||
assertEquals(1, NotAvailableValue.INSTANCE.compareTo(null));
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core.values;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import net.azib.ipscan.config.Config;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* NotScannedValueTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class NotScannedValueTest {
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
assertEquals(NotScannedValue.INSTANCE, NotScannedValue.INSTANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
Config.initialize();
|
||||
assertEquals(Config.getGlobal().notScannedText, NotScannedValue.INSTANCE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareTo() throws Exception {
|
||||
assertTrue(Comparable.class.isAssignableFrom(NotScannedValue.class));
|
||||
assertEquals(0, NotScannedValue.INSTANCE.compareTo(NotScannedValue.INSTANCE));
|
||||
assertEquals(-1, NotScannedValue.INSTANCE.compareTo("Hello"));
|
||||
assertEquals(1, NotScannedValue.INSTANCE.compareTo(null));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.core.values;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class NumericListValueTest {
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
assertEquals("", new NumericListValue(Collections.EMPTY_LIST, true).toString());
|
||||
assertEquals("1", new NumericListValue(Arrays.asList(new Object[] {1}), true).toString());
|
||||
assertEquals("1-3", new NumericListValue(Arrays.asList(new Object[] {1, 2, 3}), true).toString());
|
||||
assertEquals("1-3", new NumericListValue(new TreeSet<Integer>(Arrays.asList(new Integer[] {2, 3, 1})), true).toString());
|
||||
assertEquals("1,2,3", new NumericListValue(Arrays.asList(new Object[] {1, 2, 3}), false).toString());
|
||||
assertEquals("1,5-6,15", new NumericListValue(Arrays.asList(new Object[] {1, 5, 6, 15}), true).toString());
|
||||
assertEquals("103,85,89,1", new NumericListValue(Arrays.asList(new Object[] {103, 85, 89, 1}), true).toString());
|
||||
}
|
||||
}
|
||||
@ -1,145 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import junit.framework.ComparisonFailure;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* TestCase for Exporters.
|
||||
* It contains initialization and generic tests for any Exporter.
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public abstract class AbstractExporterTestCase {
|
||||
|
||||
protected Exporter exporter;
|
||||
protected ByteArrayOutputStream outputStream;
|
||||
protected String outputContent;
|
||||
|
||||
protected abstract Exporter createExporter();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
exporter = createExporter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLabel() {
|
||||
assertNotNull(Labels.getLabel(exporter.getLabel()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilenameExtension() {
|
||||
assertNotNull(exporter.getFilenameExtension());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamFlushAndClose() throws IOException {
|
||||
final boolean wasClosed[] = new boolean[] {false, false};
|
||||
Exporter exporter2 = createExporter();
|
||||
OutputStream mockOutputStream = new OutputStream() {
|
||||
public void write(int b) throws IOException {
|
||||
}
|
||||
public void close() throws IOException {
|
||||
wasClosed[0] = true;
|
||||
}
|
||||
public void flush() {
|
||||
wasClosed[1] = true;
|
||||
}
|
||||
};
|
||||
exporter2.start(mockOutputStream, "feederstuff");
|
||||
// output something to ensure that the flush will be called
|
||||
exporter2.setFetchers(new String[] {Labels.getLabel("fetcher.ip"), Labels.getLabel("fetcher.ports")});
|
||||
exporter2.nextAdressResults(new Object[] {"1", "2"});
|
||||
// this should invoke flush among other things
|
||||
exporter2.end();
|
||||
// close: no
|
||||
assertFalse(wasClosed[0]);
|
||||
// flush: yes
|
||||
assertTrue(wasClosed[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasic() throws Exception {
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {"IP", "hello", "fetcher2"});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost().getHostAddress(), "world", new Integer(53)});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost().getHostAddress(), "buga", new Integer(-1)});
|
||||
exporter.end();
|
||||
assertContains(InetAddress.getLocalHost().getHostAddress());
|
||||
assertContains("hello");
|
||||
assertContains("fetcher2");
|
||||
assertContains("world");
|
||||
assertContains("53");
|
||||
assertContains("buga");
|
||||
assertContains("-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithoutAppend() throws IOException {
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {"IP", "fetcher1", "mega long fetcher 2"});
|
||||
exporter.end();
|
||||
assertContains("IP");
|
||||
assertContains("fetcher1");
|
||||
assertContains("mega long fetcher 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithAppend() throws IOException {
|
||||
exporter.setAppend(true);
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {"IP", "fetcher1", "mega long fetcher 2"});
|
||||
exporter.end();
|
||||
assertNotContains("IP");
|
||||
assertNotContains("fetcher1");
|
||||
assertNotContains("mega long fetcher 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClone() throws CloneNotSupportedException {
|
||||
Exporter exporter2 = (Exporter) exporter.clone();
|
||||
assertNotSame(exporter, exporter2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextAddressResultsWithNulls() throws IOException {
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {"IP", "fetcher1", "mega long fetcher 2"});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost(), null, null});
|
||||
exporter.end();
|
||||
}
|
||||
|
||||
protected void assertContains(String string, boolean contains) throws IOException {
|
||||
if (outputContent == null) {
|
||||
outputStream.close();
|
||||
// TODO: encoding???
|
||||
outputContent = new String(outputStream.toByteArray());
|
||||
}
|
||||
|
||||
if (!((outputContent.indexOf(string) >= 0) ^ (!contains))) {
|
||||
throw new ComparisonFailure("Contains check failed", string, outputContent);
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertContains(String string) throws IOException {
|
||||
assertContains(string, true);
|
||||
}
|
||||
|
||||
protected void assertNotContains(String string) throws IOException {
|
||||
assertContains(string, false);
|
||||
}
|
||||
}
|
||||
@ -1,52 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* CSV Exporter Test
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class CSVExporterTest extends AbstractExporterTestCase {
|
||||
|
||||
protected Exporter createExporter() {
|
||||
return new CSVExporter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCSVSafeString() {
|
||||
assertEquals(".a.bb.c.d.", ((CSVExporter)exporter).csvSafeString(",a,bb,c,d,"));
|
||||
assertEquals("", ((CSVExporter)exporter).csvSafeString(""));
|
||||
assertEquals("uuuuhha;", ((CSVExporter)exporter).csvSafeString("uuuuhha;"));
|
||||
assertEquals("", ((CSVExporter)exporter).csvSafeString(null));
|
||||
assertEquals("123", ((CSVExporter)exporter).csvSafeString(new Long(123)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithoutAppend() throws IOException {
|
||||
exporter.start(outputStream, null);
|
||||
exporter.setFetchers(new String[] {"fet1", "hello2", "Mega Fetcher", "oops, comma here"});
|
||||
exporter.end();
|
||||
assertContains("fet1");
|
||||
assertContains("Mega Fetcher");
|
||||
assertContains("oops. comma here");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextAddressResults() throws IOException {
|
||||
exporter.start(outputStream, null);
|
||||
exporter.setFetchers(new String[] {"fet1", "hello2"});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost(), "oops, comma"});
|
||||
exporter.end();
|
||||
assertContains("oops. comma");
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,112 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collections;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import net.azib.ipscan.core.ScanningResult;
|
||||
import net.azib.ipscan.core.ScanningResultList;
|
||||
import net.azib.ipscan.exporters.ExportProcessor.ScanningResultSelector;
|
||||
import net.azib.ipscan.fetchers.FetcherRegistry;
|
||||
import net.azib.ipscan.fetchers.IPFetcher;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ExportProcessorTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class ExportProcessorTest {
|
||||
|
||||
private FetcherRegistry fetcherRegistry;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
fetcherRegistry = createMock(FetcherRegistry.class);
|
||||
expect(fetcherRegistry.getSelectedFetchers())
|
||||
.andReturn(Collections.singletonList(new IPFetcher())).anyTimes();
|
||||
replay(fetcherRegistry);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
verify(fetcherRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcess() throws Exception {
|
||||
File file = File.createTempFile("exportTest", "txt");
|
||||
ExportProcessor exportProcessor = new ExportProcessor(new TXTExporter(), file.getAbsolutePath());
|
||||
|
||||
ScanningResultList scanningResultList = new ScanningResultList(fetcherRegistry);
|
||||
scanningResultList.add(InetAddress.getByName("192.168.0.13"));
|
||||
exportProcessor.process(scanningResultList, "megaFeeder", null);
|
||||
|
||||
String content = readFileContent(file);
|
||||
|
||||
assertTrue(content.indexOf("megaFeeder") > 0);
|
||||
assertTrue(content.indexOf(Labels.getLabel(new IPFetcher().getLabel())) > 0);
|
||||
assertTrue(content.indexOf("fooBar") < 0);
|
||||
assertTrue(content.indexOf("192.168.0.13") > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param file
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
private String readFileContent(File file) throws FileNotFoundException, IOException {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null) {
|
||||
buffer.append(line);
|
||||
}
|
||||
reader.close();
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessWithSelector() throws Exception {
|
||||
File file = File.createTempFile("exportTest", "txt");
|
||||
ExportProcessor exportProcessor = new ExportProcessor(new TXTExporter(), file.getAbsolutePath());
|
||||
|
||||
ScanningResultList scanningResultList = new ScanningResultList(fetcherRegistry);
|
||||
|
||||
scanningResultList.add(InetAddress.getByName("192.168.13.66"));
|
||||
scanningResultList.add(InetAddress.getByName("192.168.13.67"));
|
||||
scanningResultList.add(InetAddress.getByName("192.168.13.76"));
|
||||
|
||||
exportProcessor.process(scanningResultList, "feeder2", new ScanningResultSelector() {
|
||||
public boolean isResultSelected(int index, ScanningResult result) {
|
||||
// select only IP addresses ending with 6
|
||||
return ((String)result.getValues().get(0)).endsWith("6");
|
||||
}
|
||||
});
|
||||
|
||||
String content = readFileContent(file);
|
||||
|
||||
assertTrue(content.indexOf("feeder2") > 0);
|
||||
assertTrue(content.indexOf("192.168.13.66") > 0);
|
||||
assertTrue(content.indexOf("192.168.13.67") < 0);
|
||||
assertTrue(content.indexOf("192.168.13.76") > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,60 +0,0 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ExporterRegistryTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class ExporterRegistryTest {
|
||||
|
||||
private ExporterRegistry exporterRegistry = new ExporterRegistry(new Exporter[] {new TXTExporter(), new CSVExporter()});
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
for (Iterator<?> i = exporterRegistry.iterator(); i.hasNext(); ) {
|
||||
Exporter exporter = (Exporter) i.next();
|
||||
assertNotNull(exporter);
|
||||
assertNotNull(exporter.getFilenameExtension());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() {
|
||||
Exporter exporter;
|
||||
|
||||
exporter = exporterRegistry.createExporter("aa.abc." + new TXTExporter().getFilenameExtension());
|
||||
assertTrue(exporter instanceof TXTExporter);
|
||||
|
||||
exporter = exporterRegistry.createExporter("/tmp/foo/megafile." + new TXTExporter().getFilenameExtension());
|
||||
assertTrue(exporter instanceof TXTExporter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateFailed() {
|
||||
try {
|
||||
exporterRegistry.createExporter("noextension");
|
||||
fail();
|
||||
}
|
||||
catch (ExporterException e) {
|
||||
assertEquals("exporter.unknown", e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
exporterRegistry.createExporter("unknown.extension");
|
||||
fail();
|
||||
}
|
||||
catch (ExporterException e) {
|
||||
assertEquals("exporter.unknown", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,87 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
|
||||
/**
|
||||
* IP List Exporter Test
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class IPListExporterTest extends AbstractExporterTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Labels.initialize(Locale.ENGLISH);
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected Exporter createExporter() {
|
||||
return new IPListExporter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasic() throws IOException {
|
||||
Labels labels = Labels.getInstance();
|
||||
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {"fetcher1", labels.get("fetcher.ip"), "mega long fetcher 2", labels.get("fetcher.ports")});
|
||||
exporter.nextAdressResults(new Object[] {"", "123", "", "1,23; 4-6 78"});
|
||||
exporter.end();
|
||||
|
||||
assertContains("123:1");
|
||||
assertContains("123:23");
|
||||
assertContains("123:4");
|
||||
assertContains("123:5");
|
||||
assertContains("123:6");
|
||||
assertContains("123:78");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithAppend() throws IOException {
|
||||
// default implementation doesn't work
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithoutAppend() throws IOException {
|
||||
// default implementation doesn't work
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextAddressResultsWithNulls() throws IOException {
|
||||
Labels labels = Labels.getInstance();
|
||||
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {labels.get("fetcher.ip"), "fetcher1", labels.get("fetcher.ports")});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost(), null, null});
|
||||
exporter.end();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindFetcherByLabel() {
|
||||
Labels labels = Labels.getInstance();
|
||||
|
||||
assertEquals(0, IPListExporter.findFetcherByLabel("fetcher.ip", new String[] {labels.get("fetcher.ip")}));
|
||||
assertEquals(3, IPListExporter.findFetcherByLabel("fetcher.ip", new String[] {"a", "b", "c", labels.get("fetcher.ip")}));
|
||||
assertEquals(1, IPListExporter.findFetcherByLabel("fetcher.ports", new String[] {labels.get("fetcher.ports") + "x", labels.get("fetcher.ports"), "mmmm"}));
|
||||
|
||||
try {
|
||||
IPListExporter.findFetcherByLabel("fetcher.ip", new String[] {"1", "2"});
|
||||
fail();
|
||||
}
|
||||
catch (ExporterException e) {
|
||||
assertEquals("fetcher.notFound", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.azib.ipscan.config.Version;
|
||||
|
||||
/**
|
||||
* TXT Exporter Test
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class TXTExporterTest extends AbstractExporterTestCase {
|
||||
|
||||
protected Exporter createExporter() {
|
||||
return new TXTExporter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPad() {
|
||||
assertEquals("a ", ((TXTExporter)exporter).pad("a", 1));
|
||||
assertEquals(" ", ((TXTExporter)exporter).pad("", 0));
|
||||
assertEquals("abc ", ((TXTExporter)exporter).pad("abc", 20));
|
||||
assertEquals(" ", ((TXTExporter)exporter).pad(null, 5));
|
||||
assertEquals("5 ", ((TXTExporter)exporter).pad(new Integer(5), 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderWithoutAppend() throws IOException {
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.end();
|
||||
assertContains(Version.FULL_NAME);
|
||||
assertContains(Version.WEBSITE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderWithAppend() throws IOException {
|
||||
exporter.setAppend(true);
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.end();
|
||||
assertNotContains(Version.FULL_NAME);
|
||||
assertNotContains(Version.WEBSITE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithAppend() throws IOException {
|
||||
super.testFetchersWithAppend();
|
||||
assertTrue(((TXTExporter)exporter).padLengths != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithoutAppend() throws IOException {
|
||||
super.testFetchersWithoutAppend();
|
||||
assertTrue(((TXTExporter)exporter).padLengths != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFeederInfo() throws IOException {
|
||||
exporter.start(outputStream, "192.168.1.1 - 192.168.3.255");
|
||||
exporter.end();
|
||||
assertContains("192.168.1.1 - 192.168.3.255");
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,98 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.exporters;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import net.azib.ipscan.config.Version;
|
||||
|
||||
/**
|
||||
* XML Exporter Test
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class XMLExporterTest extends AbstractExporterTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Labels.initialize(Locale.ENGLISH);
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected Exporter createExporter() {
|
||||
return new XMLExporter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderWithoutAppend() throws IOException {
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.end();
|
||||
assertContains(Version.FULL_NAME);
|
||||
assertContains(Version.WEBSITE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithoutAppend() throws IOException {
|
||||
exporter.start(outputStream, "feederstuff");
|
||||
exporter.setFetchers(new String[] {"fetcher1", Labels.getLabel("fetcher.ip"), "mega long fetcher 2"});
|
||||
exporter.nextAdressResults(new Object[] {"", "123", ""});
|
||||
exporter.end();
|
||||
assertContains("IP");
|
||||
assertContains("address=\"123\"");
|
||||
assertContains("fetcher1");
|
||||
assertContains("mega long fetcher 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchersWithAppend() {
|
||||
try {
|
||||
exporter.setAppend(true);
|
||||
fail();
|
||||
}
|
||||
catch (ExporterException e) {
|
||||
assertEquals("xml.noAppend", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFeederInfoWithName() throws IOException {
|
||||
exporter.start(outputStream, "Blah: 192.168.1.1 - 192.168.3.255");
|
||||
exporter.end();
|
||||
assertContains("name=\"Blah\"");
|
||||
assertContains("192.168.1.1 - 192.168.3.255");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFeederInfoNoName() throws IOException {
|
||||
exporter.start(outputStream, "Booga 192.168.1.1/123");
|
||||
exporter.end();
|
||||
assertContains("Booga 192.168.1.1/123");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidXML() throws Exception {
|
||||
exporter.start(outputStream, "<megaInfo'''");
|
||||
exporter.setFetchers(new String[] {"IP", "hello", "fet::cher2"});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost().getHostAddress(), "w?:orld'", new Integer(53)});
|
||||
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost().getHostAddress(), "bug>>a", new Integer(-1)});
|
||||
exporter.end();
|
||||
assertContains("<megaInfo'''");
|
||||
|
||||
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
documentBuilder.parse(new ByteArrayInputStream(outputContent.getBytes(XMLExporter.ENCODING)));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.feeders;
|
||||
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import junit.framework.Assert;
|
||||
|
||||
/**
|
||||
* FeederTestUtils
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class FeederTestUtils {
|
||||
|
||||
public static void assertFeederException(String message, FeederException e) {
|
||||
// assert that the message is correct
|
||||
Assert.assertEquals(e.getMessage(), message);
|
||||
// check that corresponding label exists
|
||||
Assert.assertNotNull(Labels.getLabel("exception.FeederException." + message));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,127 +0,0 @@
|
||||
package net.azib.ipscan.feeders;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static net.azib.ipscan.feeders.FeederTestUtils.*;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test of FileFeeder
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class FileFeederTest {
|
||||
|
||||
@Test
|
||||
public void testHappyPath() throws FeederException {
|
||||
StringReader reader = new StringReader("10.11.12.13 10.11.12.14 10.11.12.15");
|
||||
FileFeeder fileFeeder = new FileFeeder();
|
||||
fileFeeder.initialize(reader);
|
||||
assertTrue(fileFeeder.hasNext());
|
||||
assertEquals("10.11.12.13", fileFeeder.next().getHostAddress());
|
||||
assertTrue(fileFeeder.hasNext());
|
||||
assertEquals("10.11.12.14", fileFeeder.next().getHostAddress());
|
||||
assertTrue(fileFeeder.hasNext());
|
||||
assertEquals("10.11.12.15", fileFeeder.next().getHostAddress());
|
||||
assertFalse(fileFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringParams() {
|
||||
try {
|
||||
FileFeeder fileFeeder = new FileFeeder();
|
||||
assertEquals(1, fileFeeder.initialize(new String[] {"build.xml"}));
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertEquals("file.nothingFound", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoFile() {
|
||||
try {
|
||||
new FileFeeder().initialize("no_such_file.txt");
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("file.notExists", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNothingFound() {
|
||||
try {
|
||||
StringReader reader = new StringReader("no ip addresses here");
|
||||
new FileFeeder().initialize(reader);
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
FeederTestUtils.assertFeederException("file.nothingFound", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtractFromDifferentFormats() {
|
||||
|
||||
assertAddressCount("The 127.0.0.1 is the localhost IP,\n but 192.168.255.255 is probably a broadcast IP", 2);
|
||||
|
||||
assertAddressCount("1.1.1.,1245\n2.2.2.2:123\n3.3.3.3.3.3\n\n\n9.9.9.9999", 2);
|
||||
|
||||
assertAddressCount("1.2.3.4", 1);
|
||||
|
||||
assertAddressCount("1.2.3.4:125\n2.3.4.255:347", 2);
|
||||
|
||||
assertAddressCount("255.255.255.255\n\n\n\t0.0.0.0", 2);
|
||||
|
||||
// This test fails under GCJ, probably it doesn't normalize IP addresses,
|
||||
// passed to the InetAddress and throws UnknownHostException because of the leading zero
|
||||
// assertAddressCount("09.001.005.006", 1);
|
||||
|
||||
assertAddressCount("999.999.999.999,1.1.01.1", 1);
|
||||
|
||||
assertAddressCount("<xml>66.87.99.128</xml>\n<xml>000.87.99.129</xml>0000.1.1.1", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPercentageComplete() throws Exception {
|
||||
StringReader reader = new StringReader("1.2.3.4, 2.3.4.5, mega cool 0.0.0.0");
|
||||
FileFeeder fileFeeder = new FileFeeder();
|
||||
fileFeeder.initialize(reader);
|
||||
assertEquals(0, fileFeeder.getPercentageComplete());
|
||||
fileFeeder.next();
|
||||
assertEquals(33, fileFeeder.getPercentageComplete());
|
||||
fileFeeder.next();
|
||||
assertEquals(67, fileFeeder.getPercentageComplete());
|
||||
fileFeeder.next();
|
||||
assertEquals(100, fileFeeder.getPercentageComplete());
|
||||
|
||||
reader = new StringReader("255.255.255.255");
|
||||
fileFeeder.initialize(reader);
|
||||
assertEquals(0, fileFeeder.getPercentageComplete());
|
||||
fileFeeder.next();
|
||||
assertEquals(100, fileFeeder.getPercentageComplete());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInfo() {
|
||||
FileFeeder fileFeeder = new FileFeeder();
|
||||
StringReader reader = new StringReader("255.255.255.255, 2.3.4.5, mega cool 0.0.0.0");
|
||||
fileFeeder.initialize(reader);
|
||||
assertEquals("3", fileFeeder.getInfo());
|
||||
}
|
||||
|
||||
private void assertAddressCount(String s, int addressCount) {
|
||||
StringReader reader = new StringReader(s);
|
||||
FileFeeder feeder = new FileFeeder();
|
||||
feeder.initialize(reader);
|
||||
int numAddresses = 0;
|
||||
while (feeder.hasNext()) {
|
||||
feeder.next();
|
||||
numAddresses++;
|
||||
}
|
||||
assertEquals(addressCount, numAddresses);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,144 +0,0 @@
|
||||
package net.azib.ipscan.feeders;
|
||||
|
||||
import static net.azib.ipscan.feeders.FeederTestUtils.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test of RandomFeeder
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class RandomFeederTest {
|
||||
|
||||
@Test
|
||||
public void testHappyPath() throws FeederException {
|
||||
RandomFeeder randomFeeder = new RandomFeeder();
|
||||
assertEquals(3, randomFeeder.initialize(new String[] {"255.255.255.255", "255...0", "2"}));
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
assertTrue(randomFeeder.next().getHostAddress().startsWith("255.255.255"));
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
assertTrue(randomFeeder.next().getHostAddress().startsWith("255.255.255"));
|
||||
assertFalse(randomFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidCount() {
|
||||
try {
|
||||
new RandomFeeder().initialize(new String[] {"1.1.1.1", "2.2.2.2", "abc"});
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("random.invalidCount", e);
|
||||
}
|
||||
try {
|
||||
new RandomFeeder().initialize("1.1.1.1", "1.1.1.1", 0);
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("random.invalidCount", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMalformedIP() {
|
||||
try {
|
||||
new RandomFeeder().initialize("abc", "10.11.12.10", 1);
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("malformedIP", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidNetmask() {
|
||||
try {
|
||||
new RandomFeeder().initialize("1.1.1.1", "invalid", 1);
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("invalidNetmask", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullMask() {
|
||||
RandomFeeder randomFeeder = null;
|
||||
randomFeeder = new RandomFeeder();
|
||||
randomFeeder.initialize("1.2.3.45", "255.255.255.255", 1);
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
assertEquals("1.2.3.45", randomFeeder.next().getHostAddress());
|
||||
assertFalse(randomFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyMask() {
|
||||
RandomFeeder randomFeeder = null;
|
||||
randomFeeder = new RandomFeeder();
|
||||
randomFeeder.initialize("1.2.3.45", "0.0.0.0", 1);
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
assertFalse("1.2.3.45".equals(randomFeeder.next().getHostAddress()));
|
||||
assertFalse(randomFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaskStartEnd() {
|
||||
RandomFeeder randomFeeder = null;
|
||||
randomFeeder = new RandomFeeder();
|
||||
randomFeeder.initialize("1.2.3.45", "255.0.0.255", 1);
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
String address = randomFeeder.next().getHostAddress();
|
||||
assertTrue(address.startsWith("1."));
|
||||
assertTrue(address.endsWith(".45"));
|
||||
assertFalse(randomFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferent() {
|
||||
RandomFeeder randomFeeder = null;
|
||||
String address = null;
|
||||
|
||||
randomFeeder = new RandomFeeder();
|
||||
randomFeeder.initialize("1.2.3.45", "255.0.255.0", 2);
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
address = randomFeeder.next().getHostAddress();
|
||||
assertFalse("1.2.3.45".equals(address));
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
assertFalse(address.equals(randomFeeder.next().getHostAddress()));
|
||||
assertFalse(randomFeeder.hasNext());
|
||||
|
||||
randomFeeder.initialize("1.2.3.45", "255.0.127.0", 1);
|
||||
assertTrue(randomFeeder.hasNext());
|
||||
assertFalse(address.equals(randomFeeder.next().getHostAddress()));
|
||||
assertFalse(randomFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPercentageComplete() throws Exception {
|
||||
RandomFeeder randomFeeder = new RandomFeeder();
|
||||
randomFeeder.initialize("100.11.12.13", "100.11.12.15", 3);
|
||||
assertEquals(0, randomFeeder.getPercentageComplete());
|
||||
randomFeeder.next();
|
||||
assertEquals(33, randomFeeder.getPercentageComplete());
|
||||
randomFeeder.next();
|
||||
assertEquals(67, randomFeeder.getPercentageComplete());
|
||||
randomFeeder.next();
|
||||
assertEquals(100, randomFeeder.getPercentageComplete());
|
||||
|
||||
randomFeeder.initialize("255.255.255.255", "255.255.255.255", 1);
|
||||
assertEquals(0, randomFeeder.getPercentageComplete());
|
||||
randomFeeder.next();
|
||||
assertEquals(100, randomFeeder.getPercentageComplete());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInfo() {
|
||||
RandomFeeder randomFeeder = new RandomFeeder();
|
||||
randomFeeder.initialize("100.11.12.13", "100.11.12.15", 3);
|
||||
assertEquals("100.11.12.13 / 100.11.12.15: 3", randomFeeder.getInfo());
|
||||
randomFeeder.initialize("0.0.0.0", "255.255.255.255", 129876);
|
||||
assertEquals("0.0.0.0 / 255.255.255.255: 129876", randomFeeder.getInfo());
|
||||
}
|
||||
}
|
||||
@ -1,99 +0,0 @@
|
||||
package net.azib.ipscan.feeders;
|
||||
|
||||
import static net.azib.ipscan.feeders.FeederTestUtils.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test of RangeFeeder
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class RangeFeederTest {
|
||||
|
||||
@Test
|
||||
public void testHappyPath() throws FeederException {
|
||||
RangeFeeder rangeFeeder = new RangeFeeder();
|
||||
assertEquals(2, rangeFeeder.initialize(new String[] {"10.11.12.13", "10.11.12.15"}));
|
||||
assertTrue(rangeFeeder.hasNext());
|
||||
assertEquals("10.11.12.13", rangeFeeder.next().getHostAddress());
|
||||
assertTrue(rangeFeeder.hasNext());
|
||||
assertEquals("10.11.12.14", rangeFeeder.next().getHostAddress());
|
||||
assertTrue(rangeFeeder.hasNext());
|
||||
assertEquals("10.11.12.15", rangeFeeder.next().getHostAddress());
|
||||
assertFalse(rangeFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidRange() {
|
||||
try {
|
||||
new RangeFeeder().initialize("10.11.12.13", "10.11.12.10");
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("range.greaterThan", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMalformedIP() {
|
||||
try {
|
||||
new RangeFeeder().initialize("10.11.12.abc", "10.11.12.10");
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("malformedIP", e);
|
||||
}
|
||||
try {
|
||||
new RangeFeeder().initialize("10.11.12.1", "ziga");
|
||||
fail();
|
||||
}
|
||||
catch (FeederException e) {
|
||||
assertFeederException("malformedIP", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtremeValues() {
|
||||
RangeFeeder rangeFeeder = null;
|
||||
|
||||
rangeFeeder = new RangeFeeder();
|
||||
rangeFeeder.initialize("0.0.0.0", "0.0.0.0");
|
||||
assertTrue(rangeFeeder.hasNext());
|
||||
assertEquals("0.0.0.0", rangeFeeder.next().getHostAddress());
|
||||
assertFalse(rangeFeeder.hasNext());
|
||||
|
||||
rangeFeeder.initialize("255.255.255.255", "255.255.255.255");
|
||||
assertTrue(rangeFeeder.hasNext());
|
||||
assertEquals("255.255.255.255", rangeFeeder.next().getHostAddress());
|
||||
assertFalse(rangeFeeder.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPercentageComplete() throws Exception {
|
||||
RangeFeeder rangeFeeder = new RangeFeeder();
|
||||
rangeFeeder.initialize("100.11.12.13", "100.11.12.15");
|
||||
assertEquals(0, rangeFeeder.getPercentageComplete());
|
||||
rangeFeeder.next();
|
||||
assertEquals(33, rangeFeeder.getPercentageComplete());
|
||||
rangeFeeder.next();
|
||||
assertEquals(67, rangeFeeder.getPercentageComplete());
|
||||
rangeFeeder.next();
|
||||
assertEquals(100, rangeFeeder.getPercentageComplete());
|
||||
|
||||
rangeFeeder.initialize("255.255.255.255", "255.255.255.255");
|
||||
assertEquals(0, rangeFeeder.getPercentageComplete());
|
||||
rangeFeeder.next();
|
||||
assertEquals(100, rangeFeeder.getPercentageComplete());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInfo() {
|
||||
RangeFeeder rangeFeeder = new RangeFeeder();
|
||||
rangeFeeder.initialize("100.11.12.13", "100.11.12.13");
|
||||
assertEquals("100.11.12.13 - 100.11.12.13", rangeFeeder.getInfo());
|
||||
rangeFeeder.initialize("0.0.0.0", "255.255.255.255");
|
||||
assertEquals("0.0.0.0 - 255.255.255.255", rangeFeeder.getInfo());
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import net.azib.ipscan.config.Config;
|
||||
import net.azib.ipscan.config.Labels;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* TestCase for Fetchers.
|
||||
* It contains initialization and generic tests for any Fetcher.
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public abstract class AbstractFetcherTestCase {
|
||||
|
||||
Fetcher fetcher;
|
||||
|
||||
@BeforeClass
|
||||
public static void globalSetUp() {
|
||||
// some fetchers are Configurable and therefore need an initialized Config
|
||||
Config.initialize();
|
||||
}
|
||||
|
||||
@Before
|
||||
public abstract void setUp() throws Exception;
|
||||
|
||||
@Test
|
||||
public void testLabel() {
|
||||
assertNotNull(Labels.getLabel(fetcher.getLabel()));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,107 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class FetcherRegistryImplTest {
|
||||
|
||||
private Preferences preferences;
|
||||
|
||||
private Fetcher ipFetcher;
|
||||
private HostnameFetcher hostnameFetcher;
|
||||
private CommentFetcher commentFetcher;
|
||||
private FetcherRegistry fetcherRegistry;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
preferences = Preferences.userRoot().node("ipscan-test");
|
||||
preferences.clear();
|
||||
|
||||
ipFetcher = new IPFetcher();
|
||||
hostnameFetcher = new HostnameFetcher();
|
||||
commentFetcher = new CommentFetcher();
|
||||
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
preferences.removeNode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() throws Exception {
|
||||
assertEquals(3, fetcherRegistry.getRegisteredFetchers().size());
|
||||
assertEquals(3, fetcherRegistry.getSelectedFetchers().size());
|
||||
}
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void testModifyRegisteredFetchers() throws Exception {
|
||||
fetcherRegistry.getRegisteredFetchers().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSelectedFetcherIndex() throws Exception {
|
||||
assertEquals(0, fetcherRegistry.getSelectedFetcherIndex(ipFetcher.getLabel()));
|
||||
assertEquals(1, fetcherRegistry.getSelectedFetcherIndex(hostnameFetcher.getLabel()));
|
||||
assertEquals(2, fetcherRegistry.getSelectedFetcherIndex(commentFetcher.getLabel()));
|
||||
assertEquals(-1, fetcherRegistry.getSelectedFetcherIndex("blah-blah"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadPreferences() throws Exception {
|
||||
preferences.remove(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS);
|
||||
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
|
||||
assertEquals(3, fetcherRegistry.getSelectedFetchers().size());
|
||||
|
||||
preferences.put(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS, hostnameFetcher.getLabel() + "###" + commentFetcher.getLabel());
|
||||
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
|
||||
assertEquals(2, fetcherRegistry.getSelectedFetchers().size());
|
||||
Iterator<?> iterator = fetcherRegistry.getSelectedFetchers().iterator();
|
||||
assertSame(hostnameFetcher, iterator.next());
|
||||
assertSame(commentFetcher, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateSelectedFetchers() throws Exception {
|
||||
// retain only one selected fetcher
|
||||
fetcherRegistry.updateSelectedFetchers(new String[] {ipFetcher.getLabel()});
|
||||
assertEquals(1, fetcherRegistry.getSelectedFetchers().size());
|
||||
Iterator<?> iterator = fetcherRegistry.getSelectedFetchers().iterator();
|
||||
assertEquals(ipFetcher.getLabel(), ((Fetcher)iterator.next()).getLabel());
|
||||
assertEquals(ipFetcher.getLabel(), preferences.get(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS, null));
|
||||
|
||||
// now return a fetcher back
|
||||
fetcherRegistry.updateSelectedFetchers(new String[] {commentFetcher.getLabel(), ipFetcher.getLabel()});
|
||||
assertEquals(2, fetcherRegistry.getSelectedFetchers().size());
|
||||
iterator = fetcherRegistry.getSelectedFetchers().iterator();
|
||||
assertEquals(commentFetcher.getLabel(), ((Fetcher)iterator.next()).getLabel());
|
||||
assertEquals(ipFetcher.getLabel(), ((Fetcher)iterator.next()).getLabel());
|
||||
assertEquals(commentFetcher.getLabel() + "###" + ipFetcher.getLabel(), preferences.get(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListener() throws Exception {
|
||||
final boolean listenerWasCalled[] = {false};
|
||||
fetcherRegistry.addListener(new FetcherRegistryUpdateListener() {
|
||||
public void handleUpdateOfSelectedFetchers(FetcherRegistry fetcherRegistry) {
|
||||
assertSame(FetcherRegistryImplTest.this.fetcherRegistry, fetcherRegistry);
|
||||
assertEquals(0, fetcherRegistry.getSelectedFetchers().size());
|
||||
listenerWasCalled[0] = true;
|
||||
}
|
||||
});
|
||||
fetcherRegistry.updateSelectedFetchers(new String[] {});
|
||||
assertTrue(listenerWasCalled[0]);
|
||||
}
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.azib.ipscan.core.ScanningSubject;
|
||||
|
||||
/**
|
||||
* HostnameFetcherTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class HostnameFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new HostnameFetcher();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScan() throws UnknownHostException {
|
||||
// Some of these tests are run inside of if's to prevent their failing on certain network configurations
|
||||
if (!InetAddress.getLocalHost().getCanonicalHostName().equals(InetAddress.getLocalHost().getHostAddress()))
|
||||
assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), fetcher.scan(new ScanningSubject(InetAddress.getLocalHost())));
|
||||
InetAddress wwweeAddress = InetAddress.getByName("194.204.33.30");
|
||||
if (!wwweeAddress.equals("194.204.33.30"))
|
||||
assertEquals(wwweeAddress.getCanonicalHostName(), fetcher.scan(new ScanningSubject(wwweeAddress)));
|
||||
assertNull(fetcher.scan(new ScanningSubject(InetAddress.getByName("255.255.255.255"))));
|
||||
assertNull(fetcher.scan(new ScanningSubject(InetAddress.getByName("172.13.66.254"))));
|
||||
}
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.azib.ipscan.core.ScanningSubject;
|
||||
|
||||
/**
|
||||
* IPFetcherTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class IPFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new IPFetcher();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScan() throws UnknownHostException {
|
||||
assertEquals(InetAddress.getLocalHost().getHostAddress(), fetcher.scan(new ScanningSubject(InetAddress.getLocalHost())));
|
||||
assertEquals("255.255.255.255", fetcher.scan(new ScanningSubject(InetAddress.getByName("255.255.255.255"))));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import net.azib.ipscan.config.Config;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
/**
|
||||
* PingFetcherTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class PingFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new PingFetcher(null, Config.getGlobal());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import net.azib.ipscan.config.Config;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
/**
|
||||
* PingTTLFetcherTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class PingTTLFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new PingTTLFetcher(null, Config.getGlobal());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.fetchers;
|
||||
|
||||
import net.azib.ipscan.config.Config;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
/**
|
||||
* PortsFetcherTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class PortsFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new PortsFetcher(Config.getGlobal());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.gui;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
import net.azib.ipscan.fetchers.FetcherRegistry;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Anton Keks
|
||||
*
|
||||
*/
|
||||
public class SelectFetchersDialogTest {
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void testSaveFetchersToRegistry() {
|
||||
FetcherRegistry fetcherRegistry = createMock(FetcherRegistry.class);
|
||||
fetcherRegistry.updateSelectedFetchers(aryEq(new String[] {"fetcher.ip", "fetcher.blah", "fetcher.hello"}));
|
||||
replay(fetcherRegistry);
|
||||
|
||||
SelectFetchersDialog selectFetchersDialog = new SelectFetchersDialog(fetcherRegistry);
|
||||
|
||||
selectFetchersDialog.registeredFetcherLabelsByNames.put("IP", "fetcher.ip");
|
||||
selectFetchersDialog.registeredFetcherLabelsByNames.put("Hello", "fetcher.hello");
|
||||
selectFetchersDialog.registeredFetcherLabelsByNames.put("Blah", "fetcher.blah");
|
||||
|
||||
selectFetchersDialog.saveFetchersToRegistry(new String[] {"Blah", "Hello"});
|
||||
|
||||
verify(fetcherRegistry);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,76 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.gui.actions;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import net.azib.ipscan.config.Config;
|
||||
import net.azib.ipscan.config.Labels;
|
||||
import net.azib.ipscan.fetchers.FetcherRegistry;
|
||||
import net.azib.ipscan.gui.UserErrorException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* OpenerLauncherTest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class OpenerLauncherTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Config.initialize();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceValues() {
|
||||
FetcherRegistry fetcherRegistry = createMock(FetcherRegistry.class);
|
||||
expect(fetcherRegistry.getSelectedFetcherIndex("fetcher.ip")).andReturn(0).times(3);
|
||||
expect(fetcherRegistry.getSelectedFetcherIndex("fetcher.hostname")).andReturn(1);
|
||||
expect(fetcherRegistry.getSelectedFetcherIndex("fetcher.comment")).andReturn(2);
|
||||
expect(fetcherRegistry.getSelectedFetcherIndex("noSuchFetcher")).andReturn(-1);
|
||||
replay(fetcherRegistry);
|
||||
|
||||
OpenerLauncher ol = new OpenerLauncher(fetcherRegistry, null) {
|
||||
String getScannedValue(int selectedItem, int fetcherIndex) {
|
||||
switch (fetcherIndex) {
|
||||
case 0:
|
||||
return "127.0.0.1";
|
||||
case 1:
|
||||
return "HOSTNAME";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
assertEquals("\\\\127.0.0.1", ol.prepareOpenerStringForItem("\\\\${fetcher.ip}", 0));
|
||||
assertEquals("HOSTNAME$$$127.0.0.1xxx${}", ol.prepareOpenerStringForItem("${fetcher.hostname}$$$${fetcher.ip}xxx${}", 0));
|
||||
assertEquals("http://127.0.0.1:80/www", ol.prepareOpenerStringForItem("http://${fetcher.ip}:80/www", 0));
|
||||
|
||||
try {
|
||||
ol.prepareOpenerStringForItem("${noSuchFetcher}", 0);
|
||||
fail();
|
||||
}
|
||||
catch (UserErrorException e) {
|
||||
assertEquals(Labels.getLabel("exception.UserErrorException.opener.unknownFetcher") + "noSuchFetcher", e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
ol.prepareOpenerStringForItem("${fetcher.comment}", 0);
|
||||
fail();
|
||||
}
|
||||
catch (UserErrorException e) {
|
||||
assertEquals(Labels.getLabel("exception.UserErrorException.opener.nullFetcherValue") + "fetcher.comment", e.getMessage());
|
||||
}
|
||||
|
||||
verify(fetcherRegistry);
|
||||
}
|
||||
}
|
||||
@ -1,63 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.azib.ipscan.gui.feeders;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
|
||||
import net.azib.ipscan.feeders.Feeder;
|
||||
import net.azib.ipscan.feeders.RangeFeeder;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* AbstractFeederGUITest
|
||||
*
|
||||
* @author anton
|
||||
*/
|
||||
public class AbstractFeederGUITest {
|
||||
|
||||
private AbstractFeederGUI feederGUI;
|
||||
|
||||
@BeforeClass
|
||||
public static void globalSetUp() {
|
||||
System.setProperty("java.library.path", "../swt/lib");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
feederGUI = new AbstractFeederGUI(new Shell()) {
|
||||
|
||||
protected void initialize() {
|
||||
}
|
||||
|
||||
public String getFeederName() {
|
||||
return "Mega Feeder";
|
||||
}
|
||||
|
||||
public Feeder getFeeder() {
|
||||
feeder = new RangeFeeder();
|
||||
feeder.initialize(new String[] {"127.0.0.1", "127.0.0.2"});
|
||||
return feeder;
|
||||
}
|
||||
|
||||
public String serialize() {
|
||||
return "";
|
||||
}
|
||||
|
||||
public void unserialize(String serialized) {
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInfo() {
|
||||
assertEquals("Mega Feeder: 127.0.0.1 - 127.0.0.2", feederGUI.getInfo());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user