ipaddressport_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  23. fail := []string{
  24. // bad IP specification
  25. ":4001",
  26. "127.0:8080",
  27. "123:456",
  28. // bad port specification
  29. "127.0.0.1:foo",
  30. "127.0.0.1:",
  31. // unix sockets not supported
  32. "unix://",
  33. "unix://tmp/etcd.sock",
  34. // bad strings
  35. "somewhere",
  36. "234#$",
  37. "file://foo/bar",
  38. "http://hello",
  39. }
  40. for i, tt := range pass {
  41. f := &IPAddressPort{}
  42. if err := f.Set(tt); err != nil {
  43. t.Errorf("#%d: unexpected error from IPAddressPort.Set(%q): %v", i, tt, err)
  44. }
  45. }
  46. for i, tt := range fail {
  47. f := &IPAddressPort{}
  48. if err := f.Set(tt); err == nil {
  49. t.Errorf("#%d: expected error from IPAddressPort.Set(%q)", i, tt)
  50. }
  51. }
  52. }
  53. func TestIPAddressPortString(t *testing.T) {
  54. f := &IPAddressPort{}
  55. if err := f.Set("127.0.0.1:4001"); err != nil {
  56. t.Fatalf("unexpected error: %v", err)
  57. }
  58. want := "127.0.0.1:4001"
  59. got := f.String()
  60. if want != got {
  61. t.Fatalf("IPAddressPort.String() value should be %q, got %q", want, got)
  62. }
  63. }