| 1 |
package messages |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/jcmturner/gokrb5/v8/iana" |
| 10 |
"github.com/jcmturner/gokrb5/v8/iana/addrtype" |
| 11 |
"github.com/jcmturner/gokrb5/v8/iana/msgtype" |
| 12 |
"github.com/jcmturner/gokrb5/v8/iana/nametype" |
| 13 |
"github.com/jcmturner/gokrb5/v8/test/testdata" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
) |
| 16 |
|
| 17 |
func TestUnmarshalKRBCred(t *testing.T) { |
| 18 |
t.Parallel() |
| 19 |
var a KRBCred |
| 20 |
b, err := hex.DecodeString(testdata.MarshaledKRB5cred) |
| 21 |
if err != nil { |
| 22 |
t.Fatalf("Test vector read error: %v", err) |
| 23 |
} |
| 24 |
err = a.Unmarshal(b) |
| 25 |
if err != nil { |
| 26 |
t.Fatalf("Unmarshal error: %v", err) |
| 27 |
} |
| 28 |
assert.Equal(t, iana.PVNO, a.PVNO, "PVNO not as expected") |
| 29 |
assert.Equal(t, msgtype.KRB_CRED, a.MsgType, "Message type not as expected") |
| 30 |
assert.Equal(t, 2, len(a.Tickets), "Number of tickets not as expected") |
| 31 |
for i, tkt := range a.Tickets { |
| 32 |
assert.Equal(t, iana.PVNO, tkt.TktVNO, fmt.Sprintf("Ticket (%v) ticket-vno not as expected", i+1)) |
| 33 |
assert.Equal(t, testdata.TEST_REALM, tkt.Realm, fmt.Sprintf("Ticket (%v) realm not as expected", i+1)) |
| 34 |
assert.Equal(t, nametype.KRB_NT_PRINCIPAL, tkt.SName.NameType, fmt.Sprintf("Ticket (%v) SName NameType not as expected", i+1)) |
| 35 |
assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(tkt.SName.NameString), fmt.Sprintf("Ticket (%v) SName does not have the expected number of NameStrings", i+1)) |
| 36 |
assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, tkt.SName.NameString, fmt.Sprintf("Ticket (%v) SName name string entries not as expected", i+1)) |
| 37 |
assert.Equal(t, testdata.TEST_ETYPE, tkt.EncPart.EType, fmt.Sprintf("Ticket (%v) encPart etype not as expected", i+1)) |
| 38 |
assert.Equal(t, iana.PVNO, tkt.EncPart.KVNO, fmt.Sprintf("Ticket (%v) encPart KVNO not as expected", i+1)) |
| 39 |
assert.Equal(t, []byte(testdata.TEST_CIPHERTEXT), tkt.EncPart.Cipher, fmt.Sprintf("Ticket (%v) encPart cipher not as expected", i+1)) |
| 40 |
} |
| 41 |
assert.Equal(t, testdata.TEST_ETYPE, a.EncPart.EType, "encPart etype not as expected") |
| 42 |
assert.Equal(t, iana.PVNO, a.EncPart.KVNO, "encPart KVNO not as expected") |
| 43 |
assert.Equal(t, []byte(testdata.TEST_CIPHERTEXT), a.EncPart.Cipher, "encPart cipher not as expected") |
| 44 |
} |
| 45 |
|
| 46 |
func TestUnmarshalEncCredPart(t *testing.T) { |
| 47 |
t.Parallel() |
| 48 |
var a EncKrbCredPart |
| 49 |
b, err := hex.DecodeString(testdata.MarshaledKRB5enc_cred_part) |
| 50 |
if err != nil { |
| 51 |
t.Fatalf("Test vector read error: %v", err) |
| 52 |
} |
| 53 |
err = a.Unmarshal(b) |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("Unmarshal error: %v", err) |
| 56 |
} |
| 57 |
//Parse the test time value into a time.Time type |
| 58 |
tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) |
| 59 |
|
| 60 |
assert.Equal(t, 2, len(a.TicketInfo), "Number of ticket info items not as expected") |
| 61 |
for i, tkt := range a.TicketInfo { |
| 62 |
assert.Equal(t, int32(1), tkt.Key.KeyType, fmt.Sprintf("Key type not as expected in ticket info item %d", i+1)) |
| 63 |
assert.Equal(t, []byte("12345678"), tkt.Key.KeyValue, fmt.Sprintf("Key value not as expected in ticket info item %d", i+1)) |
| 64 |
assert.Equal(t, testdata.TEST_REALM, tkt.PRealm, fmt.Sprintf("PRealm not as expected on ticket info item %d", i+1)) |
| 65 |
assert.Equal(t, nametype.KRB_NT_PRINCIPAL, tkt.PName.NameType, fmt.Sprintf("Ticket info (%v) PName NameType not as expected", i+1)) |
| 66 |
assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(tkt.PName.NameString), fmt.Sprintf("Ticket info (%v) PName does not have the expected number of NameStrings", i+1)) |
| 67 |
assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, tkt.PName.NameString, fmt.Sprintf("Ticket info (%v) PName name string entries not as expected", i+1)) |
| 68 |
assert.Equal(t, "fedcba98", hex.EncodeToString(tkt.Flags.Bytes), fmt.Sprintf("Flags not as expected on ticket info %d", i+1)) |
| 69 |
assert.Equal(t, tt, tkt.AuthTime, fmt.Sprintf("Auth time value not as expected for ticket info %d", i+1)) |
| 70 |
assert.Equal(t, tt, tkt.StartTime, fmt.Sprintf("Start time value not as expected for ticket info %d", i+1)) |
| 71 |
assert.Equal(t, tt, tkt.EndTime, fmt.Sprintf("End time value not as expected for ticket info %d", i+1)) |
| 72 |
assert.Equal(t, tt, tkt.RenewTill, fmt.Sprintf("Renew Till time value not as expected for ticket info %d", i+1)) |
| 73 |
assert.Equal(t, nametype.KRB_NT_PRINCIPAL, tkt.SName.NameType, fmt.Sprintf("Ticket info (%v) PName NameType not as expected", i+1)) |
| 74 |
assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(tkt.SName.NameString), fmt.Sprintf("Ticket info (%v) PName does not have the expected number of NameStrings", i+1)) |
| 75 |
assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, tkt.SName.NameString, fmt.Sprintf("Ticket info (%v) PName name string entries not as expected", i+1)) |
| 76 |
assert.Equal(t, 2, len(tkt.CAddr), "Number of client addresses not as expected") |
| 77 |
for j, addr := range tkt.CAddr { |
| 78 |
assert.Equal(t, addrtype.IPv4, addr.AddrType, fmt.Sprintf("Host address type not as expected for address item %d within ticket info %d", j+1, i+1)) |
| 79 |
assert.Equal(t, "12d00023", hex.EncodeToString(addr.Address), fmt.Sprintf("Host address not as expected for address item %d within ticket info %d", j+1, i+1)) |
| 80 |
} |
| 81 |
} |
| 82 |
assert.Equal(t, testdata.TEST_NONCE, a.Nouce, "Nouce not as expected") |
| 83 |
assert.Equal(t, tt, a.Timestamp, "Timestamp not as expected") |
| 84 |
assert.Equal(t, 123456, a.Usec, "Microseconds not as expected") |
| 85 |
assert.Equal(t, addrtype.IPv4, a.SAddress.AddrType, "SAddress type not as expected") |
| 86 |
assert.Equal(t, "12d00023", hex.EncodeToString(a.SAddress.Address), "Address not as expected for SAddress") |
| 87 |
assert.Equal(t, addrtype.IPv4, a.RAddress.AddrType, "RAddress type not as expected") |
| 88 |
assert.Equal(t, "12d00023", hex.EncodeToString(a.RAddress.Address), "Address not as expected for RAddress") |
| 89 |
} |
| 90 |
|
| 91 |
func TestUnmarshalEncCredPart_optionalsNULL(t *testing.T) { |
| 92 |
t.Parallel() |
| 93 |
var a EncKrbCredPart |
| 94 |
b, err := hex.DecodeString(testdata.MarshaledKRB5enc_cred_partOptionalsNULL) |
| 95 |
if err != nil { |
| 96 |
t.Fatalf("Test vector read error: %v", err) |
| 97 |
} |
| 98 |
err = a.Unmarshal(b) |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("Unmarshal error: %v", err) |
| 101 |
} |
| 102 |
//Parse the test time value into a time.Time type |
| 103 |
tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) |
| 104 |
|
| 105 |
assert.Equal(t, 2, len(a.TicketInfo), "Number of ticket info items not as expected") |
| 106 |
//1st Ticket |
| 107 |
i := 0 |
| 108 |
assert.Equal(t, int32(1), a.TicketInfo[i].Key.KeyType, fmt.Sprintf("Key type not as expected in ticket info item %d", i+1)) |
| 109 |
assert.Equal(t, []byte("12345678"), a.TicketInfo[i].Key.KeyValue, fmt.Sprintf("Key value not as expected in ticket info item %d", i+1)) |
| 110 |
|
| 111 |
//2nd Ticket |
| 112 |
i = 1 |
| 113 |
assert.Equal(t, int32(1), a.TicketInfo[i].Key.KeyType, fmt.Sprintf("Key type not as expected in ticket info item %d", i+1)) |
| 114 |
assert.Equal(t, []byte("12345678"), a.TicketInfo[i].Key.KeyValue, fmt.Sprintf("Key value not as expected in ticket info item %d", i+1)) |
| 115 |
assert.Equal(t, testdata.TEST_REALM, a.TicketInfo[i].PRealm, fmt.Sprintf("PRealm not as expected on ticket info item %d", i+1)) |
| 116 |
assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.TicketInfo[i].PName.NameType, fmt.Sprintf("Ticket info (%v) PName NameType not as expected", i+1)) |
| 117 |
assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.TicketInfo[i].PName.NameString), fmt.Sprintf("Ticket info (%v) PName does not have the expected number of NameStrings", i+1)) |
| 118 |
assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.TicketInfo[i].PName.NameString, fmt.Sprintf("Ticket info (%v) PName name string entries not as expected", i+1)) |
| 119 |
assert.Equal(t, "fedcba98", hex.EncodeToString(a.TicketInfo[i].Flags.Bytes), fmt.Sprintf("Flags not as expected on ticket info %d", i+1)) |
| 120 |
assert.Equal(t, tt, a.TicketInfo[i].AuthTime, fmt.Sprintf("Auth time value not as expected for ticket info %d", i+1)) |
| 121 |
assert.Equal(t, tt, a.TicketInfo[i].StartTime, fmt.Sprintf("Start time value not as expected for ticket info %d", i+1)) |
| 122 |
assert.Equal(t, tt, a.TicketInfo[i].EndTime, fmt.Sprintf("End time value not as expected for ticket info %d", i+1)) |
| 123 |
assert.Equal(t, tt, a.TicketInfo[i].RenewTill, fmt.Sprintf("Renew Till time value not as expected for ticket info %d", i+1)) |
| 124 |
assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.TicketInfo[i].SName.NameType, fmt.Sprintf("Ticket info (%v) PName NameType not as expected", i+1)) |
| 125 |
assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.TicketInfo[i].SName.NameString), fmt.Sprintf("Ticket info (%v) PName does not have the expected number of NameStrings", i+1)) |
| 126 |
assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.TicketInfo[i].SName.NameString, fmt.Sprintf("Ticket info (%v) PName name string entries not as expected", i+1)) |
| 127 |
assert.Equal(t, 2, len(a.TicketInfo[i].CAddr), "Number of client addresses not as expected") |
| 128 |
for j, addr := range a.TicketInfo[i].CAddr { |
| 129 |
assert.Equal(t, addrtype.IPv4, addr.AddrType, fmt.Sprintf("Host address type not as expected for address item %d within ticket info %d", j+1, i+1)) |
| 130 |
assert.Equal(t, "12d00023", hex.EncodeToString(addr.Address), fmt.Sprintf("Host address not as expected for address item %d within ticket info %d", j+1, i+1)) |
| 131 |
} |
| 132 |
} |