go.chromium.org/luci@v0.0.0-20250314024836-d9a61d0730e6/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  	"go.chromium.org/luci/common/testing/ftt"
    26  	"go.chromium.org/luci/common/testing/truth/assert"
    27  	"go.chromium.org/luci/common/testing/truth/should"
    28  )
    29  
    30  func TestConfigBundle(t *testing.T) {
    31  	ftt.Run("Empty map", t, func(t *ftt.Test) {
    32  		var b ConfigBundle
    33  		blob, err := serializeBundle(b)
    34  		assert.Loosely(t, err, should.BeNil)
    35  
    36  		b, unknown, err := deserializeBundle(blob)
    37  		assert.Loosely(t, err, should.BeNil)
    38  		assert.Loosely(t, unknown, should.BeNil)
    39  		assert.Loosely(t, len(b), should.BeZero)
    40  	})
    41  
    42  	ftt.Run("Non-empty map", t, func(t *ftt.Test) {
    43  		// We use well-known proto types in this test to avoid depending on some
    44  		// other random proto messages. It doesn't matter what proto messages are
    45  		// used here.
    46  		b1 := ConfigBundle{
    47  			"a": &timestamppb.Timestamp{Seconds: 1},
    48  			"b": &durationpb.Duration{Seconds: 2},
    49  		}
    50  		blob, err := serializeBundle(b1)
    51  		assert.Loosely(t, err, should.BeNil)
    52  
    53  		b2, unknown, err := deserializeBundle(blob)
    54  		assert.Loosely(t, err, should.BeNil)
    55  		assert.Loosely(t, unknown, should.BeNil)
    56  		assert.Loosely(t, b2, should.HaveLength(len(b1)))
    57  		for k := range b2 {
    58  			assert.Loosely(t, b2[k], should.Match(b1[k]))
    59  		}
    60  	})
    61  
    62  	ftt.Run("Unknown proto", t, func(t *ftt.Test) {
    63  		items := []blobWithType{
    64  			{"abc", "unknown.type", []byte("zzz")},
    65  		}
    66  		out := bytes.Buffer{}
    67  		assert.Loosely(t, gob.NewEncoder(&out).Encode(items), should.BeNil)
    68  
    69  		b, unknown, err := deserializeBundle(out.Bytes())
    70  		assert.Loosely(t, err, should.BeNil)
    71  		assert.Loosely(t, unknown, should.Match(items))
    72  		assert.Loosely(t, len(b), should.BeZero)
    73  	})
    74  
    75  	ftt.Run("Rejects nil", t, func(t *ftt.Test) {
    76  		_, err := serializeBundle(ConfigBundle{"abc": nil})
    77  		assert.Loosely(t, err, should.NotBeNil)
    78  	})
    79  }