utils_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2020, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "os"
  22. "strings"
  23. "testing"
  24. )
  25. func TestStripIPAddresses(t *testing.T) {
  26. testCases := []struct {
  27. description string
  28. input string
  29. expectedOutput string
  30. }{
  31. {
  32. "IPv4 address",
  33. "prefix 192.168.0.1 suffix",
  34. "prefix [redacted] suffix",
  35. },
  36. {
  37. "IPv6 address",
  38. "prefix 2001:0db8:0000:0000:0000:ff00:0042:8329 suffix",
  39. "prefix [redacted] suffix",
  40. },
  41. {
  42. "Remove leading zeros IPv6 address",
  43. "prefix 2001:db8:0:0:0:ff00:42:8329 suffix",
  44. "prefix [redacted] suffix",
  45. },
  46. {
  47. "Omit consecutive zeros sections IPv6 address",
  48. "prefix 2001:db8::ff00:42:8329 suffix",
  49. "prefix [redacted] suffix",
  50. },
  51. {
  52. "IPv4 mapped/translated/embedded address",
  53. "prefix 0::ffff:192.168.0.1, 0::ffff:0:192.168.0.1, 64:ff9b::192.168.0.1 suffix",
  54. "prefix [redacted], [redacted], [redacted] suffix",
  55. },
  56. {
  57. "IPv4 address and port",
  58. "read tcp 127.0.0.1:1025->127.0.0.1:8000: use of closed network connection",
  59. "read tcp [redacted]->[redacted]: use of closed network connection",
  60. },
  61. {
  62. "IPv6 address and port",
  63. "read tcp [2001:db8::ff00:42:8329]:1025->[2001:db8::ff00:42:8329]:8000: use of closed network connection",
  64. "read tcp [redacted]->[redacted]: use of closed network connection",
  65. },
  66. {
  67. "Loopback IPv6 address and invalid port number",
  68. "dial tcp [::1]:88888: network is unreachable",
  69. "dial tcp [redacted]: network is unreachable",
  70. },
  71. {
  72. "Numbers and periods",
  73. "prefix 192. 168. 0. 1 suffix",
  74. "prefix 192. 168. 0. 1 suffix",
  75. },
  76. {
  77. "Hex string and colon",
  78. "prefix 0123456789abcdef: suffix",
  79. "prefix 0123456789abcdef: suffix",
  80. },
  81. {
  82. "Colons",
  83. "prefix :: suffix",
  84. "prefix :: suffix",
  85. },
  86. {
  87. "Notice",
  88. `{"data":{"SSHClientVersion":"SSH-2.0-C","candidateNumber":0,"diagnosticID":"se0XVQ/4","dialPortNumber":"4000","establishedTunnelsCount":0,"isReplay":false,"networkLatencyMultiplier":2.8284780852763953,"networkType":"WIFI","protocol":"OSSH","region":"US","upstream_ossh_padding":7077},"noticeType":"ConnectedServer","timestamp":"2020-12-16T14:07:02.030Z"}`,
  89. `{"data":{"SSHClientVersion":"SSH-2.0-C","candidateNumber":0,"diagnosticID":"se0XVQ/4","dialPortNumber":"4000","establishedTunnelsCount":0,"isReplay":false,"networkLatencyMultiplier":2.8284780852763953,"networkType":"WIFI","protocol":"OSSH","region":"US","upstream_ossh_padding":7077},"noticeType":"ConnectedServer","timestamp":"2020-12-16T14:07:02.030Z"}`,
  90. },
  91. }
  92. for _, testCase := range testCases {
  93. t.Run(testCase.description, func(t *testing.T) {
  94. output := StripIPAddressesString(testCase.input)
  95. if output != testCase.expectedOutput {
  96. t.Errorf("unexpected output: %s", output)
  97. }
  98. })
  99. }
  100. }
  101. func TestStripFilePaths(t *testing.T) {
  102. testCases := []struct {
  103. description string
  104. input string
  105. expectedOutput string
  106. filePaths []string
  107. }{
  108. {
  109. "Absolute path",
  110. "prefix /a suffix",
  111. "prefix [redacted] suffix",
  112. nil,
  113. },
  114. {
  115. "Absolute path with directories",
  116. "prefix /a/b/c/d suffix",
  117. "prefix [redacted] suffix",
  118. nil,
  119. },
  120. {
  121. "Relative path 1",
  122. "prefix ./a/b/c/d suffix",
  123. "prefix [redacted] suffix",
  124. nil,
  125. },
  126. {
  127. "Relative path 2",
  128. "prefix a/b/c/d suffix",
  129. "prefix [redacted] suffix",
  130. nil,
  131. },
  132. {
  133. "Relative path 3",
  134. "prefix ../a/b/c/d/../ suffix",
  135. "prefix [redacted] suffix",
  136. nil,
  137. },
  138. {
  139. "File path with home directory tilde",
  140. "prefix ~/a/b/c/d suffix",
  141. "prefix [redacted] suffix",
  142. nil,
  143. },
  144. {
  145. "Multiple file paths",
  146. "prefix /a/b c/d suffix",
  147. "prefix [redacted] [redacted] suffix",
  148. nil,
  149. },
  150. {
  151. "File path with percent encoded spaces",
  152. "prefix /a/b%20c/d suffix",
  153. "prefix [redacted] suffix",
  154. nil,
  155. },
  156. {
  157. "Strip file paths unhandled case",
  158. "prefix /a/file name with spaces /e/f/g/ suffix",
  159. "prefix [redacted] name with spaces [redacted] suffix",
  160. nil,
  161. },
  162. {
  163. "Strip file paths catch unhandled case with provided path",
  164. "prefix /a/file name with spaces /e/f/g/ suffix",
  165. "prefix [redacted] [redacted] suffix",
  166. []string{"/a/file name with spaces"},
  167. },
  168. }
  169. for _, testCase := range testCases {
  170. t.Run(testCase.description, func(t *testing.T) {
  171. // For convenience replace separators in input string and
  172. // file paths with the OS-specific path separator here instead
  173. // constructing the test input strings with os.PathSeparator.
  174. input := strings.ReplaceAll(testCase.input, "/", string(os.PathSeparator))
  175. var filePaths []string
  176. for _, filePath := range testCase.filePaths {
  177. filePaths = append(filePaths, strings.ReplaceAll(filePath, "/", string(os.PathSeparator)))
  178. }
  179. output := StripFilePaths(input, filePaths...)
  180. if output != testCase.expectedOutput {
  181. t.Errorf("unexpected output: %s", output)
  182. }
  183. })
  184. }
  185. }