1 package acme
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/letsencrypt/boulder/core"
8 "github.com/letsencrypt/boulder/test"
9 )
10
11 func TestNewChallengeStrategy(t *testing.T) {
12 testCases := []struct {
13 Name string
14 InputName string
15 ExpectedError string
16 ExpectedStratType string
17 }{
18 {
19 Name: "unknown name",
20 InputName: "hyper-quauntum-math-mesh-challenge",
21 ExpectedError: `ChallengeStrategy "HYPER-QUAUNTUM-MATH-MESH-CHALLENGE" unknown`,
22 },
23 {
24 Name: "known name, HTTP-01",
25 InputName: "HTTP-01",
26 ExpectedStratType: "*acme.preferredTypeChallengeStrategy",
27 },
28 {
29 Name: "known name, DNS-01",
30 InputName: "DNS-01",
31 ExpectedStratType: "*acme.preferredTypeChallengeStrategy",
32 },
33 {
34 Name: "known name, TLS-ALPN-01",
35 InputName: "TLS-ALPN-01",
36 ExpectedStratType: "*acme.preferredTypeChallengeStrategy",
37 },
38 {
39 Name: "known name, RANDOM",
40 InputName: "RANDOM",
41 ExpectedStratType: "*acme.randomChallengeStrategy",
42 },
43 {
44 Name: "known name, mixed case",
45 InputName: "rAnDoM",
46 ExpectedStratType: "*acme.randomChallengeStrategy",
47 },
48 }
49
50 for _, tc := range testCases {
51 t.Run(tc.Name, func(t *testing.T) {
52 strategy, err := NewChallengeStrategy(tc.InputName)
53 if err == nil && tc.ExpectedError != "" {
54 t.Errorf("Expected %q got no error\n", tc.ExpectedError)
55 } else if err != nil {
56 test.AssertEquals(t, err.Error(), tc.ExpectedError)
57 } else if err == nil && tc.ExpectedError == "" {
58 test.AssertEquals(t, fmt.Sprintf("%T", strategy), tc.ExpectedStratType)
59 }
60 })
61 }
62 }
63
64 func TestPickChallenge(t *testing.T) {
65 exampleDNSChall := core.Challenge{
66 Type: "dns-01",
67 }
68 exampleAuthz := &core.Authorization{
69 ID: "1234",
70 Challenges: []core.Challenge{
71 {
72 Type: "arm-wrestling",
73 },
74 exampleDNSChall,
75 {
76 Type: "http-01",
77 },
78 },
79 }
80
81 testCases := []struct {
82 Name string
83 StratName string
84 InputAuthz *core.Authorization
85 ExpectedError string
86 ExpectedChallenge *core.Challenge
87 }{
88 {
89 Name: "Preferred type strategy, nil input authz",
90 StratName: "http-01",
91 ExpectedError: ErrPickChallengeNilAuthz.Error(),
92 },
93 {
94 Name: "Random type strategy, nil input authz",
95 StratName: "random",
96 ExpectedError: ErrPickChallengeNilAuthz.Error(),
97 },
98 {
99 Name: "Preferred type strategy, nil input authz challenges",
100 StratName: "http-01",
101 InputAuthz: &core.Authorization{},
102 ExpectedError: ErrPickChallengeAuthzMissingChallenges.Error(),
103 },
104 {
105 Name: "Random type strategy, nil input authz challenges",
106 StratName: "random",
107 InputAuthz: &core.Authorization{},
108 ExpectedError: ErrPickChallengeAuthzMissingChallenges.Error(),
109 },
110 {
111 Name: "Preferred type strategy, no challenge of type",
112 StratName: "tls-alpn-01",
113 InputAuthz: exampleAuthz,
114 ExpectedError: `authorization (ID "1234") had no "tls-alpn-01" type challenge`,
115 },
116 {
117 Name: "Preferred type strategy, challenge of type present",
118 StratName: "dns-01",
119 InputAuthz: exampleAuthz,
120 ExpectedChallenge: &exampleDNSChall,
121 },
122 }
123
124 for _, tc := range testCases {
125 t.Run(tc.Name, func(t *testing.T) {
126 strategy, err := NewChallengeStrategy(tc.StratName)
127 test.AssertNotError(t, err, "Failed to create challenge strategy")
128 chall, err := strategy.PickChallenge(tc.InputAuthz)
129 if err == nil && tc.ExpectedError != "" {
130 t.Errorf("Expected %q got no error\n", tc.ExpectedError)
131 } else if err != nil {
132 test.AssertEquals(t, err.Error(), tc.ExpectedError)
133 } else if err == nil && tc.ExpectedError == "" {
134 test.AssertDeepEquals(t, chall, tc.ExpectedChallenge)
135 }
136 })
137 }
138 }
139
View as plain text