-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmip_test.go
165 lines (148 loc) · 4.49 KB
/
kmip_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
154
155
156
157
158
159
160
161
162
163
164
165
package kmip_test
import (
"encoding/hex"
"slices"
"testing"
"time"
"github.com/ovh/kmip-go"
"github.com/ovh/kmip-go/kmiptest"
"github.com/ovh/kmip-go/payloads"
"github.com/ovh/kmip-go/ttlv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncodeDecodeRequest(t *testing.T) {
msg := kmip.NewRequestMessage(kmip.V1_4, &payloads.DiscoverVersionsRequestPayload{
ProtocolVersion: []kmip.ProtocolVersion{kmip.V1_4, kmip.V1_0},
}, &payloads.DiscoverVersionsRequestPayload{})
for _, tc := range []struct {
name string
marshal func(any) []byte
unmarshal func([]byte, any) error
}{
{"TTLV", ttlv.MarshalTTLV, ttlv.UnmarshalTTLV},
{"XML", ttlv.MarshalXML, ttlv.UnmarshalXML},
} {
t.Run(tc.name, func(t *testing.T) {
bytes := tc.marshal(&msg)
nmsg := kmip.RequestMessage{}
err := tc.unmarshal(bytes, &nmsg)
require.NoError(t, err)
assert.Equal(t, msg, nmsg)
})
}
}
func TestEncodeDecodeResponse(t *testing.T) {
msg := kmip.ResponseMessage{
Header: kmip.ResponseHeader{
ProtocolVersion: kmip.V1_2,
TimeStamp: time.Now().Round(time.Second),
BatchCount: 1,
},
BatchItem: []kmip.ResponseBatchItem{
{
Operation: kmip.OperationDiscoverVersions,
ResponsePayload: &payloads.DiscoverVersionsResponsePayload{
ProtocolVersion: []kmip.ProtocolVersion{
kmip.V1_4, kmip.V1_0,
},
},
},
},
}
for _, tc := range []struct {
name string
marshal func(any) []byte
unmarshal func([]byte, any) error
}{
{"TTLV", ttlv.MarshalTTLV, ttlv.UnmarshalTTLV},
{"XML", ttlv.MarshalXML, ttlv.UnmarshalXML},
} {
bytes := tc.marshal(&msg)
nmsg := kmip.ResponseMessage{}
err := tc.unmarshal(bytes, &nmsg)
require.NoError(t, err)
assert.Equal(t, msg, nmsg)
}
}
func BenchmarkKmipEncode(b *testing.B) {
msg := kmip.NewRequestMessage(kmip.V1_4, &payloads.DiscoverVersionsRequestPayload{
ProtocolVersion: []kmip.ProtocolVersion{kmip.V1_4, kmip.V1_0},
}, &payloads.DiscoverVersionsRequestPayload{
ProtocolVersion: []kmip.ProtocolVersion{kmip.V1_4, kmip.V1_0},
})
enc := ttlv.NewTTLVEncoder()
enc.Any(&msg)
b.ResetTimer()
for range b.N {
enc.Clear()
enc.Any(&msg)
}
}
func BenchmarkKmipDecode(b *testing.B) {
data, _ := hex.DecodeString("42007801000000A04200770100000038420069010000002042006A0200000004000000010000000042006B0200000004000000040000000042000D0200000004000000010000000042000F010000005842005C05000000040000001E000000004200930800000010B28CED4885814A6AAFF3CB1552FF0A524200790100000028420069010000002042006A0200000004000000010000000042006B02000000040000000400000000")
d := kmip.RequestMessage{}
for range b.N {
dec, _ := ttlv.NewTTLVDecoder(data)
_ = dec.Any(&d)
}
}
func TestParseAndMarshalOasisTests(t *testing.T) {
for _, vers := range kmiptest.TestCaseVersions {
suites := kmiptest.ListTestSuites(t, "kmiptest/testdata", vers)
for _, e := range suites {
name := vers + "/" + e
t.Run(name, func(t *testing.T) {
if slices.Contains(kmiptest.UnsupportedTestCases, name) {
t.Skip("Test case not supported")
}
ts := kmiptest.LoadTestSuite(t, "kmiptest/testdata", vers, e)
var err error
for _, tc := range ts.TestCases {
{
raw := ttlv.MarshalTTLV(tc.RequestMessage)
msg := kmip.RequestMessage{}
err = ttlv.UnmarshalTTLV(raw, &msg)
require.NoError(t, err)
require.EqualValues(t, tc.RequestMessage, msg)
}
{
raw := ttlv.MarshalTTLV(tc.ResponseMessage)
msg := kmip.ResponseMessage{}
err = ttlv.UnmarshalTTLV(raw, &msg)
require.NoError(t, err)
require.EqualValues(t, tc.ResponseMessage, msg)
}
{
raw := ttlv.MarshalXML(tc.RequestMessage)
msg := kmip.RequestMessage{}
err = ttlv.UnmarshalXML(raw, &msg)
require.NoError(t, err)
require.EqualValues(t, tc.RequestMessage, msg)
}
{
raw := ttlv.MarshalXML(tc.ResponseMessage)
msg := kmip.ResponseMessage{}
err = ttlv.UnmarshalXML(raw, &msg)
require.NoError(t, err)
require.EqualValues(t, tc.ResponseMessage, msg)
}
{
raw := ttlv.MarshalJSON(tc.RequestMessage)
msg := kmip.RequestMessage{}
err = ttlv.UnmarshalJSON(raw, &msg)
require.NoError(t, err)
require.EqualValues(t, tc.RequestMessage, msg)
}
{
raw := ttlv.MarshalJSON(tc.ResponseMessage)
msg := kmip.ResponseMessage{}
err = ttlv.UnmarshalJSON(raw, &msg)
require.NoError(t, err)
require.EqualValues(t, tc.ResponseMessage, msg)
}
}
})
}
}
}