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