sentry_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "fmt"
  9. "os"
  10. "testing"
  11. )
  12. func TestParseVersion(t *testing.T) {
  13. cases := []struct {
  14. longVersion string
  15. parsed version
  16. }{
  17. {
  18. longVersion: `syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) [email protected] 2019-05-23 16:08:14 UTC`,
  19. parsed: version{
  20. version: "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep",
  21. tag: "v1.1.4-rc.1",
  22. commit: "6aaae618",
  23. codename: "Erbium Earthworm",
  24. runtime: "go1.12.5",
  25. goos: "darwin",
  26. goarch: "amd64",
  27. builder: "[email protected]",
  28. },
  29. },
  30. {
  31. longVersion: `syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) [email protected] 2019-05-23 16:08:14 UTC [foo, bar]`,
  32. parsed: version{
  33. version: "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep",
  34. tag: "v1.1.4-rc.1",
  35. commit: "6aaae618",
  36. codename: "Erbium Earthworm",
  37. runtime: "go1.12.5",
  38. goos: "darwin",
  39. goarch: "amd64",
  40. builder: "[email protected]",
  41. extra: []string{"foo", "bar"},
  42. },
  43. },
  44. }
  45. for _, tc := range cases {
  46. v, err := parseVersion(tc.longVersion)
  47. if err != nil {
  48. t.Errorf("%s\nerror: %v\n", tc.longVersion, err)
  49. continue
  50. }
  51. if fmt.Sprint(v) != fmt.Sprint(tc.parsed) {
  52. t.Errorf("%s\nA: %v\nE: %v\n", tc.longVersion, v, tc.parsed)
  53. }
  54. }
  55. }
  56. func TestParseReport(t *testing.T) {
  57. bs, err := os.ReadFile("_testdata/panic.log")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. pkt, err := parseCrashReport("1/2/345", bs)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. bs, err = pkt.JSON()
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. fmt.Printf("%s\n", bs)
  70. }
  71. func TestCrashReportFingerprint(t *testing.T) {
  72. cases := []struct {
  73. message, exp string
  74. ldb bool
  75. }{
  76. {
  77. message: "panic: leveldb/table: corruption on data-block (pos=51308946): checksum mismatch, want=0xa89f9aa0 got=0xd27cc4c7 [file=004003.ldb]",
  78. exp: "panic: leveldb/table: corruption on data-block (pos=x): checksum mismatch, want=0xX got=0xX [file=x.ldb]",
  79. ldb: true,
  80. },
  81. {
  82. message: "panic: leveldb/table: corruption on table-footer (pos=248): bad magic number [file=001370.ldb]",
  83. exp: "panic: leveldb/table: corruption on table-footer (pos=x): bad magic number [file=x.ldb]",
  84. ldb: true,
  85. },
  86. {
  87. message: "panic: runtime error: slice bounds out of range [4294967283:4194304]",
  88. exp: "panic: runtime error: slice bounds out of range [x]",
  89. },
  90. {
  91. message: "panic: runtime error: slice bounds out of range [-2:]",
  92. exp: "panic: runtime error: slice bounds out of range [x]",
  93. },
  94. {
  95. message: "panic: runtime error: slice bounds out of range [:4294967283] with capacity 32768",
  96. exp: "panic: runtime error: slice bounds out of range [x] with capacity x",
  97. },
  98. {
  99. message: "panic: runtime error: index out of range [0] with length 0",
  100. exp: "panic: runtime error: index out of range [x] with length x",
  101. },
  102. {
  103. message: `panic: leveldb: internal key "\x01", len=1: invalid length`,
  104. exp: `panic: leveldb: internal key "x", len=x: invalid length`,
  105. ldb: true,
  106. },
  107. {
  108. message: `panic: write /var/syncthing/config/index-v0.14.0.db/2732813.log: cannot allocate memory`,
  109. exp: `panic: write x: cannot allocate memory`,
  110. ldb: true,
  111. },
  112. {
  113. 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).`,
  114. exp: `panic: filling Blocks: read x: Error de datos (comprobación de redundancia cíclica).`,
  115. ldb: true,
  116. },
  117. }
  118. for i, tc := range cases {
  119. fingerprint := crashReportFingerprint(tc.message)
  120. expLen := 2
  121. if tc.ldb {
  122. expLen = 1
  123. }
  124. if l := len(fingerprint); l != expLen {
  125. t.Errorf("tc %v: Unexpected fingerprint length: %v != %v", i, l, expLen)
  126. } else if msg := fingerprint[expLen-1]; msg != tc.exp {
  127. t.Errorf("tc %v:\n\"%v\" !=\n\"%v\"", i, msg, tc.exp)
  128. }
  129. }
  130. }