go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/rpc/pagination/token_test.go (about) 1 // Copyright 2021 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 pagination 16 17 import ( 18 "context" 19 "testing" 20 21 "google.golang.org/grpc/codes" 22 "google.golang.org/protobuf/types/known/timestamppb" 23 24 "go.chromium.org/luci/common/clock/testclock" 25 "go.chromium.org/luci/server/secrets" 26 27 . "github.com/smartystreets/goconvey/convey" 28 . "go.chromium.org/luci/common/testing/assertions" 29 ) 30 31 func TestPageTokens(t *testing.T) { 32 t.Parallel() 33 ctx := secrets.GeneratePrimaryTinkAEADForTest(context.Background()) 34 35 Convey("Page token round trip", t, func() { 36 Convey("not empty", func() { 37 // Any proto type should work. 38 in := timestamppb.New(testclock.TestRecentTimeUTC) 39 token, err := EncryptPageToken(ctx, in) 40 So(err, ShouldBeNil) 41 out := ×tamppb.Timestamp{} 42 So(DecryptPageToken(ctx, token, out), ShouldBeNil) 43 So(out, ShouldResembleProto, in) 44 }) 45 Convey("empty page token", func() { 46 token, err := EncryptPageToken(ctx, nil) 47 So(err, ShouldBeNil) 48 So(token, ShouldResemble, "") 49 out := ×tamppb.Timestamp{} 50 So(DecryptPageToken(ctx, token, out), ShouldBeNil) 51 So(out, ShouldResembleProto, ×tamppb.Timestamp{}) 52 }) 53 Convey("empty page token with typed nil", func() { 54 var typedNil *timestamppb.Timestamp 55 token, err := EncryptPageToken(ctx, typedNil) 56 So(err, ShouldBeNil) 57 So(token, ShouldResemble, "") 58 }) 59 }) 60 61 Convey("Bad page token", t, func() { 62 dst := ×tamppb.Timestamp{Seconds: 1, Nanos: 2} 63 goodToken, err := EncryptPageToken(ctx, timestamppb.New(testclock.TestRecentTimeUTC)) 64 So(err, ShouldBeNil) 65 tokenBytes := []byte(goodToken) 66 tokenBytes[10] = '\\' 67 err = DecryptPageToken(ctx, string(tokenBytes), dst) 68 So(err, ShouldErrLike, "illegal base64") 69 So(err, ShouldHaveAppStatus, codes.InvalidArgument, "invalid page token") 70 }) 71 }