port_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2016 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. package net
  5. import "testing"
  6. var parsePortTests = []struct {
  7. service string
  8. port int
  9. needsLookup bool
  10. }{
  11. {"", 0, false},
  12. // Decimal number literals
  13. {"-1073741825", -1 << 30, false},
  14. {"-1073741824", -1 << 30, false},
  15. {"-1073741823", -(1<<30 - 1), false},
  16. {"-123456789", -123456789, false},
  17. {"-1", -1, false},
  18. {"-0", 0, false},
  19. {"0", 0, false},
  20. {"+0", 0, false},
  21. {"+1", 1, false},
  22. {"65535", 65535, false},
  23. {"65536", 65536, false},
  24. {"123456789", 123456789, false},
  25. {"1073741822", 1<<30 - 2, false},
  26. {"1073741823", 1<<30 - 1, false},
  27. {"1073741824", 1<<30 - 1, false},
  28. {"1073741825", 1<<30 - 1, false},
  29. // Others
  30. {"abc", 0, true},
  31. {"9pfs", 0, true},
  32. {"123badport", 0, true},
  33. {"bad123port", 0, true},
  34. {"badport123", 0, true},
  35. {"123456789badport", 0, true},
  36. {"-2147483649badport", 0, true},
  37. {"2147483649badport", 0, true},
  38. }
  39. func TestParsePort(t *testing.T) {
  40. // The following test cases are cribbed from the strconv
  41. for _, tt := range parsePortTests {
  42. if port, needsLookup := parsePort(tt.service); port != tt.port || needsLookup != tt.needsLookup {
  43. t.Errorf("parsePort(%q) = %d, %t; want %d, %t", tt.service, port, needsLookup, tt.port, tt.needsLookup)
  44. }
  45. }
  46. }