github.com/jcmturner/gokrb5/v8@v8.4.4/client/cache_test.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/jcmturner/gokrb5/v8/messages"
    10  	"github.com/jcmturner/gokrb5/v8/types"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCache_addEntry_getEntry_remove_clear(t *testing.T) {
    15  	t.Parallel()
    16  	c := NewCache()
    17  	cnt := 10
    18  	var wg sync.WaitGroup
    19  	for i := 0; i < cnt; i++ {
    20  		wg.Add(1)
    21  		tkt := messages.Ticket{
    22  			SName: types.PrincipalName{
    23  				NameType:   1,
    24  				NameString: []string{fmt.Sprintf("%d", i), "test.cache"},
    25  			},
    26  		}
    27  		key := types.EncryptionKey{
    28  			KeyType:  1,
    29  			KeyValue: []byte{byte(i)},
    30  		}
    31  		go func(i int) {
    32  			e := c.addEntry(tkt, time.Unix(int64(0+i), 0).UTC(), time.Unix(int64(10+i), 0).UTC(), time.Unix(int64(20+i), 0).UTC(), time.Unix(int64(30+i), 0).UTC(), key)
    33  			assert.Equal(t, fmt.Sprintf("%d/test.cache", i), e.SPN, "SPN cache key not as expected")
    34  			wg.Done()
    35  		}(i)
    36  	}
    37  	wg.Wait()
    38  	for i := 0; i < cnt; i++ {
    39  		wg.Add(1)
    40  		go func(i int) {
    41  			e, ok := c.getEntry(fmt.Sprintf("%d/test.cache", i))
    42  			assert.True(t, ok, "cache entry %d was not found", i)
    43  			assert.Equal(t, time.Unix(int64(0+i), 0).UTC(), e.AuthTime, "auth time not as expected")
    44  			assert.Equal(t, time.Unix(int64(10+i), 0).UTC(), e.StartTime, "start time not as expected")
    45  			assert.Equal(t, time.Unix(int64(20+i), 0).UTC(), e.EndTime, "end time not as expected")
    46  			assert.Equal(t, time.Unix(int64(30+i), 0).UTC(), e.RenewTill, "renew time not as expected")
    47  			assert.Equal(t, []string{fmt.Sprintf("%d", i), "test.cache"}, e.Ticket.SName.NameString, "ticket not correct")
    48  			assert.Equal(t, []byte{byte(i)}, e.SessionKey.KeyValue, "session key not correct")
    49  			wg.Done()
    50  		}(i)
    51  	}
    52  	wg.Wait()
    53  	_, ok := c.getEntry(fmt.Sprintf("%d/test.cache", cnt+1))
    54  	assert.False(t, ok, "entry found in cache when it shouldn't have been")
    55  
    56  	// Remove just the even entries
    57  	for i := 0; i < cnt; i += 2 {
    58  		wg.Add(1)
    59  		go func(i int) {
    60  			c.RemoveEntry(fmt.Sprintf("%d/test.cache", i))
    61  			wg.Done()
    62  		}(i)
    63  	}
    64  	wg.Wait()
    65  
    66  	for i := 0; i < cnt; i++ {
    67  		wg.Add(1)
    68  		go func(i int) {
    69  			if i%2 == 0 {
    70  				_, ok := c.getEntry(fmt.Sprintf("%d/test.cache", cnt+1))
    71  				assert.False(t, ok, "entry %d found in cache when it shouldn't have been", i)
    72  			} else {
    73  				e, ok := c.getEntry(fmt.Sprintf("%d/test.cache", i))
    74  				assert.True(t, ok, "cache entry %d was not found", i)
    75  				assert.Equal(t, time.Unix(int64(0+i), 0).UTC(), e.AuthTime, "auth time not as expected")
    76  				assert.Equal(t, time.Unix(int64(10+i), 0).UTC(), e.StartTime, "start time not as expected")
    77  				assert.Equal(t, time.Unix(int64(20+i), 0).UTC(), e.EndTime, "end time not as expected")
    78  				assert.Equal(t, time.Unix(int64(30+i), 0).UTC(), e.RenewTill, "renew time not as expected")
    79  				assert.Equal(t, []string{fmt.Sprintf("%d", i), "test.cache"}, e.Ticket.SName.NameString, "ticket not correct")
    80  				assert.Equal(t, []byte{byte(i)}, e.SessionKey.KeyValue, "session key not correct")
    81  			}
    82  			wg.Done()
    83  		}(i)
    84  	}
    85  	wg.Wait()
    86  
    87  	// Clear the cache
    88  	c.clear()
    89  	for i := 0; i < cnt; i++ {
    90  		wg.Add(1)
    91  		go func(i int) {
    92  			_, ok := c.getEntry(fmt.Sprintf("%d/test.cache", cnt+1))
    93  			assert.False(t, ok, "entry %d found in cache when it shouldn't have been", i)
    94  			wg.Done()
    95  		}(i)
    96  	}
    97  	wg.Wait()
    98  }
    99  
   100  func TestCache_JSON(t *testing.T) {
   101  	t.Parallel()
   102  	c := NewCache()
   103  	cnt := 3
   104  	for i := 0; i < cnt; i++ {
   105  		tkt := messages.Ticket{
   106  			SName: types.PrincipalName{
   107  				NameType:   1,
   108  				NameString: []string{fmt.Sprintf("%d", i), "test.cache"},
   109  			},
   110  		}
   111  		key := types.EncryptionKey{
   112  			KeyType:  1,
   113  			KeyValue: []byte{byte(i)},
   114  		}
   115  		e := c.addEntry(tkt, time.Unix(int64(0+i), 0).UTC(), time.Unix(int64(10+i), 0).UTC(), time.Unix(int64(20+i), 0).UTC(), time.Unix(int64(30+i), 0).UTC(), key)
   116  		assert.Equal(t, fmt.Sprintf("%d/test.cache", i), e.SPN, "SPN cache key not as expected")
   117  	}
   118  	expected := `[
   119    {
   120      "SPN": "0/test.cache",
   121      "AuthTime": "1970-01-01T00:00:00Z",
   122      "StartTime": "1970-01-01T00:00:10Z",
   123      "EndTime": "1970-01-01T00:00:20Z",
   124      "RenewTill": "1970-01-01T00:00:30Z"
   125    },
   126    {
   127      "SPN": "1/test.cache",
   128      "AuthTime": "1970-01-01T00:00:01Z",
   129      "StartTime": "1970-01-01T00:00:11Z",
   130      "EndTime": "1970-01-01T00:00:21Z",
   131      "RenewTill": "1970-01-01T00:00:31Z"
   132    },
   133    {
   134      "SPN": "2/test.cache",
   135      "AuthTime": "1970-01-01T00:00:02Z",
   136      "StartTime": "1970-01-01T00:00:12Z",
   137      "EndTime": "1970-01-01T00:00:22Z",
   138      "RenewTill": "1970-01-01T00:00:32Z"
   139    }
   140  ]`
   141  	j, err := c.JSON()
   142  	if err != nil {
   143  		t.Errorf("error getting json output of cache: %v", err)
   144  	}
   145  	assert.Equal(t, expected, j, "json output not as expected")
   146  }