You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
10 KiB

  1. package p2p_test
  2. import (
  3. "net"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/ed25519"
  8. "github.com/tendermint/tendermint/p2p"
  9. )
  10. func TestNewNodeID(t *testing.T) {
  11. // Most tests are in TestNodeID_Validate, this just checks that it's validated.
  12. testcases := []struct {
  13. input string
  14. expect p2p.NodeID
  15. ok bool
  16. }{
  17. {"", "", false},
  18. {"foo", "", false},
  19. {"00112233445566778899aabbccddeeff00112233", "00112233445566778899aabbccddeeff00112233", true},
  20. {"00112233445566778899AABBCCDDEEFF00112233", "00112233445566778899aabbccddeeff00112233", true},
  21. {"00112233445566778899aabbccddeeff0011223", "", false},
  22. {"00112233445566778899aabbccddeeff0011223g", "", false},
  23. }
  24. for _, tc := range testcases {
  25. tc := tc
  26. t.Run(tc.input, func(t *testing.T) {
  27. id, err := p2p.NewNodeID(tc.input)
  28. if !tc.ok {
  29. require.Error(t, err)
  30. } else {
  31. require.NoError(t, err)
  32. require.Equal(t, id, tc.expect)
  33. }
  34. })
  35. }
  36. }
  37. func TestNewNodeIDFromPubKey(t *testing.T) {
  38. privKey := ed25519.GenPrivKeyFromSecret([]byte("foo"))
  39. nodeID := p2p.NodeIDFromPubKey(privKey.PubKey())
  40. require.Equal(t, p2p.NodeID("045f5600654182cfeaccfe6cb19f0642e8a59898"), nodeID)
  41. }
  42. func TestNodeID_Bytes(t *testing.T) {
  43. testcases := []struct {
  44. nodeID p2p.NodeID
  45. expect []byte
  46. ok bool
  47. }{
  48. {"", []byte{}, true},
  49. {"01f0", []byte{0x01, 0xf0}, true},
  50. {"01F0", []byte{0x01, 0xf0}, true},
  51. {"01F", nil, false},
  52. {"01g0", nil, false},
  53. }
  54. for _, tc := range testcases {
  55. tc := tc
  56. t.Run(string(tc.nodeID), func(t *testing.T) {
  57. bz, err := tc.nodeID.Bytes()
  58. if tc.ok {
  59. require.NoError(t, err)
  60. require.Equal(t, tc.expect, bz)
  61. } else {
  62. require.Error(t, err)
  63. }
  64. })
  65. }
  66. }
  67. func TestNodeID_Validate(t *testing.T) {
  68. testcases := []struct {
  69. nodeID p2p.NodeID
  70. ok bool
  71. }{
  72. {"", false},
  73. {"00", false},
  74. {"00112233445566778899aabbccddeeff00112233", true},
  75. {"00112233445566778899aabbccddeeff001122334", false},
  76. {"00112233445566778899aabbccddeeffgg001122", false},
  77. {"00112233445566778899AABBCCDDEEFF00112233", false},
  78. }
  79. for _, tc := range testcases {
  80. tc := tc
  81. t.Run(string(tc.nodeID), func(t *testing.T) {
  82. err := tc.nodeID.Validate()
  83. if tc.ok {
  84. require.NoError(t, err)
  85. } else {
  86. require.Error(t, err)
  87. }
  88. })
  89. }
  90. }
  91. func TestParseNodeAddress(t *testing.T) {
  92. user := "00112233445566778899aabbccddeeff00112233"
  93. id := p2p.NodeID(user)
  94. testcases := []struct {
  95. url string
  96. expect p2p.NodeAddress
  97. ok bool
  98. }{
  99. // Valid addresses.
  100. {
  101. "mconn://" + user + "@127.0.0.1:26657/some/path?foo=bar",
  102. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "127.0.0.1", Port: 26657, Path: "/some/path?foo=bar"},
  103. true,
  104. },
  105. {
  106. "TCP://" + strings.ToUpper(user) + "@hostname.DOMAIN:8080/Path/%f0%9f%91%8B#Anchor",
  107. p2p.NodeAddress{Protocol: "tcp", NodeID: id, Hostname: "hostname.domain", Port: 8080, Path: "/Path/👋#Anchor"},
  108. true,
  109. },
  110. {
  111. user + "@127.0.0.1",
  112. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "127.0.0.1"},
  113. true,
  114. },
  115. {
  116. user + "@hostname.domain",
  117. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "hostname.domain"},
  118. true,
  119. },
  120. {
  121. user + "@hostname.domain:80",
  122. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "hostname.domain", Port: 80},
  123. true,
  124. },
  125. {
  126. user + "@%F0%9F%91%8B",
  127. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "👋"},
  128. true,
  129. },
  130. {
  131. user + "@%F0%9F%91%8B:80/path",
  132. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "👋", Port: 80, Path: "/path"},
  133. true,
  134. },
  135. {
  136. user + "@127.0.0.1:26657",
  137. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "127.0.0.1", Port: 26657},
  138. true,
  139. },
  140. {
  141. user + "@127.0.0.1:26657/path",
  142. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "127.0.0.1", Port: 26657, Path: "/path"},
  143. true,
  144. },
  145. {
  146. user + "@0.0.0.0:0",
  147. p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "0.0.0.0", Port: 0},
  148. true,
  149. },
  150. {
  151. "memory:" + user,
  152. p2p.NodeAddress{Protocol: "memory", NodeID: id, Path: user},
  153. true,
  154. },
  155. // Invalid addresses.
  156. {"", p2p.NodeAddress{}, false},
  157. {"127.0.0.1", p2p.NodeAddress{}, false},
  158. {"hostname", p2p.NodeAddress{}, false},
  159. {"scheme:", p2p.NodeAddress{}, false},
  160. {"memory:foo", p2p.NodeAddress{}, false},
  161. {user + "@%F%F0", p2p.NodeAddress{}, false},
  162. {"//" + user + "@127.0.0.1", p2p.NodeAddress{}, false},
  163. {"://" + user + "@127.0.0.1", p2p.NodeAddress{}, false},
  164. {"mconn://foo@127.0.0.1", p2p.NodeAddress{}, false},
  165. {"mconn://" + user + "@127.0.0.1:65536", p2p.NodeAddress{}, false},
  166. {"mconn://" + user + "@:80", p2p.NodeAddress{}, false},
  167. }
  168. for _, tc := range testcases {
  169. tc := tc
  170. t.Run(tc.url, func(t *testing.T) {
  171. address, err := p2p.ParseNodeAddress(tc.url)
  172. if !tc.ok {
  173. require.Error(t, err)
  174. } else {
  175. require.NoError(t, err)
  176. require.Equal(t, tc.expect, address)
  177. }
  178. })
  179. }
  180. }
  181. func TestNodeAddress_Resolve(t *testing.T) {
  182. id := p2p.NodeID("00112233445566778899aabbccddeeff00112233")
  183. testcases := []struct {
  184. address p2p.NodeAddress
  185. expect p2p.Endpoint
  186. ok bool
  187. }{
  188. // Valid networked addresses (with hostname).
  189. {
  190. p2p.NodeAddress{Protocol: "tcp", Hostname: "127.0.0.1", Port: 80, Path: "/path"},
  191. p2p.Endpoint{Protocol: "tcp", IP: net.IPv4(127, 0, 0, 1), Port: 80, Path: "/path"},
  192. true,
  193. },
  194. {
  195. p2p.NodeAddress{Protocol: "tcp", Hostname: "localhost", Port: 80, Path: "/path"},
  196. p2p.Endpoint{Protocol: "tcp", IP: net.IPv4(127, 0, 0, 1), Port: 80, Path: "/path"},
  197. true,
  198. },
  199. {
  200. p2p.NodeAddress{Protocol: "tcp", Hostname: "localhost", Port: 80, Path: "/path"},
  201. p2p.Endpoint{Protocol: "tcp", IP: net.IPv6loopback, Port: 80, Path: "/path"},
  202. true,
  203. },
  204. {
  205. p2p.NodeAddress{Protocol: "tcp", Hostname: "127.0.0.1"},
  206. p2p.Endpoint{Protocol: "tcp", IP: net.IPv4(127, 0, 0, 1)},
  207. true,
  208. },
  209. {
  210. p2p.NodeAddress{Protocol: "tcp", Hostname: "::1"},
  211. p2p.Endpoint{Protocol: "tcp", IP: net.IPv6loopback},
  212. true,
  213. },
  214. {
  215. p2p.NodeAddress{Protocol: "tcp", Hostname: "8.8.8.8"},
  216. p2p.Endpoint{Protocol: "tcp", IP: net.IPv4(8, 8, 8, 8)},
  217. true,
  218. },
  219. {
  220. p2p.NodeAddress{Protocol: "tcp", Hostname: "2001:0db8::ff00:0042:8329"},
  221. p2p.Endpoint{Protocol: "tcp", IP: []byte{
  222. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x83, 0x29}},
  223. true,
  224. },
  225. {
  226. p2p.NodeAddress{Protocol: "tcp", Hostname: "some.missing.host.tendermint.com"},
  227. p2p.Endpoint{},
  228. false,
  229. },
  230. // Valid non-networked addresses.
  231. {
  232. p2p.NodeAddress{Protocol: "memory", NodeID: id},
  233. p2p.Endpoint{Protocol: "memory", Path: string(id)},
  234. true,
  235. },
  236. {
  237. p2p.NodeAddress{Protocol: "memory", NodeID: id, Path: string(id)},
  238. p2p.Endpoint{Protocol: "memory", Path: string(id)},
  239. true,
  240. },
  241. // Invalid addresses.
  242. {p2p.NodeAddress{}, p2p.Endpoint{}, false},
  243. {p2p.NodeAddress{Hostname: "127.0.0.1"}, p2p.Endpoint{}, false},
  244. {p2p.NodeAddress{Protocol: "tcp", Hostname: "127.0.0.1:80"}, p2p.Endpoint{}, false},
  245. {p2p.NodeAddress{Protocol: "memory"}, p2p.Endpoint{}, false},
  246. {p2p.NodeAddress{Protocol: "memory", Path: string(id)}, p2p.Endpoint{}, false},
  247. {p2p.NodeAddress{Protocol: "tcp", Hostname: "💥"}, p2p.Endpoint{}, false},
  248. }
  249. for _, tc := range testcases {
  250. tc := tc
  251. t.Run(tc.address.String(), func(t *testing.T) {
  252. endpoints, err := tc.address.Resolve(ctx)
  253. if !tc.ok {
  254. require.Error(t, err)
  255. return
  256. }
  257. require.Contains(t, endpoints, tc.expect)
  258. })
  259. }
  260. }
  261. func TestNodeAddress_String(t *testing.T) {
  262. id := p2p.NodeID("00112233445566778899aabbccddeeff00112233")
  263. user := string(id)
  264. testcases := []struct {
  265. address p2p.NodeAddress
  266. expect string
  267. }{
  268. // Valid networked addresses (with hostname).
  269. {
  270. p2p.NodeAddress{Protocol: "tcp", NodeID: id, Hostname: "host", Port: 80, Path: "/path/sub?foo=bar&x=y#anchor"},
  271. "tcp://" + user + "@host:80/path/sub%3Ffoo=bar&x=y%23anchor",
  272. },
  273. {
  274. p2p.NodeAddress{Protocol: "tcp", NodeID: id, Hostname: "host.domain"},
  275. "tcp://" + user + "@host.domain",
  276. },
  277. {
  278. p2p.NodeAddress{NodeID: id, Hostname: "host", Port: 80, Path: "foo/bar"},
  279. user + "@host:80/foo/bar",
  280. },
  281. // Valid non-networked addresses (without hostname).
  282. {
  283. p2p.NodeAddress{Protocol: "memory", NodeID: id, Path: string(id)},
  284. "memory:" + user,
  285. },
  286. {
  287. p2p.NodeAddress{Protocol: "memory", NodeID: id},
  288. "memory:" + user,
  289. },
  290. // Addresses with weird contents, which are technically fine (not harmful).
  291. {
  292. p2p.NodeAddress{Protocol: "💬", NodeID: "👨", Hostname: "💻", Port: 80, Path: "🛣"},
  293. "💬://%F0%9F%91%A8@%F0%9F%92%BB:80/%F0%9F%9B%A3",
  294. },
  295. // Partial (invalid) addresses.
  296. {p2p.NodeAddress{}, ""},
  297. {p2p.NodeAddress{NodeID: id}, user + "@"},
  298. {p2p.NodeAddress{Protocol: "tcp"}, "tcp:"},
  299. {p2p.NodeAddress{Hostname: "host"}, "host"},
  300. {p2p.NodeAddress{Port: 80}, ""},
  301. {p2p.NodeAddress{Path: "path"}, "/path"},
  302. {p2p.NodeAddress{NodeID: id, Port: 80}, user + "@"},
  303. {p2p.NodeAddress{Protocol: "tcp", Hostname: "host"}, "tcp://host"},
  304. {
  305. p2p.NodeAddress{Protocol: "memory", NodeID: id, Path: "path"},
  306. "memory://00112233445566778899aabbccddeeff00112233@/path",
  307. },
  308. {
  309. p2p.NodeAddress{Protocol: "memory", NodeID: id, Port: 80},
  310. "memory:00112233445566778899aabbccddeeff00112233",
  311. },
  312. }
  313. for _, tc := range testcases {
  314. tc := tc
  315. t.Run(tc.address.String(), func(t *testing.T) {
  316. require.Equal(t, tc.expect, tc.address.String())
  317. })
  318. }
  319. }
  320. func TestNodeAddress_Validate(t *testing.T) {
  321. id := p2p.NodeID("00112233445566778899aabbccddeeff00112233")
  322. testcases := []struct {
  323. address p2p.NodeAddress
  324. ok bool
  325. }{
  326. // Valid addresses.
  327. {p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "host", Port: 80, Path: "/path"}, true},
  328. {p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "host"}, true},
  329. {p2p.NodeAddress{Protocol: "mconn", NodeID: id, Path: "path"}, true},
  330. {p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "👋", Path: "👋"}, true},
  331. // Invalid addresses.
  332. {p2p.NodeAddress{}, false},
  333. {p2p.NodeAddress{NodeID: "foo", Hostname: "host"}, false},
  334. {p2p.NodeAddress{Protocol: "mconn", NodeID: id}, true},
  335. {p2p.NodeAddress{Protocol: "mconn", NodeID: "foo", Hostname: "host"}, false},
  336. {p2p.NodeAddress{Protocol: "mconn", NodeID: id, Port: 80, Path: "path"}, false},
  337. }
  338. for _, tc := range testcases {
  339. tc := tc
  340. t.Run(tc.address.String(), func(t *testing.T) {
  341. err := tc.address.Validate()
  342. if tc.ok {
  343. require.NoError(t, err)
  344. } else {
  345. require.Error(t, err)
  346. }
  347. })
  348. }
  349. }