keytab_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package keytab
  2. import (
  3. "encoding/base64"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "gopkg.in/jcmturner/gokrb5.v7/test/testdata"
  12. )
  13. func TestUnmarshal(t *testing.T) {
  14. t.Parallel()
  15. b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  16. kt := New()
  17. err := kt.Unmarshal(b)
  18. if err != nil {
  19. t.Fatalf("Error parsing keytab data: %v\n", err)
  20. }
  21. assert.Equal(t, uint8(2), kt.version, "Keytab version not as expected")
  22. assert.Equal(t, uint32(1), kt.Entries[0].KVNO, "KVNO not as expected")
  23. assert.Equal(t, uint8(1), kt.Entries[0].KVNO8, "KVNO8 not as expected")
  24. assert.Equal(t, time.Unix(1505669592, 0), kt.Entries[0].Timestamp, "Timestamp not as expected")
  25. assert.Equal(t, int32(17), kt.Entries[0].Key.KeyType, "Key's EType not as expected")
  26. assert.Equal(t, "698c4df8e9f60e7eea5a21bf4526ad25", hex.EncodeToString(kt.Entries[0].Key.KeyValue), "Key material not as expected")
  27. assert.Equal(t, int16(1), kt.Entries[0].Principal.NumComponents, "Number of components in principal not as expected")
  28. assert.Equal(t, int32(1), kt.Entries[0].Principal.NameType, "Name type of principal not as expected")
  29. assert.Equal(t, "TEST.GOKRB5", kt.Entries[0].Principal.Realm, "Realm of principal not as expected")
  30. assert.Equal(t, "testuser1", kt.Entries[0].Principal.Components[0], "Component in principal not as expected")
  31. }
  32. func TestMarshal(t *testing.T) {
  33. t.Parallel()
  34. b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  35. kt := New()
  36. err := kt.Unmarshal(b)
  37. if err != nil {
  38. t.Fatalf("Error parsing keytab data: %v\n", err)
  39. }
  40. mb, err := kt.Marshal()
  41. if err != nil {
  42. t.Fatalf("Error marshaling: %v", err)
  43. }
  44. assert.Equal(t, b, mb, "Marshaled bytes not the same as input bytes")
  45. err = kt.Unmarshal(mb)
  46. if err != nil {
  47. t.Fatalf("Error parsing marshaled bytes: %v", err)
  48. }
  49. }
  50. func TestLoad(t *testing.T) {
  51. t.Parallel()
  52. f := "test/testdata/testuser1.testtab"
  53. cwd, _ := os.Getwd()
  54. dir := os.Getenv("TRAVIS_BUILD_DIR")
  55. if dir != "" {
  56. f = dir + "/" + f
  57. } else if filepath.Base(cwd) == "keytab" {
  58. f = "../" + f
  59. }
  60. kt, err := Load(f)
  61. if err != nil {
  62. t.Fatalf("could not load keytab: %v", err)
  63. }
  64. assert.Equal(t, uint8(2), kt.version, "keytab version not as expected")
  65. assert.Equal(t, 12, len(kt.Entries), "keytab entry count not as expected: %+v", *kt)
  66. for _, e := range kt.Entries {
  67. if e.Principal.Realm != "TEST.GOKRB5" {
  68. t.Error("principal realm not as expected")
  69. }
  70. if e.Principal.NameType != int32(1) {
  71. t.Error("name type not as expected")
  72. }
  73. if e.Principal.NumComponents != int16(1) {
  74. t.Error("number of component not as expected")
  75. }
  76. if len(e.Principal.Components) != 1 {
  77. t.Error("number of component not as expected")
  78. }
  79. if e.Principal.Components[0] != "testuser1" {
  80. t.Error("principal components not as expected")
  81. }
  82. if e.Timestamp.IsZero() {
  83. t.Error("entry timestamp incorrect")
  84. }
  85. if e.KVNO == uint32(0) {
  86. t.Error("entry kvno not as expected")
  87. }
  88. if e.KVNO8 == uint8(0) {
  89. t.Error("entry kvno8 not as expected")
  90. }
  91. }
  92. }
  93. // This test provides inputs to readBytes that previously
  94. // caused a panic.
  95. func TestReadBytes(t *testing.T) {
  96. var endian binary.ByteOrder
  97. endian = binary.BigEndian
  98. p := 0
  99. if _, err := readBytes(nil, &p, 1, &endian); err == nil {
  100. t.Fatal("err should be populated because s was given that exceeds array length")
  101. }
  102. if _, err := readBytes(nil, &p, -1, &endian); err == nil {
  103. t.Fatal("err should be given because negative s was given")
  104. }
  105. }
  106. func TestUnmarshalPotentialPanics(t *testing.T) {
  107. kt := New()
  108. // Test a good keytab with bad bytes to unmarshal. These should
  109. // return errors, but not panic.
  110. if err := kt.Unmarshal(nil); err == nil {
  111. t.Fatal("should have errored, input is absent")
  112. }
  113. if err := kt.Unmarshal([]byte{}); err == nil {
  114. t.Fatal("should have errored, input is empty")
  115. }
  116. // Incorrect first byte.
  117. if err := kt.Unmarshal([]byte{4}); err == nil {
  118. t.Fatal("should have errored, input isn't long enough")
  119. }
  120. // First byte, but no further content.
  121. if err := kt.Unmarshal([]byte{5}); err == nil {
  122. t.Fatal("should have errored, input isn't long enough")
  123. }
  124. }
  125. // cxf testing stuff
  126. func TestBadKeytabs(t *testing.T) {
  127. badPayloads := make([]string, 3)
  128. badPayloads = append(badPayloads, "BQIwMDAwMDA=")
  129. badPayloads = append(badPayloads, "BQIAAAAwAAEACjAwMDAwMDAwMDAAIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw")
  130. badPayloads = append(badPayloads, "BQKAAAAA")
  131. for _, v := range badPayloads {
  132. decodedKt, _ := base64.StdEncoding.DecodeString(v)
  133. parsedKt := new(Keytab)
  134. parsedKt.Unmarshal(decodedKt)
  135. }
  136. }