nat_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package nat
  2. import (
  3. "testing"
  4. )
  5. func TestParsePort(t *testing.T) {
  6. var (
  7. p int
  8. err error
  9. )
  10. p, err = ParsePort("1234")
  11. if err != nil || p != 1234 {
  12. t.Fatal("Parsing '1234' did not succeed")
  13. }
  14. // FIXME currently this is a valid port. I don't think it should be.
  15. // I'm leaving this test commented out until we make a decision.
  16. // - erikh
  17. /*
  18. p, err = ParsePort("0123")
  19. if err != nil {
  20. t.Fatal("Successfully parsed port '0123' to '123'")
  21. }
  22. */
  23. p, err = ParsePort("asdf")
  24. if err == nil || p != 0 {
  25. t.Fatal("Parsing port 'asdf' succeeded")
  26. }
  27. p, err = ParsePort("1asdf")
  28. if err == nil || p != 0 {
  29. t.Fatal("Parsing port '1asdf' succeeded")
  30. }
  31. }
  32. func TestPort(t *testing.T) {
  33. p := NewPort("tcp", "1234")
  34. if string(p) != "1234/tcp" {
  35. t.Fatal("tcp, 1234 did not result in the string 1234/tcp")
  36. }
  37. if p.Proto() != "tcp" {
  38. t.Fatal("protocol was not tcp")
  39. }
  40. if p.Port() != "1234" {
  41. t.Fatal("port string value was not 1234")
  42. }
  43. if p.Int() != 1234 {
  44. t.Fatal("port int value was not 1234")
  45. }
  46. }
  47. func TestSplitProtoPort(t *testing.T) {
  48. var (
  49. proto string
  50. port string
  51. )
  52. proto, port = SplitProtoPort("1234/tcp")
  53. if proto != "tcp" || port != "1234" {
  54. t.Fatal("Could not split 1234/tcp properly")
  55. }
  56. proto, port = SplitProtoPort("")
  57. if proto != "" || port != "" {
  58. t.Fatal("parsing an empty string yielded surprising results", proto, port)
  59. }
  60. proto, port = SplitProtoPort("1234")
  61. if proto != "tcp" || port != "1234" {
  62. t.Fatal("tcp is not the default protocol for portspec '1234'", proto, port)
  63. }
  64. proto, port = SplitProtoPort("1234/")
  65. if proto != "tcp" || port != "1234" {
  66. t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
  67. }
  68. proto, port = SplitProtoPort("/tcp")
  69. if proto != "" || port != "" {
  70. t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
  71. }
  72. }
  73. func TestParsePortSpecs(t *testing.T) {
  74. var (
  75. portMap map[Port]struct{}
  76. bindingMap map[Port][]PortBinding
  77. err error
  78. )
  79. portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"})
  80. if err != nil {
  81. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  82. }
  83. if _, ok := portMap[Port("1234/tcp")]; !ok {
  84. t.Fatal("1234/tcp was not parsed properly")
  85. }
  86. if _, ok := portMap[Port("2345/udp")]; !ok {
  87. t.Fatal("2345/udp was not parsed properly")
  88. }
  89. for portspec, bindings := range bindingMap {
  90. if len(bindings) != 1 {
  91. t.Fatalf("%s should have exactly one binding", portspec)
  92. }
  93. if bindings[0].HostIp != "" {
  94. t.Fatalf("HostIp should not be set for %s", portspec)
  95. }
  96. if bindings[0].HostPort != "" {
  97. t.Fatalf("HostPort should not be set for %s", portspec)
  98. }
  99. }
  100. portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"})
  101. if err != nil {
  102. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  103. }
  104. if _, ok := portMap[Port("1234/tcp")]; !ok {
  105. t.Fatal("1234/tcp was not parsed properly")
  106. }
  107. if _, ok := portMap[Port("2345/udp")]; !ok {
  108. t.Fatal("2345/udp was not parsed properly")
  109. }
  110. for portspec, bindings := range bindingMap {
  111. _, port := SplitProtoPort(string(portspec))
  112. if len(bindings) != 1 {
  113. t.Fatalf("%s should have exactly one binding", portspec)
  114. }
  115. if bindings[0].HostIp != "" {
  116. t.Fatalf("HostIp should not be set for %s", portspec)
  117. }
  118. if bindings[0].HostPort != port {
  119. t.Fatalf("HostPort should be %s for %s", port, portspec)
  120. }
  121. }
  122. portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"})
  123. if err != nil {
  124. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  125. }
  126. if _, ok := portMap[Port("1234/tcp")]; !ok {
  127. t.Fatal("1234/tcp was not parsed properly")
  128. }
  129. if _, ok := portMap[Port("2345/udp")]; !ok {
  130. t.Fatal("2345/udp was not parsed properly")
  131. }
  132. for portspec, bindings := range bindingMap {
  133. _, port := SplitProtoPort(string(portspec))
  134. if len(bindings) != 1 {
  135. t.Fatalf("%s should have exactly one binding", portspec)
  136. }
  137. if bindings[0].HostIp != "0.0.0.0" {
  138. t.Fatalf("HostIp is not 0.0.0.0 for %s", portspec)
  139. }
  140. if bindings[0].HostPort != port {
  141. t.Fatalf("HostPort should be %s for %s", port, portspec)
  142. }
  143. }
  144. _, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"})
  145. if err == nil {
  146. t.Fatal("Received no error while trying to parse a hostname instead of ip")
  147. }
  148. }
  149. func TestParsePortSpecsWithRange(t *testing.T) {
  150. var (
  151. portMap map[Port]struct{}
  152. bindingMap map[Port][]PortBinding
  153. err error
  154. )
  155. portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp"})
  156. if err != nil {
  157. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  158. }
  159. if _, ok := portMap[Port("1235/tcp")]; !ok {
  160. t.Fatal("1234/tcp was not parsed properly")
  161. }
  162. if _, ok := portMap[Port("2346/udp")]; !ok {
  163. t.Fatal("2345/udp was not parsed properly")
  164. }
  165. for portspec, bindings := range bindingMap {
  166. if len(bindings) != 1 {
  167. t.Fatalf("%s should have exactly one binding", portspec)
  168. }
  169. if bindings[0].HostIp != "" {
  170. t.Fatalf("HostIp should not be set for %s", portspec)
  171. }
  172. if bindings[0].HostPort != "" {
  173. t.Fatalf("HostPort should not be set for %s", portspec)
  174. }
  175. }
  176. portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp"})
  177. if err != nil {
  178. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  179. }
  180. if _, ok := portMap[Port("1235/tcp")]; !ok {
  181. t.Fatal("1234/tcp was not parsed properly")
  182. }
  183. if _, ok := portMap[Port("2346/udp")]; !ok {
  184. t.Fatal("2345/udp was not parsed properly")
  185. }
  186. for portspec, bindings := range bindingMap {
  187. _, port := SplitProtoPort(string(portspec))
  188. if len(bindings) != 1 {
  189. t.Fatalf("%s should have exactly one binding", portspec)
  190. }
  191. if bindings[0].HostIp != "" {
  192. t.Fatalf("HostIp should not be set for %s", portspec)
  193. }
  194. if bindings[0].HostPort != port {
  195. t.Fatalf("HostPort should be %s for %s", port, portspec)
  196. }
  197. }
  198. portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp"})
  199. if err != nil {
  200. t.Fatalf("Error while processing ParsePortSpecs: %s", err)
  201. }
  202. if _, ok := portMap[Port("1235/tcp")]; !ok {
  203. t.Fatal("1234/tcp was not parsed properly")
  204. }
  205. if _, ok := portMap[Port("2346/udp")]; !ok {
  206. t.Fatal("2345/udp was not parsed properly")
  207. }
  208. for portspec, bindings := range bindingMap {
  209. _, port := SplitProtoPort(string(portspec))
  210. if len(bindings) != 1 || bindings[0].HostIp != "0.0.0.0" || bindings[0].HostPort != port {
  211. t.Fatalf("Expect single binding to port %s but found %s", port, bindings)
  212. }
  213. }
  214. _, _, err = ParsePortSpecs([]string{"localhost:1234-1236:1234-1236/tcp"})
  215. if err == nil {
  216. t.Fatal("Received no error while trying to parse a hostname instead of ip")
  217. }
  218. }