utils_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "testing"
  22. )
  23. func TestStripIPAddresses(t *testing.T) {
  24. testCases := []struct {
  25. description string
  26. input string
  27. expectedOutput string
  28. }{
  29. {
  30. "IPv4 address",
  31. "prefix 192.168.0.1 suffix",
  32. "prefix [redacted] suffix",
  33. },
  34. {
  35. "IPv6 address",
  36. "prefix 2001:0db8:0000:0000:0000:ff00:0042:8329 suffix",
  37. "prefix [redacted] suffix",
  38. },
  39. {
  40. "Remove leading zeros IPv6 address",
  41. "prefix 2001:db8:0:0:0:ff00:42:8329 suffix",
  42. "prefix [redacted] suffix",
  43. },
  44. {
  45. "Omit consecutive zeros sections IPv6 address",
  46. "prefix 2001:db8::ff00:42:8329 suffix",
  47. "prefix [redacted] suffix",
  48. },
  49. {
  50. "IPv4 mapped/translated/embedded address",
  51. "prefix 0::ffff:192.168.0.1, 0::ffff:0:192.168.0.1, 64:ff9b::192.168.0.1 suffix",
  52. "prefix [redacted], [redacted], [redacted] suffix",
  53. },
  54. {
  55. "IPv4 address and port",
  56. "read tcp 127.0.0.1:1025->127.0.0.1:8000: use of closed network connection",
  57. "read tcp [redacted]->[redacted]: use of closed network connection",
  58. },
  59. {
  60. "IPv6 address and port",
  61. "read tcp [2001:db8::ff00:42:8329]:1025->[2001:db8::ff00:42:8329]:8000: use of closed network connection",
  62. "read tcp [redacted]->[redacted]: use of closed network connection",
  63. },
  64. {
  65. "Loopback IPv6 address and invalid port number",
  66. "dial tcp [::1]:88888: network is unreachable",
  67. "dial tcp [redacted]: network is unreachable",
  68. },
  69. {
  70. "Numbers and periods",
  71. "prefix 192. 168. 0. 1 suffix",
  72. "prefix 192. 168. 0. 1 suffix",
  73. },
  74. {
  75. "Hex string and colon",
  76. "prefix 0123456789abcdef: suffix",
  77. "prefix 0123456789abcdef: suffix",
  78. },
  79. {
  80. "Colons",
  81. "prefix :: suffix",
  82. "prefix :: suffix",
  83. },
  84. {
  85. "Notice",
  86. `{"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"}`,
  87. `{"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"}`,
  88. },
  89. }
  90. for _, testCase := range testCases {
  91. t.Run(testCase.description, func(t *testing.T) {
  92. output := StripIPAddressesString(testCase.input)
  93. if output != testCase.expectedOutput {
  94. t.Errorf("unexpected output: %s", output)
  95. }
  96. })
  97. }
  98. }