go.chromium.org/luci@v0.0.0-20250314024836-d9a61d0730e6/tokenserver/appengine/impl/certconfig/ca_test.go (about)

     1  // Copyright 2016 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 certconfig
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/appengine/gaetesting"
    22  	"go.chromium.org/luci/common/clock/testclock"
    23  	"go.chromium.org/luci/common/testing/ftt"
    24  	"go.chromium.org/luci/common/testing/truth/assert"
    25  	"go.chromium.org/luci/common/testing/truth/should"
    26  	ds "go.chromium.org/luci/gae/service/datastore"
    27  	"go.chromium.org/luci/server/caching"
    28  )
    29  
    30  func TestListCAs(t *testing.T) {
    31  	ftt.Run("ListCAs works", t, func(t *ftt.Test) {
    32  		ctx := gaetesting.TestingContext()
    33  
    34  		// Empty.
    35  		cas, err := ListCAs(ctx)
    36  		assert.Loosely(t, err, should.BeNil)
    37  		assert.Loosely(t, len(cas), should.BeZero)
    38  
    39  		// Add some.
    40  		err = ds.Put(ctx, &CA{CN: "abc", Removed: true}, &CA{CN: "def"})
    41  		assert.Loosely(t, err, should.BeNil)
    42  		ds.GetTestable(ctx).CatchupIndexes()
    43  
    44  		cas, err = ListCAs(ctx)
    45  		assert.Loosely(t, err, should.BeNil)
    46  		assert.Loosely(t, cas, should.Match([]string{"def"}))
    47  	})
    48  }
    49  
    50  func TestCAUniqueIDToCNMapLoadStore(t *testing.T) {
    51  	ftt.Run("CAUniqueIDToCNMap Load and Store works", t, func(t *ftt.Test) {
    52  		ctx := gaetesting.TestingContext()
    53  
    54  		// Empty.
    55  		mapping, err := LoadCAUniqueIDToCNMap(ctx)
    56  		assert.Loosely(t, err, should.BeNil)
    57  		assert.Loosely(t, len(mapping), should.BeZero)
    58  
    59  		// Store some.
    60  		toStore := map[int64]string{
    61  			1: "abc",
    62  			2: "def",
    63  		}
    64  		err = StoreCAUniqueIDToCNMap(ctx, toStore)
    65  		assert.Loosely(t, err, should.BeNil)
    66  
    67  		// Not empty now.
    68  		mapping, err = LoadCAUniqueIDToCNMap(ctx)
    69  		assert.Loosely(t, err, should.BeNil)
    70  		assert.Loosely(t, mapping, should.Match(toStore))
    71  	})
    72  }
    73  
    74  func TestGetCAByUniqueID(t *testing.T) {
    75  	ftt.Run("GetCAByUniqueID works", t, func(t *ftt.Test) {
    76  		ctx := gaetesting.TestingContext()
    77  		ctx = caching.WithEmptyProcessCache(ctx)
    78  		ctx, clk := testclock.UseTime(ctx, testclock.TestTimeUTC)
    79  
    80  		// Empty now.
    81  		val, err := GetCAByUniqueID(ctx, 1)
    82  		assert.Loosely(t, err, should.BeNil)
    83  		assert.Loosely(t, val, should.BeEmpty)
    84  
    85  		// Add some.
    86  		err = StoreCAUniqueIDToCNMap(ctx, map[int64]string{
    87  			1: "abc",
    88  			2: "def",
    89  		})
    90  		assert.Loosely(t, err, should.BeNil)
    91  
    92  		// Still empty (cached old value).
    93  		val, err = GetCAByUniqueID(ctx, 1)
    94  		assert.Loosely(t, err, should.BeNil)
    95  		assert.Loosely(t, val, should.BeEmpty)
    96  
    97  		// Updated after cache expires.
    98  		clk.Add(2 * time.Minute)
    99  		val, err = GetCAByUniqueID(ctx, 1)
   100  		assert.Loosely(t, err, should.BeNil)
   101  		assert.Loosely(t, val, should.Equal("abc"))
   102  	})
   103  }