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 | package acmeserver |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/smallstep/certificates/authority/policy" |
| 8 | "github.com/smallstep/certificates/authority/provisioner" |
| 9 | ) |
| 10 | |
| 11 | func TestPolicyNormalizeAllowRules(t *testing.T) { |
| 12 | type fields struct { |
| 13 | Allow *RuleSet |
| 14 | Deny *RuleSet |
| 15 | AllowWildcardNames bool |
| 16 | } |
| 17 | tests := []struct { |
| 18 | name string |
| 19 | fields fields |
| 20 | want *policy.X509NameOptions |
| 21 | }{ |
| 22 | { |
| 23 | name: "providing no rules results in 'nil'", |
| 24 | fields: fields{}, |
| 25 | want: nil, |
| 26 | }, |
| 27 | { |
| 28 | name: "providing 'nil' Allow rules results in 'nil', regardless of Deny rules", |
| 29 | fields: fields{ |
| 30 | Allow: nil, |
| 31 | Deny: &RuleSet{}, |
| 32 | AllowWildcardNames: true, |
| 33 | }, |
| 34 | want: nil, |
| 35 | }, |
| 36 | { |
| 37 | name: "providing empty Allow rules results in 'nil', regardless of Deny rules", |
| 38 | fields: fields{ |
| 39 | Allow: &RuleSet{ |
| 40 | Domains: []string{}, |
| 41 | IPRanges: []string{}, |
| 42 | }, |
| 43 | }, |
| 44 | want: nil, |
| 45 | }, |
| 46 | { |
| 47 | name: "rules configured in Allow are returned in X509NameOptions", |
| 48 | fields: fields{ |
| 49 | Allow: &RuleSet{ |
| 50 | Domains: []string{"example.com"}, |
| 51 | IPRanges: []string{"127.0.0.1/32"}, |
| 52 | }, |
| 53 | }, |
| 54 | want: &policy.X509NameOptions{ |
| 55 | DNSDomains: []string{"example.com"}, |
| 56 | IPRanges: []string{"127.0.0.1/32"}, |
| 57 | }, |
| 58 | }, |
| 59 | } |
| 60 | for _, tt := range tests { |
| 61 | t.Run(tt.name, func(t *testing.T) { |
| 62 | p := &Policy{ |
| 63 | Allow: tt.fields.Allow, |
| 64 | Deny: tt.fields.Deny, |
| 65 | AllowWildcardNames: tt.fields.AllowWildcardNames, |
| 66 | } |
| 67 | if got := p.normalizeAllowRules(); !reflect.DeepEqual(got, tt.want) { |
| 68 | t.Errorf("Policy.normalizeAllowRules() = %v, want %v", got, tt.want) |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestPolicy_normalizeDenyRules(t *testing.T) { |
| 75 | type fields struct { |
| 76 | Allow *RuleSet |
| 77 | Deny *RuleSet |
| 78 | AllowWildcardNames bool |
| 79 | } |
| 80 | tests := []struct { |
| 81 | name string |
| 82 | fields fields |
| 83 | want *policy.X509NameOptions |
| 84 | }{ |
| 85 | { |
| 86 | name: "providing no rules results in 'nil'", |
| 87 | fields: fields{}, |
| 88 | want: nil, |
| 89 | }, |
| 90 | { |
| 91 | name: "providing 'nil' Deny rules results in 'nil', regardless of Allow rules", |
| 92 | fields: fields{ |
| 93 | Deny: nil, |
| 94 | Allow: &RuleSet{}, |
| 95 | AllowWildcardNames: true, |
| 96 | }, |
| 97 | want: nil, |
| 98 | }, |
| 99 | { |
| 100 | name: "providing empty Deny rules results in 'nil', regardless of Allow rules", |
| 101 | fields: fields{ |
| 102 | Deny: &RuleSet{ |
| 103 | Domains: []string{}, |
| 104 | IPRanges: []string{}, |
| 105 | }, |
| 106 | }, |
| 107 | want: nil, |
| 108 | }, |
| 109 | { |
| 110 | name: "rules configured in Deny are returned in X509NameOptions", |
| 111 | fields: fields{ |
| 112 | Deny: &RuleSet{ |
| 113 | Domains: []string{"example.com"}, |
| 114 | IPRanges: []string{"127.0.0.1/32"}, |
| 115 | }, |
| 116 | }, |
| 117 | want: &policy.X509NameOptions{ |
| 118 | DNSDomains: []string{"example.com"}, |
| 119 | IPRanges: []string{"127.0.0.1/32"}, |
| 120 | }, |
| 121 | }, |
| 122 | } |
| 123 | for _, tt := range tests { |
| 124 | t.Run(tt.name, func(t *testing.T) { |
| 125 | p := &Policy{ |
| 126 | Allow: tt.fields.Allow, |
| 127 | Deny: tt.fields.Deny, |
| 128 | AllowWildcardNames: tt.fields.AllowWildcardNames, |
| 129 | } |
| 130 | if got := p.normalizeDenyRules(); !reflect.DeepEqual(got, tt.want) { |
| 131 | t.Errorf("Policy.normalizeDenyRules() = %v, want %v", got, tt.want) |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestPolicy_normalizeRules(t *testing.T) { |
| 138 | tests := []struct { |
| 139 | name string |
| 140 | policy *Policy |
| 141 | want *provisioner.X509Options |
| 142 | }{ |
| 143 | { |
| 144 | name: "'nil' policy results in 'nil' options", |
| 145 | policy: nil, |
| 146 | want: nil, |
| 147 | }, |
| 148 | { |
| 149 | name: "'nil' Allow/Deny rules and disallowing wildcard names result in 'nil' X509Options", |
| 150 | policy: &Policy{ |
| 151 | Allow: nil, |
| 152 | Deny: nil, |
| 153 | AllowWildcardNames: false, |
| 154 | }, |
| 155 | want: nil, |
| 156 | }, |
| 157 | { |
| 158 | name: "'nil' Allow/Deny rules and allowing wildcard names result in 'nil' Allow/Deny rules in X509Options but allowing wildcard names in X509Options", |
| 159 | policy: &Policy{ |
| 160 | Allow: nil, |
| 161 | Deny: nil, |
| 162 | AllowWildcardNames: true, |
| 163 | }, |
| 164 | want: &provisioner.X509Options{ |
| 165 | AllowWildcardNames: true, |
| 166 | }, |
| 167 | }, |
| 168 | } |
| 169 | for _, tt := range tests { |
| 170 | t.Run(tt.name, func(t *testing.T) { |
| 171 | if got := tt.policy.normalizeRules(); !reflect.DeepEqual(got, tt.want) { |
| 172 | t.Errorf("Policy.normalizeRules() = %v, want %v", got, tt.want) |
| 173 | } |
| 174 | }) |
| 175 | } |
| 176 | } |