File: iputils_test.go

package info (click to toggle)
rootlesskit 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: sh: 433; makefile: 25
file content (51 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (3)
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
package iputils

import (
	"net"
	"testing"
)

func TestAddIPInt(t *testing.T) {
	type testCase struct {
		s        string
		i        int
		expected string
	}
	testCases := []testCase{
		{
			"10.0.2.0",
			100,
			"10.0.2.100",
		},
		{
			"255.255.255.100",
			155,
			"255.255.255.255",
		},
		{
			"255.255.255.100",
			156,
			"",
		},
	}
	for i, tc := range testCases {
		ip := net.ParseIP(tc.s)
		if ip == nil {
			t.Fatalf("invalid IP: %q", tc.s)
		}
		gotIP, err := AddIPInt(ip, tc.i)
		if tc.expected == "" {
			if err == nil {
				t.Fatalf("#%d: expected error, got no error", i)
			}
		} else {
			if err != nil {
				t.Fatalf("#%d: expected no error, got %q", i, err)
			}
			got := gotIP.String()
			if got != tc.expected {
				t.Fatalf("#%d: expected %q, got %q", i, tc.expected, got)
			}
		}
	}
}