refactor: move i/netx/archival structs to i/model (#659)

We recently started moving core data structures inside of the
internal/model package as detailed in https://github.com/ooni/probe/issues/1885.

The chief reason to do that is to have a set of fundamental
shared data types to help us rationalize the codebase.

This specific diff moves internal/netx/archival's core data types
inside the internal/model package. While there, it also refactors the
existing tests to improve their quality. Additionally, we also added
an extra test to ensure `ArchivalHTTPBody` is an alias for
`ArchivalMaybeBinaryData`, which is required to ensure the
custom JSON serialization process works for it.

We're doing that because both internal/netx/archival and
internal/measurex define their own archival data structures.

We developed measurex using its own structures because it
allowed to iterate more quickly. Now that we have sketched
out measurex, the time has come to consolidate.

My overall aim is to spend a few more hours this week on
engineering measurex. This work is preliminary work before
we finish up both measurex and websteps.

We described this cleanup in https://github.com/ooni/probe/issues/1957.
This commit is contained in:
Simone Basso 2022-01-10 11:25:52 +01:00 committed by GitHub
commit 730373cc75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 645 additions and 606 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/gorilla/websocket"
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@ -566,352 +565,6 @@ func TestNewTLSHandshakesList(t *testing.T) {
}
}
func TestExtSpec_AddTo(t *testing.T) {
m := new(model.Measurement)
archival.ExtDNS.AddTo(m)
expected := map[string]int64{"dnst": 0}
if d := cmp.Diff(m.Extensions, expected); d != "" {
t.Fatal(d)
}
}
var binaryInput = []uint8{
0x57, 0xe5, 0x79, 0xfb, 0xa6, 0xbb, 0x0d, 0xbc, 0xce, 0xbd, 0xa7, 0xa0,
0xba, 0xa4, 0x78, 0x78, 0x12, 0x59, 0xee, 0x68, 0x39, 0xa4, 0x07, 0x98,
0xc5, 0x3e, 0xbc, 0x55, 0xcb, 0xfe, 0x34, 0x3c, 0x7e, 0x1b, 0x5a, 0xb3,
0x22, 0x9d, 0xc1, 0x2d, 0x6e, 0xca, 0x5b, 0xf1, 0x10, 0x25, 0x47, 0x1e,
0x44, 0xe2, 0x2d, 0x60, 0x08, 0xea, 0xb0, 0x0a, 0xcc, 0x05, 0x48, 0xa0,
0xf5, 0x78, 0x38, 0xf0, 0xdb, 0x3f, 0x9d, 0x9f, 0x25, 0x6f, 0x89, 0x00,
0x96, 0x93, 0xaf, 0x43, 0xac, 0x4d, 0xc9, 0xac, 0x13, 0xdb, 0x22, 0xbe,
0x7a, 0x7d, 0xd9, 0x24, 0xa2, 0x52, 0x69, 0xd8, 0x89, 0xc1, 0xd1, 0x57,
0xaa, 0x04, 0x2b, 0xa2, 0xd8, 0xb1, 0x19, 0xf6, 0xd5, 0x11, 0x39, 0xbb,
0x80, 0xcf, 0x86, 0xf9, 0x5f, 0x9d, 0x8c, 0xab, 0xf5, 0xc5, 0x74, 0x24,
0x3a, 0xa2, 0xd4, 0x40, 0x4e, 0xd7, 0x10, 0x1f,
}
var encodedBinaryInput = []byte(`{"data":"V+V5+6a7DbzOvaeguqR4eBJZ7mg5pAeYxT68Vcv+NDx+G1qzIp3BLW7KW/EQJUceROItYAjqsArMBUig9Xg48Ns/nZ8lb4kAlpOvQ6xNyawT2yK+en3ZJKJSadiJwdFXqgQrotixGfbVETm7gM+G+V+djKv1xXQkOqLUQE7XEB8=","format":"base64"}`)
func TestMaybeBinaryValue_MarshalJSON(t *testing.T) {
type fields struct {
Value string
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{{
name: "with string input",
fields: fields{
Value: "antani",
},
want: []byte(`"antani"`),
wantErr: false,
}, {
name: "with binary input",
fields: fields{
Value: string(binaryInput),
},
want: encodedBinaryInput,
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hb := archival.MaybeBinaryValue{
Value: tt.fields.Value,
}
got, err := hb.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("MaybeBinaryValue.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Error(cmp.Diff(got, tt.want))
}
})
}
}
func TestMaybeBinaryValue_UnmarshalJSON(t *testing.T) {
type fields struct {
WantValue string
}
type args struct {
d []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{{
name: "with string input",
fields: fields{
WantValue: "xo",
},
args: args{d: []byte(`"xo"`)},
wantErr: false,
}, {
name: "with nil input",
fields: fields{
WantValue: "",
},
args: args{d: nil},
wantErr: true,
}, {
name: "with missing/invalid format",
fields: fields{
WantValue: "",
},
args: args{d: []byte(`{"format": "foo"}`)},
wantErr: true,
}, {
name: "with missing data",
fields: fields{
WantValue: "",
},
args: args{d: []byte(`{"format": "base64"}`)},
wantErr: true,
}, {
name: "with invalid base64 data",
fields: fields{
WantValue: "",
},
args: args{d: []byte(`{"format": "base64", "data": "x"}`)},
wantErr: true,
}, {
name: "with valid base64 data",
fields: fields{
WantValue: string(binaryInput),
},
args: args{d: encodedBinaryInput},
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hb := &archival.MaybeBinaryValue{}
if err := hb.UnmarshalJSON(tt.args.d); (err != nil) != tt.wantErr {
t.Errorf("MaybeBinaryValue.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
if d := cmp.Diff(tt.fields.WantValue, hb.Value); d != "" {
t.Error(d)
}
})
}
}
func TestHTTPHeader_MarshalJSON(t *testing.T) {
type fields struct {
Key string
Value archival.MaybeBinaryValue
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{{
name: "with string value",
fields: fields{
Key: "Content-Type",
Value: archival.MaybeBinaryValue{
Value: "text/plain",
},
},
want: []byte(`["Content-Type","text/plain"]`),
wantErr: false,
}, {
name: "with binary value",
fields: fields{
Key: "Content-Type",
Value: archival.MaybeBinaryValue{
Value: string(binaryInput),
},
},
want: []byte(`["Content-Type",` + string(encodedBinaryInput) + `]`),
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hh := archival.HTTPHeader{
Key: tt.fields.Key,
Value: tt.fields.Value,
}
got, err := hh.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("HTTPHeader.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Error(cmp.Diff(got, tt.want))
}
})
}
}
func TestHTTPHeader_UnmarshalJSON(t *testing.T) {
type fields struct {
WantKey string
WantValue archival.MaybeBinaryValue
}
type args struct {
d []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{{
name: "with invalid input",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`{}`),
},
wantErr: true,
}, {
name: "with unexpected number of items",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`[]`),
},
wantErr: true,
}, {
name: "with first item not being a string",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`[0,0]`),
},
wantErr: true,
}, {
name: "with both items being a string",
fields: fields{
WantKey: "x",
WantValue: archival.MaybeBinaryValue{
Value: "y",
},
},
args: args{
d: []byte(`["x","y"]`),
},
wantErr: false,
}, {
name: "with second item not being a map[string]interface{}",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`["x",[]]`),
},
wantErr: true,
}, {
name: "with missing format key in second item",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`["x",{}]`),
},
wantErr: true,
}, {
name: "with format value not being base64",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`["x",{"format":1}]`),
},
wantErr: true,
}, {
name: "with missing data field",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`["x",{"format":"base64"}]`),
},
wantErr: true,
}, {
name: "with data not being a string",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`["x",{"format":"base64","data":1}]`),
},
wantErr: true,
}, {
name: "with data not being base64",
fields: fields{
WantKey: "",
WantValue: archival.MaybeBinaryValue{
Value: "",
},
},
args: args{
d: []byte(`["x",{"format":"base64","data":"xx"}]`),
},
wantErr: true,
}, {
name: "with correctly encoded base64 data",
fields: fields{
WantKey: "x",
WantValue: archival.MaybeBinaryValue{
Value: string(binaryInput),
},
},
args: args{
d: []byte(`["x",` + string(encodedBinaryInput) + `]`),
},
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hh := &archival.HTTPHeader{}
if err := hh.UnmarshalJSON(tt.args.d); (err != nil) != tt.wantErr {
t.Errorf("HTTPHeader.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
expect := &archival.HTTPHeader{
Key: tt.fields.WantKey,
Value: tt.fields.WantValue,
}
if d := cmp.Diff(hh, expect); d != "" {
t.Error(d)
}
})
}
}
func TestNewFailure(t *testing.T) {
type args struct {
err error