-
Notifications
You must be signed in to change notification settings - Fork 77
/
with_test.go
153 lines (146 loc) · 3.47 KB
/
with_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
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
package ydb //nolint:testpackage
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"os"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/certificates"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/conn"
)
func TestWithCertificatesCached(t *testing.T) {
ca := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: pkix.Name{
Organization: []string{"Company, INC."},
Country: []string{"US"},
Province: []string{""},
Locality: []string{"San Francisco"},
StreetAddress: []string{"Golden Gate Bridge"},
PostalCode: []string{"94016"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
require.NoError(t, err)
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
require.NoError(t, err)
caPEM := new(bytes.Buffer)
err = pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
require.NoError(t, err)
f, err := os.CreateTemp(os.TempDir(), "ca.pem")
require.NoError(t, err)
_, err = f.Write(caPEM.Bytes())
require.NoError(t, err)
defer os.Remove(f.Name())
var (
n = 100
hitCounter uint64
missCounter uint64
ctx = context.TODO()
)
for _, test := range []struct {
name string
options []Option
expMiss uint64
expHit uint64
}{
{
"no cache",
[]Option{},
0,
0,
},
{
"file cache",
[]Option{
WithCertificatesFromFile(f.Name(),
certificates.FromFileOnHit(func() {
atomic.AddUint64(&hitCounter, 1)
}),
certificates.FromFileOnMiss(func() {
atomic.AddUint64(&missCounter, 1)
}),
),
},
0,
uint64(n),
},
{
"pem cache",
[]Option{
WithCertificatesFromPem(caPEM.Bytes(),
certificates.FromPemOnHit(func() {
atomic.AddUint64(&hitCounter, 1)
}),
certificates.FromPemMiss(func() {
atomic.AddUint64(&missCounter, 1)
}),
),
},
0,
uint64(n),
},
{
"pem&file cache",
[]Option{
WithCertificatesFromFile(f.Name(),
certificates.FromFileOnHit(func() {
atomic.AddUint64(&hitCounter, 1)
}),
certificates.FromFileOnMiss(func() {
atomic.AddUint64(&missCounter, 1)
}),
),
WithCertificatesFromPem(caPEM.Bytes(),
certificates.FromPemOnHit(func() {
atomic.AddUint64(&hitCounter, 1)
}),
certificates.FromPemMiss(func() {
atomic.AddUint64(&missCounter, 1)
}),
),
},
0,
uint64(n * 2),
},
} {
t.Run(test.name, func(t *testing.T) {
db, err := driverFromOptions(ctx,
append(
test.options,
withConnPool(conn.NewPool(context.Background(), config.New())), //nolint:contextcheck
)...,
)
require.NoError(t, err)
hitCounter, missCounter = 0, 0
for i := 0; i < n; i++ {
_, _, err := db.with(ctx,
func(ctx context.Context, c *Driver) error {
return nil // nothing to do
},
)
require.NoError(t, err)
}
require.Equal(t, test.expHit, hitCounter)
require.Equal(t, test.expMiss, missCounter)
})
}
}