File: config_test.go

package info (click to toggle)
golang-github-coredhcp-coredhcp 0.0.0%2Bgit.20250806.f7e98e4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 460 kB
  • sloc: makefile: 8; sh: 6
file content (52 lines) | stat: -rw-r--r-- 2,319 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
// Copyright 2018-present the CoreDHCP Authors. All rights reserved
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

package config

import "testing"

func TestSplitHostPort(t *testing.T) {
	testcases := []struct {
		hostport string
		ip       string
		zone     string
		port     string
		err      bool // Should return an error (ie true for err != nil)
	}{
		{"0.0.0.0:67", "0.0.0.0", "", "67", false},
		{"192.0.2.0", "192.0.2.0", "", "", false},
		{"192.0.2.9%eth0", "192.0.2.9", "eth0", "", false},
		{"0.0.0.0%eth0:67", "0.0.0.0", "eth0", "67", false},
		{"0.0.0.0:20%eth0:67", "0.0.0.0", "eth0", "67", true},
		{"2001:db8::1:547", "", "", "547", true}, // [] mandatory for v6
		{"[::]:547", "::", "", "547", false},
		{"[fe80::1%eth0]", "fe80::1", "eth0", "", false},
		{"[fe80::1]:eth1", "fe80::1", "", "eth1", false},             // no validation of ports in this function
		{"fe80::1%eth0:547", "fe80::1", "eth0", "547", true},         // [] mandatory for v6 even with %zone
		{"fe80::1%eth0", "fe80::1", "eth0", "547", true},             // [] mandatory for v6 even without port
		{"[2001:db8::2]47", "fe80::1", "eth0", "547", true},          // garbage after []
		{"[ff02::1:2]%srv_u:547", "ff02::1:2", "srv_u", "547", true}, // FIXME: Linux `ss` format, we should accept but net.SplitHostPort doesn't
		{":http", "", "", "http", false},
		{"%eth0:80", "", "eth0", "80", false},          // janky, but looks valid enough for "[::%eth0]:80" imo
		{"%eth0", "", "eth0", "", false},               // janky
		{"fe80::1]:80", "fe80::1", "", "80", true},     // unbalanced ]
		{"fe80::1%eth0]", "fe80::1", "eth0", "", true}, // unbalanced ], no port
		{"", "", "", "", false},                        // trivial case, still valid
	}

	for _, tc := range testcases {
		ip, zone, port, err := splitHostPort(tc.hostport)
		if tc.err != (err != nil) {
			errState := "not "
			if tc.err {
				errState = ""
			}
			t.Errorf("Mismatched error state: %s should %serror (got err: %v)\n", tc.hostport, errState, err)
			continue
		}
		if err == nil && (ip != tc.ip || zone != tc.zone || port != tc.port) {
			t.Errorf("%s => \"%s\", \"%s\", \"%s\" expected \"%s\",\"%s\",\"%s\"\n", tc.hostport, ip, zone, port, tc.ip, tc.zone, tc.port)
		}
	}
}