* value objects now have nicer names

* NumericRangeList is now properly comparable

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@342 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2008-03-30 19:09:34 +00:00
parent a9ae556166
commit 84d14762c5
19 changed files with 124 additions and 105 deletions

2
TODO
View File

@ -1,8 +1,6 @@
Before 3.0:
* NAV and NSV -> enum
* rename abortScanning() to something else
* getLabel() vs getId() + getName() in plugins
* command-line scanning start
* fetcher-specific options
* multiple port support web-detect

View File

@ -7,8 +7,8 @@ package net.azib.ipscan.core;
import java.net.InetAddress;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
@ -41,14 +41,14 @@ public class Scanner {
int fetcherIndex = 0;
boolean isScanningInterrupted = false;
for (Fetcher fetcher : fetcherRegistry.getSelectedFetchers()) {
Object value = NotScannedValue.INSTANCE;
Object value = NotScanned.VALUE;
if (!scanningSubject.isAddressScanningAborted() && !isScanningInterrupted) {
// run the fetcher
value = fetcher.scan(scanningSubject);
// check if scanning was interrupted
isScanningInterrupted = Thread.currentThread().isInterrupted();
if (value == null)
value = isScanningInterrupted ? NotScannedValue.INSTANCE : NotAvailableValue.INSTANCE;
value = isScanningInterrupted ? NotScanned.VALUE : NotAvailable.VALUE;
}
// store the value
result.setValue(fetcherIndex, value);

View File

@ -13,17 +13,17 @@ import java.net.InetAddress;
*
* @author Anton Keks
*/
public class InetAddressValue implements Comparable<InetAddressValue> {
public class InetAddressHolder implements Comparable<InetAddressHolder> {
private String s;
private byte[] a;
public InetAddressValue(InetAddress address) {
public InetAddressHolder(InetAddress address) {
s = address.getHostAddress();
a = address.getAddress();
}
public int compareTo(InetAddressValue that) {
public int compareTo(InetAddressHolder that) {
byte[] b1 = this.a;
byte[] b2 = that.a;
@ -59,7 +59,7 @@ public class InetAddressValue implements Comparable<InetAddressValue> {
return false;
if (getClass() != obj.getClass())
return false;
final InetAddressValue other = (InetAddressValue) obj;
final InetAddressHolder other = (InetAddressHolder) obj;
if (s == null) {
if (other.s != null)
return false;

View File

@ -11,11 +11,11 @@ import net.azib.ipscan.config.Config;
*
* @author Anton Keks
*/
public class NotAvailableValue implements Comparable<Object> {
public class NotAvailable implements Comparable<Object> {
public static final NotAvailableValue INSTANCE = new NotAvailableValue();
public static final NotAvailable VALUE = new NotAvailable();
private NotAvailableValue() {}
private NotAvailable() {}
/**
* Displays a user-friendly text string :-)

View File

@ -11,11 +11,11 @@ import net.azib.ipscan.config.Config;
*
* @author Anton Keks
*/
public class NotScannedValue implements Comparable<Object> {
public class NotScanned implements Comparable<Object> {
public static final NotScannedValue INSTANCE = new NotScannedValue();
public static final NotScanned VALUE = new NotScanned();
private NotScannedValue() {}
private NotScanned() {}
/**
* Displays a user-friendly text string :-)

View File

@ -16,7 +16,7 @@ import java.util.Collection;
*
* @author Anton Keks
*/
public class NumericListValue {
public class NumericRangeList implements Comparable<NumericRangeList> {
private static final long serialVersionUID = 1L;
@ -28,7 +28,7 @@ public class NumericListValue {
* @param numbers Collections of Numbers (must be sorted for ranges to work)
* @param displayAsRanges whether toString() outputs all number or their ranges
*/
public NumericListValue(Collection<Integer> numbers, boolean displayAsRanges) {
public NumericRangeList(Collection<Integer> numbers, boolean displayAsRanges) {
// copy numbers to an array (unfortunately toArray() cannot be used because int[] is not an Object[])
this.numbers = new int[numbers.size()];
int c = 0;
@ -81,4 +81,16 @@ public class NumericListValue {
return sb.toString();
}
public int compareTo(NumericRangeList that) {
// compare length
int result = this.numbers.length - that.numbers.length;
if (result == 0) {
// compare contents if length is the same
for (int i = 0; i < this.numbers.length && result == 0; i++) {
result = this.numbers[i] - that.numbers[i];
}
}
return result;
}
}

View File

@ -6,8 +6,8 @@ package net.azib.ipscan.fetchers;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.plugins.Pluggable;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
/**
* Interface of all IP Fetchers.
@ -41,7 +41,7 @@ public interface Fetcher extends Cloneable, Pluggable {
* Does the actual fetching.
* @param subject the scanning subject, containing an IP address
* @return the fetched data (a String in most cases), null in case of any error.
* Special values may also be returned, such as {@link NotAvailableValue} or {@link NotScannedValue}
* Special values may also be returned, such as {@link NotAvailable} or {@link NotScanned}
*/
public Object scan(ScanningSubject subject);

View File

@ -9,8 +9,8 @@ import java.util.SortedSet;
import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.core.values.NumericListValue;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.core.values.NumericRangeList;
/**
* FilteredPortsFetcher uses the scanning results of PortsFetcher to display filtered ports.
@ -30,9 +30,9 @@ public class FilteredPortsFetcher extends PortsFetcher {
public Object scan(ScanningSubject subject) {
boolean portsScanned = scanPorts(subject);
if (!portsScanned)
return NotScannedValue.INSTANCE;
return NotScanned.VALUE;
SortedSet<Integer> filteredPorts = getFilteredPorts(subject);
return filteredPorts.size() > 0 ? new NumericListValue(filteredPorts, displayAsRanges) : null;
return filteredPorts.size() > 0 ? new NumericRangeList(filteredPorts, displayAsRanges) : null;
}
}

View File

@ -5,7 +5,7 @@
package net.azib.ipscan.fetchers;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.values.InetAddressValue;
import net.azib.ipscan.core.values.InetAddressHolder;
/**
* Dummy fetcher, which is able to return the textual representation
@ -22,7 +22,7 @@ public class IPFetcher extends AbstractFetcher {
}
public Object scan(ScanningSubject subject) {
return new InetAddressValue(subject.getAddress());
return new InetAddressHolder(subject.getAddress());
}
}

View File

@ -17,9 +17,9 @@ import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.core.PortIterator;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.ScanningResult.ResultType;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.core.values.NumericListValue;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.core.values.NumericRangeList;
/**
* PortsFetcher scans TCP ports.
@ -51,7 +51,7 @@ public class PortsFetcher extends AbstractFetcher {
@Override
public String getFullName() {
int numPorts = new PortIterator(config.portString).size();
return getName() + " " + (numPorts > 0 ? "[" + numPorts + "]" : NotAvailableValue.INSTANCE);
return getName() + " " + (numPorts > 0 ? "[" + numPorts + "]" : NotAvailable.VALUE);
}
/**
@ -142,14 +142,14 @@ public class PortsFetcher extends AbstractFetcher {
public Object scan(ScanningSubject subject) {
boolean portsScanned = scanPorts(subject);
if (!portsScanned)
return NotScannedValue.INSTANCE;
return NotScanned.VALUE;
SortedSet<Integer> openPorts = getOpenPorts(subject);
boolean portsFound = openPorts.size() > 0;
if (portsFound) {
subject.setResultType(ResultType.WITH_PORTS);
}
return portsFound ? new NumericListValue(openPorts, displayAsRanges) : null;
return portsFound ? new NumericRangeList(openPorts, displayAsRanges) : null;
}
public void init() {

View File

@ -18,8 +18,8 @@ import java.util.HashSet;
import java.util.Set;
import net.azib.ipscan.core.ScanningResult.ResultType;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.fetchers.AbstractFetcher;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
@ -66,9 +66,9 @@ public class ScannerTest {
assertEquals(InetAddress.getLocalHost(), scanningResult.getAddress());
assertEquals(4, scanningResult.getValues().size());
assertEquals("blah", scanningResult.getValues().get(0));
assertEquals(NotAvailableValue.INSTANCE, scanningResult.getValues().get(1));
assertEquals(NotAvailable.VALUE, scanningResult.getValues().get(1));
assertEquals("666 ms", scanningResult.getValues().get(2));
assertEquals(NotScannedValue.INSTANCE, scanningResult.getValues().get(3));
assertEquals(NotScanned.VALUE, scanningResult.getValues().get(3));
}
@Test
@ -88,8 +88,8 @@ public class ScannerTest {
assertEquals(InetAddress.getLocalHost(), scanningResult.getAddress());
assertEquals(3, scanningResult.getValues().size());
assertEquals("plainValue", scanningResult.getValues().get(0));
assertEquals(NotScannedValue.INSTANCE, scanningResult.getValues().get(1));
assertEquals(NotScannedValue.INSTANCE, scanningResult.getValues().get(2));
assertEquals(NotScanned.VALUE, scanningResult.getValues().get(1));
assertEquals(NotScanned.VALUE, scanningResult.getValues().get(2));
// reset interrupted flag
assertTrue(Thread.interrupted());

View File

@ -26,7 +26,7 @@ import net.azib.ipscan.core.ScanningResultList.ScanInfo;
import net.azib.ipscan.core.ScanningResultList.StopScanningListener;
import net.azib.ipscan.core.state.ScanningState;
import net.azib.ipscan.core.state.StateMachine;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.feeders.Feeder;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
@ -310,7 +310,7 @@ public class ScanningResultListTest {
@Test
public void testFindText() throws Exception {
scanningResults.registerAtIndex(0, scanningResults.createResult(InetAddress.getByName("127.9.9.1")));
scanningResults.getResult(0).setValue(1, NotScannedValue.INSTANCE);
scanningResults.getResult(0).setValue(1, NotScanned.VALUE);
scanningResults.registerAtIndex(1, scanningResults.createResult(InetAddress.getByName("127.9.9.2")));
scanningResults.getResult(1).setValue(1, new Long(123456789L));
scanningResults.registerAtIndex(2, scanningResults.createResult(InetAddress.getByName("127.9.9.3")));

View File

@ -17,19 +17,19 @@ import org.junit.Test;
*
* @author Anton Keks
*/
public class InetAddressValueTest {
public class InetAddressHolderTest {
@Test
public void testToString() throws Exception {
InetAddressValue av = new InetAddressValue(InetAddress.getLocalHost());
InetAddressHolder av = new InetAddressHolder(InetAddress.getLocalHost());
assertEquals(InetAddress.getLocalHost().getHostAddress(), av.toString());
}
@Test
public void testCompareTo() throws Exception {
InetAddressValue av2 = new InetAddressValue(InetAddress.getByName("192.168.0.2"));
InetAddressValue av10 = new InetAddressValue(InetAddress.getByName("192.168.0.10"));
InetAddressValue av127 = new InetAddressValue(InetAddress.getByName("192.168.0.127"));
InetAddressValue av253 = new InetAddressValue(InetAddress.getByName("192.168.0.253"));
InetAddressHolder av2 = new InetAddressHolder(InetAddress.getByName("192.168.0.2"));
InetAddressHolder av10 = new InetAddressHolder(InetAddress.getByName("192.168.0.10"));
InetAddressHolder av127 = new InetAddressHolder(InetAddress.getByName("192.168.0.127"));
InetAddressHolder av253 = new InetAddressHolder(InetAddress.getByName("192.168.0.253"));
assertEquals(-1, av2.compareTo(av10));
assertEquals(1, av10.compareTo(av2));
assertEquals(0, av2.compareTo(av2));
@ -42,8 +42,8 @@ public class InetAddressValueTest {
@Test
public void testEqualsHashCode() throws Exception {
InetAddressValue av1 = new InetAddressValue(InetAddress.getByName("192.168.0.2"));
InetAddressValue av2 = new InetAddressValue(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, 0, 2}));
InetAddressHolder av1 = new InetAddressHolder(InetAddress.getByName("192.168.0.2"));
InetAddressHolder av2 = new InetAddressHolder(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, 0, 2}));
assertEquals(av1, av2);
assertEquals(av1.hashCode(), av2.hashCode());
assertFalse(av1.equals(null));

View File

@ -14,23 +14,23 @@ import org.junit.Test;
*
* @author Anton Keks
*/
public class NotAvailableValueTest {
public class NotAvailableTest {
@Test
public void testEquals() throws Exception {
assertEquals(NotAvailableValue.INSTANCE, NotAvailableValue.INSTANCE);
assertEquals(NotAvailable.VALUE, NotAvailable.VALUE);
}
@Test
public void testToString() throws Exception {
assertEquals(Config.getConfig().forScanner().notAvailableText, NotAvailableValue.INSTANCE.toString());
assertEquals(Config.getConfig().forScanner().notAvailableText, NotAvailable.VALUE.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));
assertTrue(Comparable.class.isAssignableFrom(NotAvailable.class));
assertEquals(0, NotAvailable.VALUE.compareTo(NotAvailable.VALUE));
assertEquals(-1, NotAvailable.VALUE.compareTo("Hello"));
assertEquals(1, NotAvailable.VALUE.compareTo(null));
}
}

View File

@ -14,24 +14,24 @@ import org.junit.Test;
*
* @author Anton Keks
*/
public class NotScannedValueTest {
public class NotScannedTest {
@Test
public void testEquals() throws Exception {
assertEquals(NotScannedValue.INSTANCE, NotScannedValue.INSTANCE);
assertEquals(NotScanned.VALUE, NotScanned.VALUE);
}
@Test
public void testToString() throws Exception {
assertEquals(Config.getConfig().forScanner().notScannedText, NotScannedValue.INSTANCE.toString());
assertEquals(Config.getConfig().forScanner().notScannedText, NotScanned.VALUE.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));
assertTrue(Comparable.class.isAssignableFrom(NotScanned.class));
assertEquals(0, NotScanned.VALUE.compareTo(NotScanned.VALUE));
assertEquals(-1, NotScanned.VALUE.compareTo("Hello"));
assertEquals(1, NotScanned.VALUE.compareTo(null));
}
}

View File

@ -1,33 +0,0 @@
/**
* This file is a part of Angry IP Scanner source code,
* see http://www.azib.net/ for more information.
* Licensed under GPLv2.
*/
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.<Integer>emptyList(), true).toString());
assertEquals("1", new NumericListValue(Arrays.asList(1), true).toString());
assertEquals("1,2", new NumericListValue(Arrays.asList(1, 2), true).toString());
assertEquals("1-3", new NumericListValue(Arrays.asList(1, 2, 3), true).toString());
assertEquals("1-3", new NumericListValue(new TreeSet<Integer>(Arrays.asList(2, 3, 1)), true).toString());
assertEquals("1,2,3", new NumericListValue(Arrays.asList(1, 2, 3), false).toString());
assertEquals("1,5,6,15", new NumericListValue(Arrays.asList(1, 5, 6, 15), true).toString());
assertEquals("1,5-8,15", new NumericListValue(Arrays.asList(1, 5, 6, 7, 8, 15), true).toString());
assertEquals("103,85,89,1", new NumericListValue(Arrays.asList(103, 85, 89, 1), true).toString());
}
}

View File

@ -0,0 +1,42 @@
/**
* This file is a part of Angry IP Scanner source code,
* see http://www.azib.net/ for more information.
* Licensed under GPLv2.
*/
package net.azib.ipscan.core.values;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.TreeSet;
import org.junit.Test;
/**
* @author Anton Keks
*/
public class NumericRangeListTest {
@Test
public void testToString() {
assertEquals("", new NumericRangeList(Collections.<Integer>emptyList(), true).toString());
assertEquals("1", new NumericRangeList(Arrays.asList(1), true).toString());
assertEquals("1,2", new NumericRangeList(Arrays.asList(1, 2), true).toString());
assertEquals("1-3", new NumericRangeList(Arrays.asList(1, 2, 3), true).toString());
assertEquals("1-3", new NumericRangeList(new TreeSet<Integer>(Arrays.asList(2, 3, 1)), true).toString());
assertEquals("1,2,3", new NumericRangeList(Arrays.asList(1, 2, 3), false).toString());
assertEquals("1,5,6,15", new NumericRangeList(Arrays.asList(1, 5, 6, 15), true).toString());
assertEquals("1,5-8,15", new NumericRangeList(Arrays.asList(1, 5, 6, 7, 8, 15), true).toString());
assertEquals("103,85,89,1", new NumericRangeList(Arrays.asList(103, 85, 89, 1), true).toString());
}
@Test
public void testCompateTo() throws Exception {
assertTrue(new NumericRangeList(Arrays.asList(22), false).compareTo(new NumericRangeList(Arrays.asList(80), false)) < 0);
assertTrue(new NumericRangeList(Arrays.asList(80), false).compareTo(new NumericRangeList(Arrays.asList(22), false)) > 0);
assertTrue(new NumericRangeList(Arrays.asList(255), false).compareTo(new NumericRangeList(Arrays.asList(255), false)) == 0);
assertTrue(new NumericRangeList(Arrays.asList(1, 2), false).compareTo(new NumericRangeList(Arrays.asList(8080), false)) > 0);
assertTrue(new NumericRangeList(Arrays.asList(22, 25, 27, 28), false).compareTo(new NumericRangeList(Arrays.asList(22, 25, 26, 300), false)) > 0);
}
}

View File

@ -13,8 +13,8 @@ import java.net.Socket;
import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NumericListValue;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NumericRangeList;
import org.junit.Before;
import org.junit.Test;
@ -37,7 +37,7 @@ public class PortsFetcherTest extends AbstractFetcherTestCase {
@Test
public void numberOfPortsInFullName() throws Exception {
config.portString = "";
assertEquals(fetcher.getName() + " " + NotAvailableValue.INSTANCE, fetcher.getFullName());
assertEquals(fetcher.getName() + " " + NotAvailable.VALUE, fetcher.getFullName());
config.portString = "1-3";
assertEquals(fetcher.getName() + " [3]", fetcher.getFullName());
@ -107,7 +107,7 @@ public class PortsFetcherTest extends AbstractFetcherTestCase {
synchronized (server) {
server.wait();
}
NumericListValue value = (NumericListValue) fetcher.scan(new ScanningSubject(InetAddress.getLocalHost()));
NumericRangeList value = (NumericRangeList) fetcher.scan(new ScanningSubject(InetAddress.getLocalHost()));
assertEquals(config.portString, value.toString());
fetcher.cleanup();

View File

@ -18,8 +18,8 @@ import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.ScanningResult;
import net.azib.ipscan.core.ScanningResultList;
import net.azib.ipscan.core.UserErrorException;
import net.azib.ipscan.core.values.InetAddressValue;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.InetAddressHolder;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.feeders.Feeder;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
@ -47,9 +47,9 @@ public class OpenerLauncherTest {
ScanningResultList scanningResults = new ScanningResultList(fetcherRegistry);
scanningResults.initNewScan(createMockFeeder("info"));
ScanningResult result = scanningResults.createResult(InetAddress.getByName("127.0.0.1"));
result.setValue(0, new InetAddressValue(InetAddress.getByName("127.0.0.1")));
result.setValue(0, new InetAddressHolder(InetAddress.getByName("127.0.0.1")));
result.setValue(1, "HOSTNAME");
result.setValue(2, NotAvailableValue.INSTANCE);
result.setValue(2, NotAvailable.VALUE);
scanningResults.registerAtIndex(0, result);
OpenerLauncher ol = new OpenerLauncher(fetcherRegistry, scanningResults);
@ -57,7 +57,7 @@ public class OpenerLauncherTest {
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));
assertEquals(NotAvailableValue.INSTANCE.toString() + ", xx", ol.prepareOpenerStringForItem("${fetcher.ping}, xx", 0));
assertEquals(NotAvailable.VALUE.toString() + ", xx", ol.prepareOpenerStringForItem("${fetcher.ping}, xx", 0));
try {
ol.prepareOpenerStringForItem("${noSuchFetcher}", 0);