go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/strpair_test.go (about)

     1  // Copyright 2019 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 pbutil
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  
    24  	"go.chromium.org/luci/common/data/strpair"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  	pb "go.chromium.org/luci/resultdb/proto/v1"
    27  )
    28  
    29  func TestStringPairs(t *testing.T) {
    30  	t.Parallel()
    31  	Convey(`Works`, t, func() {
    32  		So(StringPairs("k1", "v1", "k2", "v2"), ShouldResemble, []*pb.StringPair{
    33  			{Key: "k1", Value: "v1"},
    34  			{Key: "k2", Value: "v2"},
    35  		})
    36  
    37  		Convey(`and fails if provided with incomplete pairs`, func() {
    38  			tokens := []string{"k1", "v1", "k2"}
    39  			So(func() { StringPairs(tokens...) }, ShouldPanicWith,
    40  				fmt.Sprintf("odd number of tokens in %q", tokens))
    41  		})
    42  
    43  		Convey(`when provided key:val string`, func() {
    44  			pair, err := StringPairFromString("key/k:v")
    45  			So(err, ShouldBeNil)
    46  			So(pair, ShouldResembleProto, &pb.StringPair{Key: "key/k", Value: "v"})
    47  		})
    48  
    49  		Convey(`when provided multiline value string`, func() {
    50  			pair, err := StringPairFromString("key/k:multiline\nstring\nvalue")
    51  			So(err, ShouldBeNil)
    52  			So(pair, ShouldResembleProto, &pb.StringPair{Key: "key/k", Value: "multiline\nstring\nvalue"})
    53  		})
    54  	})
    55  	Convey(`StringPairFromStringUnvalidated`, t, func() {
    56  		Convey(`valid key:val string`, func() {
    57  			pair := StringPairFromStringUnvalidated("key/k:v")
    58  			So(pair, ShouldResembleProto, &pb.StringPair{Key: "key/k", Value: "v"})
    59  		})
    60  
    61  		Convey(`invalid string with no colon`, func() {
    62  			pair := StringPairFromStringUnvalidated("key/k")
    63  			So(pair, ShouldResembleProto, &pb.StringPair{Key: "key/k", Value: ""})
    64  		})
    65  
    66  		Convey(`invalid chars in key`, func() {
    67  			pair := StringPairFromStringUnvalidated("##key/k:v")
    68  			So(pair, ShouldResembleProto, &pb.StringPair{Key: "##key/k", Value: "v"})
    69  		})
    70  	})
    71  }
    72  
    73  func TestValidateStringPair(t *testing.T) {
    74  	t.Parallel()
    75  	Convey(`TestValidateStringPairs`, t, func() {
    76  		Convey(`empty`, func() {
    77  			err := ValidateStringPair(StringPair("", ""))
    78  			So(err, ShouldErrLike, `key: unspecified`)
    79  		})
    80  
    81  		Convey(`invalid key`, func() {
    82  			err := ValidateStringPair(StringPair("1", ""))
    83  			So(err, ShouldErrLike, `key: does not match`)
    84  		})
    85  
    86  		Convey(`long key`, func() {
    87  			err := ValidateStringPair(StringPair(strings.Repeat("a", 1000), ""))
    88  			So(err, ShouldErrLike, `key length must be less or equal to 64`)
    89  		})
    90  
    91  		Convey(`long value`, func() {
    92  			err := ValidateStringPair(StringPair("a", strings.Repeat("a", 1000)))
    93  			So(err, ShouldErrLike, `value length must be less or equal to 256`)
    94  		})
    95  
    96  		Convey(`multiline value`, func() {
    97  			err := ValidateStringPair(StringPair("a", "multi\nline\nvalue"))
    98  			So(err, ShouldBeNil)
    99  		})
   100  
   101  		Convey(`valid`, func() {
   102  			err := ValidateStringPair(StringPair("a", "b"))
   103  			So(err, ShouldBeNil)
   104  		})
   105  	})
   106  }
   107  
   108  func TestFromStrpairMap(t *testing.T) {
   109  	t.Parallel()
   110  	Convey(`FromStrpairMap`, t, func() {
   111  		m := strpair.Map{}
   112  		m.Add("k1", "v1")
   113  		m.Add("k2", "v1")
   114  		m.Add("k2", "v2")
   115  
   116  		So(FromStrpairMap(m), ShouldResembleProto, StringPairs(
   117  			"k1", "v1",
   118  			"k2", "v1",
   119  			"k2", "v2",
   120  		))
   121  	})
   122  }