|
3 | 3 | package sockets
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "fmt" |
6 | 7 | "io/ioutil"
|
7 | 8 | "net"
|
8 | 9 | "os"
|
| 10 | + "os/exec" |
9 | 11 | "strconv"
|
10 | 12 | "testing"
|
11 | 13 | )
|
12 | 14 |
|
13 | 15 | func TestActivation(t *testing.T) {
|
14 |
| - lt, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv6loopback}) |
15 |
| - if err != nil { |
16 |
| - t.Fatal(err) |
17 |
| - } |
18 |
| - if c, err := lt.SyscallConn(); err == nil { |
19 |
| - var fd uintptr |
20 |
| - c.Control(func(nfd uintptr) { |
21 |
| - fd = nfd |
22 |
| - }) |
23 |
| - if fd != 3 { |
24 |
| - t.Fatal("expected fd to be 3") |
| 16 | + testTcpAddr := os.Getenv("SOCKET_TEST_TCP_ADDR") |
| 17 | + testUdpAddr := os.Getenv("SOCKET_TEST_UDP_ADDR") |
| 18 | + if testTcpAddr == "" { |
| 19 | + lt, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv6loopback}) |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + tcpAddr := lt.Addr() |
| 24 | + ltf, err := lt.File() |
| 25 | + lt.Close() |
| 26 | + if err != nil { |
| 27 | + t.Fatal(err) |
25 | 28 | }
|
26 |
| - } else { |
27 |
| - t.Fatal(err) |
28 |
| - } |
29 | 29 |
|
30 |
| - lu, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv6loopback}) |
31 |
| - if err != nil { |
32 |
| - t.Fatal(err) |
33 |
| - } |
34 |
| - if c, err := lu.SyscallConn(); err == nil { |
35 |
| - var fd uintptr |
36 |
| - c.Control(func(nfd uintptr) { |
37 |
| - fd = nfd |
38 |
| - }) |
39 |
| - if fd != 4 { |
40 |
| - t.Fatal("expected fd to be 3") |
| 30 | + lu, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv6loopback}) |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + udpAddr := lu.LocalAddr() |
| 35 | + luf, err := lu.File() |
| 36 | + lu.Close() |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
41 | 39 | }
|
42 |
| - } else { |
43 |
| - t.Fatal(err) |
44 |
| - } |
45 | 40 |
|
46 |
| - os.Setenv("LISTEN_FDS", "2") |
47 |
| - os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid())) |
48 |
| - os.Setenv("LISTEN_FDNAMES", "tcp:udp") |
49 |
| - tcp, err := TakeListeners("tcp") |
50 |
| - if err != nil { |
51 |
| - t.Fatal(err) |
52 |
| - } |
53 |
| - if len(tcp) != 1 { |
54 |
| - t.Fatalf("expected 1 listener, got %d", len(tcp)) |
55 |
| - } |
| 41 | + cmd := exec.Command("/proc/self/exe", "-test.run", "TestActivation") |
| 42 | + cmd.ExtraFiles = append(cmd.ExtraFiles, ltf, luf) |
| 43 | + cmd.Stdout = os.Stdout |
| 44 | + cmd.Stderr = os.Stderr |
| 45 | + cmd.Env = append( |
| 46 | + cmd.Env, |
| 47 | + "LISTEN_FDS=2", |
| 48 | + "LISTEN_FDNAMES=tcp:udp", |
| 49 | + fmt.Sprintf("SOCKET_TEST_TCP_ADDR=%s", tcpAddr), |
| 50 | + fmt.Sprintf("SOCKET_TEST_UDP_ADDR=%s", udpAddr), |
| 51 | + ) |
| 52 | + err = cmd.Run() |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + } else { |
| 57 | + // can't set this from the parent because we can't run anything between fork/exec. |
| 58 | + os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid())) |
| 59 | + tcp, err := TakeListeners("tcp") |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + if len(tcp) != 1 { |
| 64 | + t.Fatalf("expected 1 listener, got %d", len(tcp)) |
| 65 | + } |
56 | 66 |
|
57 |
| - go func() { |
58 |
| - l := tcp[0] |
59 |
| - c, err := l.Accept() |
| 67 | + go func() { |
| 68 | + l := tcp[0] |
| 69 | + c, err := l.Accept() |
| 70 | + if err != nil { |
| 71 | + t.Error(err) |
| 72 | + return |
| 73 | + } |
| 74 | + c.Write([]byte("foobar")) |
| 75 | + c.Close() |
| 76 | + }() |
| 77 | + c, err := net.Dial("tcp6", testTcpAddr) |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + b, err := ioutil.ReadAll(c) |
60 | 82 | if err != nil {
|
61 | 83 | t.Error(err)
|
62 |
| - return |
63 | 84 | }
|
64 |
| - c.Write([]byte("foobar")) |
65 |
| - c.Close() |
66 |
| - }() |
67 |
| - c, err := net.DialTCP("tcp", nil, lt.Addr().(*net.TCPAddr)) |
68 |
| - if err != nil { |
69 |
| - t.Fatal(err) |
70 |
| - } |
71 |
| - b, err := ioutil.ReadAll(c) |
72 |
| - if err != nil { |
73 |
| - t.Error(err) |
74 |
| - } |
75 |
| - if string(b) != "foobar" { |
76 |
| - t.Error("wrong message") |
77 |
| - } |
78 |
| - udp, err := TakePacketConns("udp") |
79 |
| - if err != nil { |
80 |
| - t.Fatal(err) |
81 |
| - } |
82 |
| - if len(udp) != 1 { |
83 |
| - t.Fatalf("expected 1 listener, got %d", len(tcp)) |
84 |
| - } |
85 |
| - if udp[0].LocalAddr().String() != lu.LocalAddr().String() { |
86 |
| - t.Fatal("got the wrong udp listener") |
| 85 | + if string(b) != "foobar" { |
| 86 | + t.Error("wrong message") |
| 87 | + } |
| 88 | + udp, err := TakePacketConns("udp") |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + if len(udp) != 1 { |
| 93 | + t.Fatalf("expected 1 listener, got %d", len(tcp)) |
| 94 | + } |
| 95 | + if udp[0].LocalAddr().String() != testUdpAddr { |
| 96 | + t.Fatal("got the wrong udp listener") |
| 97 | + } |
87 | 98 | }
|
88 | 99 | }
|
0 commit comments