diff --git a/internal/logparsers/parsers.go b/internal/logparsers/parsers.go index 0d816c6..4258662 100644 --- a/internal/logparsers/parsers.go +++ b/internal/logparsers/parsers.go @@ -107,14 +107,6 @@ func parseTelemLogLine(line string) (frame can.Frame, ts time.Time, err error) { // strip trailng newline since we rely on it being gone line = strings.TrimSpace(line) - // these files tend to get corrupted. - // there are all kinds of nasties that can happen. - // defense against random panics - defer func() { - if r := recover(); r != nil { - err = NewFormatError("caught panic", nil) - } - }() a := telemRegex.FindStringSubmatch(line) if a == nil || len(a) != 5 { err = NewFormatError("no regex match", nil) @@ -135,7 +127,7 @@ func parseTelemLogLine(line string) (frame can.Frame, ts time.Time, err error) { } ts = time.Unix(unixSeconds, unixMillis*int64(time.Millisecond)) - // VALIDATION STEP: sometimes the data gets really whack. + // VALIDATION STEP: sometimes the data gets really whack, but remains valid. // We check that the time is between 2017 and 2032. // Realistically we will not be using this software then. // TODO: add this diff --git a/internal/logparsers/parsers_test.go b/internal/logparsers/parsers_test.go index 529cdfa..961ed92 100644 --- a/internal/logparsers/parsers_test.go +++ b/internal/logparsers/parsers_test.go @@ -23,11 +23,11 @@ func Test_parseCanDumpLine(t *testing.T) { name: "test normal data", args: args{dumpLine: "(1684538768.521889) can0 200#8D643546"}, wantFrame: can.Frame{ - Id: can.CanID{Id: 0x200, Extended: false}, + Id: can.CanID{Id: 0x200, Extended: false}, Data: []byte{0x8d, 0x64, 0x35, 0x46}, Kind: can.CanDataFrame, }, - wantTs: time.Unix(1684538768, 521889 * int64(time.Microsecond)), + wantTs: time.Unix(1684538768, 521889*int64(time.Microsecond)), wantErr: false, }, // TODO: add extended id test case @@ -54,11 +54,11 @@ func Test_parseCanDumpLine_errors(t *testing.T) { // this test tries a bunch of failure cases to ensure that they are caught and not panicking. tests := []struct { - name string + name string input string }{ { - name: "garbage input", + name: "garbage input", input: "hoiseorhijkl", }, { @@ -91,5 +91,83 @@ func Test_parseCanDumpLine_errors(t *testing.T) { t.Fatalf("parseCanDumpLine() expected error but instead got f = %v, ts = %v", f, ts) } }) - } + } +} + +func Test_parseTelemLogLine(t *testing.T) { + type args struct { + line string + } + tests := []struct { + name string + args args + wantFrame can.Frame + wantTs time.Time + wantErr bool + }{ + { + name: "basic test", + args: args{line: "1698180835.318 0619D80564080EBE241"}, + wantFrame: can.Frame{ + Id: can.CanID{Id: 0x61, Extended: false}, + Data: []byte{0x9D, 0x80, 0x56, 0x40, 0x80, 0xEB, 0xE2, 0x41}, + Kind: can.CanDataFrame, + }, + wantTs: time.Unix(1698180835, 318*int64(time.Millisecond)), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotFrame, gotTs, err := parseTelemLogLine(tt.args.line) + if (err != nil) != tt.wantErr { + t.Errorf("parseTelemLogLine() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotFrame, tt.wantFrame) { + t.Errorf("parseTelemLogLine() gotFrame = %v, want %v", gotFrame, tt.wantFrame) + } + if !reflect.DeepEqual(gotTs, tt.wantTs) { + t.Errorf("parseTelemLogLine() gotTs = %v, want %v", gotTs, tt.wantTs) + } + }) + } +} + +func Test_parseTelemLogLine_errors(t *testing.T) { + tests := []struct { + name string + input string + }{ + { + name: "garbage input", + input: "ajl;ksdoifhge\xEB", + }, + { + name: "bad data length", + input: "1698180835.318 0619D80564080EBE24", + }, + { + name: "bad timestamp", + input: "99999999999999999999999999999999999999999999999.318 0619D80564080EBE24", + }, + { + name: "invalid hex characters", + input: "1698180835.318 0619D805640X0EBE24", + }, + { + name: "utf8 corruption", + input: "1698180835.318 0619\xed\xa0\x80fsadfD805640X0EBE24", + }, + + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f, ts, err := parseTelemLogLine(tt.input) + if err == nil { + t.Fatalf("parseTelemLogLine() expected error but instead got f = %v, ts = %v", f, ts) + } + }) + } }