github.com/jcmturner/gokrb5/v8@v8.4.4/config/hosts_test.go (about)

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