Skip to content

Commit

Permalink
chore: write tests for ArchivalDNSLookupResult (ooni#1326)
Browse files Browse the repository at this point in the history
My aim here is to have marshal/unmarshal tests for all the toplevel data
structures inside the internal/model/archival.go file.

Once I have done that, I can have additional confidence about changing
the structure fields and simplifying the code.

All of this is preparatory work to automatically scrub HTTP measurements
which is something we should really do before continuing to improve the
boostrap process in light of ooni/probe#2531.

I have also added currently skipped tests for other structures as
placeholder, but I did not want to submit a too large PR, hence for now
we will just have tests for ArchivalDNSLookupResult. More PRs will
follow and they will add additional unit tests.
  • Loading branch information
bassosimone authored and Murphy-OrangeMud committed Feb 13, 2024
1 parent 64154e0 commit 2c515b5
Showing 1 changed file with 247 additions and 0 deletions.
247 changes: 247 additions & 0 deletions internal/model/archival_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,243 @@ func TestHTTPBody(t *testing.T) {
}
}

// This test ensures that ArchivalDNSLookupResult is WAI
func TestArchivalDNSLookupResult(t *testing.T) {

// This test ensures that we correctly serialize to JSON.
t.Run("MarshalJSON", func(t *testing.T) {
// testcase is a test case defined by this function
type testcase struct {
// name is the name of the test case
name string

// input is the input struct
input model.ArchivalDNSLookupResult

// expectErr is the error we expect to see or nil
expectErr error

// expectData is the data we expect to see
expectData []byte
}

cases := []testcase{{
name: "serialization of a successful DNS lookup",
input: model.ArchivalDNSLookupResult{
Answers: []model.ArchivalDNSAnswer{{
ASN: 15169,
ASOrgName: "Google LLC",
AnswerType: "A",
Hostname: "",
IPv4: "8.8.8.8",
IPv6: "",
TTL: nil,
}, {
ASN: 15169,
ASOrgName: "Google LLC",
AnswerType: "AAAA",
Hostname: "",
IPv4: "",
IPv6: "2001:4860:4860::8888",
TTL: nil,
}},
Engine: "getaddrinfo",
Failure: nil,
GetaddrinfoError: 0,
Hostname: "dns.google",
QueryType: "ANY",
RawResponse: nil,
Rcode: 0,
ResolverHostname: nil,
ResolverPort: nil,
ResolverAddress: "",
T0: 0.5,
T: 0.7,
Tags: []string{"dns"},
TransactionID: 44,
},
expectErr: nil,
expectData: []byte(`{"answers":[{"asn":15169,"as_org_name":"Google LLC","answer_type":"A","ipv4":"8.8.8.8","ttl":null},{"asn":15169,"as_org_name":"Google LLC","answer_type":"AAAA","ipv6":"2001:4860:4860::8888","ttl":null}],"engine":"getaddrinfo","failure":null,"hostname":"dns.google","query_type":"ANY","resolver_hostname":null,"resolver_port":null,"resolver_address":"","t0":0.5,"t":0.7,"tags":["dns"],"transaction_id":44}`),
}, {
name: "serialization of a failed DNS lookup",
input: model.ArchivalDNSLookupResult{
Answers: nil,
Engine: "getaddrinfo",
Failure: (func() *string {
s := netxlite.FailureDNSNXDOMAINError
return &s
}()),
GetaddrinfoError: 5,
Hostname: "dns.googlex",
QueryType: "ANY",
RawResponse: nil,
Rcode: 0,
ResolverHostname: nil,
ResolverPort: nil,
ResolverAddress: "",
T0: 0.5,
T: 0.77,
Tags: []string{"dns"},
TransactionID: 43,
},
expectErr: nil,
expectData: []byte(`{"answers":null,"engine":"getaddrinfo","failure":"dns_nxdomain_error","getaddrinfo_error":5,"hostname":"dns.googlex","query_type":"ANY","resolver_hostname":null,"resolver_port":null,"resolver_address":"","t0":0.5,"t":0.77,"tags":["dns"],"transaction_id":43}`),
}}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// serialize to JSON
data, err := json.Marshal(tc.input)

t.Log("got this error", err)
t.Log("got this raw data", data)
t.Logf("converted to string: %s", string(data))

// handle errors
switch {
case err == nil && tc.expectErr != nil:
t.Fatal("expected", tc.expectErr, "got", err)

case err != nil && tc.expectErr == nil:
t.Fatal("expected", tc.expectErr, "got", err)

case err != nil && tc.expectErr != nil:
if err.Error() != tc.expectErr.Error() {
t.Fatal("expected", tc.expectErr, "got", err)
}

case err == nil && tc.expectErr == nil:
// all good--fallthrough
}

// make sure the serialization is OK
if diff := cmp.Diff(tc.expectData, data); diff != "" {
t.Fatal(diff)
}
})
}
})

// This test ensures that we can unmarshal from the JSON representation
t.Run("UnmarshalJSON", func(t *testing.T) {
// testcase is a test case defined by this function
type testcase struct {
// name is the name of the test case
name string

// input is the binary input
input []byte

// expectErr is the error we expect to see or nil
expectErr error

// expectStruct is the struct we expect to see
expectStruct model.ArchivalDNSLookupResult
}

cases := []testcase{{
name: "deserialization of a successful DNS lookup",
expectErr: nil,
input: []byte(`{"answers":[{"asn":15169,"as_org_name":"Google LLC","answer_type":"A","ipv4":"8.8.8.8","ttl":null},{"asn":15169,"as_org_name":"Google LLC","answer_type":"AAAA","ipv6":"2001:4860:4860::8888","ttl":null}],"engine":"getaddrinfo","failure":null,"hostname":"dns.google","query_type":"ANY","resolver_hostname":null,"resolver_port":null,"resolver_address":"","t0":0.5,"t":0.7,"tags":["dns"],"transaction_id":44}`),
expectStruct: model.ArchivalDNSLookupResult{
Answers: []model.ArchivalDNSAnswer{{
ASN: 15169,
ASOrgName: "Google LLC",
AnswerType: "A",
Hostname: "",
IPv4: "8.8.8.8",
IPv6: "",
TTL: nil,
}, {
ASN: 15169,
ASOrgName: "Google LLC",
AnswerType: "AAAA",
Hostname: "",
IPv4: "",
IPv6: "2001:4860:4860::8888",
TTL: nil,
}},
Engine: "getaddrinfo",
Failure: nil,
GetaddrinfoError: 0,
Hostname: "dns.google",
QueryType: "ANY",
RawResponse: nil,
Rcode: 0,
ResolverHostname: nil,
ResolverPort: nil,
ResolverAddress: "",
T0: 0.5,
T: 0.7,
Tags: []string{"dns"},
TransactionID: 44,
},
}, {
name: "deserialization of a failed DNS lookup",
input: []byte(`{"answers":null,"engine":"getaddrinfo","failure":"dns_nxdomain_error","getaddrinfo_error":5,"hostname":"dns.googlex","query_type":"ANY","resolver_hostname":null,"resolver_port":null,"resolver_address":"","t0":0.5,"t":0.77,"tags":["dns"],"transaction_id":43}`),
expectErr: nil,
expectStruct: model.ArchivalDNSLookupResult{
Answers: nil,
Engine: "getaddrinfo",
Failure: (func() *string {
s := netxlite.FailureDNSNXDOMAINError
return &s
}()),
GetaddrinfoError: 5,
Hostname: "dns.googlex",
QueryType: "ANY",
RawResponse: nil,
Rcode: 0,
ResolverHostname: nil,
ResolverPort: nil,
ResolverAddress: "",
T0: 0.5,
T: 0.77,
Tags: []string{"dns"},
TransactionID: 43,
},
}}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// parse the JSON
var data model.ArchivalDNSLookupResult
err := json.Unmarshal(tc.input, &data)

t.Log("got this error", err)
t.Logf("got this struct %+v", data)

// handle errors
switch {
case err == nil && tc.expectErr != nil:
t.Fatal("expected", tc.expectErr, "got", err)

case err != nil && tc.expectErr == nil:
t.Fatal("expected", tc.expectErr, "got", err)

case err != nil && tc.expectErr != nil:
if err.Error() != tc.expectErr.Error() {
t.Fatal("expected", tc.expectErr, "got", err)
}

case err == nil && tc.expectErr == nil:
// all good--fallthrough
}

// make sure the deserialization is OK
if diff := cmp.Diff(tc.expectStruct, data); diff != "" {
t.Fatal(diff)
}
})
}
})
}

// This test ensures that ArchivalTCPConnectResult is WAI
func TestArchivalTCPConnectResult(t *testing.T) {
t.Skip("not implemented")
}

// This test ensures that ArchivalTLSOrQUICHandshakeResult is WAI
func TestArchivalTLSOrQUICHandshakeResult(t *testing.T) {

Expand Down Expand Up @@ -1015,3 +1252,13 @@ func TestArchivalTLSOrQUICHandshakeResult(t *testing.T) {
}
})
}

// This test ensures that ArchivalHTTPRequestResult is WAI
func TestArchivalHTTPRequestResult(t *testing.T) {
t.Skip("not implemented")
}

// This test ensures that ArchivalNetworkEvent is WAI
func TestArchivalNetworkEvent(t *testing.T) {
t.Skip("not implemented")
}

0 comments on commit 2c515b5

Please sign in to comment.