hosts_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package config
  2. import (
  3. "testing"
  4. "github.com/jcmturner/gokrb5/v8/test"
  5. "github.com/jcmturner/gokrb5/v8/test/testdata"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestConfig_GetKDCsUsesConfiguredKDC(t *testing.T) {
  9. t.Parallel()
  10. // This test is meant to cover the fix for
  11. // https://github.com/jcmturner/gokrb5/issues/332
  12. krb5ConfWithKDCAndDNSLookupKDC := `
  13. [libdefaults]
  14. dns_lookup_kdc = true
  15. [realms]
  16. TEST.GOKRB5 = {
  17. kdc = kdc2b.test.gokrb5:88
  18. }
  19. `
  20. c, err := NewFromString(krb5ConfWithKDCAndDNSLookupKDC)
  21. if err != nil {
  22. t.Fatalf("Error loading config: %v", err)
  23. }
  24. count, kdcs, err := c.GetKDCs("TEST.GOKRB5", false)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if count != 1 {
  29. t.Fatalf("expected 1 but received %d", count)
  30. }
  31. if kdcs[1] != "kdc2b.test.gokrb5:88" {
  32. t.Fatalf("expected kdc2b.test.gokrb5:88 but received %s", kdcs[1])
  33. }
  34. }
  35. func TestResolveKDC(t *testing.T) {
  36. test.Privileged(t)
  37. c, err := NewFromString(testdata.KRB5_CONF)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. // Store the original value for realms since we'll use them in our
  42. // second test.
  43. originalRealms := c.Realms
  44. // For our first test, let's check that we discover the expected
  45. // KDCs when they're not provided and we should be looking them up.
  46. c.LibDefaults.DNSLookupKDC = true
  47. c.Realms = make([]Realm, 0)
  48. count, res, err := c.GetKDCs(c.LibDefaults.DefaultRealm, true)
  49. if err != nil {
  50. t.Errorf("error resolving KDC via DNS TCP: %v", err)
  51. }
  52. assert.Equal(t, 5, count, "Number of SRV records not as expected: %v", res)
  53. assert.Equal(t, count, len(res), "Map size does not match: %v", res)
  54. expected := []string{
  55. "kdc.test.gokrb5:88",
  56. "kdc1a.test.gokrb5:88",
  57. "kdc2a.test.gokrb5:88",
  58. "kdc1b.test.gokrb5:88",
  59. "kdc2b.test.gokrb5:88",
  60. }
  61. for _, s := range expected {
  62. var found bool
  63. for _, v := range res {
  64. if s == v {
  65. found = true
  66. break
  67. }
  68. }
  69. assert.True(t, found, "Record %s not found in results", s)
  70. }
  71. // For our second check, verify that when we shouldn't be looking them up,
  72. // we get the expected value.
  73. c.LibDefaults.DNSLookupKDC = false
  74. c.Realms = originalRealms
  75. _, res, err = c.GetKDCs(c.LibDefaults.DefaultRealm, true)
  76. if err != nil {
  77. t.Errorf("error resolving KDCs from config: %v", err)
  78. }
  79. assert.Equal(t, "127.0.0.1:88", res[1], "KDC not read from config as expected")
  80. }