commit 6f2f34b1c87682b5fa1234b410a6920e550d8189 Author: iwakeh iwakeh@torproject.org Date: Tue Sep 5 13:21:28 2017 +0000
Add tests and adapt build.xml to run these.
Also added test sources to checkstyle target. --- build.xml | 71 ++++++++++-- .../exonerator/ExoneraTorServletTest.java | 68 ++++++++++++ .../torproject/exonerator/QueryResponseTest.java | 122 +++++++++++++++++++++ 3 files changed, 250 insertions(+), 11 deletions(-)
diff --git a/build.xml b/build.xml index 4e05077..2dc70f0 100644 --- a/build.xml +++ b/build.xml @@ -6,27 +6,46 @@ <property name="webapp" value="src/main/webapp"/> <property name="generated" value="generated/"/> <property name="classes" value="${generated}/classes"/> + <property name="testclasses" value="${generated}/testclasses/"/> <property name="dist" value="${generated}/dist"/> <property name="testresources" value="src/test/resources/"/> + <property name="testsources" value="src/test/java"/> + <property name="testresult" value="${generated}/test-results"/> <property name="libs" value="lib"/> <property name="webxmlfile" value="src/main/webapp/web.xml"/> <property name="warfile" value="${dist}/exonerator.war"/> <property name="source-and-target-java-version" value="1.7" /> <property name="metricslibversion" value="2.0.0" /> + <patternset id="runtime" > + <include name="metrics-lib-${metricslibversion}.jar"/> + <include name="commons-compress-1.13.jar"/> + <include name="commons-codec-1.10.jar"/> + <include name="commons-lang-2.6.jar"/> + <include name="gson-2.4.jar" /> + <include name="postgresql-9.4.1212.jar"/> + <include name="servlet-api-3.0.jar"/> + <include name="logback-core-1.1.9.jar" /> + <include name="logback-classic-1.1.9.jar" /> + <include name="slf4j-api-1.7.22.jar"/> + <include name="xz-1.6.jar"/> + </patternset> <path id="classpath"> <pathelement path="${classes}"/> <fileset dir="${libs}"> - <include name="metrics-lib-${metricslibversion}.jar"/> - <include name="commons-compress-1.13.jar"/> - <include name="commons-codec-1.10.jar"/> - <include name="commons-lang-2.6.jar"/> - <include name="gson-2.4.jar" /> - <include name="postgresql-9.4.1212.jar"/> - <include name="servlet-api-3.0.jar"/> - <include name="logback-core-1.1.9.jar" /> - <include name="logback-classic-1.1.9.jar" /> - <include name="slf4j-api-1.7.22.jar"/> - <include name="xz-1.6.jar"/> + <patternset refid="runtime" /> + </fileset> + </path> + <path id="test.classpath"> + <pathelement path="${classes}"/> + <pathelement path="${resources}"/> + <pathelement path="${testclasses}"/> + <pathelement path="${testresources}"/> + <fileset dir="${libs}"> + <patternset refid="runtime" /> + </fileset> + <fileset dir="${libs}"> + <include name="junit4-4.12.jar"/> + <include name="hamcrest-all-1.3.jar"/> </fileset> </path> <path id="checkstyle.classpath" > @@ -44,6 +63,7 @@ <target name="init"> <copy file="${resources}/config" tofile="${basedir}/config"/> <mkdir dir="${classes}"/> + <mkdir dir="${testclasses}"/> <mkdir dir="${dist}"/> </target>
@@ -62,6 +82,33 @@ </javac> </target>
+ <target name="testcompile" depends="init"> + <javac destdir="${testclasses}" + srcdir="${testsources}" + source="${source-and-target-java-version}" + target="${source-and-target-java-version}" + debug="true" + deprecation="true" + optimize="false" + failonerror="true" + includeantruntime="false"> + <classpath refid="test.classpath"/> + </javac> + </target> + + <target name="test" depends="compile,testcompile"> + <junit fork="true" + haltonfailure="true" + printsummary="on"> + <classpath refid="test.classpath"/> + <formatter type="plain" usefile="false"/> + <batchtest> + <fileset dir="${testclasses}" + includes="**/*Test.class"/> + </batchtest> + </junit> + </target> + <!-- Import descriptors into the ExoneraTor database. --> <target name="run" depends="compile"> <java fork="true" @@ -78,6 +125,8 @@ <checkstyle config="${testresources}/metrics_checks.xml"> <fileset dir="${sources}" includes="**/*.java" /> + <fileset dir="${testsources}" + includes="**/*.java" /> <classpath> <path refid="classpath" /> <path refid="checkstyle.classpath" /> diff --git a/src/test/java/org/torproject/exonerator/ExoneraTorServletTest.java b/src/test/java/org/torproject/exonerator/ExoneraTorServletTest.java new file mode 100644 index 0000000..b2e3ed4 --- /dev/null +++ b/src/test/java/org/torproject/exonerator/ExoneraTorServletTest.java @@ -0,0 +1,68 @@ +/* Copyright 2017 The Tor Project + * See LICENSE for licensing information */ + +package org.torproject.exonerator; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collection; + +public class ExoneraTorServletTest { + + private static final String[][] ipTestData + = { // input, output + {"86.95.81.23", "86.95.81.23"}, + {"786.95.81.23", null}, + {"86.95.81.2334", null}, + {null, ""}, + {"", ""}, + {"[2a01:4f8:190:514a::2]", "2a01:4f8:190:514a::2"}, + {"2a01:4f8:1234:0:5678:0:9abc:d", "2a01:4f8:1234:0:5678:0:9abc:d"}, + {"2a01:4f8:1234:0:5678:0:9abc:u", null}, + {"2a01:4f8::0:9abc:d", "2a01:4f8::0:9abc:d"}, + {"2A01:4F8::0:9ABC:D", "2a01:4f8::0:9abc:d"}, + {"2a01:4f8:::0:9abc:d", null}, + {"[[2a01:4f8:190:514a::2]]", null}, + {"2a01:4f8:190:514a::2", "2a01:4f8:190:514a::2"} + }; + + @Test + public void testIpParsing() { + for (String[] data : ipTestData) { + assertEquals(data[1], ExoneraTorServlet.parseIpParameter(data[0])); + } + } + + private static final String[][] timestampTestData + = { // input, output + {"2000- 10-10", "2000-10-10"}, + {"2010-12-16 +0001", "2010-12-16"}, + {"2010-12-16 CEST", "2010-12-16"}, + {"2010-12-16abcd", "2010-12-16"}, + {"2010-12-16", "2010-12-16"}, + {"2000-10-10 12:10:00", "2000-10-10"}, + {"2000-10-10 1210-04-05", "2000-10-10"}, + {"20.10.16", null}, + {null, ""}, + {"", ""}, + {"2010-12 16", null}, + {"2010-\t12-\t16", "2010-12-16"}, + {"2010- 12- \t16", "2010-12-16"}, + {"2003-12-\t16", "2003-12-16"}, + {"2004-10-10\t", "2004-10-10"}, + {"\n2005-10-10\t\t", "2005-10-10"}, + {" 2001-10-10 ", "2001-10-10"} + }; + + @Test + public void testTimestampParsing() { + for (String[] data : timestampTestData) { + assertEquals(data[1], ExoneraTorServlet.parseTimestampParameter(data[0])); + } + } + +} + diff --git a/src/test/java/org/torproject/exonerator/QueryResponseTest.java b/src/test/java/org/torproject/exonerator/QueryResponseTest.java new file mode 100644 index 0000000..8c79949 --- /dev/null +++ b/src/test/java/org/torproject/exonerator/QueryResponseTest.java @@ -0,0 +1,122 @@ +/* Copyright 2017 The Tor Project + * See LICENSE for licensing information */ + +package org.torproject.exonerator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.io.StringReader; +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +public class QueryResponseTest { + + /** Test data structure: QueryResponse, JSON string. */ + @Parameters + public static Collection<Object[]> testData() { + return Arrays.asList(new Object[][] { + {null, + "{"version":"0.3"," + + ""query_address":"12.13.14.15"," + + ""query_date":"2016-12-12"," + + ""first_date_in_database":"2016-01-01"," + + ""last_date_in_database":"2016-12-31"," + + ""relevant_statuses":true,"matches":[{"timestamp":"2016-12-03"," + + ""addresses":["12.13.14.15"," + + ""12.13.14.16"],"fingerprint":"fingerprint-not-checked"," + + ""nickname":"some name"," + + ""exit":true},{"timestamp":"2012-12-03"," + + ""addresses":["12.13.20.15"," + + ""12.13.20.16"],"fingerprint":"fingerprint2-not-checked"," + + ""nickname":"some name2"," + + ""exit":false}],"nearby_addresses":["12.13.14.15"," + + ""12.13.14.16"]}"}, + {new QueryResponse("1.1", null, null, null, + null, false, null, null), + "{"version":"1.1","relevant_statuses":false}"}, + {new QueryResponse("1.0", "12.13.14.15", "2016-12-12", "2016-01-01", + "2016-12-31", true, + new QueryResponse.Match[]{new QueryResponse.Match("2016-12-03", + new String[] {"12.13.14.15", "12.13.14.16"}, + "fingerprint-not-checked", "some name", true), + new QueryResponse.Match("2012-12-03", + new String[] {"12.13.20.15", "12.13.20.16"}, + "fingerprint2-not-checked", "some name2", false)}, + new String[] {"12.13.14.15", "12.13.14.16"}), + "{"version":"1.0"," + + ""query_address":"12.13.14.15"," + + ""query_date":"2016-12-12"," + + ""first_date_in_database":"2016-01-01"," + + ""last_date_in_database":"2016-12-31"," + + ""relevant_statuses":true,"matches":[{"timestamp":"2016-12-03"," + + ""addresses":["12.13.14.15"," + + ""12.13.14.16"],"fingerprint":"fingerprint-not-checked"," + + ""nickname":"some name"," + + ""exit":true},{"timestamp":"2012-12-03"," + + ""addresses":["12.13.20.15"," + + ""12.13.20.16"],"fingerprint":"fingerprint2-not-checked"," + + ""nickname":"some name2"," + + ""exit":false}],"nearby_addresses":["12.13.14.15"," + + ""12.13.14.16"]}"}, + {new QueryResponse("1.0", "12.13.14.15", "2016-12-12", "2016-01-01", + "2016-12-31", false, + new QueryResponse.Match[]{new QueryResponse.Match("2016-12-03", + new String[] {"12.13.14.15", "12.13.14.16"}, + "fingerprint-not-checked", "some name", null), + new QueryResponse.Match("2012-12-03", + new String[] {"12.13.20.15", "12.13.20.16"}, + "fingerprint2-not-checked", "some name2", true)}, + new String[] {"12.13.14.15", "12.13.14.16"}), + "{"version":"1.0"," + + ""query_address":"12.13.14.15"," + + ""query_date":"2016-12-12"," + + ""first_date_in_database":"2016-01-01"," + + ""last_date_in_database":"2016-12-31"," + + ""relevant_statuses":false,"matches":[{"timestamp":"2016-12-03"," + + ""addresses":["12.13.14.15"," + + ""12.13.14.16"],"fingerprint":"fingerprint-not-checked"," + + ""nickname":"some name"},{"timestamp":"2012-12-03"," + + ""addresses":["12.13.20.15"," + + ""12.13.20.16"],"fingerprint":"fingerprint2-not-checked"," + + ""nickname":"some name2"," + + ""exit":true}],"nearby_addresses":["12.13.14.15"," + + ""12.13.14.16"]}"} + }); + } + + private QueryResponse queryResponse; + private String json; + + public QueryResponseTest(QueryResponse qr, String json) { + this.queryResponse = qr; + this.json = json; + } + + @Test + public void testJsonReading() { + if (null == this.queryResponse) { + assertNull(QueryResponse.fromJson(new StringReader(this.json))); + } else { + assertEquals(json, + QueryResponse.toJson(QueryResponse.fromJson(new StringReader(this.json)))); + } + } + + @Test + public void testJsonWriting() { + if (null == this.queryResponse) { + return; + } + assertEquals(json, QueryResponse.toJson(this.queryResponse)); + } + +} +