MCPcopy
hub / github.com/nytimes/gizmo / TestResuseKeySource

Function TestResuseKeySource

auth/keys_test.go:16–75  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestResuseKeySource(t *testing.T) {
17 testTime := time.Date(2018, 10, 29, 12, 0, 0, 0, time.UTC)
18
19 TimeNow = func() time.Time { return testTime }
20
21 firstKeys, err := NewPublicKeySetFromJSON([]byte(testGoogleCerts), 1*time.Second)
22 if err != nil {
23 t.Errorf("unexpected error creating key set: %s", err)
24 return
25 }
26
27 nextKeys := PublicKeySet{
28 Expiry: testTime.Add(2 * time.Second),
29 Keys: map[string]*rsa.PublicKey{
30 "8289d54280b76712de41cd2ef95972b123be9ac0": &rsa.PublicKey{N: testGoogle(testGoogleKey2), E: 65537},
31 },
32 }
33
34 reuser := NewReusePublicKeySource(firstKeys, testKeySource{keys: nextKeys})
35
36 // first get, firstKeys are not expired and should be returned
37
38 gotKeys, err := reuser.Get(context.Background())
39 if err != nil {
40 t.Errorf("unexpected error getting keys: %s", err)
41 return
42 }
43 if !cmp.Equal(gotKeys, firstKeys, cmpopts.IgnoreUnexported(big.Int{})) {
44 t.Errorf("first keys did not match expectations: %s", cmp.Diff(gotKeys, firstKeys,
45 cmpopts.IgnoreUnexported(big.Int{})))
46 return
47 }
48
49 // move time forward, expire the first keys
50 TimeNow = func() time.Time { return testTime.Add(1500 * time.Millisecond) }
51
52 gotKeys, err = reuser.Get(context.Background())
53 if err != nil {
54 t.Errorf("unexpected error getting keys: %s", err)
55 return
56 }
57 if !cmp.Equal(gotKeys, nextKeys, cmpopts.IgnoreUnexported(big.Int{})) {
58 t.Errorf("next keys did not match expectations: %s", cmp.Diff(gotKeys, nextKeys,
59 cmpopts.IgnoreUnexported(big.Int{})))
60 return
61 }
62
63 // verify get works
64 k, err := gotKeys.GetKey("8289d54280b76712de41cd2ef95972b123be9ac0")
65 if err != nil {
66 t.Errorf("unexpected error getting key: %s", err)
67 }
68 if !cmp.Equal(k, nextKeys.Keys["8289d54280b76712de41cd2ef95972b123be9ac0"],
69 cmpopts.IgnoreUnexported(big.Int{})) {
70 t.Errorf("next keys did not match expectations: %s",
71 cmp.Diff(k, nextKeys.Keys["8289d54280b76712de41cd2ef95972b123be9ac0"],
72 cmpopts.IgnoreUnexported(big.Int{})))
73 return

Callers

nothing calls this directly

Calls 5

NewPublicKeySetFromJSONFunction · 0.85
testGoogleFunction · 0.85
NewReusePublicKeySourceFunction · 0.85
GetKeyMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…