go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/lex64/encoding_utils_test.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package lex64 16 17 import ( 18 "bytes" 19 "encoding/base64" 20 "testing" 21 "testing/quick" 22 23 "github.com/google/go-cmp/cmp" 24 ) 25 26 // TestDoEncodeDoDecode tests writing a message, encoding it, and then decoding it. 27 func TestDoEncodeDoDecode(t *testing.T) { 28 t.Parallel() 29 30 cases := []struct { 31 name string 32 encoding *base64.Encoding 33 in []byte 34 out []byte 35 ok bool 36 }{ 37 { 38 name: "empty string", 39 encoding: base64.StdEncoding, 40 in: []byte{}, 41 out: nil, 42 ok: true, 43 }, 44 { 45 name: "simple string", 46 encoding: base64.StdEncoding, 47 in: []byte("a\n"), 48 out: []byte("YQo="), 49 ok: true, 50 }, 51 { 52 name: "bigger string", 53 encoding: base64.StdEncoding, 54 in: []byte("c38541d2-0a5d-4341-bba6-df2b38835bc9\n"), 55 out: []byte("YzM4NTQxZDItMGE1ZC00MzQxLWJiYTYtZGYyYjM4ODM1YmM5Cg=="), 56 ok: true, 57 }, 58 { 59 name: "sample string", 60 encoding: base64.StdEncoding, 61 in: []byte{0xf1, 0xc5, 0x60, 0xbe, 0x24, 0x7e, 0xc3}, 62 out: []byte("8cVgviR+ww=="), 63 ok: true, 64 }, 65 } 66 67 for _, tt := range cases { 68 tt := tt 69 t.Run(tt.name, func(t *testing.T) { 70 t.Parallel() 71 out, err := doEncode(tt.encoding, tt.in) 72 switch { 73 case err == nil && !tt.ok: 74 t.Errorf("error is unexpectedly nil") 75 case err != nil && tt.ok: 76 t.Errorf("unexpected error: %s", err) 77 } 78 if diff := cmp.Diff(tt.out, out); diff != "" { 79 t.Errorf("unexpected diff (-want +got): %s", diff) 80 } 81 roundTrip, err := doDecode(tt.encoding, out) 82 switch { 83 case err == nil && !tt.ok: 84 t.Errorf("error is unexpectedly nil") 85 case err != nil && tt.ok: 86 t.Errorf("unexpected error: %s", err) 87 } 88 if diff := cmp.Diff(tt.in, roundTrip); diff != "" { 89 t.Errorf("unexpected diff (-want +got): %s", diff) 90 } 91 }) 92 } 93 } 94 95 // TestRoundTrip tests that arbitrary messages can be encoded and decoded. 96 func TestRoundTrip(t *testing.T) { 97 t.Parallel() 98 99 doesRoundTrip := func(input []byte) bool { 100 encoded, err := doEncode(base64.StdEncoding, input) 101 if err != nil { 102 panic(err) 103 } 104 roundTripped, err := doDecode(base64.StdEncoding, encoded) 105 if err != nil { 106 panic(err) 107 } 108 return bytes.Equal(input, roundTripped) 109 } 110 111 if err := quick.Check(doesRoundTrip, nil); err != nil { 112 t.Errorf("unexpected error: %s", err) 113 } 114 }