ipaddressport_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package flags
  15. import (
  16. "testing"
  17. )
  18. func TestIPAddressPortSet(t *testing.T) {
  19. pass := []string{
  20. "1.2.3.4:8080",
  21. "10.1.1.1:80",
  22. "[2001:db8::1]:8080",
  23. }
  24. fail := []string{
  25. // bad IP specification
  26. ":4001",
  27. "127.0:8080",
  28. "123:456",
  29. // bad port specification
  30. "127.0.0.1:foo",
  31. "127.0.0.1:",
  32. // unix sockets not supported
  33. "unix://",
  34. "unix://tmp/etcd.sock",
  35. // bad strings
  36. "somewhere",
  37. "234#$",
  38. "file://foo/bar",
  39. "http://hello",
  40. "2001:db8::1",
  41. "2001:db8::1:1",
  42. }
  43. for i, tt := range pass {
  44. f := &IPAddressPort{}
  45. if err := f.Set(tt); err != nil {
  46. t.Errorf("#%d: unexpected error from IPAddressPort.Set(%q): %v", i, tt, err)
  47. }
  48. }
  49. for i, tt := range fail {
  50. f := &IPAddressPort{}
  51. if err := f.Set(tt); err == nil {
  52. t.Errorf("#%d: expected error from IPAddressPort.Set(%q)", i, tt)
  53. }
  54. }
  55. }
  56. func TestIPAddressPortString(t *testing.T) {
  57. addresses := []string{
  58. "[2001:db8::1:1234]:4001",
  59. "127.0.0.1:4001",
  60. }
  61. for i, tt := range addresses {
  62. f := &IPAddressPort{}
  63. if err := f.Set(tt); err != nil {
  64. t.Errorf("#%d: unexpected error: %v", i, err)
  65. }
  66. want := tt
  67. got := f.String()
  68. if want != got {
  69. t.Errorf("#%d: IPAddressPort.String() value should be %q, got %q", i, want, got)
  70. }
  71. }
  72. }