This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
141 lines (130 loc) · 4.41 KB
/
collector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package engine
import (
"context"
"errors"
"fmt"
"testing"
"github.com/measurement-kit/engine/internal/nettest"
"github.com/measurement-kit/engine/model"
)
const origMeasurement = `{
"data_format_version": "0.2.0",
"input": "torproject.org",
"measurement_start_time": "2016-06-04 17:53:13",
"probe_asn": "AS0",
"probe_cc": "ZZ",
"probe_ip": "127.0.0.1",
"software_name": "ooniprobe-android",
"software_version": "2.0.0",
"test_keys": {
"failure": null,
"received": [],
"sent": []
},
"test_name": "tcp_connect",
"test_runtime": 0.253494024276733,
"test_start_time": "2016-06-04 17:53:13",
"test_version": "0.0.1"
}`
// TestCollectorSubmitIntegration covers the common case of submitting
// a measurement to the OONI collector.
func TestCollectorSubmitIntegration(t *testing.T) {
task := NewCollectorSubmitTask("ooniprobe-android", "2.1.0", origMeasurement)
results := task.Run()
fmt.Println(results.Logs)
fmt.Println(results.UpdatedSerializedMeasurement)
fmt.Println(results.UpdatedReportID)
if !results.Good {
t.Fatal("resubmission failed")
}
}
// TestCollectorSubmitExpectFailureV200 covers the case where we want
// a measurement to fail because the client is ooniprobe-android v2.0.0.
func TestCollectorSubmitExpectFailureV200(t *testing.T) {
task := NewCollectorSubmitTask("ooniprobe-android", "2.0.0", origMeasurement)
results := task.Run()
fmt.Println(results.Logs)
fmt.Println(results.UpdatedSerializedMeasurement)
fmt.Println(results.UpdatedReportID)
if results.Good {
t.Fatal("we expected a failure here")
}
}
// TestCollectorSubmitConstructorAndSetters ensures that we can use
// either the constructor or the setters to configure the task.
func TestCollectorSubmitConstructorAndSetters(t *testing.T) {
task := NewCollectorSubmitTask("ooniprobe-android", "2.0.0", origMeasurement)
if task.SoftwareName != "ooniprobe-android" {
t.Fatal("the constructor cannot set the softwareName")
}
if task.SoftwareVersion != "2.0.0" {
t.Fatal("the constructor cannot set the softwareVersion")
}
if task.SerializedMeasurement != origMeasurement {
t.Fatal("the constructor cannot set the serializedMeasurement")
}
if task.Timeout != defaultTimeout {
t.Fatal("the constructor cannot set the timeout")
}
}
// TestCollectorSubmitUnmarshalError covers the case where we're
// passed an invalid serialized JSON.
func TestCollectorSubmitUnmarshalError(t *testing.T) {
task := NewCollectorSubmitTask("ooniprobe-android", "2.1.0", "{")
results := task.Run()
if results.Good {
t.Fatal("We expected a failure here")
}
}
// TestCollectorSubmitInvalidTimeout covers the case where we're
// passed an invalid timeout value.
func TestCollectorSubmitInvalidTimeout(t *testing.T) {
task := NewCollectorSubmitTask("ooniprobe-android", "2.1.0", origMeasurement)
task.Timeout = -1
results := task.Run()
if results.Good {
t.Fatal("We expected a failure here")
}
}
// TestCollectorSubmitDiscoverFailure covers the case where there
// is a failure when discovering available collectors.
func TestCollectorSubmitDiscoverFailure(t *testing.T) {
savedFunc := discoverAvailableCollectors
discoverAvailableCollectors = func(ctx context.Context, nt *nettest.Nettest) error {
return errors.New("mocked error")
}
task := NewCollectorSubmitTask("ooniprobe-android", "2.1.0", origMeasurement)
results := task.Run()
if results.Good {
t.Fatal("We expected a failure here")
}
discoverAvailableCollectors = savedFunc
}
// TestCollectorSubmitSubmitFailure covers the case where there
// is a failure when submitting the actual measurement.
func TestCollectorSubmitSubmitFailure(t *testing.T) {
savedFunc := submitMeasurement
submitMeasurement = func(ctx context.Context, nt *nettest.Nettest, m *model.Measurement) error {
return errors.New("mocked error")
}
task := NewCollectorSubmitTask("ooniprobe-android", "2.1.0", origMeasurement)
results := task.Run()
if results.Good {
t.Fatal("We expected a failure here")
}
submitMeasurement = savedFunc
}
// TestCollectorSubmitMarshalFailure covers the case where there
// is a failure when marshalling the updated measurement.
func TestCollectorSubmitMarshalFailure(t *testing.T) {
savedFunc := jsonMarshal
jsonMarshal = func(m *model.Measurement) ([]byte, error) {
return nil, errors.New("mocked error")
}
task := NewCollectorSubmitTask("ooniprobe-android", "2.1.0", origMeasurement)
results := task.Run()
if results.Good {
t.Fatal("We expected a failure here")
}
jsonMarshal = savedFunc
}