2019-06-10 23:19:11 -07:00
// Copyright (C) 2019 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"io/ioutil"
"testing"
)
func TestParseVersion ( t * testing . T ) {
cases := [ ] struct {
longVersion string
parsed version
} {
{
longVersion : ` syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) jb@kvin.kastelo.net 2019-05-23 16:08:14 UTC ` ,
parsed : version {
version : "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep" ,
tag : "v1.1.4-rc.1" ,
commit : "6aaae618" ,
codename : "Erbium Earthworm" ,
runtime : "go1.12.5" ,
goos : "darwin" ,
goarch : "amd64" ,
builder : "jb@kvin.kastelo.net" ,
} ,
} ,
2020-06-03 06:00:46 -07:00
{
longVersion : ` syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) jb@kvin.kastelo.net 2019-05-23 16:08:14 UTC [foo, bar] ` ,
parsed : version {
version : "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep" ,
tag : "v1.1.4-rc.1" ,
commit : "6aaae618" ,
codename : "Erbium Earthworm" ,
runtime : "go1.12.5" ,
goos : "darwin" ,
goarch : "amd64" ,
builder : "jb@kvin.kastelo.net" ,
extra : [ ] string { "foo" , "bar" } ,
} ,
} ,
2019-06-10 23:19:11 -07:00
}
for _ , tc := range cases {
v , err := parseVersion ( tc . longVersion )
if err != nil {
2020-06-03 06:00:46 -07:00
t . Errorf ( "%s\nerror: %v\n" , tc . longVersion , err )
2019-06-10 23:19:11 -07:00
continue
}
2020-06-03 06:00:46 -07:00
if fmt . Sprint ( v ) != fmt . Sprint ( tc . parsed ) {
t . Errorf ( "%s\nA: %v\nE: %v\n" , tc . longVersion , v , tc . parsed )
2019-06-10 23:19:11 -07:00
}
}
}
func TestParseReport ( t * testing . T ) {
bs , err := ioutil . ReadFile ( "_testdata/panic.log" )
if err != nil {
t . Fatal ( err )
}
2020-10-07 01:05:13 -07:00
pkt , err := parseCrashReport ( "1/2/345" , bs )
2019-06-10 23:19:11 -07:00
if err != nil {
t . Fatal ( err )
}
bs , err = pkt . JSON ( )
if err != nil {
t . Fatal ( err )
}
fmt . Printf ( "%s\n" , bs )
}
2020-10-08 08:37:45 -07:00
func TestCrashReportFingerprint ( t * testing . T ) {
cases := [ ] struct {
message , exp string
ldb bool
} {
{
message : "panic: leveldb/table: corruption on data-block (pos=51308946): checksum mismatch, want=0xa89f9aa0 got=0xd27cc4c7 [file=004003.ldb]" ,
exp : "panic: leveldb/table: corruption on data-block (pos=x): checksum mismatch, want=0xX got=0xX [file=x.ldb]" ,
ldb : true ,
} ,
{
message : "panic: leveldb/table: corruption on table-footer (pos=248): bad magic number [file=001370.ldb]" ,
exp : "panic: leveldb/table: corruption on table-footer (pos=x): bad magic number [file=x.ldb]" ,
ldb : true ,
} ,
{
message : "panic: runtime error: slice bounds out of range [4294967283:4194304]" ,
exp : "panic: runtime error: slice bounds out of range [x]" ,
} ,
{
message : "panic: runtime error: slice bounds out of range [-2:]" ,
exp : "panic: runtime error: slice bounds out of range [x]" ,
} ,
{
message : "panic: runtime error: slice bounds out of range [:4294967283] with capacity 32768" ,
exp : "panic: runtime error: slice bounds out of range [x] with capacity x" ,
} ,
{
message : "panic: runtime error: index out of range [0] with length 0" ,
exp : "panic: runtime error: index out of range [x] with length x" ,
} ,
{
message : ` panic: leveldb: internal key "\x01", len=1: invalid length ` ,
exp : ` panic: leveldb: internal key "x", len=x: invalid length ` ,
ldb : true ,
} ,
{
message : ` panic: write /var/syncthing/config/index-v0.14.0.db/2732813.log: cannot allocate memory ` ,
exp : ` panic: write x: cannot allocate memory ` ,
ldb : true ,
} ,
{
message : ` panic: filling Blocks: read C:\Users\Serv-Resp-Tizayuca\AppData\Local\Syncthing\index-v0.14.0.db\006561.ldb: Error de datos (comprobación de redundancia cíclica). ` ,
exp : ` panic: filling Blocks: read x: Error de datos (comprobación de redundancia cíclica). ` ,
ldb : true ,
} ,
}
for i , tc := range cases {
fingerprint := crashReportFingerprint ( tc . message )
expLen := 2
if tc . ldb {
expLen = 1
}
if l := len ( fingerprint ) ; l != expLen {
t . Errorf ( "tc %v: Unexpected fingerprint length: %v != %v" , i , l , expLen )
} else if msg := fingerprint [ expLen - 1 ] ; msg != tc . exp {
t . Errorf ( "tc %v:\n\"%v\" !=\n\"%v\"" , i , msg , tc . exp )
}
}
}