utils_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "fmt"
  23. "math"
  24. "testing"
  25. "time"
  26. )
  27. func TestMakeRandomPeriod(t *testing.T) {
  28. min := 1 * time.Nanosecond
  29. max := 10000 * time.Nanosecond
  30. res1, err := MakeRandomPeriod(min, max)
  31. if err != nil {
  32. t.Error("MakeRandomPeriod failed: %s", err)
  33. }
  34. if res1 < min {
  35. t.Error("duration should not be less than min")
  36. }
  37. if res1 > max {
  38. t.Error("duration should not be more than max")
  39. }
  40. res2, err := MakeRandomPeriod(min, max)
  41. if err != nil {
  42. t.Error("MakeRandomPeriod failed: %s", err)
  43. }
  44. if res1 == res2 {
  45. t.Error("duration should have randomness difference between calls")
  46. }
  47. }
  48. func TestJitter(t *testing.T) {
  49. testCases := []struct {
  50. n int64
  51. factor float64
  52. expectedMin int64
  53. expectedMax int64
  54. }{
  55. {100, 0.1, 90, 110},
  56. {1000, 0.3, 700, 1300},
  57. }
  58. for _, testCase := range testCases {
  59. t.Run(fmt.Sprintf("jitter case: %+v", testCase), func(t *testing.T) {
  60. min := int64(math.MaxInt64)
  61. max := int64(0)
  62. for i := 0; i < 100000; i++ {
  63. x := Jitter(testCase.n, testCase.factor)
  64. if x < min {
  65. min = x
  66. }
  67. if x > max {
  68. max = x
  69. }
  70. }
  71. if min != testCase.expectedMin {
  72. t.Errorf("unexpected minimum jittered value: %d", min)
  73. }
  74. if max != testCase.expectedMax {
  75. t.Errorf("unexpected maximum jittered value: %d", max)
  76. }
  77. })
  78. }
  79. }
  80. func TestCompress(t *testing.T) {
  81. originalData := []byte("test data")
  82. compressedData := Compress(originalData)
  83. decompressedData, err := Decompress(compressedData)
  84. if err != nil {
  85. t.Error("Uncompress failed: %s", err)
  86. }
  87. if bytes.Compare(originalData, decompressedData) != 0 {
  88. t.Error("decompressed data doesn't match original data")
  89. }
  90. }