utils_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "testing"
  23. "time"
  24. )
  25. func TestMakeRandomPeriod(t *testing.T) {
  26. min := 1 * time.Nanosecond
  27. max := 10000 * time.Nanosecond
  28. res1, err := MakeRandomPeriod(min, max)
  29. if err != nil {
  30. t.Error("MakeRandomPeriod failed: %s", err)
  31. }
  32. if res1 < min {
  33. t.Error("duration should not be less than min")
  34. }
  35. if res1 > max {
  36. t.Error("duration should not be more than max")
  37. }
  38. res2, err := MakeRandomPeriod(min, max)
  39. if err != nil {
  40. t.Error("MakeRandomPeriod failed: %s", err)
  41. }
  42. if res1 == res2 {
  43. t.Error("duration should have randomness difference between calls")
  44. }
  45. }
  46. func TestCompress(t *testing.T) {
  47. originalData := []byte("test data")
  48. compressedData := Compress(originalData)
  49. decompressedData, err := Decompress(compressedData)
  50. if err != nil {
  51. t.Error("Uncompress failed: %s", err)
  52. }
  53. if bytes.Compare(originalData, decompressedData) != 0 {
  54. t.Error("decompressed data doesn't match original data")
  55. }
  56. }