github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/utils/portcache/portcache_test.go (about) 1 // +build !windows 2 3 package portcache 4 5 import ( 6 "testing" 7 8 . "github.com/smartystreets/goconvey/convey" 9 "go.aporeto.io/enforcerd/trireme-lib/utils/portspec" 10 ) 11 12 func TestNewPortCache(t *testing.T) { 13 Convey("When I creat a new port cache", t, func() { 14 p := NewPortCache("test") 15 Convey("The cache must be initilized", func() { 16 So(p, ShouldNotBeNil) 17 So(p.ports, ShouldNotBeNil) 18 So(p.ranges, ShouldNotBeNil) 19 }) 20 }) 21 } 22 23 func TestAddPortSpec(t *testing.T) { 24 Convey("Given an initialized cached", t, func() { 25 p := NewPortCache("test") 26 Convey("When I add a port spec with a single port, it must added to the map", func() { 27 s, err := portspec.NewPortSpec(10, 10, "10") 28 So(err, ShouldBeNil) 29 p.AddPortSpec(s) 30 stored, err := p.ports.Get(uint16(10)) 31 So(err, ShouldBeNil) 32 So(stored.(*portspec.PortSpec), ShouldNotBeNil) 33 So(stored.(*portspec.PortSpec).Max, ShouldEqual, 10) 34 So(stored.(*portspec.PortSpec).Min, ShouldEqual, 10) 35 So(stored.(*portspec.PortSpec).Value().(string), ShouldResemble, "10") 36 }) 37 38 Convey("When I add a port spec with a range of ports, be added to the list", func() { 39 s, err := portspec.NewPortSpec(10, 20, "range") 40 So(err, ShouldBeNil) 41 p.AddPortSpec(s) 42 So(len(p.ranges), ShouldEqual, 1) 43 So(p.ranges[0].Min, ShouldEqual, 10) 44 So(p.ranges[0].Max, ShouldEqual, 20) 45 So(p.ranges[0].Value().(string), ShouldResemble, "range") 46 }) 47 }) 48 } 49 50 func TestSearch(t *testing.T) { 51 Convey("Given an initialized cache", t, func() { 52 p := NewPortCache("test") 53 Convey("When I add both single ports and rages", func() { 54 s, err := portspec.NewPortSpec(10, 20, "range1") 55 So(err, ShouldBeNil) 56 p.AddPortSpec(s) 57 58 s, err = portspec.NewPortSpec(30, 40, "range2") 59 So(err, ShouldBeNil) 60 p.AddPortSpec(s) 61 62 s, err = portspec.NewPortSpec(50, 60, "range3") 63 So(err, ShouldBeNil) 64 p.AddPortSpec(s) 65 66 s, err = portspec.NewPortSpec(15, 15, "15") 67 So(err, ShouldBeNil) 68 p.AddPortSpec(s) 69 70 s, err = portspec.NewPortSpec(25, 25, "25") 71 So(err, ShouldBeNil) 72 p.AddPortSpec(s) 73 74 Convey("If I match both exact and range, I should get the exact", func() { 75 s, err := p.GetSpecValueFromPort(15) 76 So(err, ShouldBeNil) 77 So(s.(string), ShouldResemble, "15") 78 s, err = p.GetSpecValueFromPort(25) 79 So(err, ShouldBeNil) 80 So(s.(string), ShouldResemble, "25") 81 }) 82 83 Convey("If I match the range beginning, I should get the result", func() { 84 s, err := p.GetSpecValueFromPort(10) 85 So(err, ShouldBeNil) 86 So(s.(string), ShouldResemble, "range1") 87 }) 88 89 Convey("If I match the range end , I should get the result", func() { 90 s, err := p.GetSpecValueFromPort(19) 91 So(err, ShouldBeNil) 92 So(s.(string), ShouldResemble, "range1") 93 s, err = p.GetSpecValueFromPort(39) 94 So(err, ShouldBeNil) 95 So(s.(string), ShouldResemble, "range2") 96 s, err = p.GetSpecValueFromPort(59) 97 So(err, ShouldBeNil) 98 So(s.(string), ShouldResemble, "range3") 99 }) 100 101 Convey("Last number is included ", func() { 102 _, err := p.GetSpecValueFromPort(20) 103 So(err, ShouldBeNil) 104 }) 105 }) 106 }) 107 } 108 109 func TestGetAll(t *testing.T) { 110 Convey("Given an initialized cache", t, func() { 111 p := NewPortCache("test") 112 Convey("When I add both single ports and rages", func() { 113 s, err := portspec.NewPortSpec(10, 20, "range1") 114 So(err, ShouldBeNil) 115 p.AddPortSpec(s) 116 117 s, err = portspec.NewPortSpec(30, 40, "range2") 118 So(err, ShouldBeNil) 119 p.AddPortSpec(s) 120 121 s, err = portspec.NewPortSpec(50, 60, "range3") 122 So(err, ShouldBeNil) 123 p.AddPortSpec(s) 124 125 s, err = portspec.NewPortSpec(15, 15, "15") 126 So(err, ShouldBeNil) 127 p.AddPortSpec(s) 128 129 s, err = portspec.NewPortSpec(25, 25, "25") 130 So(err, ShouldBeNil) 131 p.AddPortSpec(s) 132 133 Convey("If I match both exact and range, I should get the exact", func() { 134 s, err := p.GetAllSpecValueFromPort(15) 135 So(err, ShouldBeNil) 136 So(len(s), ShouldEqual, 2) 137 138 So(s[0].(string), ShouldResemble, "15") 139 So(s[1].(string), ShouldResemble, "range1") 140 }) 141 }) 142 }) 143 } 144 145 func TestAddUnique(t *testing.T) { 146 Convey("Given an initialized cache", t, func() { 147 p := NewPortCache("test") 148 Convey("When I add unique entries, I should get no errors ", func() { 149 s, err := portspec.NewPortSpec(10, 20, "range1") 150 So(err, ShouldBeNil) 151 So(p.AddUnique(s), ShouldBeNil) 152 153 s, err = portspec.NewPortSpec(30, 40, "range2") 154 So(err, ShouldBeNil) 155 So(p.AddUnique(s), ShouldBeNil) 156 157 s, err = portspec.NewPortSpec(50, 60, "range3") 158 So(err, ShouldBeNil) 159 So(p.AddUnique(s), ShouldBeNil) 160 }) 161 162 Convey("When I add non-unique entries, I should get errors ", func() { 163 s, err := portspec.NewPortSpec(10, 20, "range1") 164 So(err, ShouldBeNil) 165 So(p.AddUnique(s), ShouldBeNil) 166 167 s, err = portspec.NewPortSpec(30, 40, "range1") 168 So(err, ShouldBeNil) 169 So(p.AddUnique(s), ShouldBeNil) 170 171 s, err = portspec.NewPortSpec(5, 15, "range2") 172 So(err, ShouldBeNil) 173 So(p.AddUnique(s), ShouldNotBeNil) 174 175 s, err = portspec.NewPortSpec(15, 25, "range2") 176 So(err, ShouldBeNil) 177 So(p.AddUnique(s), ShouldNotBeNil) 178 179 s, err = portspec.NewPortSpec(5, 25, "range3") 180 So(err, ShouldBeNil) 181 So(p.AddUnique(s), ShouldNotBeNil) 182 183 s, err = portspec.NewPortSpec(5, 5, "range3") 184 So(err, ShouldBeNil) 185 So(p.AddUnique(s), ShouldBeNil) 186 187 s, err = portspec.NewPortSpec(15, 15, "range3") 188 So(err, ShouldBeNil) 189 So(p.AddUnique(s), ShouldNotBeNil) 190 191 s, err = portspec.NewPortSpec(25, 25, "range3") 192 So(err, ShouldBeNil) 193 So(p.AddUnique(s), ShouldBeNil) 194 }) 195 196 Convey("When I match error'd unique entries and a valid range, I should get the valid range only", func() { 197 s, err := portspec.NewPortSpec(10, 20, "range1") 198 So(err, ShouldBeNil) 199 So(p.AddUnique(s), ShouldBeNil) 200 201 s, err = portspec.NewPortSpec(5, 15, "range2") 202 So(err, ShouldBeNil) 203 So(p.AddUnique(s), ShouldNotBeNil) 204 205 s, err = portspec.NewPortSpec(15, 15, "15") 206 So(err, ShouldBeNil) 207 So(p.AddUnique(s), ShouldNotBeNil) 208 209 a, err := p.GetAllSpecValueFromPort(15) 210 So(err, ShouldBeNil) 211 So(len(a), ShouldEqual, 1) 212 So(a[0].(string), ShouldResemble, "range1") 213 }) 214 }) 215 } 216 217 func TestRemoveStringPort(t *testing.T) { 218 Convey("Given an initialized cache", t, func() { 219 p := NewPortCache("test") 220 221 s, err := portspec.NewPortSpec(10, 20, "range1") 222 So(err, ShouldBeNil) 223 So(p.AddUnique(s), ShouldBeNil) 224 225 s, err = portspec.NewPortSpec(30, 40, "range2") 226 So(err, ShouldBeNil) 227 So(p.AddUnique(s), ShouldBeNil) 228 229 s, err = portspec.NewPortSpec(100, 100, "range3") 230 So(err, ShouldBeNil) 231 So(p.AddUnique(s), ShouldBeNil) 232 233 Convey("When I remove valid entries, there should be no errors", func() { 234 So(p.RemoveStringPorts("10:20"), ShouldBeNil) 235 So(p.RemoveStringPorts("30:40"), ShouldBeNil) 236 So(p.RemoveStringPorts("100:100"), ShouldBeNil) 237 }) 238 239 Convey("When I remove invalid entries, I should get an error", func() { 240 So(p.RemoveStringPorts("100:200"), ShouldNotBeNil) 241 So(p.RemoveStringPorts("10:40"), ShouldNotBeNil) 242 }) 243 }) 244 }