github.com/jcmturner/gokrb5/v8@v8.4.4/credentials/ccache_test.go (about) 1 package credentials 2 3 import ( 4 "encoding/hex" 5 "testing" 6 7 "github.com/jcmturner/gokrb5/v8/iana/nametype" 8 "github.com/jcmturner/gokrb5/v8/test/testdata" 9 "github.com/jcmturner/gokrb5/v8/types" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestParse(t *testing.T) { 14 t.Parallel() 15 b, err := hex.DecodeString(testdata.CCACHE_TEST) 16 if err != nil { 17 t.Fatal("Error decoding test data") 18 } 19 c := new(CCache) 20 err = c.Unmarshal(b) 21 if err != nil { 22 t.Fatalf("Error parsing cache: %v", err) 23 } 24 assert.Equal(t, uint8(4), c.Version, "Version not as expected") 25 assert.Equal(t, 1, len(c.Header.fields), "Number of header fields not as expected") 26 assert.Equal(t, uint16(1), c.Header.fields[0].tag, "Header tag not as expected") 27 assert.Equal(t, uint16(8), c.Header.fields[0].length, "Length of header not as expected") 28 assert.Equal(t, "TEST.GOKRB5", c.DefaultPrincipal.Realm, "Default client principal realm not as expected") 29 assert.Equal(t, "testuser1", c.DefaultPrincipal.PrincipalName.PrincipalNameString(), "Default client principaal name not as expected") 30 assert.Equal(t, 3, len(c.Credentials), "Number of credentials not as expected") 31 tgtpn := types.PrincipalName{ 32 NameType: nametype.KRB_NT_SRV_INST, 33 NameString: []string{"krbtgt", "TEST.GOKRB5"}, 34 } 35 assert.True(t, c.Contains(tgtpn), "Cache does not contain TGT credential") 36 httppn := types.PrincipalName{ 37 NameType: nametype.KRB_NT_PRINCIPAL, 38 NameString: []string{"HTTP", "host.test.gokrb5"}, 39 } 40 assert.True(t, c.Contains(httppn), "Cache does not contain HTTP SPN credential") 41 } 42 43 func TestCCache_GetClientPrincipalName(t *testing.T) { 44 t.Parallel() 45 b, err := hex.DecodeString(testdata.CCACHE_TEST) 46 if err != nil { 47 t.Fatal("Error decoding test data") 48 } 49 c := new(CCache) 50 err = c.Unmarshal(b) 51 if err != nil { 52 t.Fatalf("Error parsing cache: %v", err) 53 } 54 pn := types.PrincipalName{ 55 NameType: nametype.KRB_NT_PRINCIPAL, 56 NameString: []string{"testuser1"}, 57 } 58 assert.Equal(t, pn, c.GetClientPrincipalName(), "Client PrincipalName not as expected") 59 } 60 61 func TestCCache_GetClientCredentials(t *testing.T) { 62 t.Parallel() 63 b, err := hex.DecodeString(testdata.CCACHE_TEST) 64 if err != nil { 65 t.Fatal("Error decoding test data") 66 } 67 c := new(CCache) 68 err = c.Unmarshal(b) 69 if err != nil { 70 t.Fatalf("Error parsing cache: %v", err) 71 } 72 pn := types.PrincipalName{ 73 NameType: nametype.KRB_NT_PRINCIPAL, 74 NameString: []string{"testuser1"}, 75 } 76 cred := c.GetClientCredentials() 77 assert.Equal(t, "TEST.GOKRB5", cred.Domain(), "Client realm in credential not as expected") 78 assert.Equal(t, pn, cred.CName(), "Client Principal Name not as expected") 79 assert.Equal(t, "testuser1", cred.UserName(), "Username not as expected") 80 } 81 82 func TestCCache_GetClientRealm(t *testing.T) { 83 t.Parallel() 84 b, err := hex.DecodeString(testdata.CCACHE_TEST) 85 if err != nil { 86 t.Fatal("Error decoding test data") 87 } 88 c := new(CCache) 89 err = c.Unmarshal(b) 90 if err != nil { 91 t.Fatalf("Error parsing cache: %v", err) 92 } 93 assert.Equal(t, "TEST.GOKRB5", c.GetClientRealm(), "Client realm not as expected") 94 } 95 96 func TestCCache_GetEntry(t *testing.T) { 97 t.Parallel() 98 b, err := hex.DecodeString(testdata.CCACHE_TEST) 99 if err != nil { 100 t.Fatal("Error decoding test data") 101 } 102 c := new(CCache) 103 err = c.Unmarshal(b) 104 if err != nil { 105 t.Fatalf("Error parsing cache: %v", err) 106 } 107 httppn := types.PrincipalName{ 108 NameType: nametype.KRB_NT_PRINCIPAL, 109 NameString: []string{"HTTP", "host.test.gokrb5"}, 110 } 111 cred, ok := c.GetEntry(httppn) 112 if !ok { 113 t.Fatal("Could not get entry from CCache as not found") 114 } 115 assert.Equal(t, httppn, cred.Server.PrincipalName, "Credential does not have the right server principal name") 116 } 117 118 func TestCCache_GetEntries(t *testing.T) { 119 t.Parallel() 120 b, err := hex.DecodeString(testdata.CCACHE_TEST) 121 if err != nil { 122 t.Fatal("Error decoding test data") 123 } 124 c := new(CCache) 125 err = c.Unmarshal(b) 126 if err != nil { 127 t.Fatalf("Error parsing cache: %v", err) 128 } 129 creds := c.GetEntries() 130 assert.Equal(t, 2, len(creds), "Number of credentials entries not as expected") 131 }