Debian Codegraph @

caddyconfig/httpcaddyfile/pkiapp_test.go

raw symbols SCIP
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 httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/
16
17 import (
18 "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/"
19 "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/"
20 "timescip-go gomod github.com/golang/go/src go1.25.1 time/"
21
22 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/"
23 )
24
25 func TestParsePKIApp_maintenanceIntervalAndRenewalWindowRatioscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/TestParsePKIApp_maintenanceIntervalAndRenewalWindowRatio().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 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 := caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Adapterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Adapter#{ServerTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Adapter#ServerType.: ServerTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ServerType#{}}
38 out, _, err := adapter.Adaptscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Adapter#Adapt().([]byte(input), nil)
39 if err != nil {
40 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#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 := jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Unmarshal().(out, &cfg); err != nil {
54 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("unmarshal config: %v", err)
55 }
56
57 ca, ok := cfg.Apps.PKI.CertificateAuthorities["local"]
58 if !ok {
59 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().("expected certificate_authorities.local to exist")
60 }
61 wantInterval := 5 * timescip-go gomod github.com/golang/go/src go1.25.1 time/.Minutescip-go gomod github.com/golang/go/src go1.25.1 time/Minute..Nanosecondsscip-go gomod github.com/golang/go/src go1.25.1 time/Duration#Nanoseconds().()
62 if ca.MaintenanceInterval != wantInterval {
63 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("maintenance_interval = %d, want %d (5m)", ca.MaintenanceInterval, wantInterval)
64 }
65 if ca.RenewalWindowRatio != 0.15 {
66 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("renewal_window_ratio = %v, want 0.15", ca.RenewalWindowRatio)
67 }
68 }
69
70 func TestParsePKIApp_renewalWindowRatioInvalidscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/TestParsePKIApp_renewalWindowRatioInvalid().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
71 input := `{
72 pki {
73 ca local {
74 renewal_window_ratio 1.5
75 }
76 }
77 }
78 :8080 {
79 }
80 `
81 adapter := caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Adapterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Adapter#{ServerTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Adapter#ServerType.: ServerTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ServerType#{}}
82 _, _, err := adapter.Adaptscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Adapter#Adapt().([]byte(input), nil)
83 if err == nil {
84 t.Errorscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Error().("expected error for renewal_window_ratio > 1")
85 }
86 }