sockets_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "fmt"
  5. "runtime"
  6. "testing"
  7. )
  8. func TestParseSocketAddress(t *testing.T) {
  9. en := "unix"
  10. ea := "/tmp/test"
  11. var eerr error = nil
  12. test := func(spec string) {
  13. n, a, err := ParseSocketAddress(spec)
  14. if err != eerr {
  15. if eerr == nil {
  16. t.Fatalf("Parsing of %s failed with unexpected error: %s", spec, err)
  17. }
  18. if err == nil {
  19. t.Fatalf("Parsing of %s did not fail, unexpectedly", spec)
  20. }
  21. return
  22. }
  23. if a != ea {
  24. t.Fatalf("actual != expected, %s != %s, when parsing %s", a, ea, spec)
  25. }
  26. if n != en {
  27. t.Fatalf("actual != expected, %s != %s, when parsing %s", n, en, spec)
  28. }
  29. }
  30. testf := func(spec string, netw string, addr string) {
  31. eerr = nil
  32. en = netw
  33. ea = addr
  34. test(spec)
  35. }
  36. teste := func(spec string, e string) {
  37. eerr = fmt.Errorf(e)
  38. test(spec)
  39. }
  40. test("unix:/tmp/test")
  41. if runtime.GOOS == "linux" {
  42. ea = "@test"
  43. } else {
  44. eerr = fmt.Errorf("bad kitty")
  45. }
  46. test("unix:@test")
  47. testf("tcp:localhost:123", "tcp", "localhost:123")
  48. testf("tcp:1.1.1.1:123", "ip", "1.1.1.1:123")
  49. testf("tcp:fe80::1", "ip", "fe80::1")
  50. teste("xxx", "bad kitty")
  51. teste("xxx:yyy", "bad kitty")
  52. teste(":yyy", "bad kitty")
  53. }