| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"gopkg.in/jcmturner/gokrb5.v5/iana" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v5/iana/adtype" |
| 12 |
"gopkg.in/jcmturner/gokrb5.v5/iana/nametype" |
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/testdata" |
| 14 |
) |
| 15 |
|
| 16 |
func unmarshalAuthenticatorTest(t *testing.T, v string) Authenticator { |
| 17 |
var a Authenticator |
| 18 |
|
| 19 |
b, err := hex.DecodeString(testdata.TestVectors[v]) |
| 20 |
if err != nil { |
| 21 |
t.Fatalf("Test vector read error of %s: %v\n", v, err) |
| 22 |
} |
| 23 |
err = a.Unmarshal(b) |
| 24 |
if err != nil { |
| 25 |
t.Fatalf("Unmarshal error of %s: %v\n", v, err) |
| 26 |
} |
| 27 |
return a |
| 28 |
} |
| 29 |
func TestUnmarshalAuthenticator(t *testing.T) { |
| 30 |
t.Parallel() |
| 31 |
a := unmarshalAuthenticatorTest(t, "encode_krb5_authenticator") |
| 32 |
|
| 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, "encode_krb5_authenticator(optionalsempty)") |
| 56 |
|
| 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, "encode_krb5_authenticator(optionalsNULL)") |
| 71 |
|
| 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 |
v := "encode_krb5_authenticator" |
| 87 |
b, err := hex.DecodeString(testdata.TestVectors[v]) |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("Test vector read error of %s: %v\n", v, err) |
| 90 |
} |
| 91 |
err = a.Unmarshal(b) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("Unmarshal error of %s: %v\n", v, err) |
| 94 |
} |
| 95 |
mb, err := a.Marshal() |
| 96 |
if err != nil { |
| 97 |
t.Fatalf("Marshal of ticket errored: %v", err) |
| 98 |
} |
| 99 |
assert.Equal(t, b, mb, "Marshal bytes of Authenticator not as expected") |
| 100 |
} |