Debian Codegraph @

messages/KRBPriv_test.go

raw symbols SCIP
1 package messages
2
3 import (
4 "encoding/hex"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9 "gopkg.in/jcmturner/gokrb5.v5/iana"
10 "gopkg.in/jcmturner/gokrb5.v5/iana/addrtype"
11 "gopkg.in/jcmturner/gokrb5.v5/iana/msgtype"
12 "gopkg.in/jcmturner/gokrb5.v5/testdata"
13 "gopkg.in/jcmturner/gokrb5.v5/types"
14 )
15
16 func TestUnmarshalKRBPriv(t *testing.T) {
17 t.Parallel()
18 var a KRBPriv
19 v := "encode_krb5_priv"
20 b, err := hex.DecodeString(testdata.TestVectors[v])
21 if err != nil {
22 t.Fatalf("Test vector read error of %s: %v\n", v, err)
23 }
24 err = a.Unmarshal(b)
25 if err != nil {
26 t.Fatalf("Unmarshal error of %s: %v\n", v, err)
27 }
28 assert.Equal(t, iana.PVNO, a.PVNO, "PVNO not as expected")
29 assert.Equal(t, msgtype.KRB_PRIV, a.MsgType, "Message type not as expected")
30 assert.Equal(t, iana.PVNO, a.EncPart.KVNO, "EncPart KVNO not as expected")
31 assert.Equal(t, testdata.TEST_ETYPE, a.EncPart.EType, "EncPart etype not as expected")
32 assert.Equal(t, []byte(testdata.TEST_CIPHERTEXT), a.EncPart.Cipher, "Cipher text of EncPart not as expected")
33 }
34
35 func TestUnmarshalEncPrivPart(t *testing.T) {
36 t.Parallel()
37 var a EncKrbPrivPart
38 v := "encode_krb5_enc_priv_part"
39 b, err := hex.DecodeString(testdata.TestVectors[v])
40 if err != nil {
41 t.Fatalf("Test vector read error of %s: %v\n", v, err)
42 }
43 err = a.Unmarshal(b)
44 if err != nil {
45 t.Fatalf("Unmarshal error of %s: %v\n", v, err)
46 }
47 //Parse the test time value into a time.Time type
48 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME)
49
50 assert.Equal(t, "krb5data", string(a.UserData), "User data not as expected")
51 assert.Equal(t, tt, a.Timestamp, "Timestamp not as expected")
52 assert.Equal(t, 123456, a.Usec, "Microseconds not as expected")
53 assert.Equal(t, int64(17), a.SequenceNumber, "Sequence number not as expected")
54 assert.Equal(t, addrtype.IPv4, a.SAddress.AddrType, "SAddress type not as expected")
55 assert.Equal(t, "12d00023", hex.EncodeToString(a.SAddress.Address), "Address not as expected for SAddress")
56 assert.Equal(t, addrtype.IPv4, a.RAddress.AddrType, "RAddress type not as expected")
57 assert.Equal(t, "12d00023", hex.EncodeToString(a.RAddress.Address), "Address not as expected for RAddress")
58 }
59
60 func TestUnmarshalEncPrivPart_optionalsNULL(t *testing.T) {
61 t.Parallel()
62 var a EncKrbPrivPart
63 v := "encode_krb5_enc_priv_part(optionalsNULL)"
64 b, err := hex.DecodeString(testdata.TestVectors[v])
65 if err != nil {
66 t.Fatalf("Test vector read error of %s: %v\n", v, err)
67 }
68 err = a.Unmarshal(b)
69 if err != nil {
70 t.Fatalf("Unmarshal error of %s: %v\n", v, err)
71 }
72 assert.Equal(t, "krb5data", string(a.UserData), "User data not as expected")
73 assert.Equal(t, addrtype.IPv4, a.SAddress.AddrType, "SAddress type not as expected")
74 assert.Equal(t, "12d00023", hex.EncodeToString(a.SAddress.Address), "Address not as expected for SAddress")
75 }
76
77 func TestMarshalKRBPriv(t *testing.T) {
78 t.Parallel()
79 var a KRBPriv
80 v := "encode_krb5_priv"
81 b, err := hex.DecodeString(testdata.TestVectors[v])
82 if err != nil {
83 t.Fatalf("Test vector read error of %s: %v", v, err)
84 }
85 err = a.Unmarshal(b)
86 if err != nil {
87 t.Fatalf("Unmarshal error of %s: %v", v, err)
88 }
89 mb, err := a.Marshal()
90 if err != nil {
91 t.Fatalf("error marshaling KRBPriv: %v", err)
92 }
93 assert.Equal(t, b, mb, "marshaled bytes not as expected")
94
95 v = "encode_krb5_enc_priv_part"
96 be, err := hex.DecodeString(testdata.TestVectors[v])
97 if err != nil {
98 t.Fatalf("Test vector read error of %s: %v", v, err)
99 }
100 err = a.DecryptedEncPart.Unmarshal(be)
101 if err != nil {
102 t.Fatalf("Unmarshal error of %s: %v", v, err)
103 }
104 mb, err = a.Marshal()
105 if err != nil {
106 t.Fatalf("error marshaling KRBPriv: %v", err)
107 }
108 assert.Equal(t, b, mb, "marshaled bytes not as expected when it has decrypted encpart")
109 }
110
111 func TestKRBPriv_EncryptEncPart(t *testing.T) {
112 t.Parallel()
113 var a KRBPriv
114 v := "encode_krb5_priv"
115 b, err := hex.DecodeString(testdata.TestVectors[v])
116 if err != nil {
117 t.Fatalf("Test vector read error of %s: %v", v, err)
118 }
119 err = a.Unmarshal(b)
120 if err != nil {
121 t.Fatalf("Unmarshal error of %s: %v", v, err)
122 }
123 v = "encode_krb5_enc_priv_part"
124 b, err = hex.DecodeString(testdata.TestVectors[v])
125 if err != nil {
126 t.Fatalf("Test vector read error of %s: %v", v, err)
127 }
128 err = a.DecryptedEncPart.Unmarshal(b)
129 if err != nil {
130 t.Fatalf("Unmarshal error of %s: %v", v, err)
131 }
132 key := types.EncryptionKey{
133 KeyType: int32(18),
134 KeyValue: []byte("12345678901234567890123456789012"),
135 }
136 err = a.EncryptEncPart(key)
137 if err != nil {
138 t.Fatalf("error encrypting encpart: %v", err)
139 }
140 }