Debian Codegraph @

modules/caddytls/tls_wildcard_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 caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/
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
21 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
22 )
23
24 func TestAvoidDuplicateAutomationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TestAvoidDuplicateAutomation().(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#) {
25 tests := []struct {
26 name string
27 automateNames []string
28 expectedToManage bool
29 }{
30 {
31 name: "do not manage if wildcard is automated",
32 automateNames: []string{"*.example.com"},
33 expectedToManage: false,
34 },
35 {
36 name: "manage if no automation configured",
37 automateNames: []string{},
38 expectedToManage: true,
39 },
40 {
41 name: "manage if explicitly requested even when wildcard automated",
42 automateNames: []string{"*.example.com", "sub.example.com"},
43 expectedToManage: true,
44 },
45 }
46
47 for _, tc := range tests {
48 t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tc.name, func(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#) {
49 automateJSON, err := jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Marshal().(tc.automateNames)
50 if err != nil {
51 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().(err)
52 }
53
54 tlsApp := &TLSscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TLS#{
55 Automationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TLS#Automation.: &AutomationConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/AutomationConfig#{
56 Policiesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/AutomationConfig#Policies.: []*AutomationPolicyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/AutomationPolicy#{
57 {
58 IssuersRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/AutomationPolicy#IssuersRaw.: []jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/RawMessage#{
59 []byte(`{"module": "internal"}`),
60 },
61 },
62 },
63 },
64 CertificatesRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TLS#CertificatesRaw.: map[string]jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/RawMessage#{
65 "automate": automateJSON,
66 },
67 }
68
69 var cfg caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Configscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Config#
70 ctx, err := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ProvisionContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ProvisionContext().(&cfg)
71 if err != nil {
72 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().(err)
73 }
74
75 if err := tlsApp.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TLS#Provision().(ctx); err != nil {
76 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().(err)
77 }
78
79 // simulate a case wherein the HTTP app starts first and
80 // tells the TLS app about the following auto-HTTPS domains
81 httpDomains := map[string]struct{}{"sub.example.com": {}}
82 if err := tlsApp.Managescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TLS#Manage().(httpDomains); err != nil {
83 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().(err)
84 }
85
86 _, actuallyManaged := tlsApp.managingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TLS#managing.["sub.example.com"]
87 if actuallyManaged != tc.expectedToManage {
88 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().(
89 "expected sub.example.com individually managed: %v, got: %v",
90 tc.expectedToManage,
91 actuallyManaged,
92 )
93 }
94 })
95 }
96 }