portlist_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2021, 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 common
  20. import (
  21. "encoding/json"
  22. "strings"
  23. "testing"
  24. "unicode"
  25. )
  26. func TestPortList(t *testing.T) {
  27. var p *PortList
  28. err := json.Unmarshal([]byte("[1.5]"), &p)
  29. if err == nil {
  30. t.Fatalf("unexpected parse of float port number")
  31. }
  32. err = json.Unmarshal([]byte("[-1]"), &p)
  33. if err == nil {
  34. t.Fatalf("unexpected parse of negative port number")
  35. }
  36. err = json.Unmarshal([]byte("[0]"), &p)
  37. if err == nil {
  38. t.Fatalf("unexpected parse of invalid port number")
  39. }
  40. err = json.Unmarshal([]byte("[65536]"), &p)
  41. if err == nil {
  42. t.Fatalf("unexpected parse of invalid port number")
  43. }
  44. err = json.Unmarshal([]byte("[[2,1]]"), &p)
  45. if err == nil {
  46. t.Fatalf("unexpected parse of invalid port range")
  47. }
  48. p = nil
  49. if p.Lookup(1) != false {
  50. t.Fatalf("unexpected nil PortList Lookup result")
  51. }
  52. if !p.IsEmpty() {
  53. t.Fatalf("unexpected nil PortList IsEmpty result")
  54. }
  55. err = json.Unmarshal([]byte("[]"), &p)
  56. if err != nil {
  57. t.Fatalf("Unmarshal failed: %v", err)
  58. }
  59. if !p.IsEmpty() {
  60. t.Fatalf("unexpected IsEmpty result")
  61. }
  62. err = json.Unmarshal([]byte("[1]"), &p)
  63. if err != nil {
  64. t.Fatalf("Unmarshal failed: %v", err)
  65. }
  66. if p.IsEmpty() {
  67. t.Fatalf("unexpected IsEmpty result")
  68. }
  69. s := struct {
  70. List1 *PortList
  71. List2 *PortList
  72. }{}
  73. jsonString := `
  74. {
  75. "List1" : [1,2,[10,20],100,[1000,2000]],
  76. "List2" : [3,4,5,[300,400],1000,2000,[3000,3996],3997,3998,3999,4000]
  77. }
  78. `
  79. err = json.Unmarshal([]byte(jsonString), &s)
  80. if err != nil {
  81. t.Fatalf("Unmarshal failed: %v", err)
  82. }
  83. // Marshal and re-Unmarshal to exercise PortList.MarshalJSON.
  84. jsonBytes, err := json.Marshal(s)
  85. if err != nil {
  86. t.Fatalf("Marshal failed: %v", err)
  87. }
  88. strip := func(s string) string {
  89. return strings.Map(func(r rune) rune {
  90. if unicode.IsSpace(r) {
  91. return -1
  92. }
  93. return r
  94. }, s)
  95. }
  96. if strip(jsonString) != strip(string(jsonBytes)) {
  97. t.Fatalf("unexpected JSON encoding")
  98. }
  99. err = json.Unmarshal(jsonBytes, &s)
  100. if err != nil {
  101. t.Fatalf("Unmarshal failed: %v", err)
  102. }
  103. s.List1.OptimizeLookups()
  104. if s.List1.lookup != nil {
  105. t.Fatalf("unexpected lookup initialization")
  106. }
  107. s.List2.OptimizeLookups()
  108. if s.List2.lookup == nil {
  109. t.Fatalf("unexpected lookup initialization")
  110. }
  111. for port := 0; port < 65536; port++ {
  112. lookup1 := s.List1.Lookup(port)
  113. expected1 := port == 1 ||
  114. port == 2 ||
  115. (port >= 10 && port <= 20) ||
  116. port == 100 ||
  117. (port >= 1000 && port <= 2000)
  118. if lookup1 != expected1 {
  119. t.Fatalf("unexpected port lookup: %d %v", port, lookup1)
  120. }
  121. lookup2 := s.List2.Lookup(port)
  122. expected2 := port == 3 ||
  123. port == 4 ||
  124. port == 5 ||
  125. (port >= 300 && port <= 400) ||
  126. port == 1000 || port == 2000 ||
  127. (port >= 3000 && port <= 4000)
  128. if lookup2 != expected2 {
  129. t.Fatalf("unexpected port lookup: %d %v", port, lookup2)
  130. }
  131. }
  132. }
  133. func BenchmarkPortListLinear(b *testing.B) {
  134. s := struct {
  135. List PortList
  136. }{}
  137. jsonStruct := `
  138. {
  139. "List" : [1,2,3,4,5,6,7,8,9,[10,20]]
  140. }
  141. `
  142. err := json.Unmarshal([]byte(jsonStruct), &s)
  143. if err != nil {
  144. b.Fatalf("Unmarshal failed: %v", err)
  145. }
  146. s.List.OptimizeLookups()
  147. if s.List.lookup != nil {
  148. b.Fatalf("unexpected lookup initialization")
  149. }
  150. b.ResetTimer()
  151. for i := 0; i < b.N; i++ {
  152. for port := 0; port < 65536; port++ {
  153. s.List.Lookup(port)
  154. }
  155. }
  156. }
  157. func BenchmarkPortListMap(b *testing.B) {
  158. s := struct {
  159. List PortList
  160. }{}
  161. jsonStruct := `
  162. {
  163. "List" : [1,2,3,4,5,6,7,8,9,10,[11,20]]
  164. }
  165. `
  166. err := json.Unmarshal([]byte(jsonStruct), &s)
  167. if err != nil {
  168. b.Fatalf("Unmarshal failed: %v", err)
  169. }
  170. s.List.OptimizeLookups()
  171. if s.List.lookup == nil {
  172. b.Fatalf("unexpected lookup initialization")
  173. }
  174. b.ResetTimer()
  175. for i := 0; i < b.N; i++ {
  176. for port := 0; port < 65536; port++ {
  177. s.List.Lookup(port)
  178. }
  179. }
  180. }