syscall_internal_linux_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2019 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. // +build linux
  5. package unix
  6. import (
  7. "reflect"
  8. "testing"
  9. "unsafe"
  10. )
  11. func Test_anyToSockaddr(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. rsa *RawSockaddrAny
  15. sa Sockaddr
  16. err error
  17. }{
  18. {
  19. name: "AF_TIPC bad addrtype",
  20. rsa: &RawSockaddrAny{
  21. Addr: RawSockaddr{
  22. Family: AF_TIPC,
  23. },
  24. },
  25. err: EINVAL,
  26. },
  27. {
  28. name: "AF_TIPC NameSeq",
  29. rsa: (*RawSockaddrAny)(unsafe.Pointer(&RawSockaddrTIPC{
  30. Family: AF_TIPC,
  31. Addrtype: TIPC_SERVICE_RANGE,
  32. Scope: 1,
  33. Addr: (&TIPCServiceRange{
  34. Type: 1,
  35. Lower: 2,
  36. Upper: 3,
  37. }).tipcAddr(),
  38. })),
  39. sa: &SockaddrTIPC{
  40. Scope: 1,
  41. Addr: &TIPCServiceRange{
  42. Type: 1,
  43. Lower: 2,
  44. Upper: 3,
  45. },
  46. },
  47. },
  48. {
  49. name: "AF_TIPC Name",
  50. rsa: (*RawSockaddrAny)(unsafe.Pointer(&RawSockaddrTIPC{
  51. Family: AF_TIPC,
  52. Addrtype: TIPC_SERVICE_ADDR,
  53. Scope: 2,
  54. Addr: (&TIPCServiceName{
  55. Type: 1,
  56. Instance: 2,
  57. Domain: 3,
  58. }).tipcAddr(),
  59. })),
  60. sa: &SockaddrTIPC{
  61. Scope: 2,
  62. Addr: &TIPCServiceName{
  63. Type: 1,
  64. Instance: 2,
  65. Domain: 3,
  66. },
  67. },
  68. },
  69. {
  70. name: "AF_TIPC ID",
  71. rsa: (*RawSockaddrAny)(unsafe.Pointer(&RawSockaddrTIPC{
  72. Family: AF_TIPC,
  73. Addrtype: TIPC_SOCKET_ADDR,
  74. Scope: 3,
  75. Addr: (&TIPCSocketAddr{
  76. Ref: 1,
  77. Node: 2,
  78. }).tipcAddr(),
  79. })),
  80. sa: &SockaddrTIPC{
  81. Scope: 3,
  82. Addr: &TIPCSocketAddr{
  83. Ref: 1,
  84. Node: 2,
  85. },
  86. },
  87. },
  88. {
  89. name: "AF_MAX EAFNOSUPPORT",
  90. rsa: &RawSockaddrAny{
  91. Addr: RawSockaddr{
  92. Family: AF_MAX,
  93. },
  94. },
  95. err: EAFNOSUPPORT,
  96. },
  97. // TODO: expand to support other families.
  98. }
  99. for _, tt := range tests {
  100. t.Run(tt.name, func(t *testing.T) {
  101. // TODO: parameterize fd (and its setup) when needed.
  102. sa, err := anyToSockaddr(0, tt.rsa)
  103. if err != tt.err {
  104. t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
  105. }
  106. if !reflect.DeepEqual(sa, tt.sa) {
  107. t.Fatalf("unexpected Sockaddr:\n got: %#v\nwant: %#v", sa, tt.sa)
  108. }
  109. })
  110. }
  111. }
  112. func TestSockaddrTIPC_sockaddr(t *testing.T) {
  113. tests := []struct {
  114. name string
  115. sa *SockaddrTIPC
  116. raw *RawSockaddrTIPC
  117. err error
  118. }{
  119. {
  120. name: "no fields set",
  121. sa: &SockaddrTIPC{},
  122. err: EINVAL,
  123. },
  124. {
  125. name: "ID",
  126. sa: &SockaddrTIPC{
  127. Scope: 1,
  128. Addr: &TIPCSocketAddr{
  129. Ref: 1,
  130. Node: 2,
  131. },
  132. },
  133. raw: &RawSockaddrTIPC{
  134. Family: AF_TIPC,
  135. Addrtype: TIPC_SOCKET_ADDR,
  136. Scope: 1,
  137. Addr: (&TIPCSocketAddr{
  138. Ref: 1,
  139. Node: 2,
  140. }).tipcAddr(),
  141. },
  142. },
  143. {
  144. name: "NameSeq",
  145. sa: &SockaddrTIPC{
  146. Scope: 2,
  147. Addr: &TIPCServiceRange{
  148. Type: 1,
  149. Lower: 2,
  150. Upper: 3,
  151. },
  152. },
  153. raw: &RawSockaddrTIPC{
  154. Family: AF_TIPC,
  155. Addrtype: TIPC_SERVICE_RANGE,
  156. Scope: 2,
  157. Addr: (&TIPCServiceRange{
  158. Type: 1,
  159. Lower: 2,
  160. Upper: 3,
  161. }).tipcAddr(),
  162. },
  163. },
  164. {
  165. name: "Name",
  166. sa: &SockaddrTIPC{
  167. Scope: 3,
  168. Addr: &TIPCServiceName{
  169. Type: 1,
  170. Instance: 2,
  171. Domain: 3,
  172. },
  173. },
  174. raw: &RawSockaddrTIPC{
  175. Family: AF_TIPC,
  176. Addrtype: TIPC_SERVICE_ADDR,
  177. Scope: 3,
  178. Addr: (&TIPCServiceName{
  179. Type: 1,
  180. Instance: 2,
  181. Domain: 3,
  182. }).tipcAddr(),
  183. },
  184. },
  185. }
  186. for _, tt := range tests {
  187. t.Run(tt.name, func(t *testing.T) {
  188. out, l, err := tt.sa.sockaddr()
  189. if err != tt.err {
  190. t.Fatalf("unexpected error: %v, want: %v", err, tt.err)
  191. }
  192. // Must be 0 on error or a fixed size otherwise.
  193. if (tt.err != nil && l != 0) || (tt.raw != nil && l != SizeofSockaddrTIPC) {
  194. t.Fatalf("unexpected Socklen: %d", l)
  195. }
  196. if out == nil {
  197. // No pointer to cast, return early.
  198. return
  199. }
  200. raw := (*RawSockaddrTIPC)(out)
  201. if !reflect.DeepEqual(raw, tt.raw) {
  202. t.Fatalf("unexpected RawSockaddrTIPC:\n got: %#v\nwant: %#v", raw, tt.raw)
  203. }
  204. })
  205. }
  206. }