~samwhited/xmpp

ref: a2ffaf710170d692a9289c2b428a4efa90c8e703 xmpp/x509/x509_test.go -rw-r--r-- 3.8 KiB
a2ffaf71Sam Whited all: update deps 3 years ago
                                                                                
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
119
120
121
122
123
124
// Copyright 2017 Sam Whited.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package x509_test

import (
	cryptox509 "crypto/x509"
	"encoding/pem"
	"fmt"
	"testing"

	"mellium.im/xmpp/x509"
)

type crtTest struct {
	crtData   string
	srvNames  []string
	xmppAddrs []string
	err       error
}

var crtTests = [...]crtTest{
	0: {
		xmppAddrs: []string{"example.org", "conference.example.org"},
		srvNames:  []string{"_xmpp-client.example.org", "_xmpp-server.example.org", "_xmpp-server.conference.example.org"},
		crtData: `-----BEGIN CERTIFICATE-----
MIIE2zCCA8OgAwIBAgIJAPpUBVr/QTXIMA0GCSqGSIb3DQEBBQUAMIGRMRQwEgYD
VQQDDAtleGFtcGxlLm9yZzELMAkGA1UEBhMCR0IxFTATBgNVBAcMDFRoZSBJbnRl
cm5ldDEaMBgGA1UECgwRWW91ciBPcmdhbmlzYXRpb24xGDAWBgNVBAsMD1hNUFAg
RGVwYXJ0bWVudDEfMB0GCSqGSIb3DQEJARYQeG1wcEBleGFtcGxlLm9yZzAeFw0x
NzA5MDcyMjMxMzFaFw0xODA5MDcyMjMxMzFaMIGRMRQwEgYDVQQDDAtleGFtcGxl
Lm9yZzELMAkGA1UEBhMCR0IxFTATBgNVBAcMDFRoZSBJbnRlcm5ldDEaMBgGA1UE
CgwR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/yIzY2jMVayJv88kuS7BVBu6pL6Mm00X
0kmvLMvkb04IN3XYBYxsbzTscBOXUm7thGKzvGWSBAARnT24nxysyRbL0YE+gpB0
jEnlM0fUVuWWFdplC/2ktcQKUc6U47pXqpFrK2/3a28Ocn10hEw0uDVGX/UcMcRP
djT/rjumhQWaKI6yY3PEgz/GLdyiKZvH/0LsbYSQAKnxqS02EDwULcBTdSscbvM=
-----END CERTIFICATE-----`,
	},
}

func TestCertificates(t *testing.T) {
	for i, tc := range crtTests {
		blk, _ := pem.Decode([]byte(tc.crtData))
		t.Run(fmt.Sprintf("%d/Parse", i), func(t *testing.T) {
			crt, err := x509.ParseCertificate(blk.Bytes)
			switch {
			case err != tc.err:
				t.Fatal(err)
			case err != nil:
				return
			}
			doTests(t, tc, crt)
		})
		t.Run(fmt.Sprintf("%d/Cert", i), func(t *testing.T) {
			cryptocrt, err := cryptox509.ParseCertificate(blk.Bytes)
			switch {
			case err != tc.err:
				t.Error(err)
			case err != nil:
				return
			}
			crt, err := x509.FromCertificate(cryptocrt)
			switch {
			case err != tc.err:
				t.Fatal(err)
			case err != nil:
				return
			}
			doTests(t, tc, crt)
		})
	}
}

func doTests(t *testing.T, tc crtTest, crt *x509.Certificate) {
	if len(tc.srvNames) != len(crt.SRVNames) {
		t.Errorf("Wrong SRVNames want=%v, got=%v", tc.srvNames, crt.SRVNames)
	} else {
		for _, name := range tc.srvNames {
			found := false
			for _, otherName := range crt.SRVNames {
				if otherName == name {
					found = true
				}
			}
			if !found {
				t.Errorf("Wrong SRVNames want=%v, got=%v", tc.srvNames, crt.SRVNames)
				break
			}
		}
	}
	if len(tc.xmppAddrs) != len(crt.XMPPAddresses) {
		t.Errorf("Wrong XMPPAddrs want=%v, got=%v", tc.xmppAddrs, crt.XMPPAddresses)
	} else {
		for _, name := range tc.xmppAddrs {
			found := false
			for _, otherName := range crt.XMPPAddresses {
				if otherName == name {
					found = true
				}
			}
			if !found {
				t.Errorf("Wrong SRVNames want=%v, got=%v", tc.xmppAddrs, crt.XMPPAddresses)
				break
			}
		}
	}
}