if (log == null) {
log = new ConsoleLogger();
}
Logger testLogger = log.getChildLogger("test");
testLogger.info("TESTING "+next+": "+currentTest.get("description"));
if (parser == null) {
/* PREVIOUS SLOW WAY
enabledServices = new WiringServiceTable();
enabledServices.put(LogEnabled.class, log);
*/
parser = new DefaultSPF1Parser(log.getChildLogger("parser"), new DefaultTermsFactory(log.getChildLogger("termsfactory"), new WiringService() {
public void wire(Object component) throws WiringServiceException {
if (component instanceof LogEnabled) {
String[] path = component.getClass().toString().split("\\.");
((LogEnabled) component).enableLogging(log.getChildLogger("dep").getChildLogger(path[path.length-1].toLowerCase()));
}
if (component instanceof DNSServiceEnabled) {
((DNSServiceEnabled) component).enableDNSService(dns);
}
if (component instanceof SPFCheckEnabled) {
((SPFCheckEnabled) component).enableSPFChecking(spf);
}
}
}));
}
dns = new LoggingDNSService(getDNSService(), log.getChildLogger("dns"));
spf = new SPF(dns, parser, log.getChildLogger("spf"));
/* PREVIOUS SLOW WAY
// we add this after the creation because it is a loop reference
enabledServices.remove(DNSServiceEnabled.class);
enabledServices.put(DNSServiceEnabled.class, getDNSService());
enabledServices.remove(SPFCheckEnabled.class);
enabledServices.put(SPFCheckEnabled.class, spf);
*/
String ip = null;
String sender = null;
String helo = null;
if (currentTest.get("helo") != null) {
helo = (String) currentTest.get("helo");
}
if (currentTest.get("host") != null) {
ip = (String) currentTest.get("host");
}
if (currentTest.get("mailfrom") != null) {
sender = (String) currentTest.get("mailfrom");
} else {
sender = "";
}
SPFResult res = spf.checkSPF(ip, sender, helo);
String resultSPF = res.getResult();
if (currentTest.get("result") instanceof String) {
assertEquals("Test "+next+" ("+currentTest.get("description")+") failed. Returned: "+res.getResult()+" Expected: "+currentTest.get("result")+" [["+res.getResult()+"||"+res.getHeaderText()+"]]", currentTest.get("result"), res.getResult());
} else {
ArrayList results = (ArrayList) currentTest.get("result");
boolean match = false;
for (int i = 0; i < results.size(); i++) {
if (results.get(i).equals(resultSPF)) match = true;
// testLogger.debug("checking "+resultSPF+" against allowed result "+results.get(i));
}
assertTrue(match);
}
if (currentTest.get("explanation") != null) {
// Check for our default explanation!
if (currentTest.get("explanation").equals("DEFAULT") || currentTest.get("explanation").equals("postmaster") ) {
assertTrue(res.getExplanation().startsWith("http://www.openspf.org/why.html?sender="));
} else if (currentTest.get("explanation").equals("cafe:babe::1 is queried as 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.E.B.A.B.E.F.A.C.ip6.arpa")) {
// See http://java.sun.com/j2se/1.4.2/docs/api/java/net/Inet6Address.html
// For methods that return a textual representation as output value, the full form is used.
// Inet6Address will return the full form because it is unambiguous when used in combination with other textual data.
assertTrue(res.getExplanation().equals("cafe:babe:0:0:0:0:0:1 is queried as 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.E.B.A.B.E.F.A.C.ip6.arpa"));
} else {
assertEquals(currentTest.get("explanation"),res.getExplanation());
}
}
testLogger.info("PASSED. Result="+res.getResult()+" Explanation="+res.getExplanation()+" Header="+res.getHeaderText());
}