From 033082a31ef713702ca911e9be4b93de0c5467f0 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 4 Jan 2023 16:22:17 +0100 Subject: [PATCH] ParseUnix() test fix: force UTC (#1970) --- pkg/exprhelpers/exprlib.go | 14 +++++-- pkg/exprhelpers/exprlib_test.go | 67 +++++++++++++-------------------- 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/pkg/exprhelpers/exprlib.go b/pkg/exprhelpers/exprlib.go index 20797f6fa..0686b8cc5 100644 --- a/pkg/exprhelpers/exprlib.go +++ b/pkg/exprhelpers/exprlib.go @@ -292,12 +292,20 @@ func LookupHost(value string) []string { return addresses } -func ParseUnix(value string) string { +func ParseUnixTime(value string) (time.Time, error) { //Splitting string here as some unix timestamp may have milliseconds and break ParseInt i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64) if err != nil || i <= 0 { - log.Errorf("Unable to parse %s as unix timestamp.", value) + return time.Time{}, fmt.Errorf("unable to parse %s as unix timestamp", value) + } + return time.Unix(i, 0), nil +} + +func ParseUnix(value string) string { + t, err := ParseUnixTime(value) + if err != nil { + log.Error(err) return "" } - return time.Unix(i, 0).Format(time.RFC3339) + return t.Format(time.RFC3339) } diff --git a/pkg/exprhelpers/exprlib_test.go b/pkg/exprhelpers/exprlib_test.go index fa953cb7c..97a274695 100644 --- a/pkg/exprhelpers/exprlib_test.go +++ b/pkg/exprhelpers/exprlib_test.go @@ -12,6 +12,7 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/database" "github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/types" + "github.com/crowdsecurity/crowdsec/pkg/cstest" log "github.com/sirupsen/logrus" "testing" @@ -972,62 +973,46 @@ func TestGetDecisionsSinceCount(t *testing.T) { } } -func TestParseUnix(t *testing.T) { +func TestParseUnixTime(t *testing.T) { tests := []struct { - name string - env map[string]interface{} - code string - result string - err string + name string + value string + expected time.Time + expectedErr string }{ { name: "ParseUnix() test: valid value with milli", - env: map[string]interface{}{ - "unix": "1672239773.3590894", - "ParseUnix": ParseUnix, - }, - code: "ParseUnix(unix)", - result: "2022-12-28T15:02:53Z", - err: "", + value: "1672239773.3590894", + expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC), }, { name: "ParseUnix() test: valid value without milli", - env: map[string]interface{}{ - "unix": "1672239773", - "ParseUnix": ParseUnix, - }, - code: "ParseUnix(unix)", - result: "2022-12-28T15:02:53Z", - err: "", + value: "1672239773", + expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC), }, { name: "ParseUnix() test: invalid input", - env: map[string]interface{}{ - "unix": "AbcDefG!#", - "ParseUnix": ParseUnix, - }, - code: "ParseUnix(unix)", - result: "", - err: "", + value: "AbcDefG!#", + expected: time.Time{}, + expectedErr: "unable to parse AbcDefG!# as unix timestamp", }, { name: "ParseUnix() test: negative value", - env: map[string]interface{}{ - "unix": "-1000", - "ParseUnix": ParseUnix, - }, - code: "ParseUnix(unix)", - result: "", - err: "", + value: "-1000", + expected: time.Time{}, + expectedErr: "unable to parse -1000 as unix timestamp", }, } - for _, test := range tests { - program, err := expr.Compile(test.code, expr.Env(test.env)) - require.NoError(t, err) - output, err := expr.Run(program, test.env) - require.NoError(t, err) - require.Equal(t, test.result, output) - log.Printf("test '%s' : OK", test.name) + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + output, err := ParseUnixTime(tc.value) + cstest.RequireErrorContains(t, err, tc.expectedErr) + if tc.expectedErr != "" { + return + } + require.WithinDuration(t, tc.expected, output, time.Second) + }) } }