forked from ThalesGroup/kmip-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples_test.go
92 lines (79 loc) · 1.68 KB
/
examples_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
package kmip_test
import (
"bufio"
"fmt"
"net"
"time"
"github.com/Seagate/kmip-go"
"github.com/Seagate/kmip-go/kmip14"
"github.com/Seagate/kmip-go/ttlv"
"github.com/google/uuid"
)
func Example_client() {
conn, err := net.DialTimeout("tcp", "localhost:5696", 3*time.Second)
if err != nil {
panic(err)
}
biID := uuid.New()
msg := kmip.RequestMessage{
RequestHeader: kmip.RequestHeader{
ProtocolVersion: kmip.ProtocolVersion{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 2,
},
BatchCount: 1,
},
BatchItem: []kmip.RequestBatchItem{
{
UniqueBatchItemID: biID[:],
Operation: kmip14.OperationDiscoverVersions,
RequestPayload: kmip.DiscoverVersionsRequestPayload{
ProtocolVersion: []kmip.ProtocolVersion{
{ProtocolVersionMajor: 1, ProtocolVersionMinor: 2},
},
},
},
},
}
req, err := ttlv.Marshal(msg)
if err != nil {
panic(err)
}
fmt.Println(req)
_, err = conn.Write(req)
if err != nil {
panic(err)
}
buf := make([]byte, 5000)
_, err = bufio.NewReader(conn).Read(buf)
if err != nil {
panic(err)
}
resp := ttlv.TTLV(buf)
fmt.Println(resp)
}
func ExampleServer() {
listener, err := net.Listen("tcp", "0.0.0.0:5696")
if err != nil {
panic(err)
}
kmip.DefaultProtocolHandler.LogTraffic = true
kmip.DefaultOperationMux.Handle(kmip14.OperationDiscoverVersions, &kmip.DiscoverVersionsHandler{
SupportedVersions: []kmip.ProtocolVersion{
{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 4,
},
{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 3,
},
{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 2,
},
},
})
srv := kmip.Server{}
panic(srv.Serve(listener))
}