Skip to content

Commit

Permalink
Windows specific PingerRegistryTest fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bob1024 authored and angryziber committed Jun 2, 2015
1 parent d3d89b2 commit 6556698
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions test/net/azib/ipscan/core/net/PingerRegistryTest.java
@@ -1,10 +1,11 @@
/**
*
*
*/
package net.azib.ipscan.core.net;

import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.config.Platform;
import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.fetchers.FetcherException;
Expand All @@ -30,78 +31,92 @@ public void setUp() {
public void getRegisteredNames() throws Exception {
String[] names = new PingerRegistry(null).getRegisteredNames();
assertNotNull(names);
for (String name : names) {
assertNotNull(Labels.getLabel(name));
}
for (String name : names) {
assertNotNull(Labels.getLabel(name));
}
}

@Test
public void createPinger() throws Exception {
PingerRegistry registry = new PingerRegistry(null);
String[] names = registry.getRegisteredNames();
for (String name : names) {
try {
Pinger pinger = registry.createPinger(name, 0);
pinger.close();
}
catch (FetcherException e) {
// ignore in case RawSockets cannot be initialized
// under current conditions
assertEquals("pingerCreateFailure", e.getMessage());
}
}
for (String name : names) {
try {
Pinger pinger = registry.createPinger(name, 0);
pinger.close();
}
catch (FetcherException e) {
// ignore in case RawSockets cannot be initialized
// under current conditions
assertEquals("pingerCreateFailure", e.getMessage());
}
}
}

@Test
@Test
public void createDefaultPinger() throws Exception {
ScannerConfig config = Config.getConfig().forScanner();
PingerRegistry registry = new PingerRegistry(config);
config.selectedPinger = "pinger.udp";
assertTrue(registry.createPinger() instanceof UDPPinger);
}

@Test
public void checkSelectedPinger() throws Exception {
ScannerConfig config = Config.getConfig().forScanner();
PingerRegistry registry = new PingerRegistry(config);

config.selectedPinger = "pinger.udp";
assertTrue(registry.checkSelectedPinger());

config.selectedPinger = "pinger.tcp";
assertTrue(registry.checkSelectedPinger());

registry.pingers.put("pinger.dummy1", DummyPinger1.class);
config.selectedPinger = "pinger.icmp.dummy1";
assertFalse(registry.checkSelectedPinger());
assertEquals("pinger.combined", config.selectedPinger);

registry.pingers.put("pinger.dummy2", DummyPinger2.class);
config.selectedPinger = "pinger.icmp.dummy2";

checkWrongPinger(config, registry, PingerWithoutConstructor.class);
checkWrongPinger(config, registry, PingerWithIncorrectConstructor.class);
checkWrongPinger(config, registry, PingerWithWrongPing.class);
}

private void checkWrongPinger(ScannerConfig config, PingerRegistry registry, Class<? extends Pinger> wrongPingerClass) {
final String name = "pinger.icmp." + wrongPingerClass.getName();
registry.pingers.put(name, wrongPingerClass);
config.selectedPinger = name;

assertFalse(registry.checkSelectedPinger());
assertEquals("pinger.combined", config.selectedPinger);

String expectedPinger = Platform.WINDOWS ? "pinger.windows" : "pinger.combined";
assertEquals(expectedPinger, config.selectedPinger);
}

public class DummyPinger1 implements Pinger {

abstract static class AbstractWrongPinger implements Pinger {

@Override
public PingResult ping(ScanningSubject subject, int count) throws IOException {
throw new IOException("This pinger will not work!");
return null;
}


@Override
public void close() throws IOException {
}
}

public class DummyPinger2 implements Pinger {
static class PingerWithoutConstructor extends AbstractWrongPinger {
}

public static class PingerWithIncorrectConstructor extends AbstractWrongPinger {

public PingerWithIncorrectConstructor() {
}
}

static class PingerWithWrongPing extends AbstractWrongPinger {

public DummyPinger2() {
throw new RuntimeException("This pinger will not work, can't even create!");
public PingerWithWrongPing(int value) {
}

@Override
public PingResult ping(ScanningSubject subject, int count) throws IOException {
return null;
}

public void close() throws IOException {
throw new IOException("This pinger will not work!");
}
}
}

0 comments on commit 6556698

Please sign in to comment.