log_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package safelog
  2. import (
  3. "bytes"
  4. "log"
  5. "testing"
  6. )
  7. //Check to make sure that addresses split across calls to write are still scrubbed
  8. func TestLogScrubberSplit(t *testing.T) {
  9. input := []byte("test\nhttp2: panic serving [2620:101:f000:780:9097:75b1:519f:dbb8]:58344: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack\n")
  10. expected := "test\nhttp2: panic serving [scrubbed]: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack\n"
  11. var buff bytes.Buffer
  12. scrubber := &LogScrubber{Output: &buff}
  13. n, err := scrubber.Write(input[:12]) //test\nhttp2:
  14. if n != 12 {
  15. t.Errorf("wrong number of bytes %d", n)
  16. }
  17. if err != nil {
  18. t.Errorf("%q", err)
  19. }
  20. if buff.String() != "test\n" {
  21. t.Errorf("Got %q, expected %q", buff.String(), "test\n")
  22. }
  23. n, err = scrubber.Write(input[12:30]) //panic serving [2620:101:f
  24. if n != 18 {
  25. t.Errorf("wrong number of bytes %d", n)
  26. }
  27. if err != nil {
  28. t.Errorf("%q", err)
  29. }
  30. if buff.String() != "test\n" {
  31. t.Errorf("Got %q, expected %q", buff.String(), "test\n")
  32. }
  33. n, err = scrubber.Write(input[30:]) //000:780:9097:75b1:519f:dbb8]:58344: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack\n
  34. if n != (len(input) - 30) {
  35. t.Errorf("wrong number of bytes %d", n)
  36. }
  37. if err != nil {
  38. t.Errorf("%q", err)
  39. }
  40. if buff.String() != expected {
  41. t.Errorf("Got %q, expected %q", buff.String(), expected)
  42. }
  43. }
  44. //Test the log scrubber on known problematic log messages
  45. func TestLogScrubberMessages(t *testing.T) {
  46. for _, test := range []struct {
  47. input, expected string
  48. }{
  49. {
  50. "http: TLS handshake error from 129.97.208.23:38310: ",
  51. "http: TLS handshake error from [scrubbed]: \n",
  52. },
  53. {
  54. "http2: panic serving [2620:101:f000:780:9097:75b1:519f:dbb8]:58344: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack",
  55. "http2: panic serving [scrubbed]: interface conversion: *http2.responseWriter is not http.Hijacker: missing method Hijack\n",
  56. },
  57. {
  58. //Make sure it doesn't scrub fingerprint
  59. "a=fingerprint:sha-256 33:B6:FA:F6:94:CA:74:61:45:4A:D2:1F:2C:2F:75:8A:D9:EB:23:34:B2:30:E9:1B:2A:A6:A9:E0:44:72:CC:74",
  60. "a=fingerprint:sha-256 33:B6:FA:F6:94:CA:74:61:45:4A:D2:1F:2C:2F:75:8A:D9:EB:23:34:B2:30:E9:1B:2A:A6:A9:E0:44:72:CC:74\n",
  61. },
  62. {
  63. //try with enclosing parens
  64. "(1:2:3:4:c:d:e:f) {1:2:3:4:c:d:e:f}",
  65. "([scrubbed]) {[scrubbed]}\n",
  66. },
  67. {
  68. //Make sure it doesn't scrub timestamps
  69. "2019/05/08 15:37:31 starting",
  70. "2019/05/08 15:37:31 starting\n",
  71. },
  72. } {
  73. var buff bytes.Buffer
  74. log.SetFlags(0) //remove all extra log output for test comparisons
  75. log.SetOutput(&LogScrubber{Output: &buff})
  76. log.Print(test.input)
  77. if buff.String() != test.expected {
  78. t.Errorf("%q: got %q, expected %q", test.input, buff.String(), test.expected)
  79. }
  80. }
  81. }
  82. func TestLogScrubberGoodFormats(t *testing.T) {
  83. for _, addr := range []string{
  84. // IPv4
  85. "1.2.3.4",
  86. "255.255.255.255",
  87. // IPv4 with port
  88. "1.2.3.4:55",
  89. "255.255.255.255:65535",
  90. // IPv6
  91. "1:2:3:4:c:d:e:f",
  92. "1111:2222:3333:4444:CCCC:DDDD:EEEE:FFFF",
  93. // IPv6 with brackets
  94. "[1:2:3:4:c:d:e:f]",
  95. "[1111:2222:3333:4444:CCCC:DDDD:EEEE:FFFF]",
  96. // IPv6 with brackets and port
  97. "[1:2:3:4:c:d:e:f]:55",
  98. "[1111:2222:3333:4444:CCCC:DDDD:EEEE:FFFF]:65535",
  99. // compressed IPv6
  100. "::f",
  101. "::d:e:f",
  102. "1:2:3::",
  103. "1:2:3::d:e:f",
  104. "1:2:3:d:e:f::",
  105. "::1:2:3:d:e:f",
  106. "1111:2222:3333::DDDD:EEEE:FFFF",
  107. // compressed IPv6 with brackets
  108. "[::d:e:f]",
  109. "[1:2:3::]",
  110. "[1:2:3::d:e:f]",
  111. "[1111:2222:3333::DDDD:EEEE:FFFF]",
  112. "[1:2:3:4:5:6::8]",
  113. "[1::7:8]",
  114. // compressed IPv6 with brackets and port
  115. "[1::]:58344",
  116. "[::d:e:f]:55",
  117. "[1:2:3::]:55",
  118. "[1:2:3::d:e:f]:55",
  119. "[1111:2222:3333::DDDD:EEEE:FFFF]:65535",
  120. // IPv4-compatible and IPv4-mapped
  121. "::255.255.255.255",
  122. "::ffff:255.255.255.255",
  123. "[::255.255.255.255]",
  124. "[::ffff:255.255.255.255]",
  125. "[::255.255.255.255]:65535",
  126. "[::ffff:255.255.255.255]:65535",
  127. "[::ffff:0:255.255.255.255]",
  128. "[2001:db8:3:4::192.0.2.33]",
  129. } {
  130. var buff bytes.Buffer
  131. log.SetFlags(0) //remove all extra log output for test comparisons
  132. log.SetOutput(&LogScrubber{Output: &buff})
  133. log.Print(addr)
  134. if buff.String() != "[scrubbed]\n" {
  135. t.Errorf("%q: Got %q, expected %q", addr, buff.String(), "[scrubbed]\n")
  136. }
  137. }
  138. }