go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tokenserver/appengine/impl/utils/tokensigning/roundtrip_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 tokensigning
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/server/auth/delegation/messages"
    22  	"go.chromium.org/luci/server/auth/signing"
    23  	"go.chromium.org/luci/server/auth/signing/signingtest"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  	. "go.chromium.org/luci/common/testing/assertions"
    27  )
    28  
    29  func TestRoundtrip(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	ctx := context.Background()
    33  	original := &messages.Subtoken{
    34  		DelegatedIdentity: "user:delegated@example.com",
    35  		RequestorIdentity: "user:requestor@example.com",
    36  		CreationTime:      1477624966,
    37  		ValidityDuration:  3600,
    38  		Audience:          []string{"*"},
    39  		Services:          []string{"*"},
    40  	}
    41  	signer := signingtest.NewSigner(&signing.ServiceInfo{
    42  		ServiceAccountName: "service@example.com",
    43  	})
    44  
    45  	Convey("Sign/Inspect works (no signing context)", t, func() {
    46  		tokSigner := signerForTest(signer, "")
    47  		tokInspector := inspectorForTest(signer, "")
    48  
    49  		tok, err := tokSigner.SignToken(ctx, original)
    50  		So(err, ShouldBeNil)
    51  
    52  		insp, err := tokInspector.InspectToken(ctx, tok)
    53  		So(err, ShouldBeNil)
    54  
    55  		So(insp.Signed, ShouldBeTrue)
    56  		So(insp.Body, ShouldResembleProto, original)
    57  	})
    58  
    59  	Convey("Sign/Inspect works (with context)", t, func() {
    60  		tokSigner := signerForTest(signer, "Some context")
    61  		tokInspector := inspectorForTest(signer, "Some context")
    62  
    63  		tok, err := tokSigner.SignToken(ctx, original)
    64  		So(err, ShouldBeNil)
    65  
    66  		insp, err := tokInspector.InspectToken(ctx, tok)
    67  		So(err, ShouldBeNil)
    68  
    69  		So(insp.Signed, ShouldBeTrue)
    70  		So(insp.Body, ShouldResembleProto, original)
    71  	})
    72  
    73  	Convey("Sign/Inspect works (wrong context)", t, func() {
    74  		tokSigner := signerForTest(signer, "Some context")
    75  		tokInspector := inspectorForTest(signer, "Another context")
    76  
    77  		tok, err := tokSigner.SignToken(ctx, original)
    78  		So(err, ShouldBeNil)
    79  
    80  		insp, err := tokInspector.InspectToken(ctx, tok)
    81  		So(err, ShouldBeNil)
    82  
    83  		So(insp.Signed, ShouldBeFalse)
    84  	})
    85  }