1
16
17 package net
18
19 import (
20 "testing"
21
22 flag "github.com/spf13/pflag"
23 )
24
25 func TestPortRange(t *testing.T) {
26 testCases := []struct {
27 input string
28 success bool
29 expected string
30 included int
31 excluded int
32 }{
33 {"100-200", true, "100-200", 200, 201},
34 {" 100-200 ", true, "100-200", 200, 201},
35 {"0-0", true, "0-0", 0, 1},
36 {"", true, "", -1, 0},
37 {"100", true, "100-100", 100, 101},
38 {"100 - 200", false, "", -1, -1},
39 {"-100", false, "", -1, -1},
40 {"100-", false, "", -1, -1},
41 {"200-100", false, "", -1, -1},
42 {"60000-70000", false, "", -1, -1},
43 {"70000-80000", false, "", -1, -1},
44 {"70000+80000", false, "", -1, -1},
45 {"1+0", true, "1-1", 1, 2},
46 {"0+0", true, "0-0", 0, 1},
47 {"1+-1", false, "", -1, -1},
48 {"1-+1", false, "", -1, -1},
49 {"100+200", true, "100-300", 300, 301},
50 {"1+65535", false, "", -1, -1},
51 {"0+65535", true, "0-65535", 65535, 65536},
52 }
53
54 for i := range testCases {
55 tc := &testCases[i]
56 pr := &PortRange{}
57 var f flag.Value = pr
58 err := f.Set(tc.input)
59 if err != nil && tc.success {
60 t.Errorf("expected success, got %q", err)
61 continue
62 } else if err == nil && !tc.success {
63 t.Errorf("expected failure %#v", testCases[i])
64 continue
65 } else if tc.success {
66 if f.String() != tc.expected {
67 t.Errorf("expected %q, got %q", tc.expected, f.String())
68 }
69 if tc.included >= 0 && !pr.Contains(tc.included) {
70 t.Errorf("expected %q to include %d", f.String(), tc.included)
71 }
72 if tc.excluded >= 0 && pr.Contains(tc.excluded) {
73 t.Errorf("expected %q to exclude %d", f.String(), tc.excluded)
74 }
75 }
76 }
77 }
78
View as plain text