utils_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 TestMakeRandomPeriod(t *testing.T) {
  68. min := 1 * time.Nanosecond
  69. max := 10000 * time.Nanosecond
  70. res1, err := MakeRandomPeriod(min, max)
  71. if err != nil {
  72. t.Errorf("MakeRandomPeriod failed: %s", err)
  73. }
  74. if res1 < min {
  75. t.Error("duration should not be less than min")
  76. }
  77. if res1 > max {
  78. t.Error("duration should not be more than max")
  79. }
  80. res2, err := MakeRandomPeriod(min, max)
  81. if err != nil {
  82. t.Errorf("MakeRandomPeriod failed: %s", err)
  83. }
  84. if res1 == res2 {
  85. t.Error("duration should have randomness difference between calls")
  86. }
  87. }
  88. func TestJitter(t *testing.T) {
  89. testCases := []struct {
  90. n int64
  91. factor float64
  92. expectedMin int64
  93. expectedMax int64
  94. }{
  95. {100, 0.1, 90, 110},
  96. {1000, 0.3, 700, 1300},
  97. }
  98. for _, testCase := range testCases {
  99. t.Run(fmt.Sprintf("jitter case: %+v", testCase), func(t *testing.T) {
  100. min := int64(math.MaxInt64)
  101. max := int64(0)
  102. for i := 0; i < 100000; i++ {
  103. x := Jitter(testCase.n, testCase.factor)
  104. if x < min {
  105. min = x
  106. }
  107. if x > max {
  108. max = x
  109. }
  110. }
  111. if min != testCase.expectedMin {
  112. t.Errorf("unexpected minimum jittered value: %d", min)
  113. }
  114. if max != testCase.expectedMax {
  115. t.Errorf("unexpected maximum jittered value: %d", max)
  116. }
  117. })
  118. }
  119. }
  120. func TestCompress(t *testing.T) {
  121. originalData := []byte("test data")
  122. compressedData := Compress(originalData)
  123. decompressedData, err := Decompress(compressedData)
  124. if err != nil {
  125. t.Errorf("Uncompress failed: %s", err)
  126. }
  127. if bytes.Compare(originalData, decompressedData) != 0 {
  128. t.Error("decompressed data doesn't match original data")
  129. }
  130. }
  131. func TestFormatByteCount(t *testing.T) {
  132. testCases := []struct {
  133. n uint64
  134. expectedOutput string
  135. }{
  136. {500, "500B"},
  137. {1024, "1.0K"},
  138. {10000, "9.8K"},
  139. {1024*1024 + 1, "1.0M"},
  140. {100*1024*1024 + 99999, "100.1M"},
  141. }
  142. for _, testCase := range testCases {
  143. t.Run(testCase.expectedOutput, func(t *testing.T) {
  144. output := FormatByteCount(testCase.n)
  145. if output != testCase.expectedOutput {
  146. t.Errorf("unexpected output: %s", output)
  147. }
  148. })
  149. }
  150. }
  151. func TestWeightedCoinFlip(t *testing.T) {
  152. runs := 100000
  153. tolerance := 1000
  154. testCases := []struct {
  155. weight float64
  156. expectedTrues int
  157. }{
  158. {0.333, runs / 3},
  159. {0.5, runs / 2},
  160. {1.0, runs},
  161. {0.0, 0},
  162. }
  163. for _, testCase := range testCases {
  164. t.Run(fmt.Sprintf("%f", testCase.weight), func(t *testing.T) {
  165. trues := 0
  166. for i := 0; i < runs; i++ {
  167. if FlipWeightedCoin(testCase.weight) {
  168. trues++
  169. }
  170. }
  171. min := testCase.expectedTrues - tolerance
  172. if min < 0 {
  173. min = 0
  174. }
  175. max := testCase.expectedTrues + tolerance
  176. if trues < min || trues > max {
  177. t.Errorf("unexpected coin flip outcome: %f %d (+/-%d) %d",
  178. testCase.weight, testCase.expectedTrues, tolerance, trues)
  179. }
  180. })
  181. }
  182. }