|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cloudstack |
| 21 | + |
| 22 | +import ( |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 26 | +) |
| 27 | + |
| 28 | +func TestIsAllPortsTCPUDP(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + protocol string |
| 31 | + start int |
| 32 | + end int |
| 33 | + expected bool |
| 34 | + name string |
| 35 | + }{ |
| 36 | + {"tcp", 0, 0, true, "TCP with 0/0"}, |
| 37 | + {"TCP", 0, 0, true, "TCP uppercase with 0/0"}, |
| 38 | + {"udp", -1, -1, true, "UDP with -1/-1"}, |
| 39 | + {"UDP", -1, -1, true, "UDP uppercase with -1/-1"}, |
| 40 | + {"tcp", 1, 65535, true, "TCP with 1/65535"}, |
| 41 | + {"udp", 1, 65535, true, "UDP with 1/65535"}, |
| 42 | + {"tcp", 80, 80, false, "TCP with specific port"}, |
| 43 | + {"udp", 53, 53, false, "UDP with specific port"}, |
| 44 | + {"icmp", 0, 0, false, "ICMP protocol"}, |
| 45 | + {"all", 0, 0, false, "ALL protocol"}, |
| 46 | + {"tcp", 1, 1000, false, "TCP with port range"}, |
| 47 | + {"tcp", 0, 1, false, "TCP with 0/1"}, |
| 48 | + {"tcp", -1, 0, false, "TCP with -1/0"}, |
| 49 | + } |
| 50 | + |
| 51 | + for _, test := range tests { |
| 52 | + t.Run(test.name, func(t *testing.T) { |
| 53 | + result := isAllPortsTCPUDP(test.protocol, test.start, test.end) |
| 54 | + if result != test.expected { |
| 55 | + t.Errorf("isAllPortsTCPUDP(%q, %d, %d) = %v, expected %v", |
| 56 | + test.protocol, test.start, test.end, result, test.expected) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestNormalizeRemoteCIDRs(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + input string |
| 65 | + expected []string |
| 66 | + name string |
| 67 | + }{ |
| 68 | + {"", []string{}, "empty string"}, |
| 69 | + {"10.0.0.0/8", []string{"10.0.0.0/8"}, "single CIDR"}, |
| 70 | + {"10.0.0.0/8,192.168.1.0/24", []string{"10.0.0.0/8", "192.168.1.0/24"}, "two CIDRs"}, |
| 71 | + {"10.0.0.0/8, 192.168.1.0/24", []string{"10.0.0.0/8", "192.168.1.0/24"}, "two CIDRs with space"}, |
| 72 | + {" 10.0.0.0/8 , 192.168.1.0/24 ", []string{"10.0.0.0/8", "192.168.1.0/24"}, "CIDRs with extra spaces"}, |
| 73 | + {"192.168.1.0/24,10.0.0.0/8", []string{"10.0.0.0/8", "192.168.1.0/24"}, "unsorted CIDRs (should be sorted)"}, |
| 74 | + {"10.0.0.0/8,,192.168.1.0/24", []string{"10.0.0.0/8", "192.168.1.0/24"}, "empty CIDR in middle"}, |
| 75 | + {" , , ", []string{}, "only commas and spaces"}, |
| 76 | + } |
| 77 | + |
| 78 | + for _, test := range tests { |
| 79 | + t.Run(test.name, func(t *testing.T) { |
| 80 | + result := normalizeRemoteCIDRs(test.input) |
| 81 | + if len(result) != len(test.expected) { |
| 82 | + t.Errorf("normalizeRemoteCIDRs(%q) length = %d, expected %d", |
| 83 | + test.input, len(result), len(test.expected)) |
| 84 | + return |
| 85 | + } |
| 86 | + for i, v := range result { |
| 87 | + if v != test.expected[i] { |
| 88 | + t.Errorf("normalizeRemoteCIDRs(%q)[%d] = %q, expected %q", |
| 89 | + test.input, i, v, test.expected[i]) |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestNormalizeLocalCIDRs(t *testing.T) { |
| 97 | + tests := []struct { |
| 98 | + input *schema.Set |
| 99 | + expected []string |
| 100 | + name string |
| 101 | + }{ |
| 102 | + {nil, []string{}, "nil set"}, |
| 103 | + {schema.NewSet(schema.HashString, []interface{}{}), []string{}, "empty set"}, |
| 104 | + {schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8"}), []string{"10.0.0.0/8"}, "single CIDR"}, |
| 105 | + {schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8", "192.168.1.0/24"}), []string{"10.0.0.0/8", "192.168.1.0/24"}, "two CIDRs"}, |
| 106 | + {schema.NewSet(schema.HashString, []interface{}{"192.168.1.0/24", "10.0.0.0/8"}), []string{"10.0.0.0/8", "192.168.1.0/24"}, "unsorted CIDRs"}, |
| 107 | + {schema.NewSet(schema.HashString, []interface{}{" 10.0.0.0/8 ", " 192.168.1.0/24 "}), []string{"10.0.0.0/8", "192.168.1.0/24"}, "CIDRs with spaces"}, |
| 108 | + {schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8", "", "192.168.1.0/24"}), []string{"10.0.0.0/8", "192.168.1.0/24"}, "with empty string"}, |
| 109 | + } |
| 110 | + |
| 111 | + for _, test := range tests { |
| 112 | + t.Run(test.name, func(t *testing.T) { |
| 113 | + result := normalizeLocalCIDRs(test.input) |
| 114 | + if len(result) != len(test.expected) { |
| 115 | + t.Errorf("normalizeLocalCIDRs() length = %d, expected %d", |
| 116 | + len(result), len(test.expected)) |
| 117 | + return |
| 118 | + } |
| 119 | + for i, v := range result { |
| 120 | + if v != test.expected[i] { |
| 121 | + t.Errorf("normalizeLocalCIDRs()[%d] = %q, expected %q", |
| 122 | + i, v, test.expected[i]) |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestCidrSetsEqual(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + remote string |
| 132 | + local *schema.Set |
| 133 | + expected bool |
| 134 | + name string |
| 135 | + }{ |
| 136 | + {"", schema.NewSet(schema.HashString, []interface{}{}), true, "both empty"}, |
| 137 | + {"10.0.0.0/8", schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8"}), true, "single matching CIDR"}, |
| 138 | + {"10.0.0.0/8,192.168.1.0/24", schema.NewSet(schema.HashString, []interface{}{"192.168.1.0/24", "10.0.0.0/8"}), true, "multiple CIDRs different order"}, |
| 139 | + {"10.0.0.0/8, 192.168.1.0/24", schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8", "192.168.1.0/24"}), true, "remote with spaces"}, |
| 140 | + {"10.0.0.0/8", schema.NewSet(schema.HashString, []interface{}{"192.168.1.0/24"}), false, "different CIDRs"}, |
| 141 | + {"10.0.0.0/8,192.168.1.0/24", schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8"}), false, "different count"}, |
| 142 | + {"", schema.NewSet(schema.HashString, []interface{}{"10.0.0.0/8"}), false, "remote empty, local not"}, |
| 143 | + {"10.0.0.0/8", schema.NewSet(schema.HashString, []interface{}{}), false, "local empty, remote not"}, |
| 144 | + } |
| 145 | + |
| 146 | + for _, test := range tests { |
| 147 | + t.Run(test.name, func(t *testing.T) { |
| 148 | + result := cidrSetsEqual(test.remote, test.local) |
| 149 | + if result != test.expected { |
| 150 | + t.Errorf("cidrSetsEqual(%q, %v) = %v, expected %v", |
| 151 | + test.remote, test.local.List(), result, test.expected) |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments