go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/appengine/coordinator/logStreamEncoding_test.go (about)

     1  // Copyright 2015 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 coordinator
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  )
    24  
    25  func TestLogStreamEncoding(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	Convey(`Testing log stream key encode/decode`, t, func() {
    29  
    30  		Convey(`Will encode "quux" into "Key_cXV1eA~~".`, func() {
    31  			// Note that we test a key whose length is not a multiple of 3 so that we
    32  			// can assert that the padding is correct, too.
    33  			enc := encodeKey("quux")
    34  			So(enc, ShouldEqual, "Key_cXV1eA~~")
    35  		})
    36  
    37  		for _, s := range []string{
    38  			"",
    39  			"hello",
    40  			"from the outside",
    41  			"+-#$!? \t\n",
    42  		} {
    43  			Convey(fmt.Sprintf(`Can encode: %q`, s), func() {
    44  				enc := encodeKey(s)
    45  				So(enc, ShouldStartWith, encodedKeyPrefix)
    46  
    47  				Convey(`And then decode back into the original string.`, func() {
    48  					dec, err := decodeKey(enc)
    49  					So(err, ShouldBeNil)
    50  					So(dec, ShouldEqual, s)
    51  				})
    52  			})
    53  		}
    54  
    55  		Convey(`Will fail to decode a string that doesn't begin with the encoded prefix.`, func() {
    56  			_, err := decodeKey("foo")
    57  			So(err, ShouldErrLike, "encoded key missing prefix")
    58  		})
    59  
    60  		Convey(`Will fail to decode a string that's not properly encoded.`, func() {
    61  			_, err := decodeKey(encodedKeyPrefix + "!!!")
    62  			So(err, ShouldErrLike, "failed to decode key")
    63  		})
    64  	})
    65  }