Skip to content

Commit

Permalink
netx: use constants to represent all operations (#678)
Browse files Browse the repository at this point in the history
This is less error prone. I consider this yak shaving to make the
code more robust before continuing with #646.

While there reduce linter warnings.
  • Loading branch information
bassosimone authored Jun 9, 2020
1 parent d6316d6 commit 9ead0e1
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 108 deletions.
3 changes: 2 additions & 1 deletion experiment/dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ooni/probe-engine/model"
"github.com/ooni/probe-engine/netx/archival"
"github.com/ooni/probe-engine/netx/httptransport"
"github.com/ooni/probe-engine/netx/modelx"
"github.com/ooni/probe-engine/netx/trace"
)

Expand Down Expand Up @@ -182,7 +183,7 @@ func (r runner) measure(
// of the latest connect time. We should have one sample in most
// cases, because the connection should be persistent.
for _, ev := range r.saver.Read() {
if ev.Name == "connect" {
if ev.Name == modelx.ConnectOperation {
connectTime = ev.Duration.Seconds()
}
}
Expand Down
5 changes: 3 additions & 2 deletions experiment/dash/dash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ooni/probe-engine/experiment/handler"
"github.com/ooni/probe-engine/internal/mockable"
"github.com/ooni/probe-engine/model"
"github.com/ooni/probe-engine/netx/modelx"
"github.com/ooni/probe-engine/netx/trace"
)

Expand Down Expand Up @@ -109,7 +110,7 @@ func TestUnitRunnerLoopMeasureFailure(t *testing.T) {
func TestUnitRunnerLoopCollectFailure(t *testing.T) {
expected := errors.New("mocked error")
saver := new(trace.Saver)
saver.Write(trace.Event{Name: "connect", Duration: 150 * time.Millisecond})
saver.Write(trace.Event{Name: modelx.ConnectOperation, Duration: 150 * time.Millisecond})
r := runner{
callbacks: handler.NewPrinterCallbacks(log.Log),
httpClient: &http.Client{
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestUnitRunnerLoopCollectFailure(t *testing.T) {

func TestUnitRunnerLoopSuccess(t *testing.T) {
saver := new(trace.Saver)
saver.Write(trace.Event{Name: "connect", Duration: 150 * time.Millisecond})
saver.Write(trace.Event{Name: modelx.ConnectOperation, Duration: 150 * time.Millisecond})
r := runner{
callbacks: handler.NewPrinterCallbacks(log.Log),
httpClient: &http.Client{
Expand Down
3 changes: 2 additions & 1 deletion experiment/tor/tor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ooni/probe-engine/internal/oonidatamodel"
"github.com/ooni/probe-engine/internal/oonitemplates"
"github.com/ooni/probe-engine/model"
"github.com/ooni/probe-engine/netx/modelx"
)

const (
Expand Down Expand Up @@ -66,7 +67,7 @@ func (tr *TargetResults) fillSummary() {
if len(tr.TCPConnect) < 1 {
return
}
tr.Summary["connect"] = Summary{
tr.Summary[modelx.ConnectOperation] = Summary{
Failure: tr.TCPConnect[0].Status.Failure,
}
switch tr.TargetProtocol {
Expand Down
19 changes: 10 additions & 9 deletions experiment/tor/tor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ooni/probe-engine/internal/oonitemplates"
"github.com/ooni/probe-engine/internal/orchestra"
"github.com/ooni/probe-engine/model"
"github.com/ooni/probe-engine/netx/modelx"
)

func TestUnitNewExperimentMeasurer(t *testing.T) {
Expand Down Expand Up @@ -128,25 +129,25 @@ func TestIntegrationMeasurerMeasureGood(t *testing.T) {
}

var staticTestingTargets = []model.TorTarget{
model.TorTarget{
{
Address: "192.95.36.142:443",
Params: map[string][]string{
"cert": []string{
"cert": {
"qUVQ0srL1JI/vO6V6m/24anYXiJD3QP2HgzUKQtQ7GRqqUvs7P+tG43RtAqdhLOALP7DJQ",
},
"iat-mode": []string{"1"},
"iat-mode": {"1"},
},
Protocol: "obfs4",
},
model.TorTarget{
{
Address: "66.111.2.131:9030",
Protocol: "dir_port",
},
model.TorTarget{
{
Address: "66.111.2.131:9001",
Protocol: "or_port",
},
model.TorTarget{
{
Address: "1.1.1.1:80",
Protocol: "tcp",
},
Expand Down Expand Up @@ -398,7 +399,7 @@ func TestUnitSummary(t *testing.T) {
if len(tr.Summary) != 1 {
t.Fatal("cannot find expected entry")
}
if *tr.Summary["connect"].Failure != failure {
if *tr.Summary[modelx.ConnectOperation].Failure != failure {
t.Fatal("invalid failure")
}
})
Expand All @@ -417,7 +418,7 @@ func TestUnitSummary(t *testing.T) {
if len(tr.Summary) != 2 {
t.Fatal("cannot find expected entry")
}
if tr.Summary["connect"].Failure != nil {
if tr.Summary[modelx.ConnectOperation].Failure != nil {
t.Fatal("invalid failure")
}
if *tr.Summary["handshake"].Failure != failure {
Expand All @@ -441,7 +442,7 @@ func TestUnitSummary(t *testing.T) {
if len(tr.Summary) < 1 {
t.Fatal("cannot find expected entry")
}
if tr.Summary["connect"].Failure != nil {
if tr.Summary[modelx.ConnectOperation].Failure != nil {
t.Fatal("invalid failure")
}
if handshake == nil {
Expand Down
3 changes: 2 additions & 1 deletion experiment/urlgetter/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ooni/probe-engine/experiment/urlgetter"
"github.com/ooni/probe-engine/internal/mockable"
"github.com/ooni/probe-engine/netx/modelx"
)

func TestGetterWithCancelledContextVanilla(t *testing.T) {
Expand Down Expand Up @@ -394,7 +395,7 @@ func TestGetterIntegration(t *testing.T) {
resolveStart = true
case "resolve_done":
resolveDone = true
case "connect":
case modelx.ConnectOperation:
connect = true
case "tls_handshake_start":
tlsHandshakeStart = true
Expand Down
6 changes: 3 additions & 3 deletions internal/oonidatamodel/oonidatamodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func NewNetworkEventsList(results oonitemplates.Results) NetworkEventsList {
ConnID: in.Connect.ConnID,
DialID: in.Connect.DialID,
Failure: makeFailure(in.Connect.Error),
Operation: "connect",
Operation: modelx.ConnectOperation,
Proto: protocolName[in.Connect.ConnID >= 0],
T: in.Connect.DurationSinceBeginning.Seconds(),
TransactionID: in.Connect.TransactionID,
Expand All @@ -445,7 +445,7 @@ func NewNetworkEventsList(results oonitemplates.Results) NetworkEventsList {
out = append(out, &NetworkEvent{
ConnID: in.Read.ConnID,
Failure: makeFailure(in.Read.Error),
Operation: "read",
Operation: modelx.ReadOperation,
NumBytes: in.Read.NumBytes,
Proto: protocolName[in.Read.ConnID >= 0],
T: in.Read.DurationSinceBeginning.Seconds(),
Expand All @@ -456,7 +456,7 @@ func NewNetworkEventsList(results oonitemplates.Results) NetworkEventsList {
out = append(out, &NetworkEvent{
ConnID: in.Write.ConnID,
Failure: makeFailure(in.Write.Error),
Operation: "write",
Operation: modelx.WriteOperation,
NumBytes: in.Write.NumBytes,
Proto: protocolName[in.Write.ConnID >= 0],
T: in.Write.DurationSinceBeginning.Seconds(),
Expand Down
58 changes: 29 additions & 29 deletions internal/oonidatamodel/oonidatamodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func TestUnitNewTCPConnectListEmpty(t *testing.T) {
func TestUnitNewTCPConnectListSuccess(t *testing.T) {
out := NewTCPConnectList(oonitemplates.Results{
Connects: []*modelx.ConnectEvent{
&modelx.ConnectEvent{
{
RemoteAddress: "8.8.8.8:53",
},
&modelx.ConnectEvent{
{
RemoteAddress: "8.8.4.4:853",
},
},
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestUnitNewTCPConnectListSuccess(t *testing.T) {
func TestUnitNewTCPConnectListFailure(t *testing.T) {
out := NewTCPConnectList(oonitemplates.Results{
Connects: []*modelx.ConnectEvent{
&modelx.ConnectEvent{
{
RemoteAddress: "8.8.8.8:53",
Error: errors.New(modelx.FailureConnectionReset),
},
Expand All @@ -90,7 +90,7 @@ func TestUnitNewTCPConnectListFailure(t *testing.T) {
func TestUnitNewTCPConnectListInvalidInput(t *testing.T) {
out := NewTCPConnectList(oonitemplates.Results{
Connects: []*modelx.ConnectEvent{
&modelx.ConnectEvent{
{
RemoteAddress: "8.8.8.8",
Error: errors.New(modelx.FailureConnectionReset),
},
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestUnitNewRequestsListGood(t *testing.T) {
out := NewRequestList(oonitemplates.Results{
HTTPRequests: []*modelx.HTTPRoundTripDoneEvent{
// need two requests to test that order is inverted
&modelx.HTTPRoundTripDoneEvent{
{
RequestBodySnap: []byte("abcdefx"),
RequestHeaders: http.Header{
"Content-Type": []string{
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestUnitNewRequestsListGood(t *testing.T) {
ResponseStatusCode: 451,
MaxBodySnapSize: 10,
},
&modelx.HTTPRoundTripDoneEvent{
{
Error: errors.New("antani"),
},
},
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestUnitNewRequestsListGood(t *testing.T) {
func TestUnitNewRequestsSnaps(t *testing.T) {
out := NewRequestList(oonitemplates.Results{
HTTPRequests: []*modelx.HTTPRoundTripDoneEvent{
&modelx.HTTPRoundTripDoneEvent{
{
RequestBodySnap: []byte("abcd"),
MaxBodySnapSize: 4,
ResponseBodySnap: []byte("defg"),
Expand Down Expand Up @@ -640,14 +640,14 @@ func TestUnitNewDNSQueriesListEmpty(t *testing.T) {
func TestUnitNewDNSQueriesListSuccess(t *testing.T) {
out := NewDNSQueriesList(oonitemplates.Results{
Resolves: []*modelx.ResolveDoneEvent{
&modelx.ResolveDoneEvent{
{
Addresses: []string{
"8.8.4.4", "2001:4860:4860::8888",
},
Hostname: "dns.google",
TransportNetwork: "system",
},
&modelx.ResolveDoneEvent{
{
Error: errors.New(modelx.FailureDNSNXDOMAINError),
Hostname: "dns.googlex",
TransportNetwork: "system",
Expand Down Expand Up @@ -807,9 +807,9 @@ func TestUnitNewNetworkEventsListEmpty(t *testing.T) {
func TestUnitNewNetworkEventsListNoSuitableEvents(t *testing.T) {
out := NewNetworkEventsList(oonitemplates.Results{
NetworkEvents: []*modelx.Measurement{
&modelx.Measurement{},
&modelx.Measurement{},
&modelx.Measurement{},
{},
{},
{},
},
})
if len(out) != 0 {
Expand All @@ -820,22 +820,22 @@ func TestUnitNewNetworkEventsListNoSuitableEvents(t *testing.T) {
func TestUnitNewNetworkEventsListGood(t *testing.T) {
out := NewNetworkEventsList(oonitemplates.Results{
NetworkEvents: []*modelx.Measurement{
&modelx.Measurement{
{
Connect: &modelx.ConnectEvent{
ConnID: 555,
DurationSinceBeginning: 10 * time.Millisecond,
DialID: 17,
RemoteAddress: "1.1.1.1:443",
},
},
&modelx.Measurement{
{
Read: &modelx.ReadEvent{
ConnID: 555,
DurationSinceBeginning: 20 * time.Millisecond,
NumBytes: 1789,
},
},
&modelx.Measurement{
{
Write: &modelx.WriteEvent{
ConnID: 555,
DurationSinceBeginning: 30 * time.Millisecond,
Expand Down Expand Up @@ -863,7 +863,7 @@ func TestUnitNewNetworkEventsListGood(t *testing.T) {
if out[0].NumBytes != 0 {
t.Fatal("wrong out[0].NumBytes")
}
if out[0].Operation != "connect" {
if out[0].Operation != modelx.ConnectOperation {
t.Fatal("wrong out[0].Operation")
}
if out[0].Proto != "tcp" {
Expand All @@ -888,7 +888,7 @@ func TestUnitNewNetworkEventsListGood(t *testing.T) {
if out[1].NumBytes != 1789 {
t.Fatal("wrong out[1].NumBytes")
}
if out[1].Operation != "read" {
if out[1].Operation != modelx.ReadOperation {
t.Fatal("wrong out[1].Operation")
}
if out[1].Proto != "tcp" {
Expand All @@ -913,7 +913,7 @@ func TestUnitNewNetworkEventsListGood(t *testing.T) {
if out[2].NumBytes != 17714 {
t.Fatal("wrong out[2].NumBytes")
}
if out[2].Operation != "write" {
if out[2].Operation != modelx.WriteOperation {
t.Fatal("wrong out[2].Operation")
}
if out[2].Proto != "tcp" {
Expand All @@ -927,7 +927,7 @@ func TestUnitNewNetworkEventsListGood(t *testing.T) {
func TestUnitNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
out := NewNetworkEventsList(oonitemplates.Results{
NetworkEvents: []*modelx.Measurement{
&modelx.Measurement{
{
Connect: &modelx.ConnectEvent{
ConnID: -555,
DurationSinceBeginning: 10 * time.Millisecond,
Expand All @@ -936,15 +936,15 @@ func TestUnitNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
RemoteAddress: "1.1.1.1:443",
},
},
&modelx.Measurement{
{
Read: &modelx.ReadEvent{
ConnID: -555,
DurationSinceBeginning: 20 * time.Millisecond,
Error: errors.New("mocked error"),
NumBytes: 1789,
},
},
&modelx.Measurement{
{
Write: &modelx.WriteEvent{
ConnID: -555,
DurationSinceBeginning: 30 * time.Millisecond,
Expand Down Expand Up @@ -973,7 +973,7 @@ func TestUnitNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
if out[0].NumBytes != 0 {
t.Fatal("wrong out[0].NumBytes")
}
if out[0].Operation != "connect" {
if out[0].Operation != modelx.ConnectOperation {
t.Fatal("wrong out[0].Operation")
}
if out[0].Proto != "udp" {
Expand All @@ -998,7 +998,7 @@ func TestUnitNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
if out[1].NumBytes != 1789 {
t.Fatal("wrong out[1].NumBytes")
}
if out[1].Operation != "read" {
if out[1].Operation != modelx.ReadOperation {
t.Fatal("wrong out[1].Operation")
}
if out[1].Proto != "udp" {
Expand All @@ -1023,7 +1023,7 @@ func TestUnitNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
if out[2].NumBytes != 17714 {
t.Fatal("wrong out[2].NumBytes")
}
if out[2].Operation != "write" {
if out[2].Operation != modelx.WriteOperation {
t.Fatal("wrong out[2].Operation")
}
if out[2].Proto != "udp" {
Expand All @@ -1049,20 +1049,20 @@ func TestUnitNewTLSHandshakesListEmpty(t *testing.T) {
func TestUnitNewTLSHandshakesListSuccess(t *testing.T) {
out := NewTLSHandshakesList(oonitemplates.Results{
TLSHandshakes: []*modelx.TLSHandshakeDoneEvent{
&modelx.TLSHandshakeDoneEvent{},
&modelx.TLSHandshakeDoneEvent{
{},
{
ConnID: 12345,
Error: errors.New("mocked error"),
},
&modelx.TLSHandshakeDoneEvent{
{
ConnectionState: modelx.TLSConnectionState{
CipherSuite: tls.TLS_AES_128_GCM_SHA256,
NegotiatedProtocol: "h2",
PeerCertificates: []modelx.X509Certificate{
modelx.X509Certificate{
{
Data: []byte("deadbeef"),
},
modelx.X509Certificate{
{
Data: []byte("abad1dea"),
},
},
Expand Down
Loading

0 comments on commit 9ead0e1

Please sign in to comment.