| 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 httpcaddyfile |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "testing" |
| 20 | "time" |
| 21 | |
| 22 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 23 | ) |
| 24 | |
| 25 | func TestParsePKIApp_maintenanceIntervalAndRenewalWindowRatio(t *testing.T) { |
| 26 | input := `{ |
| 27 | pki { |
| 28 | ca local { |
| 29 | maintenance_interval 5m |
| 30 | renewal_window_ratio 0.15 |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | :8080 { |
| 35 | } |
| 36 | ` |
| 37 | adapter := caddyfile.Adapter{ServerType: ServerType{}} |
| 38 | out, _, err := adapter.Adapt([]byte(input), nil) |
| 39 | if err != nil { |
| 40 | t.Fatalf("Adapt failed: %v", err) |
| 41 | } |
| 42 | |
| 43 | var cfg struct { |
| 44 | Apps struct { |
| 45 | PKI struct { |
| 46 | CertificateAuthorities map[string]struct { |
| 47 | MaintenanceInterval int64 `json:"maintenance_interval,omitempty"` |
| 48 | RenewalWindowRatio float64 `json:"renewal_window_ratio,omitempty"` |
| 49 | } `json:"certificate_authorities,omitempty"` |
| 50 | } `json:"pki,omitempty"` |
| 51 | } `json:"apps"` |
| 52 | } |
| 53 | if err := json.Unmarshal(out, &cfg); err != nil { |
| 54 | t.Fatalf("unmarshal config: %v", err) |
| 55 | } |
| 56 | |
| 57 | ca, ok := cfg.Apps.PKI.CertificateAuthorities["local"] |
| 58 | if !ok { |
| 59 | t.Fatal("expected certificate_authorities.local to exist") |
| 60 | } |
| 61 | wantInterval := 5 * time.Minute.Nanoseconds() |
| 62 | if ca.MaintenanceInterval != wantInterval { |
| 63 | t.Errorf("maintenance_interval = %d, want %d (5m)", ca.MaintenanceInterval, wantInterval) |
| 64 | } |
| 65 | if ca.RenewalWindowRatio != 0.15 { |
| 66 | t.Errorf("renewal_window_ratio = %v, want 0.15", ca.RenewalWindowRatio) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestParsePKIApp_renewalWindowRatioInvalid(t *testing.T) { |
| 71 | input := `{ |
| 72 | pki { |
| 73 | ca local { |
| 74 | renewal_window_ratio 1.5 |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | :8080 { |
| 79 | } |
| 80 | ` |
| 81 | adapter := caddyfile.Adapter{ServerType: ServerType{}} |
| 82 | _, _, err := adapter.Adapt([]byte(input), nil) |
| 83 | if err == nil { |
| 84 | t.Error("expected error for renewal_window_ratio > 1") |
| 85 | } |
| 86 | } |