@@ -2,28 +2,21 @@ package kmipclient_test
2
2
3
3
import (
4
4
"context"
5
+ "os"
5
6
"sync"
6
7
"testing"
7
8
"time"
8
9
9
10
"github.com/ovh/kmip-go"
11
+ "github.com/ovh/kmip-go/kmipclient"
10
12
"github.com/ovh/kmip-go/kmipserver"
11
13
"github.com/ovh/kmip-go/kmiptest"
12
14
"github.com/ovh/kmip-go/payloads"
15
+ "github.com/ovh/kmip-go/ttlv"
13
16
14
17
"github.com/stretchr/testify/require"
15
18
)
16
19
17
- // func testClientRequest[Req, Resp kmip.OperationPayload](t *testing.T, tf func(*kmipclient.Client) *kmipclient.Executor[Req, Resp], f func(*testing.T, Req) (Resp, error)) (Resp, error) {
18
- // mux := kmipserver.NewBatchExecutor()
19
- // client := kmiptest.NewClientAndServer(t, mux)
20
- // req := tf(client)
21
- // mux.Route(req.RequestPayload().Operation(), kmipserver.HandleFunc(func(ctx context.Context, pl Req) (Resp, error) {
22
- // return f(t, pl)
23
- // }))
24
- // return req.Exec()
25
- // }
26
-
27
20
func TestRequest_ContextTimeout (t * testing.T ) {
28
21
mux := kmipserver .NewBatchExecutor ()
29
22
client := kmiptest .NewClientAndServer (t , mux )
@@ -215,3 +208,73 @@ func TestClone(t *testing.T) {
215
208
_ , err = client3 .Request (context .Background (), & payloads.DiscoverVersionsRequestPayload {})
216
209
require .NoError (t , err )
217
210
}
211
+
212
+ func TestVersionNegociation (t * testing.T ) {
213
+ router := kmipserver .NewBatchExecutor ()
214
+ router .Route (kmip .OperationDiscoverVersions , kmipserver .HandleFunc (func (ctx context.Context , pl * payloads.DiscoverVersionsRequestPayload ) (* payloads.DiscoverVersionsResponsePayload , error ) {
215
+ return & payloads.DiscoverVersionsResponsePayload {
216
+ ProtocolVersion : []kmip.ProtocolVersion {
217
+ kmip .V1_3 , kmip .V1_2 ,
218
+ },
219
+ }, nil
220
+ }))
221
+ addr , ca := kmiptest .NewServer (t , router )
222
+ client , err := kmipclient .Dial (
223
+ addr ,
224
+ kmipclient .WithRootCAPem ([]byte (ca )),
225
+ kmipclient .WithMiddlewares (
226
+ kmipclient .DebugMiddleware (os .Stderr , ttlv .MarshalXML ),
227
+ ),
228
+ kmipclient .WithKmipVersions (kmip .V1_2 , kmip .V1_3 ),
229
+ )
230
+ require .NoError (t , err )
231
+ require .NotNil (t , client )
232
+ require .EqualValues (t , client .Version (), kmip .V1_3 )
233
+ }
234
+
235
+ func TestVersionNegociation_NoCommon (t * testing.T ) {
236
+ router := kmipserver .NewBatchExecutor ()
237
+ router .Route (kmip .OperationDiscoverVersions , kmipserver .HandleFunc (func (ctx context.Context , pl * payloads.DiscoverVersionsRequestPayload ) (* payloads.DiscoverVersionsResponsePayload , error ) {
238
+ return & payloads.DiscoverVersionsResponsePayload {
239
+ ProtocolVersion : []kmip.ProtocolVersion {},
240
+ }, nil
241
+ }))
242
+ addr , ca := kmiptest .NewServer (t , router )
243
+ client , err := kmipclient .Dial (
244
+ addr ,
245
+ kmipclient .WithRootCAPem ([]byte (ca )),
246
+ kmipclient .WithMiddlewares (
247
+ kmipclient .DebugMiddleware (os .Stderr , ttlv .MarshalXML ),
248
+ ),
249
+ kmipclient .WithKmipVersions (kmip .V1_1 , kmip .V1_2 ),
250
+ )
251
+ require .Error (t , err )
252
+ require .Nil (t , client )
253
+ }
254
+
255
+ func TestVersionNegociation_v1_0_Fallback (t * testing.T ) {
256
+ router := kmipserver .NewBatchExecutor ()
257
+ router .Route (kmip .OperationDiscoverVersions , kmipserver .HandleFunc (func (ctx context.Context , pl * payloads.DiscoverVersionsRequestPayload ) (* payloads.DiscoverVersionsResponsePayload , error ) {
258
+ return nil , kmipserver .ErrOperationNotSupported
259
+ }))
260
+ client := kmiptest .NewClientAndServer (t , router )
261
+ require .EqualValues (t , client .Version (), kmip .V1_0 )
262
+ }
263
+
264
+ func TestVersionNegociation_v1_0_Fallback_unsupported (t * testing.T ) {
265
+ router := kmipserver .NewBatchExecutor ()
266
+ router .Route (kmip .OperationDiscoverVersions , kmipserver .HandleFunc (func (ctx context.Context , pl * payloads.DiscoverVersionsRequestPayload ) (* payloads.DiscoverVersionsResponsePayload , error ) {
267
+ return nil , kmipserver .ErrOperationNotSupported
268
+ }))
269
+ addr , ca := kmiptest .NewServer (t , router )
270
+ client , err := kmipclient .Dial (
271
+ addr ,
272
+ kmipclient .WithRootCAPem ([]byte (ca )),
273
+ kmipclient .WithMiddlewares (
274
+ kmipclient .DebugMiddleware (os .Stderr , ttlv .MarshalXML ),
275
+ ),
276
+ kmipclient .WithKmipVersions (kmip .V1_3 , kmip .V1_4 ),
277
+ )
278
+ require .Error (t , err )
279
+ require .Nil (t , client )
280
+ }
0 commit comments