...
Run Format

Source file src/net/dnsclient_test.go

Documentation: net

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"math/rand"
     9  	"testing"
    10  )
    11  
    12  func checkDistribution(t *testing.T, data []*SRV, margin float64) {
    13  	sum := 0
    14  	for _, srv := range data {
    15  		sum += int(srv.Weight)
    16  	}
    17  
    18  	results := make(map[string]int)
    19  
    20  	count := 1000
    21  	for j := 0; j < count; j++ {
    22  		d := make([]*SRV, len(data))
    23  		copy(d, data)
    24  		byPriorityWeight(d).shuffleByWeight()
    25  		key := d[0].Target
    26  		results[key] = results[key] + 1
    27  	}
    28  
    29  	actual := results[data[0].Target]
    30  	expected := float64(count) * float64(data[0].Weight) / float64(sum)
    31  	diff := float64(actual) - expected
    32  	t.Logf("actual: %v diff: %v e: %v m: %v", actual, diff, expected, margin)
    33  	if diff < 0 {
    34  		diff = -diff
    35  	}
    36  	if diff > (expected * margin) {
    37  		t.Errorf("missed target weight: expected %v, %v", expected, actual)
    38  	}
    39  }
    40  
    41  func testUniformity(t *testing.T, size int, margin float64) {
    42  	rand.Seed(1)
    43  	data := make([]*SRV, size)
    44  	for i := 0; i < size; i++ {
    45  		data[i] = &SRV{Target: string('a' + i), Weight: 1}
    46  	}
    47  	checkDistribution(t, data, margin)
    48  }
    49  
    50  func TestDNSSRVUniformity(t *testing.T) {
    51  	testUniformity(t, 2, 0.05)
    52  	testUniformity(t, 3, 0.10)
    53  	testUniformity(t, 10, 0.20)
    54  	testWeighting(t, 0.05)
    55  }
    56  
    57  func testWeighting(t *testing.T, margin float64) {
    58  	rand.Seed(1)
    59  	data := []*SRV{
    60  		{Target: "a", Weight: 60},
    61  		{Target: "b", Weight: 30},
    62  		{Target: "c", Weight: 10},
    63  	}
    64  	checkDistribution(t, data, margin)
    65  }
    66  
    67  func TestWeighting(t *testing.T) {
    68  	testWeighting(t, 0.05)
    69  }
    70  
    71  // Issue 8434: verify that Temporary returns true on an error when rcode
    72  // is SERVFAIL
    73  func TestIssue8434(t *testing.T) {
    74  	msg := &dnsMsg{
    75  		dnsMsgHdr: dnsMsgHdr{
    76  			rcode: dnsRcodeServerFailure,
    77  		},
    78  	}
    79  
    80  	_, _, err := answer("golang.org", "foo:53", msg, uint16(dnsTypeSRV))
    81  	if err == nil {
    82  		t.Fatal("expected an error")
    83  	}
    84  	if ne, ok := err.(Error); !ok {
    85  		t.Fatalf("err = %#v; wanted something supporting net.Error", err)
    86  	} else if !ne.Temporary() {
    87  		t.Fatalf("Temporary = false for err = %#v; want Temporary == true", err)
    88  	}
    89  	if de, ok := err.(*DNSError); !ok {
    90  		t.Fatalf("err = %#v; wanted a *net.DNSError", err)
    91  	} else if !de.IsTemporary {
    92  		t.Fatalf("IsTemporary = false for err = %#v; want IsTemporary == true", err)
    93  	}
    94  }
    95  
    96  // Issue 12778: verify that NXDOMAIN without RA bit errors as
    97  // "no such host" and not "server misbehaving"
    98  func TestIssue12778(t *testing.T) {
    99  	msg := &dnsMsg{
   100  		dnsMsgHdr: dnsMsgHdr{
   101  			rcode:               dnsRcodeNameError,
   102  			recursion_available: false,
   103  		},
   104  	}
   105  
   106  	_, _, err := answer("golang.org", "foo:53", msg, uint16(dnsTypeSRV))
   107  	if err == nil {
   108  		t.Fatal("expected an error")
   109  	}
   110  	de, ok := err.(*DNSError)
   111  	if !ok {
   112  		t.Fatalf("err = %#v; wanted a *net.DNSError", err)
   113  	}
   114  	if de.Err != errNoSuchHost.Error() {
   115  		t.Fatalf("Err = %#v; wanted %q", de.Err, errNoSuchHost.Error())
   116  	}
   117  }
   118  

View as plain text