github.com/jcmturner/gokrb5/v8@v8.4.4/types/Authenticator_test.go (about) 1 package types 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/adtype" 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 unmarshalAuthenticatorTest(t *testing.T, v string) Authenticator { 17 var a Authenticator 18 //t.Logf("Starting unmarshal tests of %s", v) 19 b, err := hex.DecodeString(v) 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 return a 28 } 29 func TestUnmarshalAuthenticator(t *testing.T) { 30 t.Parallel() 31 a := unmarshalAuthenticatorTest(t, testdata.MarshaledKRB5authenticator) 32 //Parse the test time value into a time.Time type 33 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 34 35 assert.Equal(t, iana.PVNO, a.AVNO, "Authenticator version number not as expected") 36 assert.Equal(t, testdata.TEST_REALM, a.CRealm, "CRealm not as expected") 37 assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.CName.NameType, "CName NameType not as expected") 38 assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.CName.NameString), "CName does not have the expected number of NameStrings") 39 assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.CName.NameString, "CName entries not as expected") 40 assert.Equal(t, int32(1), a.Cksum.CksumType, "Checksum type not as expected") 41 assert.Equal(t, []byte("1234"), a.Cksum.Checksum, "Checsum not as expected") 42 assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected") 43 assert.Equal(t, tt, a.CTime, "Client time not as expected") 44 assert.Equal(t, int32(1), a.SubKey.KeyType, "Subkey type not as expected") 45 assert.Equal(t, []byte("12345678"), a.SubKey.KeyValue, "Subkey value not as expected") 46 assert.Equal(t, 2, len(a.AuthorizationData), "Number of Authorization data items not as expected") 47 for i, entry := range a.AuthorizationData { 48 assert.Equal(t, adtype.ADIfRelevant, entry.ADType, fmt.Sprintf("Authorization type of entry %d not as expected", i+1)) 49 assert.Equal(t, []byte(testdata.TEST_AUTHORIZATION_DATA_VALUE), entry.ADData, fmt.Sprintf("Authorization data of entry %d not as expected", i+1)) 50 } 51 } 52 53 func TestUnmarshalAuthenticator_optionalsempty(t *testing.T) { 54 t.Parallel() 55 a := unmarshalAuthenticatorTest(t, testdata.MarshaledKRB5authenticatorOptionalsEmpty) 56 //Parse the test time value into a time.Time type 57 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 58 59 assert.Equal(t, iana.PVNO, a.AVNO, "Authenticator version number not as expected") 60 assert.Equal(t, testdata.TEST_REALM, a.CRealm, "CRealm not as expected") 61 assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.CName.NameType, "CName NameType not as expected") 62 assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.CName.NameString), "CName does not have the expected number of NameStrings") 63 assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.CName.NameString, "CName entries not as expected") 64 assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected") 65 assert.Equal(t, tt, a.CTime, "Client time not as expected") 66 } 67 68 func TestUnmarshalAuthenticator_optionalsNULL(t *testing.T) { 69 t.Parallel() 70 a := unmarshalAuthenticatorTest(t, testdata.MarshaledKRB5authenticatorOptionalsNULL) 71 //Parse the test time value into a time.Time type 72 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 73 74 assert.Equal(t, iana.PVNO, a.AVNO, "Authenticator version number not as expected") 75 assert.Equal(t, testdata.TEST_REALM, a.CRealm, "CRealm not as expected") 76 assert.Equal(t, nametype.KRB_NT_PRINCIPAL, a.CName.NameType, "CName NameType not as expected") 77 assert.Equal(t, len(testdata.TEST_PRINCIPALNAME_NAMESTRING), len(a.CName.NameString), "CName does not have the expected number of NameStrings") 78 assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, a.CName.NameString, "CName entries not as expected") 79 assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected") 80 assert.Equal(t, tt, a.CTime, "Client time not as expected") 81 } 82 83 func TestMarshalAuthenticator(t *testing.T) { 84 t.Parallel() 85 var a Authenticator 86 b, err := hex.DecodeString(testdata.MarshaledKRB5authenticator) 87 if err != nil { 88 t.Fatalf("Test vector read error: %v", err) 89 } 90 err = a.Unmarshal(b) 91 if err != nil { 92 t.Fatalf("Unmarshal error: %v", err) 93 } 94 mb, err := a.Marshal() 95 if err != nil { 96 t.Fatalf("Marshal of ticket errored: %v", err) 97 } 98 assert.Equal(t, b, mb, "Marshal bytes of Authenticator not as expected") 99 }