go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/msgpackpb/deteministic_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 msgpackpb
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	"github.com/vmihailenco/msgpack/v5"
    23  )
    24  
    25  func TestMsgpackPBDeterministicEncod(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	Convey(`msgpackpbDeterministicEncode`, t, func() {
    29  		Convey(`list`, func() {
    30  			val := reflect.ValueOf([]any{
    31  				10,
    32  				"Hello",
    33  				map[string]any{
    34  					"c": "lol",
    35  					"b": []string{"one", "seven"},
    36  					"a": "yo",
    37  				},
    38  				map[int16]string{
    39  					17:  "no",
    40  					71:  "maybe",
    41  					-11: "essentially",
    42  				},
    43  				map[bool]string{
    44  					true:  "ye",
    45  					false: "nah",
    46  				},
    47  				map[uint]string{
    48  					0:  "zero",
    49  					17: "seventeen",
    50  					3:  "three",
    51  				},
    52  				map[int8]string{
    53  					1: "this",
    54  					2: "map",
    55  					3: "is",
    56  					4: "array",
    57  					5: "like",
    58  				},
    59  				-2,
    60  			})
    61  			enc, err := msgpackpbDeterministicEncode(val)
    62  			So(err, ShouldBeNil)
    63  			So(enc, ShouldResemble, msgpack.RawMessage{
    64  				152,                         // 7 element array
    65  				10,                          //  10
    66  				165, 72, 101, 108, 108, 111, // "Hello"
    67  				131,                    // 3 element map
    68  				161, 97, 162, 121, 111, // "a", "yo"
    69  				161, 98, 146, 163, 111, 110, 101, 165, 115, 101, 118, 101, 110, // "b", ["one", "seven"]
    70  				161, 99, 163, 108, 111, 108, // "c", "lol"
    71  				131,                                                            // 3 element map
    72  				245, 171, 101, 115, 115, 101, 110, 116, 105, 97, 108, 108, 121, // -11, "essentially"
    73  				17, 162, 110, 111, // 17, "no"
    74  				71, 165, 109, 97, 121, 98, 101, // 71, "maybe"
    75  				130,                    // 2 element map
    76  				194, 163, 110, 97, 104, // false, "nah"
    77  				195, 162, 121, 101, // true, "ye"
    78  				131,                        // 3 element map
    79  				0, 164, 122, 101, 114, 111, // 0, "zero"
    80  				3, 165, 116, 104, 114, 101, 101, // 3, "three"
    81  				17, 169, 115, 101, 118, 101, 110, 116, 101, 101, 110, // 17, "seventeen"
    82  				149,                     // 5 element array
    83  				164, 116, 104, 105, 115, // "this"
    84  				163, 109, 97, 112, // "map"
    85  				162, 105, 115, // "is"
    86  				165, 97, 114, 114, 97, 121, // "array"
    87  				164, 108, 105, 107, 101, // "like"
    88  				254, // -2
    89  			})
    90  		})
    91  	})
    92  }