github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/extractors/linux_test.go (about) 1 // +build linux windows 2 3 package extractors 4 5 import ( 6 "encoding/hex" 7 "net" 8 "reflect" 9 "testing" 10 "time" 11 12 . "github.com/smartystreets/goconvey/convey" 13 "go.aporeto.io/enforcerd/trireme-lib/common" 14 "go.aporeto.io/enforcerd/trireme-lib/controller/constants" 15 "go.aporeto.io/enforcerd/trireme-lib/policy" 16 portspec "go.aporeto.io/enforcerd/trireme-lib/utils/portspec" 17 ) 18 19 func TestComputeFileMd5(t *testing.T) { 20 21 Convey("When I calculate the MD5 of a bad file", t, func() { 22 _, err := ComputeFileMd5("testdata/nofile") 23 Convey("I should get an error", func() { 24 So(err, ShouldNotBeNil) 25 }) 26 }) 27 28 Convey("When I calculate the MD5 of a good file", t, func() { 29 hash, err := ComputeFileMd5("testdata/curl") 30 Convey("I should get no error and the right value", func() { 31 So(err, ShouldBeNil) 32 So(hex.EncodeToString(hash), ShouldResemble, "bf7e66d7bbd0465cfcba5b1cf68a9b59") 33 }) 34 }) 35 } 36 37 func TestFindFQDN(t *testing.T) { 38 39 Convey("When I try to get the hostname of a good host", t, func() { 40 hostname := findFQDN(1000 * time.Second) 41 42 Convey("I should be able to resolve this hostname", func() { 43 addr, err := net.LookupHost(hostname) 44 So(err, ShouldBeNil) 45 So(len(addr), ShouldBeGreaterThan, 0) 46 }) 47 }) 48 } 49 50 func TestLibs(t *testing.T) { 51 52 Convey("When I try to get the libraries of a known binary", t, func() { 53 libraries := Libs("./testdata/curl") 54 Convey("I should get the execpted libraries", func() { 55 So(len(libraries), ShouldEqual, 4) 56 So(libraries, ShouldContain, "libcurl-gnutls.so.4") 57 So(libraries, ShouldContain, "libz.so.1") 58 So(libraries, ShouldContain, "libpthread.so.0") 59 So(libraries, ShouldContain, "libc.so.6") 60 }) 61 }) 62 63 Convey("When I try to get the libraries of a bad binary", t, func() { 64 65 libraries := Libs("./testdata/nofile") 66 Convey("I should get an empty array", func() { 67 So(len(libraries), ShouldEqual, 0) 68 }) 69 }) 70 } 71 72 func TestSystemdEventMetadataExtractor(t *testing.T) { 73 74 Convey("When I call the metadata extrator", t, func() { 75 76 Convey("If all data are present", func() { 77 event := &common.EventInfo{ 78 Name: "./testdata/curl", 79 Executable: "./testdata/curl", 80 PID: 1234, 81 PUID: "/1234", 82 Tags: []string{"app=web"}, 83 } 84 85 pu, err := SystemdEventMetadataExtractor(event) 86 Convey("I should get no error and a valid PU runitime", func() { 87 So(err, ShouldBeNil) 88 So(pu, ShouldNotBeNil) 89 }) 90 }) 91 }) 92 } 93 94 func TestDefaultHostMetadataExtractor(t *testing.T) { 95 96 Convey("When I call the host metadata extractor", t, func() { 97 98 Convey("If its valid data", func() { 99 100 s, _ := portspec.NewPortSpecFromString("1000", nil) // nolint 101 services := []common.Service{ 102 { 103 Protocol: uint8(6), 104 Ports: s, 105 }, 106 } 107 108 event := &common.EventInfo{ 109 Name: "Web", 110 PID: 1234, 111 PUID: "Web", 112 Tags: []string{"app=web"}, 113 Services: services, 114 } 115 116 pu, err := DefaultHostMetadataExtractor(event) 117 Convey("I should get no error and a valid PU runtimg", func() { 118 So(err, ShouldBeNil) 119 So(pu, ShouldNotBeNil) 120 So(pu.Options().CgroupName, ShouldResemble, "Web") 121 So(pu.Options().Services, ShouldResemble, services) 122 }) 123 }) 124 125 Convey("If I get invalid tags", func() { 126 127 event := &common.EventInfo{ 128 Name: "Web", 129 PID: 1234, 130 PUID: "Web", 131 Tags: []string{"invalid"}, 132 } 133 134 _, err := DefaultHostMetadataExtractor(event) 135 Convey("I should get an error", func() { 136 So(err, ShouldNotBeNil) 137 }) 138 }) 139 140 Convey("If I get an invalid PID", func() { 141 142 event := &common.EventInfo{ 143 Name: "Web", 144 PID: -1233, 145 PUID: "Web", 146 Tags: []string{"invalid"}, 147 } 148 149 _, err := DefaultHostMetadataExtractor(event) 150 Convey("I should get an error", func() { 151 So(err, ShouldNotBeNil) 152 }) 153 }) 154 }) 155 } 156 157 func Test_policyExtensions(t *testing.T) { 158 type args struct { 159 runtime policy.RuntimeReader 160 } 161 162 pur1 := policy.NewPURuntime("", 0, "", nil, nil, common.LinuxProcessPU, policy.None, nil) 163 em1 := policy.ExtendedMap{ 164 "Key": "Value", 165 } 166 options := pur1.Options() 167 options.PolicyExtensions = em1 168 pur1.SetOptions(options) 169 170 // 2nd Runtime 171 pur2 := policy.NewPURuntime("", 0, "", nil, nil, common.LinuxProcessPU, policy.None, nil) 172 options = pur2.Options() 173 pur2.SetOptions(options) 174 175 // 3rd runtime 176 pur3 := policy.NewPURuntime("", 0, "", nil, nil, common.LinuxProcessPU, policy.None, nil) 177 options = pur3.Options() 178 options.PolicyExtensions = nil 179 pur3.SetOptions(options) 180 181 tests := []struct { 182 name string 183 args args 184 wantExtensions policy.ExtendedMap 185 }{ 186 // TODO: Add test cases. 187 { 188 name: "Test if runtime is nil", 189 args: args{ 190 runtime: nil, 191 }, 192 wantExtensions: nil, 193 }, 194 { 195 name: "Test if policy extensions are nil", 196 args: args{ 197 runtime: pur3, 198 }, 199 wantExtensions: nil, 200 }, 201 { 202 name: "Test if policy extensions are defined", 203 args: args{ 204 runtime: pur1, 205 }, 206 wantExtensions: em1, 207 }, 208 { 209 name: "Test if policy extensions are not defined", 210 args: args{ 211 runtime: pur2, 212 }, 213 wantExtensions: nil, 214 }, 215 } 216 for _, tt := range tests { 217 t.Run(tt.name, func(t *testing.T) { 218 if gotExtensions := policyExtensions(tt.args.runtime); !reflect.DeepEqual(gotExtensions, tt.wantExtensions) { 219 t.Errorf("policyExtensions() = %v, want %v", gotExtensions, tt.wantExtensions) 220 } 221 }) 222 } 223 } 224 225 func TestIsHostmodePU(t *testing.T) { 226 type args struct { 227 runtime policy.RuntimeReader 228 mode constants.ModeType 229 } 230 231 pur1 := policy.NewPURuntime("", 0, "", nil, nil, common.HostPU, policy.None, nil) 232 233 // 2nd Runtime 234 pur2 := policy.NewPURuntime("", 0, "", nil, nil, common.HostNetworkPU, policy.None, nil) 235 236 // 3rd runtime 237 pur3 := policy.NewPURuntime("", 0, "", nil, nil, common.LinuxProcessPU, policy.None, nil) 238 239 tests := []struct { 240 name string 241 args args 242 want bool 243 }{ 244 { 245 name: "Test if pu type is Container", 246 args: args{ 247 runtime: nil, 248 mode: constants.RemoteContainer, 249 }, 250 want: false, 251 }, 252 { 253 name: "Test if PU type is hostpu", 254 args: args{ 255 runtime: pur1, 256 mode: constants.LocalServer, 257 }, 258 want: true, 259 }, 260 { 261 name: "Test if pu type is hostnetworkmode pu", 262 args: args{ 263 runtime: pur2, 264 mode: constants.LocalServer, 265 }, 266 want: true, 267 }, 268 { 269 name: "Test if pu type is linux pu", 270 args: args{ 271 runtime: pur3, 272 mode: constants.LocalServer, 273 }, 274 want: false, 275 }, 276 { 277 name: "Test invalid runtime", 278 args: args{ 279 runtime: nil, 280 mode: constants.LocalServer, 281 }, 282 want: false, 283 }, 284 } 285 286 for _, tt := range tests { 287 t.Run(tt.name, func(t *testing.T) { 288 if got := IsHostmodePU(tt.args.runtime, tt.args.mode); got != tt.want { 289 t.Errorf("IsHostmodePU() = %v, want %v", got, tt.want) 290 } 291 }) 292 } 293 } 294 295 func TestIsHostPU(t *testing.T) { 296 type args struct { 297 runtime policy.RuntimeReader 298 mode constants.ModeType 299 } 300 301 pur1 := policy.NewPURuntime("", 0, "", nil, nil, common.HostPU, policy.None, nil) 302 303 // 2nd Runtime 304 pur2 := policy.NewPURuntime("", 0, "", nil, nil, common.HostNetworkPU, policy.None, nil) 305 306 // 3rd runtime 307 pur3 := policy.NewPURuntime("", 0, "", nil, nil, common.LinuxProcessPU, policy.None, nil) 308 309 tests := []struct { 310 name string 311 args args 312 want bool 313 }{{ 314 name: "Test if pu type is Container", 315 args: args{ 316 runtime: nil, 317 mode: constants.RemoteContainer, 318 }, 319 want: false, 320 }, 321 { 322 name: "Test if PU type is hostpu", 323 args: args{ 324 runtime: pur1, 325 mode: constants.LocalServer, 326 }, 327 want: true, 328 }, 329 { 330 name: "Test if pu type is hostnetworkmode pu", 331 args: args{ 332 runtime: pur2, 333 mode: constants.LocalServer, 334 }, 335 want: false, 336 }, 337 { 338 name: "Test if pu type is linux pu", 339 args: args{ 340 runtime: pur3, 341 mode: constants.LocalServer, 342 }, 343 want: false, 344 }, 345 { 346 name: "Test invalid runtime", 347 args: args{ 348 runtime: nil, 349 mode: constants.LocalServer, 350 }, 351 want: false, 352 }, 353 } 354 for _, tt := range tests { 355 t.Run(tt.name, func(t *testing.T) { 356 if got := IsHostPU(tt.args.runtime, tt.args.mode); got != tt.want { 357 t.Errorf("IsHostPU() = %v, want %v", got, tt.want) 358 } 359 }) 360 } 361 }