-
Notifications
You must be signed in to change notification settings - Fork 68
/
jks_test.go
148 lines (135 loc) · 4.13 KB
/
jks_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
package kafka
import (
"os"
"testing"
"github.com/grafana/sobek"
"github.com/stretchr/testify/assert"
)
type SimpleJKSConfig struct {
jksConfig JKSConfig
err *Xk6KafkaError
}
func TestJKS(t *testing.T) {
jksConfigs := []*SimpleJKSConfig{
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-keystore.jks",
Password: "password",
ClientCertAlias: "localhost",
ClientKeyAlias: "localhost",
ClientKeyPassword: "password",
ServerCaAlias: "caroot",
},
err: nil,
},
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-keystore-not-exists.jks",
},
err: NewXk6KafkaError(
fileNotFound, "File not found: fixtures/kafka-keystore-not-exists.jks", nil),
},
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-truststore.jks",
Password: "wrong-password",
},
err: NewXk6KafkaError(
failedDecodeJKSFile, "Failed to decode JKS file: fixtures/kafka-truststore.jks", nil),
},
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-truststore.jks",
Password: "password",
},
err: NewXk6KafkaError(
failedDecodePrivateKey, "Failed to decode client's private key: fixtures/kafka-truststore.jks", nil),
},
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-keystore.jks",
Password: "password",
ServerCaAlias: "wrong-alias", // This alias does not exist in the JKS file.
},
err: NewXk6KafkaError(
failedDecodeServerCa, "Failed to decode server's CA: fixtures/kafka-keystore.jks", nil),
},
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-keystore.jks",
Password: "password",
ServerCaAlias: "caroot",
ClientKeyAlias: "wrong-alias", // These alias don't exist in the JKS file.
ClientCertAlias: "wrong-alias",
ClientKeyPassword: "password", // This password is correct.
},
err: NewXk6KafkaError(
failedDecodePrivateKey, "Failed to decode client's private key: fixtures/kafka-keystore.jks", nil),
},
{
jksConfig: JKSConfig{
Path: "fixtures/kafka-keystore.jks",
Password: "password",
ClientCertAlias: "localhost",
ClientKeyAlias: "localhost",
ClientKeyPassword: "wrong-password",
ServerCaAlias: "caroot",
},
err: NewXk6KafkaError(
failedDecodePrivateKey, "Failed to decode client's private key: fixtures/kafka-keystore.jks", nil),
},
}
k := &Kafka{}
for _, jksConfig := range jksConfigs {
jks, err := k.loadJKS(&jksConfig.jksConfig)
if jksConfig.err != nil {
assert.Equal(t, jksConfig.err.Code, err.Code)
assert.Equal(t, jksConfig.err.Message, err.Message)
if jksConfig.err.Code == failedDecodePrivateKey {
assert.NotNil(t, jks)
assert.Nil(t, jks.ClientCertsPem)
assert.Empty(t, jks.ClientCertsPem)
assert.Equal(t, jks.ClientKeyPem, "")
assert.Empty(t, jks.ClientKeyPem)
assert.NotNil(t, jks.ServerCaPem)
assert.NotEmpty(t, jks.ServerCaPem)
}
continue
}
assert.Nil(t, err)
assert.NotNil(t, jks)
assert.NotNil(t, jks.ClientCertsPem)
assert.NotEmpty(t, jks.ClientCertsPem)
assert.NotNil(t, jks.ClientKeyPem)
assert.NotEmpty(t, jks.ClientKeyPem)
assert.NotNil(t, jks.ServerCaPem)
assert.NotEmpty(t, jks.ServerCaPem)
}
}
func TestLoadJKS_Function(t *testing.T) {
test := getTestModuleInstance(t)
jks := test.module.Kafka.loadJKSFunction(sobek.FunctionCall{
Arguments: []sobek.Value{
test.module.vu.Runtime().ToValue(
map[string]interface{}{
"path": "fixtures/kafka-keystore.jks",
"password": "password",
"clientCertAlias": "localhost",
"clientKeyAlias": "localhost",
"clientKeyPassword": "password",
"serverCaAlias": "caroot",
},
),
},
})
assert.NotNil(t, jks)
jksMap := jks.ToObject(test.vu.Runtime())
assert.NotNil(t, jksMap.Get("clientCertsPem"))
assert.NotNil(t, jksMap.Get("clientKeyPem"))
assert.NotNil(t, jksMap.Get("serverCaPem"))
// Remove generated files after the test.
assert.NoError(t, os.Remove("client-cert-0.pem"))
assert.NoError(t, os.Remove("client-cert-1.pem"))
assert.NoError(t, os.Remove("client-key.pem"))
assert.NoError(t, os.Remove("server-ca.pem"))
}