Source file
src/net/file_wasip1_test.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "syscall"
11 "testing"
12 )
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 func TestWasip1FileConnNet(t *testing.T) {
28 tests := []struct {
29 filetype syscall.Filetype
30 network string
31 error error
32 }{
33 {syscall.FILETYPE_SOCKET_STREAM, "tcp", nil},
34 {syscall.FILETYPE_SOCKET_DGRAM, "udp", nil},
35 {syscall.FILETYPE_BLOCK_DEVICE, "", syscall.ENOTSOCK},
36 {syscall.FILETYPE_CHARACTER_DEVICE, "", syscall.ENOTSOCK},
37 {syscall.FILETYPE_DIRECTORY, "", syscall.ENOTSOCK},
38 {syscall.FILETYPE_REGULAR_FILE, "", syscall.ENOTSOCK},
39 {syscall.FILETYPE_SYMBOLIC_LINK, "", syscall.ENOTSOCK},
40 {syscall.FILETYPE_UNKNOWN, "", syscall.ENOTSOCK},
41 }
42 for _, test := range tests {
43 net, err := fileConnNet(test.filetype)
44 if net != test.network {
45 t.Errorf("fileConnNet: network mismatch: want=%q got=%q", test.network, net)
46 }
47 if err != test.error {
48 t.Errorf("fileConnNet: error mismatch: want=%v got=%v", test.error, err)
49 }
50 }
51 }
52
53 func TestWasip1FileListenNet(t *testing.T) {
54 tests := []struct {
55 filetype syscall.Filetype
56 network string
57 error error
58 }{
59 {syscall.FILETYPE_SOCKET_STREAM, "tcp", nil},
60 {syscall.FILETYPE_SOCKET_DGRAM, "", syscall.EOPNOTSUPP},
61 {syscall.FILETYPE_BLOCK_DEVICE, "", syscall.ENOTSOCK},
62 {syscall.FILETYPE_CHARACTER_DEVICE, "", syscall.ENOTSOCK},
63 {syscall.FILETYPE_DIRECTORY, "", syscall.ENOTSOCK},
64 {syscall.FILETYPE_REGULAR_FILE, "", syscall.ENOTSOCK},
65 {syscall.FILETYPE_SYMBOLIC_LINK, "", syscall.ENOTSOCK},
66 {syscall.FILETYPE_UNKNOWN, "", syscall.ENOTSOCK},
67 }
68 for _, test := range tests {
69 net, err := fileListenNet(test.filetype)
70 if net != test.network {
71 t.Errorf("fileListenNet: network mismatch: want=%q got=%q", test.network, net)
72 }
73 if err != test.error {
74 t.Errorf("fileListenNet: error mismatch: want=%v got=%v", test.error, err)
75 }
76 }
77 }
78
79 func TestWasip1NewFileListener(t *testing.T) {
80 if l, ok := newFileListener(newFD("tcp", -1)).(*TCPListener); !ok {
81 t.Errorf("newFileListener: tcp listener type mismatch: %T", l)
82 } else {
83 testIsTCPAddr(t, "Addr", l.Addr())
84 }
85 }
86
87 func TestWasip1NewFileConn(t *testing.T) {
88 if c, ok := newFileConn(newFD("tcp", -1)).(*TCPConn); !ok {
89 t.Errorf("newFileConn: tcp conn type mismatch: %T", c)
90 } else {
91 testIsTCPAddr(t, "LocalAddr", c.LocalAddr())
92 testIsTCPAddr(t, "RemoteAddr", c.RemoteAddr())
93 }
94 if c, ok := newFileConn(newFD("udp", -1)).(*UDPConn); !ok {
95 t.Errorf("newFileConn: udp conn type mismatch: %T", c)
96 } else {
97 testIsUDPAddr(t, "LocalAddr", c.LocalAddr())
98 testIsUDPAddr(t, "RemoteAddr", c.RemoteAddr())
99 }
100 }
101
102 func testIsTCPAddr(t *testing.T, method string, addr Addr) {
103 if _, ok := addr.(*TCPAddr); !ok {
104 t.Errorf("%s: returned address is not a *TCPAddr: %T", method, addr)
105 }
106 }
107
108 func testIsUDPAddr(t *testing.T, method string, addr Addr) {
109 if _, ok := addr.(*UDPAddr); !ok {
110 t.Errorf("%s: returned address is not a *UDPAddr: %T", method, addr)
111 }
112 }
113
View as plain text