utils_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2014, 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. "bytes"
  22. "encoding/json"
  23. "fmt"
  24. "math"
  25. "reflect"
  26. "testing"
  27. "time"
  28. )
  29. func TestGetStringSlice(t *testing.T) {
  30. originalSlice := []string{"a", "b", "c"}
  31. j, err := json.Marshal(originalSlice)
  32. if err != nil {
  33. t.Errorf("json.Marshal failed: %s", err)
  34. }
  35. var value interface{}
  36. err = json.Unmarshal(j, &value)
  37. if err != nil {
  38. t.Errorf("json.Unmarshal failed: %s", err)
  39. }
  40. newSlice, ok := GetStringSlice(value)
  41. if !ok {
  42. t.Errorf("GetStringSlice failed")
  43. }
  44. if !reflect.DeepEqual(originalSlice, newSlice) {
  45. t.Errorf("unexpected GetStringSlice output")
  46. }
  47. }
  48. func TestMakeSecureRandomPerm(t *testing.T) {
  49. for n := 0; n < 1000; n++ {
  50. perm, err := MakeSecureRandomPerm(n)
  51. if err != nil {
  52. t.Errorf("MakeSecureRandomPerm failed: %s", err)
  53. }
  54. if len(perm) != n {
  55. t.Error("unexpected permutation size")
  56. }
  57. sum := 0
  58. for i := 0; i < n; i++ {
  59. sum += perm[i]
  60. }
  61. expectedSum := (n * (n - 1)) / 2
  62. if sum != expectedSum {
  63. t.Error("unexpected permutation")
  64. }
  65. }
  66. }
  67. func TestMakeSecureRandomRange(t *testing.T) {
  68. min := 1
  69. max := 19
  70. var gotMin, gotMax bool
  71. for n := 0; n < 1000; n++ {
  72. i, err := MakeSecureRandomRange(min, max)
  73. if err != nil {
  74. t.Errorf("MakeSecureRandomRange failed: %s", err)
  75. }
  76. if i < min || i > max {
  77. t.Error("out of range")
  78. }
  79. if i == min {
  80. gotMin = true
  81. }
  82. if i == max {
  83. gotMax = true
  84. }
  85. }
  86. if !gotMin {
  87. t.Error("missing min")
  88. }
  89. if !gotMax {
  90. t.Error("missing max")
  91. }
  92. }
  93. func TestMakeSecureRandomPeriod(t *testing.T) {
  94. min := 1 * time.Nanosecond
  95. max := 10000 * time.Nanosecond
  96. different := 0
  97. for n := 0; n < 1000; n++ {
  98. res1, err := MakeSecureRandomPeriod(min, max)
  99. if err != nil {
  100. t.Errorf("MakeSecureRandomPeriod failed: %s", err)
  101. }
  102. if res1 < min {
  103. t.Error("duration should not be less than min")
  104. }
  105. if res1 > max {
  106. t.Error("duration should not be more than max")
  107. }
  108. res2, err := MakeSecureRandomPeriod(min, max)
  109. if err != nil {
  110. t.Errorf("MakeSecureRandomPeriod failed: %s", err)
  111. }
  112. if res1 != res2 {
  113. different += 1
  114. }
  115. }
  116. // res1 and res2 should be different most of the time, but it's possible
  117. // to get the same result twice in a row.
  118. if different < 900 {
  119. t.Error("duration insufficiently random")
  120. }
  121. }
  122. func TestJitter(t *testing.T) {
  123. testCases := []struct {
  124. n int64
  125. factor float64
  126. expectedMin int64
  127. expectedMax int64
  128. }{
  129. {100, 0.1, 90, 110},
  130. {1000, 0.3, 700, 1300},
  131. }
  132. for _, testCase := range testCases {
  133. t.Run(fmt.Sprintf("jitter case: %+v", testCase), func(t *testing.T) {
  134. min := int64(math.MaxInt64)
  135. max := int64(0)
  136. for i := 0; i < 100000; i++ {
  137. x := Jitter(testCase.n, testCase.factor)
  138. if x < min {
  139. min = x
  140. }
  141. if x > max {
  142. max = x
  143. }
  144. }
  145. if min != testCase.expectedMin {
  146. t.Errorf("unexpected minimum jittered value: %d", min)
  147. }
  148. if max != testCase.expectedMax {
  149. t.Errorf("unexpected maximum jittered value: %d", max)
  150. }
  151. })
  152. }
  153. }
  154. func TestCompress(t *testing.T) {
  155. originalData := []byte("test data")
  156. compressedData := Compress(originalData)
  157. decompressedData, err := Decompress(compressedData)
  158. if err != nil {
  159. t.Errorf("Uncompress failed: %s", err)
  160. }
  161. if bytes.Compare(originalData, decompressedData) != 0 {
  162. t.Error("decompressed data doesn't match original data")
  163. }
  164. }
  165. func TestFormatByteCount(t *testing.T) {
  166. testCases := []struct {
  167. n uint64
  168. expectedOutput string
  169. }{
  170. {500, "500B"},
  171. {1024, "1.0K"},
  172. {10000, "9.8K"},
  173. {1024*1024 + 1, "1.0M"},
  174. {100*1024*1024 + 99999, "100.1M"},
  175. }
  176. for _, testCase := range testCases {
  177. t.Run(testCase.expectedOutput, func(t *testing.T) {
  178. output := FormatByteCount(testCase.n)
  179. if output != testCase.expectedOutput {
  180. t.Errorf("unexpected output: %s", output)
  181. }
  182. })
  183. }
  184. }
  185. func TestWeightedCoinFlip(t *testing.T) {
  186. runs := 100000
  187. tolerance := 1000
  188. testCases := []struct {
  189. weight float64
  190. expectedTrues int
  191. }{
  192. {0.333, runs / 3},
  193. {0.5, runs / 2},
  194. {1.0, runs},
  195. {0.0, 0},
  196. }
  197. for _, testCase := range testCases {
  198. t.Run(fmt.Sprintf("%f", testCase.weight), func(t *testing.T) {
  199. trues := 0
  200. for i := 0; i < runs; i++ {
  201. if FlipWeightedCoin(testCase.weight) {
  202. trues++
  203. }
  204. }
  205. min := testCase.expectedTrues - tolerance
  206. if min < 0 {
  207. min = 0
  208. }
  209. max := testCase.expectedTrues + tolerance
  210. if trues < min || trues > max {
  211. t.Errorf("unexpected coin flip outcome: %f %d (+/-%d) %d",
  212. testCase.weight, testCase.expectedTrues, tolerance, trues)
  213. }
  214. })
  215. }
  216. }