package store import ( "path/filepath" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" ) func svcEv(status, origin int, path, ref string, ago time.Duration) core.Event { return core.Event{Time: t0.Add(-ago), IP: "200.1.1.1", Cat: core.OK, Source: "traefik", Domain: "app.example.com", Method: "GET", Path: path, UA: "Mozilla/5.0 x", Status: status, OriginStatus: origin, DurationNS: 40e6, OriginNS: 35e6, OverheadNS: 1e6, Referer: ref, Router: "https-0-abc-app@docker", Service: "https-0-abc-app@docker", Proto: "HTTP/2.0", Scheme: "https", TLSVer: "1.3", TLSCiph: "TLS_AES_128_GCM_SHA256", E: core.Enrich{Device: "desktop"}} } func TestServicesAggregateAndSnapshot(t *testing.T) { s := newStore("oracle", 100) for i := 0; i < 10; i++ { s.ObserveService(svcEv(200, 200, "/", "", time.Duration(i)*time.Minute)) } s.ObserveService(svcEv(500, 500, "/api/x/123456789012345678", "", time.Minute)) s.ObserveService(svcEv(404, 404, "/old", "https://app.example.com/home", time.Minute)) s.ObserveService(svcEv(404, 404, "/old", "https://app.example.com/home", time.Minute)) atk := svcEv(404, 404, "/.env", "", time.Minute) atk.Cat = core.Scanner s.ObserveService(atk) s.ObserveService(core.Event{Time: t0, Source: "ssh", IP: "1.1.1.1"}) // ignored now := t0.Unix() v := s.Services(now-86400, now, "", "") if len(v) != 1 || v[0].Key != "https-0-abc-app" || v[0].Alias != "app.example.com" { t.Fatalf("services %+v", v) } tot := v[0].Tot if tot.Req != 14 || tot.Cls[ClsApp] != 1 || tot.Cls[ClsAttack] != 1 || tot.Cls[ClsClientBroken] != 2 || tot.Legit != 13 { t.Fatalf("tot %+v", tot) } if tot.Proto[1] != 14 || tot.TLS[0] != 14 || tot.ApN == 0 { t.Fatalf("proto/tls/apdex %+v", tot) } if q := Quantile(tot.Dur, 0.5); q < 25 || q > 50 { t.Fatalf("p50 %v", q) } if len(v[0].Broken) != 1 || v[0].ErrRoutes["app|GET /api/x/:id"] != 1 { t.Fatalf("maps %v %v", v[0].Broken, v[0].ErrRoutes) } if got := s.Services(now-86400, now, "nope", ""); len(got) != 0 { t.Fatal("filter") } if got := s.Services(now-86400, now, "", "app.example.com"); len(got) != 1 { t.Fatal("host filter") } path := filepath.Join(t.TempDir(), "snap") if err := s.Save(path, nil); err != nil { t.Fatal(err) } s2 := newStore("oracle", 100) if _, err := s2.Load(path); err != nil { t.Fatal(err) } if v2 := s2.Services(now-86400, now, "", ""); len(v2) != 1 || v2[0].Tot.Req != 14 { t.Fatalf("after load %+v", v2) } } func TestServiceCap(t *testing.T) { s := newStore("oracle", 100) for i := 0; i < svcCap+10; i++ { e := svcEv(200, 200, "/", "", time.Minute) e.Service = "svc" + string(rune('a'+i%26)) + string(rune('a'+i/26)) s.ObserveService(e) } if n := len(s.sv.m); n > svcCap+1 { t.Fatalf("services %d", n) } } func TestCancelledIsNotServerError(t *testing.T) { s := newStore("oracle", 100) for i := 0; i < 5; i++ { s.ObserveService(svcEv(499, 0, "/x", "", time.Minute)) } e := svcEv(200, 200, "/", "", time.Minute) s.ObserveService(e) run := svcEv(404, 404, "/api/actions/runner.v1.RunnerService/FetchTask", "", time.Minute) run.Method, run.UA, run.Cat = "POST", "connect-go/1.16", core.Scanner s.ObserveService(run) now := t0.Unix() v := s.Services(now-86400, now, "", "") tot := v[0].Tot if tot.Cls[ClsApp] != 0 || tot.Cls[ClsProxy] != 0 || tot.Cls[ClsAttack] != 0 || tot.Cls[ClsClientOther] != 5 || tot.Cls[ClsClientAuth] != 1 { t.Fatalf("cls %+v", tot.Cls) } }