|
1 | 1 | package model_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "errors" |
4 | 6 | "testing"
|
5 | 7 |
|
6 | 8 | "github.com/google/go-cmp/cmp"
|
| 9 | + "github.com/google/go-cmp/cmp/cmpopts" |
7 | 10 | "github.com/ooni/probe-cli/v3/internal/model"
|
8 | 11 | "github.com/ooni/probe-cli/v3/internal/testingx"
|
9 | 12 | )
|
@@ -37,6 +40,259 @@ var archivalBinaryInput = []uint8{
|
37 | 40 | // we use this value below to test we can handle binary data
|
38 | 41 | var archivalEncodedBinaryInput = []byte(`{"data":"V+V5+6a7DbzOvaeguqR4eBJZ7mg5pAeYxT68Vcv+NDx+G1qzIp3BLW7KW/EQJUceROItYAjqsArMBUig9Xg48Ns/nZ8lb4kAlpOvQ6xNyawT2yK+en3ZJKJSadiJwdFXqgQrotixGfbVETm7gM+G+V+djKv1xXQkOqLUQE7XEB8=","format":"base64"}`)
|
39 | 42 |
|
| 43 | +func TestArchivalBinaryData(t *testing.T) { |
| 44 | + // This test verifies that we correctly serialize binary data to JSON by |
| 45 | + // producing null | {"format":"base64","data":"<base64>"} |
| 46 | + t.Run("MarshalJSON", func(t *testing.T) { |
| 47 | + // testcase is a test case defined by this function |
| 48 | + type testcase struct { |
| 49 | + // name is the name of the test case |
| 50 | + name string |
| 51 | + |
| 52 | + // input is the binary input |
| 53 | + input model.ArchivalBinaryData |
| 54 | + |
| 55 | + // expectErr is the error we expect to see or nil |
| 56 | + expectErr error |
| 57 | + |
| 58 | + // expectData is the data we expect to see |
| 59 | + expectData []byte |
| 60 | + } |
| 61 | + |
| 62 | + cases := []testcase{{ |
| 63 | + name: "with nil .Value", |
| 64 | + input: model.ArchivalBinaryData{Value: nil}, |
| 65 | + expectErr: nil, |
| 66 | + expectData: []byte("null"), |
| 67 | + }, { |
| 68 | + name: "with zero length .Value", |
| 69 | + input: model.ArchivalBinaryData{Value: []byte{}}, |
| 70 | + expectErr: nil, |
| 71 | + expectData: []byte("null"), |
| 72 | + }, { |
| 73 | + name: "with .Value being a simple binary string", |
| 74 | + input: model.ArchivalBinaryData{Value: []byte("Elliot")}, |
| 75 | + expectErr: nil, |
| 76 | + expectData: []byte(`{"data":"RWxsaW90","format":"base64"}`), |
| 77 | + }, { |
| 78 | + name: "with .Value being a long binary string", |
| 79 | + input: model.ArchivalBinaryData{Value: archivalBinaryInput}, |
| 80 | + expectErr: nil, |
| 81 | + expectData: archivalEncodedBinaryInput, |
| 82 | + }} |
| 83 | + |
| 84 | + for _, tc := range cases { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + // serialize to JSON |
| 87 | + output, err := json.Marshal(tc.input) |
| 88 | + |
| 89 | + t.Log("got this error", err) |
| 90 | + t.Log("got this binary data", output) |
| 91 | + t.Logf("converted to string: %s", string(output)) |
| 92 | + |
| 93 | + // handle errors |
| 94 | + switch { |
| 95 | + case err == nil && tc.expectErr != nil: |
| 96 | + t.Fatal("expected", tc.expectErr, "got", err) |
| 97 | + |
| 98 | + case err != nil && tc.expectErr == nil: |
| 99 | + t.Fatal("expected", tc.expectErr, "got", err) |
| 100 | + |
| 101 | + case err != nil && tc.expectErr != nil: |
| 102 | + if err.Error() != tc.expectErr.Error() { |
| 103 | + t.Fatal("expected", tc.expectErr, "got", err) |
| 104 | + } |
| 105 | + |
| 106 | + case err == nil && tc.expectErr == nil: |
| 107 | + // all good--fallthrough |
| 108 | + } |
| 109 | + |
| 110 | + if diff := cmp.Diff(tc.expectData, output); diff != "" { |
| 111 | + t.Fatal(diff) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + // This test verifies that we correctly parse binary data to JSON by |
| 118 | + // reading from null | {"format":"base64","data":"<base64>"} |
| 119 | + t.Run("UnmarshalJSON", func(t *testing.T) { |
| 120 | + // testcase is a test case defined by this function |
| 121 | + type testcase struct { |
| 122 | + // name is the name of the test case |
| 123 | + name string |
| 124 | + |
| 125 | + // input is the binary input |
| 126 | + input []byte |
| 127 | + |
| 128 | + // expectErr is the error we expect to see or nil |
| 129 | + expectErr error |
| 130 | + |
| 131 | + // expectData is the data we expect to see |
| 132 | + expectData model.ArchivalBinaryData |
| 133 | + } |
| 134 | + |
| 135 | + cases := []testcase{{ |
| 136 | + name: "with nil input array", |
| 137 | + input: nil, |
| 138 | + expectErr: errors.New("unexpected end of JSON input"), |
| 139 | + expectData: model.ArchivalBinaryData{Value: nil}, |
| 140 | + }, { |
| 141 | + name: "with zero-length input array", |
| 142 | + input: []byte{}, |
| 143 | + expectErr: errors.New("unexpected end of JSON input"), |
| 144 | + expectData: model.ArchivalBinaryData{Value: nil}, |
| 145 | + }, { |
| 146 | + name: "with binary input that is not a complete JSON", |
| 147 | + input: []byte("{"), |
| 148 | + expectErr: errors.New("unexpected end of JSON input"), |
| 149 | + expectData: model.ArchivalBinaryData{Value: nil}, |
| 150 | + }, { |
| 151 | + name: "with ~random binary data as input", |
| 152 | + input: archivalBinaryInput, |
| 153 | + expectErr: errors.New("invalid character 'W' looking for beginning of value"), |
| 154 | + expectData: model.ArchivalBinaryData{}, |
| 155 | + }, { |
| 156 | + name: "with valid JSON of the wrong type (array)", |
| 157 | + input: []byte("[]"), |
| 158 | + expectErr: errors.New("json: cannot unmarshal array into Go value of type model.archivalBinaryDataRepr"), |
| 159 | + expectData: model.ArchivalBinaryData{}, |
| 160 | + }, { |
| 161 | + name: "with valid JSON of the wrong type (number)", |
| 162 | + input: []byte("1.17"), |
| 163 | + expectErr: errors.New("json: cannot unmarshal number into Go value of type model.archivalBinaryDataRepr"), |
| 164 | + expectData: model.ArchivalBinaryData{}, |
| 165 | + }, { |
| 166 | + name: "with input being the liternal null", |
| 167 | + input: []byte(`null`), |
| 168 | + expectErr: nil, |
| 169 | + expectData: model.ArchivalBinaryData{Value: nil}, |
| 170 | + }, { |
| 171 | + name: "with empty JSON object", |
| 172 | + input: []byte("{}"), |
| 173 | + expectErr: errors.New("model: invalid binary data format: ''"), |
| 174 | + expectData: model.ArchivalBinaryData{}, |
| 175 | + }, { |
| 176 | + name: "with correct data model but invalid format", |
| 177 | + input: []byte(`{"data":"","format":"antani"}`), |
| 178 | + expectErr: errors.New("model: invalid binary data format: 'antani'"), |
| 179 | + expectData: model.ArchivalBinaryData{}, |
| 180 | + }, { |
| 181 | + name: "with correct data model and format but invalid base64 string", |
| 182 | + input: []byte(`{"data":"x","format":"base64"}`), |
| 183 | + expectErr: errors.New("illegal base64 data at input byte 0"), |
| 184 | + expectData: model.ArchivalBinaryData{}, |
| 185 | + }, { |
| 186 | + name: "with correct data model and format but empty base64 string", |
| 187 | + input: []byte(`{"data":"","format":"base64"}`), |
| 188 | + expectErr: nil, |
| 189 | + expectData: model.ArchivalBinaryData{Value: []byte{}}, |
| 190 | + }, { |
| 191 | + name: "with the encoding of a simple binary string", |
| 192 | + input: []byte(`{"data":"RWxsaW90","format":"base64"}`), |
| 193 | + expectErr: nil, |
| 194 | + expectData: model.ArchivalBinaryData{Value: []byte("Elliot")}, |
| 195 | + }, { |
| 196 | + name: "with the encoding of a complex binary string", |
| 197 | + input: archivalEncodedBinaryInput, |
| 198 | + expectErr: nil, |
| 199 | + expectData: model.ArchivalBinaryData{Value: archivalBinaryInput}, |
| 200 | + }} |
| 201 | + |
| 202 | + for _, tc := range cases { |
| 203 | + t.Run(tc.name, func(t *testing.T) { |
| 204 | + // unmarshal the raw input into an ArchivalBinaryData type |
| 205 | + var abd model.ArchivalBinaryData |
| 206 | + err := json.Unmarshal(tc.input, &abd) |
| 207 | + |
| 208 | + t.Log("got this error", err) |
| 209 | + t.Log("got this .Value filed", abd.Value) |
| 210 | + t.Logf("converted to string: %s", string(abd.Value)) |
| 211 | + |
| 212 | + // handle errors |
| 213 | + switch { |
| 214 | + case err == nil && tc.expectErr != nil: |
| 215 | + t.Fatal("expected", tc.expectErr, "got", err) |
| 216 | + |
| 217 | + case err != nil && tc.expectErr == nil: |
| 218 | + t.Fatal("expected", tc.expectErr, "got", err) |
| 219 | + |
| 220 | + case err != nil && tc.expectErr != nil: |
| 221 | + if err.Error() != tc.expectErr.Error() { |
| 222 | + t.Fatal("expected", tc.expectErr, "got", err) |
| 223 | + } |
| 224 | + |
| 225 | + case err == nil && tc.expectErr == nil: |
| 226 | + // all good--fallthrough |
| 227 | + } |
| 228 | + |
| 229 | + if diff := cmp.Diff(tc.expectData, abd); diff != "" { |
| 230 | + t.Fatal(diff) |
| 231 | + } |
| 232 | + }) |
| 233 | + } |
| 234 | + }) |
| 235 | + |
| 236 | + // This test verifies that we correctly round trip through JSON |
| 237 | + t.Run("MarshalJSON then UnmarshalJSON", func(t *testing.T) { |
| 238 | + // testcase is a test case defined by this function |
| 239 | + type testcase struct { |
| 240 | + // name is the name of the test case |
| 241 | + name string |
| 242 | + |
| 243 | + // input is the binary input |
| 244 | + input model.ArchivalBinaryData |
| 245 | + } |
| 246 | + |
| 247 | + cases := []testcase{{ |
| 248 | + name: "with nil .Value", |
| 249 | + input: model.ArchivalBinaryData{Value: nil}, |
| 250 | + }, { |
| 251 | + name: "with zero length .Value", |
| 252 | + input: model.ArchivalBinaryData{Value: []byte{}}, |
| 253 | + }, { |
| 254 | + name: "with .Value being a simple binary string", |
| 255 | + input: model.ArchivalBinaryData{Value: []byte("Elliot")}, |
| 256 | + }, { |
| 257 | + name: "with .Value being a long binary string", |
| 258 | + input: model.ArchivalBinaryData{Value: archivalBinaryInput}, |
| 259 | + }} |
| 260 | + |
| 261 | + for _, tc := range cases { |
| 262 | + t.Run(tc.name, func(t *testing.T) { |
| 263 | + // serialize to JSON |
| 264 | + output, err := json.Marshal(tc.input) |
| 265 | + |
| 266 | + t.Log("got this error", err) |
| 267 | + t.Log("got this binary data", output) |
| 268 | + t.Logf("converted to string: %s", string(output)) |
| 269 | + |
| 270 | + if err != nil { |
| 271 | + t.Fatal(err) |
| 272 | + } |
| 273 | + |
| 274 | + // parse from JSON |
| 275 | + var abc model.ArchivalBinaryData |
| 276 | + if err := json.Unmarshal(output, &abc); err != nil { |
| 277 | + t.Fatal(err) |
| 278 | + } |
| 279 | + |
| 280 | + // make sure we round tripped |
| 281 | + // |
| 282 | + // Note: the round trip is not perfect because the zero length value, |
| 283 | + // which originally is []byte{}, unmarshals to a nil value. |
| 284 | + // |
| 285 | + // Because the two are ~equivalent in Go most intents and purposes |
| 286 | + // and the wire representation does not change, this is OK(TM) |
| 287 | + diffOptions := []cmp.Option{cmpopts.EquateEmpty()} |
| 288 | + if diff := cmp.Diff(tc.input, abc, diffOptions...); diff != "" { |
| 289 | + t.Fatal(diff) |
| 290 | + } |
| 291 | + }) |
| 292 | + } |
| 293 | + }) |
| 294 | +} |
| 295 | + |
40 | 296 | func TestMaybeBinaryValue(t *testing.T) {
|
41 | 297 | t.Run("MarshalJSON", func(t *testing.T) {
|
42 | 298 | tests := []struct {
|
|
0 commit comments