github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/utils/portspec/portspec_test.go (about) 1 // +build !windows 2 3 package portspec 4 5 import ( 6 "testing" 7 8 . "github.com/smartystreets/goconvey/convey" 9 ) 10 11 func TestNewPortSpec(t *testing.T) { 12 Convey("When I create a new port spec", t, func() { 13 p, err := NewPortSpec(0, 10, "portspec") 14 So(err, ShouldBeNil) 15 Convey("The correct values must be set", func() { 16 So(p, ShouldNotBeNil) 17 So(p.Min, ShouldEqual, 0) 18 So(p.Max, ShouldEqual, 10) 19 So(p.value.(string), ShouldResemble, "portspec") 20 }) 21 }) 22 } 23 func TestNewPortSpecFromString(t *testing.T) { 24 Convey("When I create a valid single port spec from string it should succeed", t, func() { 25 p, err := NewPortSpecFromString("10", "string") 26 So(err, ShouldBeNil) 27 So(p.Min, ShouldEqual, uint16(10)) 28 So(p.Max, ShouldEqual, uint16(10)) 29 So(p.value.(string), ShouldResemble, "string") 30 }) 31 32 Convey("When I create a valid a range port spec from string it should succeed", t, func() { 33 p, err := NewPortSpecFromString("10:20", "string") 34 So(err, ShouldBeNil) 35 So(p.Min, ShouldEqual, uint16(10)) 36 So(p.Max, ShouldEqual, uint16(20)) 37 So(p.value.(string), ShouldResemble, "string") 38 }) 39 40 Convey("When I create singe port with value greater than 2^16 it shoud fail ", t, func() { 41 _, err := NewPortSpecFromString("70000", "string") 42 So(err, ShouldNotBeNil) 43 }) 44 45 Convey("When I create singe port with a negative value it should fail", t, func() { 46 _, err := NewPortSpecFromString("-1", "string") 47 So(err, ShouldNotBeNil) 48 }) 49 50 Convey("When I create a range with min > max it shoud fail", t, func() { 51 _, err := NewPortSpecFromString("20:10", "string") 52 So(err, ShouldNotBeNil) 53 }) 54 55 Convey("When I create a range with negative min or max it shoud fail", t, func() { 56 _, err := NewPortSpecFromString("-20:10", "string") 57 So(err, ShouldNotBeNil) 58 _, err = NewPortSpecFromString("-20:-110", "string") 59 So(err, ShouldNotBeNil) 60 }) 61 62 Convey("When I create a range with invalid characters it should fail", t, func() { 63 _, err := NewPortSpecFromString("10,20", "string") 64 So(err, ShouldNotBeNil) 65 }) 66 } 67 68 func TestIsMultiPort(t *testing.T) { 69 Convey("Given a portspec", t, func() { 70 s, err := NewPortSpecFromString("10:20", "string") 71 So(err, ShouldBeNil) 72 Convey("multiport should return true", func() { 73 So(s.IsMultiPort(), ShouldBeTrue) 74 }) 75 }) 76 } 77 78 func TestRange(t *testing.T) { 79 Convey("Given a portspec", t, func() { 80 Convey("If it is multiport", func() { 81 s, err := NewPortSpecFromString("10:20", "string") 82 So(err, ShouldBeNil) 83 Convey("Range should return the value ranges", func() { 84 min, max := s.Range() 85 So(min, ShouldEqual, 10) 86 So(max, ShouldEqual, 20) 87 }) 88 }) 89 90 Convey("If it is not multiport, it should return the one port", func() { 91 s, err := NewPortSpecFromString("10", "string") 92 So(err, ShouldBeNil) 93 Convey("Multiport should an error", func() { 94 min, max := s.Range() 95 So(min, ShouldEqual, 10) 96 So(max, ShouldEqual, 10) 97 }) 98 }) 99 }) 100 } 101 102 func TestSinglePort(t *testing.T) { 103 Convey("Given a portspec", t, func() { 104 Convey("If it is singleport", func() { 105 s, err := NewPortSpecFromString("10", "string") 106 So(err, ShouldBeNil) 107 Convey("Multiport should return the value ranges", func() { 108 m, err := s.SinglePort() 109 So(err, ShouldBeNil) 110 So(m, ShouldEqual, uint16(10)) 111 }) 112 }) 113 114 Convey("If it is not multiport", func() { 115 s, err := NewPortSpecFromString("10:20", "string") 116 So(err, ShouldBeNil) 117 Convey("Singleport should an error", func() { 118 _, err := s.SinglePort() 119 So(err, ShouldNotBeNil) 120 }) 121 }) 122 }) 123 } 124 125 func TestValue(t *testing.T) { 126 Convey("Given a portspec, value should return the correct value", t, func() { 127 s, err := NewPortSpecFromString("10:20", "value") 128 So(err, ShouldBeNil) 129 So(s.value.(string), ShouldResemble, "value") 130 }) 131 } 132 133 func TestOVerlap(t *testing.T) { 134 Convey("Given two portspecs that don't overlap", t, func() { 135 a, err1 := NewPortSpecFromString("10:20", "value") 136 b, err2 := NewPortSpecFromString("30:40", "value") 137 So(err1, ShouldBeNil) 138 So(err2, ShouldBeNil) 139 Convey("I should get false", func() { 140 So(a.Overlaps(b), ShouldBeFalse) 141 }) 142 }) 143 144 Convey("Given two portspecs that overlap", t, func() { 145 a, err1 := NewPortSpecFromString("10:50", "value") 146 b, err2 := NewPortSpecFromString("30:40", "value") 147 So(err1, ShouldBeNil) 148 So(err2, ShouldBeNil) 149 Convey("I should get true", func() { 150 So(a.Overlaps(b), ShouldBeTrue) 151 }) 152 }) 153 154 Convey("Given two portspecs that partially overlap", t, func() { 155 a, err1 := NewPortSpecFromString("10:35", "value") 156 b, err2 := NewPortSpecFromString("30:40", "value") 157 So(err1, ShouldBeNil) 158 So(err2, ShouldBeNil) 159 Convey("I should get true", func() { 160 So(a.Overlaps(b), ShouldBeTrue) 161 }) 162 }) 163 164 Convey("Given two portspecs that partially overlap at the end", t, func() { 165 a, err1 := NewPortSpecFromString("35:45", "value") 166 b, err2 := NewPortSpecFromString("30:40", "value") 167 So(err1, ShouldBeNil) 168 So(err2, ShouldBeNil) 169 Convey("I should get true", func() { 170 So(a.Overlaps(b), ShouldBeTrue) 171 }) 172 }) 173 174 Convey("Given two portspecs that partially overlap at a point", t, func() { 175 a, err1 := NewPortSpecFromString("10:20", "value") 176 b, err2 := NewPortSpecFromString("10", "value") 177 So(err1, ShouldBeNil) 178 So(err2, ShouldBeNil) 179 Convey("I should get true", func() { 180 So(a.Overlaps(b), ShouldBeTrue) 181 }) 182 }) 183 184 Convey("Given two portspecs that partially overlap at the end point", t, func() { 185 a, err1 := NewPortSpecFromString("10:20", "value") 186 b, err2 := NewPortSpecFromString("20", "value") 187 So(err1, ShouldBeNil) 188 So(err2, ShouldBeNil) 189 Convey("I should get true", func() { 190 So(a.Overlaps(b), ShouldBeTrue) 191 }) 192 }) 193 194 Convey("Given two portspecs that overlap", t, func() { 195 a, err1 := NewPortSpecFromString("20", "value") 196 b, err2 := NewPortSpecFromString("20", "value") 197 So(err1, ShouldBeNil) 198 So(err2, ShouldBeNil) 199 Convey("I should get true", func() { 200 So(a.Overlaps(b), ShouldBeTrue) 201 }) 202 }) 203 204 Convey("Given two portspecs that do not overlap", t, func() { 205 a, err1 := NewPortSpecFromString("80", "value") 206 b, err2 := NewPortSpecFromString("443", "value") 207 So(err1, ShouldBeNil) 208 So(err2, ShouldBeNil) 209 Convey("I should get true", func() { 210 So(a.Overlaps(b), ShouldBeFalse) 211 }) 212 }) 213 } 214 215 func TestIntersect(t *testing.T) { 216 Convey("Given two portspecs that don't intersect", t, func() { 217 a, err1 := NewPortSpecFromString("10:20", "value") 218 b, err2 := NewPortSpecFromString("30:40", "value") 219 So(err1, ShouldBeNil) 220 So(err2, ShouldBeNil) 221 Convey("I should get false", func() { 222 So(a.Intersects(b), ShouldBeFalse) 223 }) 224 }) 225 226 Convey("Given two portspecs that intersect", t, func() { 227 a, err1 := NewPortSpecFromString("10:50", "value") 228 b, err2 := NewPortSpecFromString("30:40", "value") 229 So(err1, ShouldBeNil) 230 So(err2, ShouldBeNil) 231 Convey("I should get true", func() { 232 So(a.Intersects(b), ShouldBeTrue) 233 }) 234 }) 235 236 Convey("Given two portspecs that intersect", t, func() { 237 a, err1 := NewPortSpecFromString("10:50", "value") 238 b, err2 := NewPortSpecFromString("45", "value") 239 So(err1, ShouldBeNil) 240 So(err2, ShouldBeNil) 241 Convey("I should get true", func() { 242 So(a.Intersects(b), ShouldBeTrue) 243 }) 244 }) 245 246 Convey("Given two portspecs that partially overlap", t, func() { 247 a, err1 := NewPortSpecFromString("10:35", "value") 248 b, err2 := NewPortSpecFromString("30:40", "value") 249 So(err1, ShouldBeNil) 250 So(err2, ShouldBeNil) 251 Convey("I should get true", func() { 252 So(a.Intersects(b), ShouldBeFalse) 253 }) 254 }) 255 256 Convey("Given two portspecs that partially overlap at the end", t, func() { 257 a, err1 := NewPortSpecFromString("35:45", "value") 258 b, err2 := NewPortSpecFromString("30:40", "value") 259 So(err1, ShouldBeNil) 260 So(err2, ShouldBeNil) 261 Convey("I should get true", func() { 262 So(a.Intersects(b), ShouldBeFalse) 263 }) 264 }) 265 266 Convey("Given two portspecs that partially overlap at a point", t, func() { 267 a, err1 := NewPortSpecFromString("10:20", "value") 268 b, err2 := NewPortSpecFromString("10", "value") 269 So(err1, ShouldBeNil) 270 So(err2, ShouldBeNil) 271 Convey("I should get true", func() { 272 So(a.Intersects(b), ShouldBeTrue) 273 }) 274 }) 275 276 Convey("Given two portspecs that partially overlap at the end point", t, func() { 277 a, err1 := NewPortSpecFromString("10:20", "value") 278 b, err2 := NewPortSpecFromString("20", "value") 279 So(err1, ShouldBeNil) 280 So(err2, ShouldBeNil) 281 Convey("I should get true", func() { 282 So(a.Intersects(b), ShouldBeTrue) 283 }) 284 }) 285 286 Convey("Given two portspecs that overlap", t, func() { 287 a, err1 := NewPortSpecFromString("20", "value") 288 b, err2 := NewPortSpecFromString("20", "value") 289 So(err1, ShouldBeNil) 290 So(err2, ShouldBeNil) 291 Convey("I should get true", func() { 292 So(a.Intersects(b), ShouldBeTrue) 293 }) 294 }) 295 296 Convey("Given two portspecs that do not overlap", t, func() { 297 a, err1 := NewPortSpecFromString("80", "value") 298 b, err2 := NewPortSpecFromString("443", "value") 299 So(err1, ShouldBeNil) 300 So(err2, ShouldBeNil) 301 Convey("I should get true", func() { 302 So(a.Intersects(b), ShouldBeFalse) 303 }) 304 }) 305 }