utils_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. for n := 0; n < 1000; n++ {
  97. res1, err := MakeSecureRandomPeriod(min, max)
  98. if err != nil {
  99. t.Errorf("MakeSecureRandomPeriod failed: %s", err)
  100. }
  101. if res1 < min {
  102. t.Error("duration should not be less than min")
  103. }
  104. if res1 > max {
  105. t.Error("duration should not be more than max")
  106. }
  107. res2, err := MakeSecureRandomPeriod(min, max)
  108. if err != nil {
  109. t.Errorf("MakeSecureRandomPeriod failed: %s", err)
  110. }
  111. if res1 == res2 {
  112. t.Error("duration should have randomness difference between calls")
  113. }
  114. }
  115. }
  116. func TestJitter(t *testing.T) {
  117. testCases := []struct {
  118. n int64
  119. factor float64
  120. expectedMin int64
  121. expectedMax int64
  122. }{
  123. {100, 0.1, 90, 110},
  124. {1000, 0.3, 700, 1300},
  125. }
  126. for _, testCase := range testCases {
  127. t.Run(fmt.Sprintf("jitter case: %+v", testCase), func(t *testing.T) {
  128. min := int64(math.MaxInt64)
  129. max := int64(0)
  130. for i := 0; i < 100000; i++ {
  131. x := Jitter(testCase.n, testCase.factor)
  132. if x < min {
  133. min = x
  134. }
  135. if x > max {
  136. max = x
  137. }
  138. }
  139. if min != testCase.expectedMin {
  140. t.Errorf("unexpected minimum jittered value: %d", min)
  141. }
  142. if max != testCase.expectedMax {
  143. t.Errorf("unexpected maximum jittered value: %d", max)
  144. }
  145. })
  146. }
  147. }
  148. func TestCompress(t *testing.T) {
  149. originalData := []byte("test data")
  150. compressedData := Compress(originalData)
  151. decompressedData, err := Decompress(compressedData)
  152. if err != nil {
  153. t.Errorf("Uncompress failed: %s", err)
  154. }
  155. if bytes.Compare(originalData, decompressedData) != 0 {
  156. t.Error("decompressed data doesn't match original data")
  157. }
  158. }
  159. func TestFormatByteCount(t *testing.T) {
  160. testCases := []struct {
  161. n uint64
  162. expectedOutput string
  163. }{
  164. {500, "500B"},
  165. {1024, "1.0K"},
  166. {10000, "9.8K"},
  167. {1024*1024 + 1, "1.0M"},
  168. {100*1024*1024 + 99999, "100.1M"},
  169. }
  170. for _, testCase := range testCases {
  171. t.Run(testCase.expectedOutput, func(t *testing.T) {
  172. output := FormatByteCount(testCase.n)
  173. if output != testCase.expectedOutput {
  174. t.Errorf("unexpected output: %s", output)
  175. }
  176. })
  177. }
  178. }
  179. func TestWeightedCoinFlip(t *testing.T) {
  180. runs := 100000
  181. tolerance := 1000
  182. testCases := []struct {
  183. weight float64
  184. expectedTrues int
  185. }{
  186. {0.333, runs / 3},
  187. {0.5, runs / 2},
  188. {1.0, runs},
  189. {0.0, 0},
  190. }
  191. for _, testCase := range testCases {
  192. t.Run(fmt.Sprintf("%f", testCase.weight), func(t *testing.T) {
  193. trues := 0
  194. for i := 0; i < runs; i++ {
  195. if FlipWeightedCoin(testCase.weight) {
  196. trues++
  197. }
  198. }
  199. min := testCase.expectedTrues - tolerance
  200. if min < 0 {
  201. min = 0
  202. }
  203. max := testCase.expectedTrues + tolerance
  204. if trues < min || trues > max {
  205. t.Errorf("unexpected coin flip outcome: %f %d (+/-%d) %d",
  206. testCase.weight, testCase.expectedTrues, tolerance, trues)
  207. }
  208. })
  209. }
  210. }