|
| 1 | +// Copyright The Notary Project Authors. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package crl |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "errors" |
| 19 | + "os" |
| 20 | + "sync" |
| 21 | + "testing" |
| 22 | + |
| 23 | + corecrl "github.com/notaryproject/notation-core-go/revocation/crl" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGet(t *testing.T) { |
| 27 | + cache := &CacheWithLog{} |
| 28 | + expectedErrMsg := "cache cannot be nil" |
| 29 | + _, err := cache.Get(context.Background(), "") |
| 30 | + if err == nil || err.Error() != expectedErrMsg { |
| 31 | + t.Fatalf("expected error %q, but got %q", expectedErrMsg, err) |
| 32 | + } |
| 33 | + |
| 34 | + cache = &CacheWithLog{ |
| 35 | + Cache: &dummyCache{}, |
| 36 | + } |
| 37 | + expectedErrMsg = "cache get failed" |
| 38 | + _, err = cache.Get(context.Background(), "") |
| 39 | + if err == nil || err.Error() != expectedErrMsg { |
| 40 | + t.Fatalf("expected error %q, but got %q", expectedErrMsg, err) |
| 41 | + } |
| 42 | + |
| 43 | + cache = &CacheWithLog{ |
| 44 | + Cache: &dummyCache{ |
| 45 | + cacheMiss: true, |
| 46 | + }, |
| 47 | + } |
| 48 | + _, err = cache.Get(context.Background(), "") |
| 49 | + if err == nil || !errors.Is(err, corecrl.ErrCacheMiss) { |
| 50 | + t.Fatalf("expected error %q, but got %q", corecrl.ErrCacheMiss, err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestSet(t *testing.T) { |
| 55 | + cache := &CacheWithLog{} |
| 56 | + expectedErrMsg := "cache cannot be nil" |
| 57 | + err := cache.Set(context.Background(), "", nil) |
| 58 | + if err == nil || err.Error() != expectedErrMsg { |
| 59 | + t.Fatalf("expected error %q, but got %q", expectedErrMsg, err) |
| 60 | + } |
| 61 | + |
| 62 | + cache = &CacheWithLog{ |
| 63 | + Cache: &dummyCache{}, |
| 64 | + } |
| 65 | + expectedErrMsg = "cache set failed" |
| 66 | + err = cache.Set(context.Background(), "", nil) |
| 67 | + if err == nil || err.Error() != expectedErrMsg { |
| 68 | + t.Fatalf("expected error %q, but got %q", expectedErrMsg, err) |
| 69 | + } |
| 70 | + |
| 71 | + cache = &CacheWithLog{ |
| 72 | + Cache: &dummyCache{ |
| 73 | + setSuccess: true, |
| 74 | + }, |
| 75 | + } |
| 76 | + err = cache.Set(context.Background(), "", nil) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("expected nil error, but got %q", err) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestLogDiscardErrorOnce(t *testing.T) { |
| 83 | + cache := &CacheWithLog{ |
| 84 | + Cache: &dummyCache{}, |
| 85 | + DiscardCacheError: true, |
| 86 | + } |
| 87 | + oldStderr := os.Stderr |
| 88 | + defer func() { |
| 89 | + os.Stderr = oldStderr |
| 90 | + }() |
| 91 | + testFile, err := os.CreateTemp(t.TempDir(), "testNotation") |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + defer testFile.Close() |
| 96 | + os.Stderr = testFile |
| 97 | + var wg sync.WaitGroup |
| 98 | + for i := 0; i < 3; i++ { |
| 99 | + wg.Add(1) |
| 100 | + go func() { |
| 101 | + defer wg.Done() |
| 102 | + cache.Get(context.Background(), "") |
| 103 | + cache.Set(context.Background(), "", nil) |
| 104 | + }() |
| 105 | + } |
| 106 | + wg.Wait() |
| 107 | + |
| 108 | + b, err := os.ReadFile(testFile.Name()) |
| 109 | + if err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + expectedMsg := "Warning: CRL cache error discarded. Enable debug log through '-d' for error details.\n" |
| 113 | + if string(b) != expectedMsg { |
| 114 | + t.Fatalf("expected to get %q, but got %q", expectedMsg, string(b)) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +type dummyCache struct { |
| 119 | + cacheMiss bool |
| 120 | + setSuccess bool |
| 121 | +} |
| 122 | + |
| 123 | +func (d *dummyCache) Get(ctx context.Context, url string) (*corecrl.Bundle, error) { |
| 124 | + if d.cacheMiss { |
| 125 | + return nil, corecrl.ErrCacheMiss |
| 126 | + } |
| 127 | + return nil, errors.New("cache get failed") |
| 128 | +} |
| 129 | + |
| 130 | +func (d *dummyCache) Set(ctx context.Context, url string, bundle *corecrl.Bundle) error { |
| 131 | + if d.setSuccess { |
| 132 | + return nil |
| 133 | + } |
| 134 | + return errors.New("cache set failed") |
| 135 | +} |
0 commit comments