File: cert_pool_test.go

package info (click to toggle)
golang-github-zmap-zcrypto 0.0~git20240512.0fef58d-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,856 kB
  • sloc: python: 567; sh: 124; makefile: 9
file content (186 lines) | stat: -rw-r--r-- 3,905 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package x509

import (
	"bytes"
	"crypto/rand"
	"testing"
)

func TestSubjects(t *testing.T) {
	certs := makeRandomCertsForPool(1000)
	pool := NewCertPool()
	for _, c := range certs {
		pool.AddCert(c)
	}
	subjects := pool.Subjects()
	for i := range subjects {
		if bytes.Compare(subjects[i], pool.Certificates()[i].RawSubject) != 0 {
			t.Fail()
		}
	}
}

func TestSumAndContains(t *testing.T) {
	certs1 := makeRandomCertsForPool(1000)
	pool1 := NewCertPool()
	for _, c := range certs1 {
		pool1.AddCert(c)
	}
	certs2 := makeRandomCertsForPool(1000)
	pool2 := NewCertPool()
	for _, c := range certs2 {
		pool2.AddCert(c)
	}
	sum := pool1.Sum(pool2)
	for _, c := range pool1.Certificates() {
		if !sum.Contains(c) {
			t.Fail()
		}
	}
	for _, c := range sum.Certificates() {
		if !pool1.Contains(c) && !pool2.Contains(c) {
			t.Fail()
		}
	}
}

func TestSumAndSize(t *testing.T) {
	certs1 := makeRandomCertsForPool(1000)
	pool1 := NewCertPool()
	for _, c := range certs1 {
		pool1.AddCert(c)
	}
	certs2 := makeRandomCertsForPool(1000)
	pool2 := NewCertPool()
	for _, c := range certs2 {
		pool2.AddCert(c)
	}
	sum := pool1.Sum(pool2)
	if sum.Size() != 2000 {
		t.Fail()
	}
}

func TestSumAndCovers(t *testing.T) {
	certs1 := makeRandomCertsForPool(1000)
	pool1 := NewCertPool()
	for _, c := range certs1 {
		pool1.AddCert(c)
	}
	certs2 := makeRandomCertsForPool(1000)
	pool2 := NewCertPool()
	for _, c := range certs2 {
		pool2.AddCert(c)
	}
	sum := pool1.Sum(pool2)
	if !sum.Covers(pool1) || !sum.Covers(pool2) {
		t.Fail()
	}
	if pool1.Covers(sum) || pool2.Covers(sum) {
		t.Fail()
	}
}

func makeRandomCertsForPool(n int) []*Certificate {
	out := make([]*Certificate, 0, n)
	for i := 0; i < n; i++ {
		c := new(Certificate)
		c.FingerprintSHA256 = make([]byte, 256/8)
		if _, err := rand.Read(c.FingerprintSHA256); err != nil {
			panic(err)
		}
		c.RawSubject = make([]byte, 1)
		if _, err := rand.Read(c.RawSubject); err != nil {
			panic(err)
		}
		c.SubjectKeyId = make([]byte, 2)
		if _, err := rand.Read(c.SubjectKeyId); err != nil {
			panic(err)
		}
		c.Raw = make([]byte, 2048)
		if _, err := rand.Read(c.Raw); err != nil {
			panic(err)
		}
		out = append(out, c)
	}
	return out
}

func doBench(b *testing.B, certs []*Certificate) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		pool := NewCertPool()
		for _, c := range certs {
			pool.AddCert(c)
		}
	}
}

func BenchmarkCertPoolAdd10(b *testing.B) {
	certs := makeRandomCertsForPool(10)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd100(b *testing.B) {
	certs := makeRandomCertsForPool(100)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd200(b *testing.B) {
	certs := makeRandomCertsForPool(200)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd300(b *testing.B) {
	certs := makeRandomCertsForPool(300)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd400(b *testing.B) {
	certs := makeRandomCertsForPool(400)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd500(b *testing.B) {
	certs := makeRandomCertsForPool(500)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd600(b *testing.B) {
	certs := makeRandomCertsForPool(600)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd700(b *testing.B) {
	certs := makeRandomCertsForPool(700)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd800(b *testing.B) {
	certs := makeRandomCertsForPool(800)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd900(b *testing.B) {
	certs := makeRandomCertsForPool(900)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd1000(b *testing.B) {
	certs := makeRandomCertsForPool(1000)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd10000(b *testing.B) {
	certs := makeRandomCertsForPool(10000)
	doBench(b, certs)
}

func BenchmarkCertPoolAdd100000(b *testing.B) {
	certs := makeRandomCertsForPool(100000)
	doBench(b, certs)
}