github.com/jcmturner/gokrb5/v8@v8.4.4/messages/KRBError_test.go (about) 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/errorcode" 10 "github.com/jcmturner/gokrb5/v8/iana/msgtype" 11 "github.com/jcmturner/gokrb5/v8/iana/nametype" 12 "github.com/jcmturner/gokrb5/v8/test/testdata" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestUnmarshalMarshalKRBError(t *testing.T) { 17 t.Parallel() 18 var a KRBError 19 b, err := hex.DecodeString(testdata.MarshaledKRB5error) 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 //Parse the test time value into a time.Time type 28 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 29 30 assert.Equal(t, iana.PVNO, a.PVNO, "PVNO is not as expected") 31 assert.Equal(t, msgtype.KRB_ERROR, a.MsgType, "Message type is not as expected") 32 assert.Equal(t, tt, a.CTime, "CTime not as expected") 33 assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected") 34 assert.Equal(t, tt, a.STime, "STime not as expected") 35 assert.Equal(t, 123456, a.Susec, "Service microseconds not as expected") 36 assert.Equal(t, errorcode.KRB_ERR_GENERIC, a.ErrorCode, "Error code not as expected") 37 assert.Equal(t, testdata.TEST_REALM, a.CRealm, "CRealm not as expected") 38 assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.CName.NameType, "CName NameType not as expected") 39 assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.CName.NameString), "CName does not have the expected number of NameStrings") 40 assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.CName.NameString, "CName entries not as expected") 41 assert.Equal(t, testdata.TEST_REALM, a.Realm, "Realm not as expected") 42 assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.SName.NameType, "Ticket SName NameType not as expected") 43 assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.SName.NameString), "Ticket SName does not have the expected number of NameStrings") 44 assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.SName.NameString, "Ticket SName name string entries not as expected") 45 assert.Equal(t, "krb5data", a.EText, "EText not as expected") 46 assert.Equal(t, []byte("krb5data"), a.EData, "EData not as expected") 47 48 b2, err := a.Marshal() 49 if err != nil { 50 t.Errorf("error marshalling KRBError: %v", err) 51 } 52 assert.Equal(t, b, b2, "marshalled bytes not as expected") 53 } 54 55 func TestUnmarshalMarshalKRBError_optionalsNULL(t *testing.T) { 56 t.Parallel() 57 var a KRBError 58 b, err := hex.DecodeString(testdata.MarshaledKRB5errorOptionalsNULL) 59 if err != nil { 60 t.Fatalf("Test vector read error: %v", err) 61 } 62 err = a.Unmarshal(b) 63 if err != nil { 64 t.Fatalf("Unmarshal error: %v", err) 65 } 66 //Parse the test time value into a time.Time type 67 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 68 69 assert.Equal(t, iana.PVNO, a.PVNO, "PVNO is not as expected") 70 assert.Equal(t, msgtype.KRB_ERROR, a.MsgType, "Message type is not as expected") 71 assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected") 72 assert.Equal(t, tt, a.STime, "STime not as expected") 73 assert.Equal(t, 123456, a.Susec, "Service microseconds not as expected") 74 assert.Equal(t, errorcode.KRB_ERR_GENERIC, a.ErrorCode, "Error code not as expected") 75 assert.Equal(t, testdata.TEST_REALM, a.Realm, "Realm not as expected") 76 assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.SName.NameType, "Ticket SName NameType not as expected") 77 assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.SName.NameString), "Ticket SName does not have the expected number of NameStrings") 78 assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.SName.NameString, "Ticket SName name string entries not as expected") 79 80 b2, err := a.Marshal() 81 if err != nil { 82 t.Errorf("error marshalling KRBError: %v", err) 83 } 84 assert.Equal(t, b, b2, "marshalled bytes not as expected") 85 }