|
| 1 | +package legacymodel_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + "github.com/ooni/probe-cli/v3/internal/legacy/legacymodel" |
| 8 | +) |
| 9 | + |
| 10 | +// we use this value below to test we can handle binary data |
| 11 | +var archivalBinaryInput = []uint8{ |
| 12 | + 0x57, 0xe5, 0x79, 0xfb, 0xa6, 0xbb, 0x0d, 0xbc, 0xce, 0xbd, 0xa7, 0xa0, |
| 13 | + 0xba, 0xa4, 0x78, 0x78, 0x12, 0x59, 0xee, 0x68, 0x39, 0xa4, 0x07, 0x98, |
| 14 | + 0xc5, 0x3e, 0xbc, 0x55, 0xcb, 0xfe, 0x34, 0x3c, 0x7e, 0x1b, 0x5a, 0xb3, |
| 15 | + 0x22, 0x9d, 0xc1, 0x2d, 0x6e, 0xca, 0x5b, 0xf1, 0x10, 0x25, 0x47, 0x1e, |
| 16 | + 0x44, 0xe2, 0x2d, 0x60, 0x08, 0xea, 0xb0, 0x0a, 0xcc, 0x05, 0x48, 0xa0, |
| 17 | + 0xf5, 0x78, 0x38, 0xf0, 0xdb, 0x3f, 0x9d, 0x9f, 0x25, 0x6f, 0x89, 0x00, |
| 18 | + 0x96, 0x93, 0xaf, 0x43, 0xac, 0x4d, 0xc9, 0xac, 0x13, 0xdb, 0x22, 0xbe, |
| 19 | + 0x7a, 0x7d, 0xd9, 0x24, 0xa2, 0x52, 0x69, 0xd8, 0x89, 0xc1, 0xd1, 0x57, |
| 20 | + 0xaa, 0x04, 0x2b, 0xa2, 0xd8, 0xb1, 0x19, 0xf6, 0xd5, 0x11, 0x39, 0xbb, |
| 21 | + 0x80, 0xcf, 0x86, 0xf9, 0x5f, 0x9d, 0x8c, 0xab, 0xf5, 0xc5, 0x74, 0x24, |
| 22 | + 0x3a, 0xa2, 0xd4, 0x40, 0x4e, 0xd7, 0x10, 0x1f, |
| 23 | +} |
| 24 | + |
| 25 | +// we use this value below to test we can handle binary data |
| 26 | +var archivalEncodedBinaryInput = []byte(`{"data":"V+V5+6a7DbzOvaeguqR4eBJZ7mg5pAeYxT68Vcv+NDx+G1qzIp3BLW7KW/EQJUceROItYAjqsArMBUig9Xg48Ns/nZ8lb4kAlpOvQ6xNyawT2yK+en3ZJKJSadiJwdFXqgQrotixGfbVETm7gM+G+V+djKv1xXQkOqLUQE7XEB8=","format":"base64"}`) |
| 27 | + |
| 28 | +func TestArchivalMaybeBinaryData(t *testing.T) { |
| 29 | + t.Run("MarshalJSON", func(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string // test name |
| 32 | + input string // value to marshal |
| 33 | + want []byte // expected result |
| 34 | + wantErr bool // whether we expect an error |
| 35 | + }{{ |
| 36 | + name: "with string input", |
| 37 | + input: "antani", |
| 38 | + want: []byte(`"antani"`), |
| 39 | + wantErr: false, |
| 40 | + }, { |
| 41 | + name: "with binary input", |
| 42 | + input: string(archivalBinaryInput), |
| 43 | + want: archivalEncodedBinaryInput, |
| 44 | + wantErr: false, |
| 45 | + }} |
| 46 | + for _, tt := range tests { |
| 47 | + t.Run(tt.name, func(t *testing.T) { |
| 48 | + hb := legacymodel.ArchivalMaybeBinaryData{ |
| 49 | + Value: tt.input, |
| 50 | + } |
| 51 | + got, err := hb.MarshalJSON() |
| 52 | + if (err != nil) != tt.wantErr { |
| 53 | + t.Fatalf("ArchivalMaybeBinaryData.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 54 | + } |
| 55 | + if diff := cmp.Diff(tt.want, got); diff != "" { |
| 56 | + t.Fatal(diff) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("UnmarshalJSON", func(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + name string // test name |
| 65 | + input []byte // value to unmarshal |
| 66 | + want string // expected result |
| 67 | + wantErr bool // whether we want an error |
| 68 | + }{{ |
| 69 | + name: "with string input", |
| 70 | + input: []byte(`"xo"`), |
| 71 | + want: "xo", |
| 72 | + wantErr: false, |
| 73 | + }, { |
| 74 | + name: "with nil input", |
| 75 | + input: nil, |
| 76 | + want: "", |
| 77 | + wantErr: true, |
| 78 | + }, { |
| 79 | + name: "with missing/invalid format", |
| 80 | + input: []byte(`{"format": "foo"}`), |
| 81 | + want: "", |
| 82 | + wantErr: true, |
| 83 | + }, { |
| 84 | + name: "with missing data", |
| 85 | + input: []byte(`{"format": "base64"}`), |
| 86 | + want: "", |
| 87 | + wantErr: true, |
| 88 | + }, { |
| 89 | + name: "with invalid base64 data", |
| 90 | + input: []byte(`{"format": "base64", "data": "x"}`), |
| 91 | + want: "", |
| 92 | + wantErr: true, |
| 93 | + }, { |
| 94 | + name: "with valid base64 data", |
| 95 | + input: archivalEncodedBinaryInput, |
| 96 | + want: string(archivalBinaryInput), |
| 97 | + wantErr: false, |
| 98 | + }} |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + hb := &legacymodel.ArchivalMaybeBinaryData{} |
| 102 | + if err := hb.UnmarshalJSON(tt.input); (err != nil) != tt.wantErr { |
| 103 | + t.Fatalf("ArchivalMaybeBinaryData.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 104 | + } |
| 105 | + if d := cmp.Diff(tt.want, hb.Value); d != "" { |
| 106 | + t.Fatal(d) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | + }) |
| 111 | +} |
0 commit comments