bugfix: IP:Port list exporter now handles empty results properly

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@411 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2008-04-16 20:24:12 +00:00
parent 399789c040
commit 883500a0e0
5 changed files with 32 additions and 29 deletions

View File

@ -7,6 +7,9 @@ import java.io.IOException;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.PortIterator;
import net.azib.ipscan.core.values.NumericRangeList;
import net.azib.ipscan.fetchers.IPFetcher;
import net.azib.ipscan.fetchers.PortsFetcher;
/**
* IP List Exporter
@ -17,7 +20,6 @@ import net.azib.ipscan.core.PortIterator;
*/
public class IPListExporter extends AbstractExporter {
/* CSV delimiter character */
static final char DELIMETER = ':';
private int ipFetcherIndex;
@ -32,20 +34,20 @@ public class IPListExporter extends AbstractExporter {
}
public void setFetchers(String[] fetcherNames) throws IOException {
ipFetcherIndex = findFetcherByLabel("fetcher.ip", fetcherNames);
portsFetcherIndex = findFetcherByLabel("fetcher.ports", fetcherNames);
ipFetcherIndex = findFetcherById(IPFetcher.ID, fetcherNames);
portsFetcherIndex = findFetcherById(PortsFetcher.ID, fetcherNames);
}
/**
* Searches for the needed fetcher by name.
*
* @param label
* @param fetcherId
* @param fetcherNames
* @return fetcher's index
* @throws ExporterException in case fetcher is not found
*/
static int findFetcherByLabel(String label, String[] fetcherNames) {
String fetcherName = Labels.getLabel(label);
static int findFetcherById(String fetcherId, String[] fetcherNames) {
String fetcherName = Labels.getLabel(fetcherId);
for (int i = 0; i < fetcherNames.length; i++) {
if (fetcherName.equals(fetcherNames[i])) {
return i;
@ -56,17 +58,10 @@ public class IPListExporter extends AbstractExporter {
public void nextAdressResults(Object[] results) throws IOException {
String address = results[ipFetcherIndex].toString();
String portList;
try {
portList = results[portsFetcherIndex].toString();
}
catch (Exception e) {
// ignore empty results
return;
}
Object ports = results[portsFetcherIndex];
if (portList != null) {
for (PortIterator i = new PortIterator(portList); i.hasNext(); ) {
if (ports != null && ports instanceof NumericRangeList) {
for (PortIterator i = new PortIterator(ports.toString()); i.hasNext(); ) {
output.println(address + DELIMETER + i.next());
}
}

View File

@ -73,7 +73,7 @@ public class XMLExporter extends AbstractExporter {
}
public void setFetchers(String[] fetcherNames) throws IOException {
ipFetcherIndex = IPListExporter.findFetcherByLabel("fetcher.ip", fetcherNames);
ipFetcherIndex = IPListExporter.findFetcherById("fetcher.ip", fetcherNames);
this.fetcherNames = fetcherNames;
}

View File

@ -30,7 +30,7 @@ import net.azib.ipscan.util.SequenceIterator;
*/
public class PortsFetcher extends AbstractFetcher {
static final String ID = "fetcher.ports";
public static final String ID = "fetcher.ports";
private static final String PARAMETER_OPEN_PORTS = "openPorts";
private static final String PARAMETER_FILTERED_PORTS = "filteredPorts";

View File

@ -11,6 +11,8 @@ import java.io.OutputStream;
import java.net.InetAddress;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.fetchers.IPFetcher;
import net.azib.ipscan.fetchers.PortsFetcher;
import junit.framework.ComparisonFailure;
import org.junit.Before;
@ -118,7 +120,7 @@ public abstract class AbstractExporterTestCase {
@Test
public void testNextAddressResultsWithNulls() throws IOException {
exporter.start(outputStream, "feederstuff");
exporter.setFetchers(new String[] {"IP", "fetcher1", "mega long fetcher 2"});
exporter.setFetchers(new String[] {Labels.getLabel(IPFetcher.ID), "fetcher1", Labels.getLabel(PortsFetcher.ID)});
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost(), null, null});
exporter.end();
}

View File

@ -7,12 +7,17 @@ import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NumericRangeList;
import net.azib.ipscan.fetchers.IPFetcher;
import net.azib.ipscan.fetchers.PortsFetcher;
/**
* IP List Exporter Test
@ -36,8 +41,8 @@ public class IPListExporterTest extends AbstractExporterTestCase {
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.setFetchers(new String[] {"fetcher1", labels.get(IPFetcher.ID), "mega long fetcher 2", labels.get(PortsFetcher.ID)});
exporter.nextAdressResults(new Object[] {"", "123", "", new NumericRangeList(Arrays.asList(1,23,4,5,6,78), true)});
exporter.end();
assertContains("123:1");
@ -59,25 +64,26 @@ public class IPListExporterTest extends AbstractExporterTestCase {
}
@Test
public void testNextAddressResultsWithNulls() throws IOException {
public void testNextAddressResultsWithNullsOrEmptyValues() throws IOException {
Labels labels = Labels.getInstance();
exporter.start(outputStream, "feederstuff");
exporter.setFetchers(new String[] {labels.get("fetcher.ip"), "fetcher1", labels.get("fetcher.ports")});
exporter.setFetchers(new String[] {labels.get(IPFetcher.ID), "fetcher1", labels.get(PortsFetcher.ID)});
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost(), null, null});
exporter.nextAdressResults(new Object[] {InetAddress.getLocalHost(), null, NotAvailable.VALUE});
exporter.end();
}
@Test
public void testFindFetcherByLabel() {
public void testFindFetcherById() {
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"}));
assertEquals(0, IPListExporter.findFetcherById(IPFetcher.ID, new String[] {labels.get(IPFetcher.ID)}));
assertEquals(3, IPListExporter.findFetcherById(IPFetcher.ID, new String[] {"a", "b", "c", labels.get(IPFetcher.ID)}));
assertEquals(1, IPListExporter.findFetcherById(PortsFetcher.ID, new String[] {labels.get(PortsFetcher.ID) + "x", labels.get(PortsFetcher.ID), "mmmm"}));
try {
IPListExporter.findFetcherByLabel("fetcher.ip", new String[] {"1", "2"});
IPListExporter.findFetcherById(IPFetcher.ID, new String[] {"1", "2"});
fail();
}
catch (ExporterException e) {