From 387a1d34a6e41ad96b78c24b34901b70290707cc Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Wed, 28 Aug 2019 00:36:04 +0200 Subject: [PATCH] MKReporterTest: verifies events match expectation --- .../ooni/mk/androidTest/MKReporterTest.java | 118 +++++++++++++++++- 1 file changed, 113 insertions(+), 5 deletions(-) diff --git a/android-libs/src/androidTest/java/io/ooni/mk/androidTest/MKReporterTest.java b/android-libs/src/androidTest/java/io/ooni/mk/androidTest/MKReporterTest.java index 379360c..b6dc664 100644 --- a/android-libs/src/androidTest/java/io/ooni/mk/androidTest/MKReporterTest.java +++ b/android-libs/src/androidTest/java/io/ooni/mk/androidTest/MKReporterTest.java @@ -8,6 +8,7 @@ import androidx.test.filters.SmallTest; import com.google.common.truth.Truth; import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; import org.junit.Test; import io.ooni.mk.MKReporterResults; @@ -33,7 +34,7 @@ private MKReporterTask initialize() { MKResourcesManager.getCABundlePath(context)); } - private MKReporterResults submit(MKReporterTask task, MKTMeasurement measurement) { + private MKReporterResults submit(MKReporterTask task, MKTMeasurement measurement, Stats expect) { final long timeout = 300; MKReporterResults results = task.maybeDiscoverAndSubmit( gson.toJson(measurement), timeout @@ -44,25 +45,132 @@ private MKReporterResults submit(MKReporterTask task, MKTMeasurement measurement System.out.println("serializedStats : " + results.getSerializedStats()); Truth.assertThat(results.isGood()).isTrue(); Truth.assertThat(results.getUpdatedReportID().length()).isGreaterThan(0); + Stats real = gson.fromJson(results.getSerializedStats(), Stats.class); + Truth.assertThat(real.equals(expect)).isTrue(); return results; } @Test public void submitOne() { + Stats expect = new Stats(); + expect.bouncer_okay = 1; + expect.load_request_okay = 1; + expect.open_report_okay = 1; + expect.update_report_okay = 1; submit(initialize(), new MKTMeasurement( testName, testVersion, "", "" - )); + ), expect); } @Test public void submitMany() { MKReporterTask task = initialize(); + + Stats expect = new Stats(); + expect.bouncer_okay = 1; + expect.load_request_okay = 1; + expect.open_report_okay = 1; + expect.update_report_okay = 1; submit(task, new MKTMeasurement( testName, testVersion, "a", "" - )); + ), expect); + + expect = new Stats(); + expect.load_request_okay = 1; + expect.update_report_okay = 1; submit(task, new MKTMeasurement( testName, testVersion, "b", "" - )); + ), expect); + + expect = new Stats(); + expect.close_report_okay = 1; + expect.load_request_okay = 1; + expect.open_report_okay = 1; + expect.update_report_okay = 1; submit(task, new MKTMeasurement( testName + "x", testVersion, "c", "" - )); + ), expect); + } + + private class Stats { + @SerializedName("bouncer_error") + public long bouncer_error = 0; + + @SerializedName("bouncer_no_collectors") + public long bouncer_no_collectors = 0; + + @SerializedName("bouncer_okay") + public long bouncer_okay = 0; + + @SerializedName("close_report_error") + public long close_report_error = 0; + + @SerializedName("close_report_okay") + public long close_report_okay = 0; + + @SerializedName("load_request_error") + public long load_request_error = 0; + + @SerializedName("load_request_okay") + public long load_request_okay = 0; + + @SerializedName("open_report_error") + public long open_report_error = 0; + + @SerializedName("open_report_okay") + public long open_report_okay = 0; + + @SerializedName("report_id_empty") + public long report_id_empty = 0; + + @SerializedName("serialize_measurement_error") + public long serialize_measurement_error = 0; + + @SerializedName("update_report_error") + public long update_report_error = 0; + + @SerializedName("update_report_okay") + public long update_report_okay = 0; + + public boolean equals(Stats other) { + if (bouncer_error != other.bouncer_error) { + return false; + } + if (bouncer_no_collectors != other.bouncer_no_collectors) { + return false; + } + if (bouncer_okay != other.bouncer_okay) { + return false; + } + if (close_report_error != other.close_report_error) { + return false; + } + if (close_report_okay != other.close_report_okay) { + return false; + } + if (load_request_error != other.load_request_error) { + return false; + } + if (load_request_okay != other.load_request_okay) { + return false; + } + if (open_report_error != other.open_report_error) { + return false; + } + if (open_report_okay != other.open_report_okay) { + return false; + } + if (report_id_empty != other.report_id_empty) { + return false; + } + if (serialize_measurement_error != other.serialize_measurement_error) { + return false; + } + if (update_report_error != other.update_report_error) { + return false; + } + if (update_report_okay != other.update_report_okay) { + return false; + } + return true; + } } }