File: sockets_test.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (59 lines) | stat: -rw-r--r-- 1,265 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package utils

import (
	"fmt"
	"runtime"
	"testing"
)

func TestParseSocketAddress(t *testing.T) {
	en := "unix"
	ea := "/tmp/test"
	var eerr error = nil

	test := func(spec string) {
		n, a, err := ParseSocketAddress(spec)
		if err != eerr {
			if eerr == nil {
				t.Fatalf("Parsing of %s failed with unexpected error: %s", spec, err)
			}
			if err == nil {
				t.Fatalf("Parsing of %s did not fail, unexpectedly", spec)
			}
			return
		}
		if a != ea {
			t.Fatalf("actual != expected, %s != %s, when parsing %s", a, ea, spec)
		}
		if n != en {
			t.Fatalf("actual != expected, %s != %s, when parsing %s", n, en, spec)
		}
	}

	testf := func(spec string, netw string, addr string) {
		eerr = nil
		en = netw
		ea = addr
		test(spec)
	}
	teste := func(spec string, e string) {
		eerr = fmt.Errorf("%s", e)
		test(spec)
	}

	test("unix:/tmp/test")
	if runtime.GOOS == "linux" {
		ea = "@test"
	} else {
		eerr = fmt.Errorf("bad kitty")
	}
	test("unix:@test")
	testf("tcp:localhost:123", "tcp", "localhost:123")
	testf("tcp:1.1.1.1:123", "ip", "1.1.1.1:123")
	testf("tcp:fe80::1", "ip", "fe80::1")
	teste("xxx", "bad kitty")
	teste("xxx:yyy", "bad kitty")
	teste(":yyy", "bad kitty")
}