aboutsummaryrefslogtreecommitdiff
path: root/pkg/dns/dns_test.go
blob: 2f034e63b0f4af374c985a2d06bb774df936c4cd (plain)
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
package dns

import (
	"context"
	"log"
	"testing"
	"time"

	"git.sigsum.org/sigsum-go/pkg/hex"
	"git.sigsum.org/sigsum-go/pkg/types"
)

func Example() {
	name := "_sigsum_v0.testonly.sigsum.org"
	pub := mustDecodePublicKey("cda2517e17dcba133eb0e71bf77473f94a77d7e61b1de4e1e64adfd0938d6182")

	timeout := 10 * time.Second
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	dr := NewDefaultResolver()
	if err := dr.Verify(ctx, name, pub); err != nil {
		log.Fatal(err.Error())
	}

	// Output:
}

func TestValidResponse(t *testing.T) {
	pub := mustDecodePublicKey("cda2517e17dcba133eb0e71bf77473f94a77d7e61b1de4e1e64adfd0938d6182")
	for _, table := range []struct {
		desc   string
		rsps   []string
		wantOK bool
	}{
		{
			desc: "invalid: upper-case hex-encoding",
			rsps: []string{
				"",
				"abc",
				"C522D929B261241EEF174B51B8472FA5D5F961892089A7B85FD25CE73271ABCA",
				"defghi",
			},
		},
		{
			desc: "valid",
			rsps: []string{
				"",
				"abc",
				"c522d929b261241eef174b51b8472fa5d5f961892089a7b85fd25ce73271abca",
				"defghi",
			},
			wantOK: true,
		},
	} {
		err := validResponse(pub, table.rsps)
		if got, want := err == nil, table.wantOK; got != want {
			t.Errorf("got error but wanted none in test %q: %v", table.desc, err)
		}
	}
}

func TestValidPrefix(t *testing.T) {
	for _, table := range []struct {
		desc   string
		name   string
		wantOK bool
	}{
		{
			desc: "invalid: bad prefix (1/2)",
			name: "x_sigsum_v0.sigsum.org",
		},
		{
			desc: "invalid: bad prefix (2/2)",
			name: "_sigsum_v0x.sigsum.org",
		},
		{
			desc:   "valid",
			name:   "_sigsum_v0.sigsum.org",
			wantOK: true,
		},
	} {
		err := validPrefix(table.name)
		if got, want := err == nil, table.wantOK; got != want {
			t.Errorf("got error but wanted none in test %q: %v", table.desc, err)
		}
	}
}

func mustDecodePublicKey(str string) *types.PublicKey {
	b, err := hex.Deserialize(str)
	if err != nil {
		log.Fatal(err.Error())
	}
	if len(b) != types.PublicKeySize {
		log.Fatal("invalid key size")
	}

	var pub types.PublicKey
	copy(pub[:], b)
	return &pub
}