cache_test.go 4.5 KB

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