1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"bytes"
"reflect"
"syscall"
"testing"
"time"
)
func TestUnixgramAutobind(t *testing.T) {
laddr := &UnixAddr{Name: "", Net: "unixgram"}
c1, err := ListenUnixgram("unixgram", laddr)
if err != nil {
t.Fatal(err)
}
defer c1.Close()
// retrieve the autobind address
autoAddr := c1.LocalAddr().(*UnixAddr)
if len(autoAddr.Name) <= 1 {
t.Fatalf("invalid autobind address: %v", autoAddr)
}
if autoAddr.Name[0] != '@' {
t.Fatalf("invalid autobind address: %v", autoAddr)
}
c2, err := DialUnix("unixgram", nil, autoAddr)
if err != nil {
t.Fatal(err)
}
defer c2.Close()
if !reflect.DeepEqual(c1.LocalAddr(), c2.RemoteAddr()) {
t.Fatalf("expected autobind address %v, got %v", c1.LocalAddr(), c2.RemoteAddr())
}
}
func TestUnixAutobindClose(t *testing.T) {
laddr := &UnixAddr{Name: "", Net: "unix"}
ln, err := ListenUnix("unix", laddr)
if err != nil {
t.Fatal(err)
}
ln.Close()
}
func TestUnixgramLinuxAbstractLongName(t *testing.T) {
if !testableNetwork("unixgram") {
t.Skip("abstract unix socket long name test")
}
// Create an abstract socket name whose length is exactly
// the maximum RawSockkaddrUnix Path len
rsu := syscall.RawSockaddrUnix{}
addrBytes := make([]byte, len(rsu.Path))
copy(addrBytes, "@abstract_test")
addr := string(addrBytes)
la, err := ResolveUnixAddr("unixgram", addr)
if err != nil {
t.Fatal(err)
}
c, err := ListenUnixgram("unixgram", la)
if err != nil {
t.Fatal(err)
}
defer c.Close()
off := make(chan bool)
data := [5]byte{1, 2, 3, 4, 5}
go func() {
defer func() { off <- true }()
s, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0)
if err != nil {
t.Error(err)
return
}
defer syscall.Close(s)
rsa := &syscall.SockaddrUnix{Name: addr}
if err := syscall.Sendto(s, data[:], 0, rsa); err != nil {
t.Error(err)
return
}
}()
<-off
b := make([]byte, 64)
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
n, from, err := c.ReadFrom(b)
if err != nil {
t.Fatal(err)
}
if from != nil {
t.Fatalf("unexpected peer address: %v", from)
}
if !bytes.Equal(b[:n], data[:]) {
t.Fatalf("got %v; want %v", b[:n], data[:])
}
}
|