package main import ( "testing" "housecarl/internal/model" ) func TestDurations(t *testing.T) { for in, want := range map[string]int{"3600": 3600, "12h": 43200, "30m": 1800, "1d": 86400, "1d12h": 129600, "1h30m": 5400} { if got, err := parseSeconds(in); err != nil || got != want { t.Errorf("parseSeconds(%q) = %d, %v; want %d", in, got, err, want) } } for _, bad := range []string{"12x", "d", "1d2x"} { if _, err := parseSeconds(bad); err == nil { t.Errorf("parseSeconds(%q) accepted", bad) } } for n, want := range map[int]string{43200: "12h", 5400: "1h30m", 90061: "1d1h1m1s", 0: "-"} { if got := fmtSeconds(n); got != want { t.Errorf("fmtSeconds(%d) = %q, want %q", n, got, want) } } } func TestLimitAndDescribe(t *testing.T) { l, err := parseLimit("3/minutes,burst=5,over,per-source") if err != nil || l.Rate != 3 || l.Per != "minute" || l.Burst != 5 || !l.Over || !l.PerSource { t.Errorf("parseLimit: %+v %v", l, err) } if _, err := parseLimit("fast"); err == nil { t.Error("a bad limit accepted") } m := model.Match{InZones: []string{"wan"}, NotSources: true, Sources: []string{"trusted"}, Services: []string{"ssh", "tcp/8080"}} if got := describe(m); got != "in wan from not trusted ssh,tcp/8080" { t.Errorf("describe: %q", got) } if got := editList([]string{"a", "b", "c"}, []string{"d"}, []string{"b"}); len(got) != 3 || got[1] != "c" || got[2] != "d" { t.Errorf("editList: %v", got) } }