|
| 1 | +package main |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestIsIPFull(t *testing.T) { |
| 6 | + |
| 7 | + assertNotateSplit("192.168.33.1", newNt("192.168.33.1"), t) |
| 8 | + assertNotateSplit("192.168.33.1/30", newNt("192.168.33.1", "/", "30"), t) |
| 9 | + assertNotateSplit("255.255.0.0", newNt("255.255.0.0"), t) |
| 10 | + assertNotateSplit("255.255.0.0/16", newNt("255.255.0.0", "/", "16"), t) |
| 11 | + assertNotateSplit("255.0", newNt("255.0"), t) |
| 12 | + assertNotateSplit("255.0/8", newNt("255.0", "/", "8"), t) |
| 13 | + assertNotateSplit("1", newNt("1"), t) |
| 14 | + assertNotateSplit("1/30", newNt("1", "/", "30"), t) |
| 15 | + |
| 16 | + assertNotateSplit("192.168.33.1", newNt("192.168.33.1"), t) |
| 17 | + assertNotateSplit("192.168.33.1+30", newNt("192.168.33.1", "+", "30"), t) |
| 18 | + assertNotateSplit("255.255.0.0", newNt("255.255.0.0"), t) |
| 19 | + assertNotateSplit("255.255.0.0+16", newNt("255.255.0.0", "+", "16"), t) |
| 20 | + assertNotateSplit("255.0", newNt("255.0"), t) |
| 21 | + assertNotateSplit("255.0+8", newNt("255.0", "+", "8"), t) |
| 22 | + assertNotateSplit("1", newNt("1"), t) |
| 23 | + assertNotateSplit("1+30", newNt("1", "+", "30"), t) |
| 24 | + |
| 25 | + assertNotateSplit("192.168.33.1", newNt("192.168.33.1"), t) |
| 26 | + assertNotateSplit("192.168.33.1-30", newNt("192.168.33.1", "-", "30"), t) |
| 27 | + assertNotateSplit("255.255.0.0", newNt("255.255.0.0"), t) |
| 28 | + assertNotateSplit("255.255.0.0-16", newNt("255.255.0.0", "-", "16"), t) |
| 29 | + assertNotateSplit("255.0", newNt("255.0"), t) |
| 30 | + assertNotateSplit("255.0-8", newNt("255.0", "-", "8"), t) |
| 31 | + assertNotateSplit("1", newNt("1"), t) |
| 32 | + assertNotateSplit("1-30", newNt("1", "-", "30"), t) |
| 33 | + |
| 34 | + assertIsCIDRNotation("192.168.33.1", false, t) |
| 35 | + assertIsCIDRNotation("192.168.33.1/30", true, t) |
| 36 | + assertIsCIDRNotation("255.255.0.0", false, t) |
| 37 | + assertIsCIDRNotation("255.255.0.0/16", true, t) |
| 38 | + assertIsCIDRNotation("255.0", false, t) |
| 39 | + assertIsCIDRNotation("255.0/8", true, t) |
| 40 | + assertIsCIDRNotation("1", false, t) |
| 41 | + assertIsCIDRNotation("1/30", true, t) |
| 42 | + |
| 43 | + assertIsIPv4Full("192.168.33.1", true, t) |
| 44 | + assertIsIPv4Full("255.255.0.0", true, t) |
| 45 | + assertIsIPv4Full("255.255.0", false, t) |
| 46 | + assertIsIPv4Full("255.0", false, t) |
| 47 | + assertIsIPv4Full("1", false, t) |
| 48 | + |
| 49 | + assertIsIPv4Fragment("192.168.33.1", true, t) |
| 50 | + assertIsIPv4Fragment("255.255.0.0", true, t) |
| 51 | + assertIsIPv4Fragment("255.255.0", true, t) |
| 52 | + assertIsIPv4Fragment("255.0", true, t) |
| 53 | + assertIsIPv4Fragment("1", false, t) |
| 54 | + |
| 55 | + assertIsInt("192.168.33.1", false, t) |
| 56 | + assertIsInt("255.255.0.0", false, t) |
| 57 | + assertIsInt("255.255.0", false, t) |
| 58 | + assertIsInt("255.0", false, t) |
| 59 | + assertIsInt("1", true, t) |
| 60 | +} |
| 61 | + |
| 62 | +func assertNotateSplit(inp string, exp tNotation, t *testing.T) { |
| 63 | + res := notateSplit(inp) |
| 64 | + if res.Pre != exp.Pre || res.Op != exp.Op || res.Suf != exp.Suf { |
| 65 | + t.Errorf( |
| 66 | + "notateSplit failed: %s -> %v != %v", |
| 67 | + inp, exp, res, |
| 68 | + ) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func newNt(params ...any) (nt tNotation) { |
| 73 | + if len(params) > 0 { |
| 74 | + nt.Pre = params[0].(string) |
| 75 | + } |
| 76 | + if len(params) > 1 { |
| 77 | + nt.Op = params[1].(string) |
| 78 | + } |
| 79 | + if len(params) > 2 { |
| 80 | + nt.Suf = params[2].(string) |
| 81 | + } |
| 82 | + return nt |
| 83 | +} |
0 commit comments