This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcaa_test.go
118 lines (110 loc) · 3.93 KB
/
caa_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2017 Opsmate, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name(s) of the above copyright
// holders shall not be used in advertising or otherwise to promote the
// sale, use or other dealings in this Software without prior written
// authorization.
package main
import (
"fmt"
"github.com/miekg/dns"
"testing"
)
type testcaseInfo struct {
subdomain string
issue string
}
const TEST_DOMAIN = "test.caarecord.org."
var testcases = []testcaseInfo{
{"none", ""},
{"caa1", "1.example.com"},
{"child.caa1", "1.example.com"},
{"grand.child.caa1", "1.example.com"},
{"cname-to-none", ""},
{"cname-to-caa1", "1.example.com"},
{"child.cname-to-caa1", "1.example.com"},
{"grand.child.cname-to-caa1", "1.example.com"},
{"cname-to-caa1-child", "1.example.com"},
{"child.cname-to-caa1-child", "1.example.com"},
{"grand.child.cname-to-caa1-child", "1.example.com"},
{"dname-to-none", ""},
{"dname-to-caa1", "1.example.com"},
{"dname-to-caa1-child", "1.example.com"},
{"child.dname-to-none", ""},
{"child.dname-to-caa1", "1.example.com"},
{"child.dname-to-caa1-child", "1.example.com"},
{"caa3.dname-to-caa1", "3.example.com"},
{"child.caa3.dname-to-caa1", "3.example.com"},
{"caa4-and-dname", "4.example.com"},
{"caa2", "2.example.com"},
{"cname-to-none.caa2", "2.example.com"},
{"cname-to-caa1.caa2", "1.example.com"},
{"cname-to-caa1-child.caa2", "1.example.com"},
{"dname-to-none.caa2", "2.example.com"},
{"dname-to-caa1.caa2", "1.example.com"},
{"dname-to-caa1-child.caa2", "1.example.com"},
{"child.dname-to-none.caa2", "2.example.com"},
{"child.dname-to-caa1.caa2", "1.example.com"},
{"child.dname-to-caa1-child.caa2", "1.example.com"},
{"caa3.dname-to-caa1.caa2", "3.example.com"},
}
func extractIssue(caa []*dns.CAA) (string, error) {
if len(caa) == 0 {
return "", nil
}
if len(caa) > 1 {
return "", fmt.Errorf("Expected no more than one CAA record, got %d", len(caa))
}
if caa[0].Tag != "issue" {
return "", fmt.Errorf("Expected an issue record, got '%s' instead", caa[0].Tag)
}
return caa[0].Value, nil
}
func TestResolveCAA(t *testing.T) {
for _, testcase := range testcases {
recursions := 0
caa, err := resolveCAA(testcase.subdomain+"."+TEST_DOMAIN, &recursions)
if err != nil {
t.Errorf("%s: got error: %s", testcase.subdomain, err)
continue
}
issue, err := extractIssue(caa)
if err != nil {
t.Errorf("%s: %s", testcase.subdomain, err)
continue
}
if issue != testcase.issue {
t.Errorf("%s: Expected issue '%s', got '%s'", testcase.subdomain, testcase.issue, issue)
continue
}
}
}
func TestInfiniteRecursion(t *testing.T) {
recursions := 0
_, err := resolveCAA("cname-to-sub."+TEST_DOMAIN, &recursions)
if err == nil {
t.Errorf("cname-to-sub: expected an error but got none")
return
}
if err.Error() != "Too many recursions" {
t.Errorf("cname-to-sub: expected a 'Too many recursions' error but got '%'", err.Error())
}
}