| 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 |
"crypto/rand" |
| 19 |
"crypto/x509" |
| 20 |
"encoding/json" |
| 21 |
"errors" |
| 22 |
"math/big" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"reflect" |
| 26 |
"runtime" |
| 27 |
"strings" |
| 28 |
"testing" |
| 29 |
"time" |
| 30 |
|
| 31 |
corecrl "github.com/notaryproject/notation-core-go/revocation/crl" |
| 32 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 33 |
) |
| 34 |
|
| 35 |
func TestCache(t *testing.T) { |
| 36 |
t.Run("file cache implement Cache interface", func(t *testing.T) { |
| 37 |
root := t.TempDir() |
| 38 |
var coreCache corecrl.Cache |
| 39 |
var err error |
| 40 |
coreCache, err = NewFileCache(root) |
| 41 |
if err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
if _, ok := coreCache.(*FileCache); !ok { |
| 45 |
t.Fatal("FileCache does not implement coreCache") |
| 46 |
} |
| 47 |
}) |
| 48 |
} |
| 49 |
|
| 50 |
func TestFileCache(t *testing.T) { |
| 51 |
now := time.Now() |
| 52 |
certChain := testhelper.GetRevokableRSAChainWithRevocations(2, false, true) |
| 53 |
crlBytes, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 54 |
Number: big.NewInt(1), |
| 55 |
NextUpdate: now.Add(time.Hour), |
| 56 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 57 |
if err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
baseCRL, err := x509.ParseRevocationList(crlBytes) |
| 61 |
if err != nil { |
| 62 |
t.Fatal(err) |
| 63 |
} |
| 64 |
|
| 65 |
ctx := context.Background() |
| 66 |
root := t.TempDir() |
| 67 |
cache, err := NewFileCache(root) |
| 68 |
t.Run("NewFileCache", func(t *testing.T) { |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("expected no error, but got %v", err) |
| 71 |
} |
| 72 |
if cache.root != root { |
| 73 |
t.Fatalf("expected root %v, but got %v", root, cache.root) |
| 74 |
} |
| 75 |
}) |
| 76 |
|
| 77 |
key := "http://example.com" |
| 78 |
t.Run("comformance", func(t *testing.T) { |
| 79 |
bundle := &corecrl.Bundle{BaseCRL: baseCRL} |
| 80 |
if err := cache.Set(ctx, key, bundle); err != nil { |
| 81 |
t.Fatal(err) |
| 82 |
} |
| 83 |
retrievedBundle, err := cache.Get(ctx, key) |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
|
| 88 |
if !reflect.DeepEqual(retrievedBundle.BaseCRL, bundle.BaseCRL) { |
| 89 |
t.Fatalf("expected BaseCRL %+v, but got %+v", bundle.BaseCRL, retrievedBundle.BaseCRL) |
| 90 |
} |
| 91 |
|
| 92 |
if bundle.DeltaCRL != nil { |
| 93 |
t.Fatalf("expected DeltaCRL to be nil, but got %+v", retrievedBundle.DeltaCRL) |
| 94 |
} |
| 95 |
}) |
| 96 |
|
| 97 |
t.Run("comformance with delta crl", func(t *testing.T) { |
| 98 |
crlBytes, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 99 |
Number: big.NewInt(2), |
| 100 |
NextUpdate: now.Add(time.Hour), |
| 101 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
deltaCRL, err := x509.ParseRevocationList(crlBytes) |
| 106 |
if err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
bundle := &corecrl.Bundle{BaseCRL: baseCRL, DeltaCRL: deltaCRL} |
| 110 |
if err := cache.Set(ctx, key, bundle); err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
retrievedBundle, err := cache.Get(ctx, key) |
| 114 |
if err != nil { |
| 115 |
t.Fatal(err) |
| 116 |
} |
| 117 |
|
| 118 |
if !reflect.DeepEqual(retrievedBundle.BaseCRL, bundle.BaseCRL) { |
| 119 |
t.Fatalf("expected BaseCRL %+v, but got %+v", bundle.BaseCRL, retrievedBundle.BaseCRL) |
| 120 |
} |
| 121 |
|
| 122 |
if !reflect.DeepEqual(retrievedBundle.DeltaCRL, bundle.DeltaCRL) { |
| 123 |
t.Fatalf("expected DeltaCRL %+v, but got %+v", bundle.DeltaCRL, retrievedBundle.DeltaCRL) |
| 124 |
} |
| 125 |
}) |
| 126 |
} |
| 127 |
|
| 128 |
func TestNewFileCacheFailed(t *testing.T) { |
| 129 |
tempDir := t.TempDir() |
| 130 |
t.Run("without permission to create cache directory", func(t *testing.T) { |
| 131 |
if runtime.GOOS == "windows" { |
| 132 |
t.Skip("skipping test on Windows") |
| 133 |
} |
| 134 |
|
| 135 |
if err := os.Chmod(tempDir, 0); err != nil { |
| 136 |
t.Fatal(err) |
| 137 |
} |
| 138 |
root := filepath.Join(tempDir, "test") |
| 139 |
_, err := NewFileCache(root) |
| 140 |
if !strings.Contains(err.Error(), "permission denied") { |
| 141 |
t.Fatalf("expected permission denied error, but got %v", err) |
| 142 |
} |
| 143 |
// restore permission |
| 144 |
if err := os.Chmod(tempDir, 0755); err != nil { |
| 145 |
t.Fatalf("failed to change permission: %v", err) |
| 146 |
} |
| 147 |
}) |
| 148 |
} |
| 149 |
|
| 150 |
func TestGetFailed(t *testing.T) { |
| 151 |
tempDir := t.TempDir() |
| 152 |
cache, err := NewFileCache(tempDir) |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
|
| 157 |
t.Run("key does not exist", func(t *testing.T) { |
| 158 |
_, err := cache.Get(context.Background(), "nonExistKey") |
| 159 |
if !errors.Is(err, corecrl.ErrCacheMiss) { |
| 160 |
t.Fatalf("expected ErrCacheMiss, but got %v", err) |
| 161 |
} |
| 162 |
}) |
| 163 |
|
| 164 |
invalidFile := filepath.Join(tempDir, cache.fileName("invalid")) |
| 165 |
if err := os.WriteFile(invalidFile, []byte("invalid"), 0644); err != nil { |
| 166 |
t.Fatalf("failed to write file: %v", err) |
| 167 |
} |
| 168 |
|
| 169 |
t.Run("no permission to read file", func(t *testing.T) { |
| 170 |
if runtime.GOOS == "windows" { |
| 171 |
t.Skip("skipping test on Windows") |
| 172 |
} |
| 173 |
|
| 174 |
if err := os.Chmod(invalidFile, 0); err != nil { |
| 175 |
t.Fatal(err) |
| 176 |
} |
| 177 |
_, err := cache.Get(context.Background(), "invalid") |
| 178 |
if err == nil || !strings.Contains(err.Error(), "permission denied") { |
| 179 |
t.Fatalf("expected permission denied error, but got %v", err) |
| 180 |
} |
| 181 |
// restore permission |
| 182 |
if err := os.Chmod(invalidFile, 0755); err != nil { |
| 183 |
t.Fatal(err) |
| 184 |
} |
| 185 |
}) |
| 186 |
|
| 187 |
t.Run("invalid content", func(t *testing.T) { |
| 188 |
_, err := cache.Get(context.Background(), "invalid") |
| 189 |
expectedErrMsg := "failed to decode file retrieved from file cache: invalid character 'i' looking for beginning of value" |
| 190 |
if err == nil || err.Error() != expectedErrMsg { |
| 191 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 192 |
} |
| 193 |
}) |
| 194 |
|
| 195 |
now := time.Now() |
| 196 |
certChain := testhelper.GetRevokableRSAChainWithRevocations(2, false, true) |
| 197 |
crlBytes, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 198 |
Number: big.NewInt(1), |
| 199 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("failed to create base CRL: %v", err) |
| 202 |
} |
| 203 |
baseCRL, err := x509.ParseRevocationList(crlBytes) |
| 204 |
if err != nil { |
| 205 |
t.Fatalf("failed to parse base CRL: %v", err) |
| 206 |
} |
| 207 |
|
| 208 |
t.Run("empty RawBaseCRL of content", func(t *testing.T) { |
| 209 |
content := fileCacheContent{ |
| 210 |
BaseCRL: []byte{}, |
| 211 |
} |
| 212 |
b, err := json.Marshal(content) |
| 213 |
if err != nil { |
| 214 |
t.Fatal(err) |
| 215 |
} |
| 216 |
invalidBundleFile := filepath.Join(tempDir, cache.fileName("invalidBundle")) |
| 217 |
if err := os.WriteFile(invalidBundleFile, b, 0644); err != nil { |
| 218 |
t.Fatalf("failed to write file: %v", err) |
| 219 |
} |
| 220 |
_, err = cache.Get(context.Background(), "invalidBundle") |
| 221 |
expectedErrMsg := "failed to parse base CRL of file retrieved from file cache: x509: malformed crl" |
| 222 |
if err == nil || err.Error() != expectedErrMsg { |
| 223 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 224 |
} |
| 225 |
}) |
| 226 |
|
| 227 |
t.Run("invalid RawBaseCRL of content", func(t *testing.T) { |
| 228 |
content := fileCacheContent{ |
| 229 |
BaseCRL: []byte("invalid"), |
| 230 |
} |
| 231 |
b, err := json.Marshal(content) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
invalidBundleFile := filepath.Join(tempDir, cache.fileName("invalidBundle")) |
| 236 |
if err := os.WriteFile(invalidBundleFile, b, 0644); err != nil { |
| 237 |
t.Fatalf("failed to write file: %v", err) |
| 238 |
} |
| 239 |
_, err = cache.Get(context.Background(), "invalidBundle") |
| 240 |
expectedErrMsg := "failed to parse base CRL of file retrieved from file cache: x509: malformed crl" |
| 241 |
if err == nil || err.Error() != expectedErrMsg { |
| 242 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 243 |
} |
| 244 |
}) |
| 245 |
|
| 246 |
t.Run("invalid RawDeltaCRL of content", func(t *testing.T) { |
| 247 |
content := fileCacheContent{ |
| 248 |
BaseCRL: baseCRL.Raw, |
| 249 |
DeltaCRL: []byte("invalid"), |
| 250 |
} |
| 251 |
b, err := json.Marshal(content) |
| 252 |
if err != nil { |
| 253 |
t.Fatal(err) |
| 254 |
} |
| 255 |
invalidBundleFile := filepath.Join(tempDir, cache.fileName("invalidBundle")) |
| 256 |
if err := os.WriteFile(invalidBundleFile, b, 0644); err != nil { |
| 257 |
t.Fatalf("failed to write file: %v", err) |
| 258 |
} |
| 259 |
_, err = cache.Get(context.Background(), "invalidBundle") |
| 260 |
expectedErrMsg := "failed to parse delta CRL of file retrieved from file cache: x509: malformed crl" |
| 261 |
if err == nil || err.Error() != expectedErrMsg { |
| 262 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 263 |
} |
| 264 |
}) |
| 265 |
|
| 266 |
t.Run("bundle with invalid NextUpdate", func(t *testing.T) { |
| 267 |
ctx := context.Background() |
| 268 |
expiredBundle := &corecrl.Bundle{BaseCRL: baseCRL} |
| 269 |
if err := cache.Set(ctx, "expiredKey", expiredBundle); err != nil { |
| 270 |
t.Fatal(err) |
| 271 |
} |
| 272 |
_, err = cache.Get(ctx, "expiredKey") |
| 273 |
expectedErrMsg := "check BaseCRL expiry failed: crl bundle retrieved from file cache does not contain valid NextUpdate" |
| 274 |
if err == nil || err.Error() != expectedErrMsg { |
| 275 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 276 |
} |
| 277 |
}) |
| 278 |
|
| 279 |
crlBytes, err = x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 280 |
Number: big.NewInt(1), |
| 281 |
NextUpdate: now.Add(-time.Hour), |
| 282 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 283 |
if err != nil { |
| 284 |
t.Fatalf("failed to create base CRL: %v", err) |
| 285 |
} |
| 286 |
expiredBaseCRL, err := x509.ParseRevocationList(crlBytes) |
| 287 |
if err != nil { |
| 288 |
t.Fatalf("failed to parse base CRL: %v", err) |
| 289 |
} |
| 290 |
t.Run("base crl in cache has expired", func(t *testing.T) { |
| 291 |
ctx := context.Background() |
| 292 |
expiredBundle := &corecrl.Bundle{BaseCRL: expiredBaseCRL} |
| 293 |
if err := cache.Set(ctx, "expiredKey", expiredBundle); err != nil { |
| 294 |
t.Fatal(err) |
| 295 |
} |
| 296 |
_, err = cache.Get(ctx, "expiredKey") |
| 297 |
if !errors.Is(err, corecrl.ErrCacheMiss) { |
| 298 |
t.Fatalf("expected ErrCacheMiss, but got %v", err) |
| 299 |
} |
| 300 |
}) |
| 301 |
|
| 302 |
t.Run("delta crl in cache has expired", func(t *testing.T) { |
| 303 |
ctx := context.Background() |
| 304 |
crlBytes, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 305 |
Number: big.NewInt(1), |
| 306 |
NextUpdate: now.Add(time.Hour), |
| 307 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 308 |
if err != nil { |
| 309 |
t.Fatalf("failed to create base CRL: %v", err) |
| 310 |
} |
| 311 |
baseCRL, err := x509.ParseRevocationList(crlBytes) |
| 312 |
if err != nil { |
| 313 |
t.Fatalf("failed to parse base CRL: %v", err) |
| 314 |
} |
| 315 |
crlBytes, err = x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 316 |
Number: big.NewInt(1), |
| 317 |
NextUpdate: now.Add(-time.Hour), |
| 318 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 319 |
if err != nil { |
| 320 |
t.Fatalf("failed to create base CRL: %v", err) |
| 321 |
} |
| 322 |
expiredDeltaCRL, err := x509.ParseRevocationList(crlBytes) |
| 323 |
if err != nil { |
| 324 |
t.Fatalf("failed to parse base CRL: %v", err) |
| 325 |
} |
| 326 |
expiredBundle := &corecrl.Bundle{BaseCRL: baseCRL, DeltaCRL: expiredDeltaCRL} |
| 327 |
if err := cache.Set(ctx, "expiredKey", expiredBundle); err != nil { |
| 328 |
t.Fatal(err) |
| 329 |
} |
| 330 |
_, err = cache.Get(ctx, "expiredKey") |
| 331 |
if !errors.Is(err, corecrl.ErrCacheMiss) { |
| 332 |
t.Fatalf("expected ErrCacheMiss, but got %v", err) |
| 333 |
} |
| 334 |
}) |
| 335 |
} |
| 336 |
|
| 337 |
func TestSetFailed(t *testing.T) { |
| 338 |
tempDir := t.TempDir() |
| 339 |
cache, err := NewFileCache(tempDir) |
| 340 |
if err != nil { |
| 341 |
t.Fatal(err) |
| 342 |
} |
| 343 |
|
| 344 |
now := time.Now() |
| 345 |
certChain := testhelper.GetRevokableRSAChainWithRevocations(2, false, true) |
| 346 |
crlBytes, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ |
| 347 |
Number: big.NewInt(1), |
| 348 |
NextUpdate: now.Add(time.Hour), |
| 349 |
}, certChain[1].Cert, certChain[1].PrivateKey) |
| 350 |
if err != nil { |
| 351 |
t.Fatal(err) |
| 352 |
} |
| 353 |
baseCRL, err := x509.ParseRevocationList(crlBytes) |
| 354 |
if err != nil { |
| 355 |
t.Fatal(err) |
| 356 |
} |
| 357 |
ctx := context.Background() |
| 358 |
key := "testKey" |
| 359 |
|
| 360 |
t.Run("nil bundle", func(t *testing.T) { |
| 361 |
err := cache.Set(ctx, key, nil) |
| 362 |
expectedErrMsg := "failed to store crl bundle in file cache: bundle cannot be nil" |
| 363 |
if err == nil || err.Error() != expectedErrMsg { |
| 364 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 365 |
} |
| 366 |
}) |
| 367 |
|
| 368 |
t.Run("nil bundle BaseCRL", func(t *testing.T) { |
| 369 |
bundle := &corecrl.Bundle{} |
| 370 |
err := cache.Set(ctx, key, bundle) |
| 371 |
expectedErrMsg := "failed to store crl bundle in file cache: bundle BaseCRL cannot be nil" |
| 372 |
if err == nil || err.Error() != expectedErrMsg { |
| 373 |
t.Fatalf("expected %s, but got %v", expectedErrMsg, err) |
| 374 |
} |
| 375 |
}) |
| 376 |
|
| 377 |
t.Run("failed to write into cache due to permission denied", func(t *testing.T) { |
| 378 |
if runtime.GOOS == "windows" { |
| 379 |
t.Skip("skipping test on Windows") |
| 380 |
} |
| 381 |
|
| 382 |
if err := os.Chmod(tempDir, 0); err != nil { |
| 383 |
t.Fatal(err) |
| 384 |
} |
| 385 |
bundle := &corecrl.Bundle{BaseCRL: baseCRL} |
| 386 |
err := cache.Set(ctx, key, bundle) |
| 387 |
if err == nil || !strings.Contains(err.Error(), "permission denied") { |
| 388 |
t.Fatalf("expected permission denied error, but got %v", err) |
| 389 |
} |
| 390 |
// restore permission |
| 391 |
if err := os.Chmod(tempDir, 0755); err != nil { |
| 392 |
t.Fatalf("failed to change permission: %v", err) |
| 393 |
} |
| 394 |
}) |
| 395 |
} |