go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tokenserver/appengine/impl/utils/policy/bundle_test.go (about)

     1  // Copyright 2017 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 policy
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/gob"
    20  	"testing"
    21  
    22  	"google.golang.org/protobuf/types/known/durationpb"
    23  	"google.golang.org/protobuf/types/known/timestamppb"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  	. "go.chromium.org/luci/common/testing/assertions"
    27  )
    28  
    29  func TestConfigBundle(t *testing.T) {
    30  	Convey("Empty map", t, func() {
    31  		var b ConfigBundle
    32  		blob, err := serializeBundle(b)
    33  		So(err, ShouldBeNil)
    34  
    35  		b, unknown, err := deserializeBundle(blob)
    36  		So(err, ShouldBeNil)
    37  		So(unknown, ShouldBeNil)
    38  		So(len(b), ShouldEqual, 0)
    39  	})
    40  
    41  	Convey("Non-empty map", t, func() {
    42  		// We use well-known proto types in this test to avoid depending on some
    43  		// other random proto messages. It doesn't matter what proto messages are
    44  		// used here.
    45  		b1 := ConfigBundle{
    46  			"a": &timestamppb.Timestamp{Seconds: 1},
    47  			"b": &durationpb.Duration{Seconds: 2},
    48  		}
    49  		blob, err := serializeBundle(b1)
    50  		So(err, ShouldBeNil)
    51  
    52  		b2, unknown, err := deserializeBundle(blob)
    53  		So(err, ShouldBeNil)
    54  		So(unknown, ShouldBeNil)
    55  		So(b2, ShouldHaveLength, len(b1))
    56  		for k := range b2 {
    57  			So(b2[k], ShouldResembleProto, b1[k])
    58  		}
    59  	})
    60  
    61  	Convey("Unknown proto", t, func() {
    62  		items := []blobWithType{
    63  			{"abc", "unknown.type", []byte("zzz")},
    64  		}
    65  		out := bytes.Buffer{}
    66  		So(gob.NewEncoder(&out).Encode(items), ShouldBeNil)
    67  
    68  		b, unknown, err := deserializeBundle(out.Bytes())
    69  		So(err, ShouldBeNil)
    70  		So(unknown, ShouldResemble, items)
    71  		So(len(b), ShouldEqual, 0)
    72  	})
    73  
    74  	Convey("Rejects nil", t, func() {
    75  		_, err := serializeBundle(ConfigBundle{"abc": nil})
    76  		So(err, ShouldNotBeNil)
    77  	})
    78  }