Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy 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 caddypki |
| 16 | |
| 17 | import ( |
| 18 | "crypto/x509" |
| 19 | "testing" |
| 20 | "time" |
| 21 | ) |
| 22 | |
| 23 | func TestCA_needsRenewal(t *testing.T) { |
| 24 | now := time.Now() |
| 25 | |
| 26 | // cert with 100 days lifetime; last 20% = 20 days before expiry |
| 27 | // So renewal window starts at (NotAfter - 20 days) |
| 28 | makeCert := func(daysUntilExpiry int, lifetimeDays int) *x509.Certificate { |
| 29 | notAfter := now.AddDate(0, 0, daysUntilExpiry) |
| 30 | notBefore := notAfter.AddDate(0, 0, -lifetimeDays) |
| 31 | return &x509.Certificate{NotBefore: notBefore, NotAfter: notAfter} |
| 32 | } |
| 33 | |
| 34 | tests := []struct { |
| 35 | name string |
| 36 | ca *CA |
| 37 | cert *x509.Certificate |
| 38 | expect bool |
| 39 | }{ |
| 40 | { |
| 41 | name: "inside renewal window with ratio 0.2", |
| 42 | ca: &CA{RenewalWindowRatio: 0.2}, |
| 43 | cert: makeCert(10, 100), |
| 44 | expect: true, |
| 45 | }, |
| 46 | { |
| 47 | name: "outside renewal window with ratio 0.2", |
| 48 | ca: &CA{RenewalWindowRatio: 0.2}, |
| 49 | cert: makeCert(50, 100), |
| 50 | expect: false, |
| 51 | }, |
| 52 | { |
| 53 | name: "outside renewal window with 21 days left", |
| 54 | ca: &CA{RenewalWindowRatio: 0.2}, |
| 55 | cert: makeCert(21, 100), |
| 56 | expect: false, |
| 57 | }, |
| 58 | { |
| 59 | name: "just inside renewal window with ratio 0.5", |
| 60 | ca: &CA{RenewalWindowRatio: 0.5}, |
| 61 | cert: makeCert(30, 100), |
| 62 | expect: true, |
| 63 | }, |
| 64 | { |
| 65 | name: "zero ratio uses default", |
| 66 | ca: &CA{RenewalWindowRatio: 0}, |
| 67 | cert: makeCert(10, 100), |
| 68 | expect: true, |
| 69 | }, |
| 70 | { |
| 71 | name: "invalid ratio uses default", |
| 72 | ca: &CA{RenewalWindowRatio: 1.5}, |
| 73 | cert: makeCert(10, 100), |
| 74 | expect: true, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | for _, tt := range tests { |
| 79 | t.Run(tt.name, func(t *testing.T) { |
| 80 | got := tt.ca.needsRenewal(tt.cert) |
| 81 | if got != tt.expect { |
| 82 | t.Errorf("needsRenewal() = %v, want %v", got, tt.expect) |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |