| 1 | //go:build linux && !appengine |
| 2 | // +build linux,!appengine |
| 3 | |
| 4 | package dns |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "net" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "golang.org/x/net/ipv4" |
| 15 | "golang.org/x/net/ipv6" |
| 16 | ) |
| 17 | |
| 18 | func TestSetUDPSocketOptions(t *testing.T) { |
| 19 | // returns an error if we cannot resolve that address |
| 20 | testFamily := func(n, addr string) error { |
| 21 | a, err := net.ResolveUDPAddr(n, addr) |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | c, err := net.ListenUDP(n, a) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if err := setUDPSocketOptions(c); err != nil { |
| 30 | t.Fatalf("failed to set socket options: %v", err) |
| 31 | } |
| 32 | ch := make(chan *SessionUDP) |
| 33 | go func() { |
| 34 | // Set some deadline so this goroutine doesn't hang forever |
| 35 | c.SetReadDeadline(time.Now().Add(time.Minute)) |
| 36 | b := make([]byte, 1) |
| 37 | _, sess, err := ReadFromSessionUDP(c, b) |
| 38 | if err != nil { |
| 39 | t.Errorf("failed to read from conn: %v", err) |
| 40 | // fallthrough to chan send below |
| 41 | } |
| 42 | ch <- sess |
| 43 | }() |
| 44 | |
| 45 | c2, err := net.Dial("udp", c.LocalAddr().String()) |
| 46 | if err != nil { |
| 47 | t.Fatalf("failed to dial udp: %v", err) |
| 48 | } |
| 49 | if _, err := c2.Write([]byte{1}); err != nil { |
| 50 | t.Fatalf("failed to write to conn: %v", err) |
| 51 | } |
| 52 | sess := <-ch |
| 53 | if sess == nil { |
| 54 | // t.Error was already called in the goroutine above. |
| 55 | t.FailNow() |
| 56 | } |
| 57 | if len(sess.context) == 0 { |
| 58 | t.Fatalf("empty session context: %v", sess) |
| 59 | } |
| 60 | ip := parseDstFromOOB(sess.context) |
| 61 | if ip == nil { |
| 62 | t.Fatalf("failed to parse dst: %v", sess) |
| 63 | } |
| 64 | if !strings.Contains(c.LocalAddr().String(), ip.String()) { |
| 65 | t.Fatalf("dst was different than listen addr: %v != %v", ip.String(), c.LocalAddr().String()) |
| 66 | } |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | // we require that ipv4 be supported |
| 71 | if err := testFamily("udp4", "127.0.0.1:0"); err != nil { |
| 72 | t.Fatalf("failed to test socket options on IPv4: %v", err) |
| 73 | } |
| 74 | // IPv6 might not be supported so these will just log |
| 75 | if err := testFamily("udp6", "[::1]:0"); err != nil { |
| 76 | t.Logf("failed to test socket options on IPv6-only: %v", err) |
| 77 | } |
| 78 | if err := testFamily("udp", "[::1]:0"); err != nil { |
| 79 | t.Logf("failed to test socket options on IPv6/IPv4: %v", err) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestParseDstFromOOB(t *testing.T) { |
| 84 | if runtime.GOARCH != "amd64" { |
| 85 | // The cmsghdr struct differs in the width (32/64-bit) of |
| 86 | // lengths and the struct padding between architectures. |
| 87 | // The data below was only written with amd64 in mind, and |
| 88 | // thus the test must be skipped on other architectures. |
| 89 | t.Skip("skipping test on unsupported architecture") |
| 90 | } |
| 91 | |
| 92 | // dst is :ffff:100.100.100.100 |
| 93 | oob := []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 100, 100, 2, 0, 0, 0} |
| 94 | dst := parseDstFromOOB(oob) |
| 95 | dst4 := dst.To4() |
| 96 | if dst4 == nil { |
| 97 | t.Errorf("failed to parse IPv4 in IPv6: %v", dst) |
| 98 | } else if dst4.String() != "100.100.100.100" { |
| 99 | t.Errorf("unexpected IPv4: %v", dst4) |
| 100 | } |
| 101 | |
| 102 | // dst is 2001:db8::1 |
| 103 | oob = []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0} |
| 104 | dst = parseDstFromOOB(oob) |
| 105 | dst6 := dst.To16() |
| 106 | if dst6 == nil { |
| 107 | t.Errorf("failed to parse IPv6: %v", dst) |
| 108 | } else if dst6.String() != "2001:db8::1" { |
| 109 | t.Errorf("unexpected IPv6: %v", dst4) |
| 110 | } |
| 111 | |
| 112 | // dst is 100.100.100.100 but was received on 10.10.10.10 |
| 113 | oob = []byte{28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 10, 10, 10, 10, 100, 100, 100, 100, 0, 0, 0, 0} |
| 114 | dst = parseDstFromOOB(oob) |
| 115 | dst4 = dst.To4() |
| 116 | if dst4 == nil { |
| 117 | t.Errorf("failed to parse IPv4: %v", dst) |
| 118 | } else if dst4.String() != "100.100.100.100" { |
| 119 | t.Errorf("unexpected IPv4: %v", dst4) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestCorrectSource(t *testing.T) { |
| 124 | if runtime.GOARCH != "amd64" { |
| 125 | // See comment above in TestParseDstFromOOB. |
| 126 | t.Skip("skipping test on unsupported architecture") |
| 127 | } |
| 128 | |
| 129 | // dst is :ffff:100.100.100.100 which should be counted as IPv4 |
| 130 | oob := []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 100, 100, 2, 0, 0, 0} |
| 131 | soob := correctSource(oob) |
| 132 | cm4 := new(ipv4.ControlMessage) |
| 133 | cm4.Src = net.ParseIP("100.100.100.100") |
| 134 | if !bytes.Equal(soob, cm4.Marshal()) { |
| 135 | t.Errorf("unexpected oob for ipv4 address: %v", soob) |
| 136 | } |
| 137 | |
| 138 | // dst is 2001:db8::1 |
| 139 | oob = []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0} |
| 140 | soob = correctSource(oob) |
| 141 | cm6 := new(ipv6.ControlMessage) |
| 142 | cm6.Src = net.ParseIP("2001:db8::1") |
| 143 | if !bytes.Equal(soob, cm6.Marshal()) { |
| 144 | t.Errorf("unexpected oob for IPv6 address: %v", soob) |
| 145 | } |
| 146 | } |