utils_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "context"
  23. "encoding/json"
  24. "net/url"
  25. "reflect"
  26. "strings"
  27. "testing"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  30. )
  31. func TestGetStringSlice(t *testing.T) {
  32. originalSlice := []string{"a", "b", "c"}
  33. j, err := json.Marshal(originalSlice)
  34. if err != nil {
  35. t.Errorf("json.Marshal failed: %s", err)
  36. }
  37. var value interface{}
  38. err = json.Unmarshal(j, &value)
  39. if err != nil {
  40. t.Errorf("json.Unmarshal failed: %s", err)
  41. }
  42. newSlice, ok := GetStringSlice(value)
  43. if !ok {
  44. t.Errorf("GetStringSlice failed")
  45. }
  46. if !reflect.DeepEqual(originalSlice, newSlice) {
  47. t.Errorf("unexpected GetStringSlice output")
  48. }
  49. }
  50. func TestCompress(t *testing.T) {
  51. originalData := []byte("test data")
  52. compressedData := Compress(originalData)
  53. decompressedData, err := Decompress(compressedData)
  54. if err != nil {
  55. t.Errorf("Uncompress failed: %s", err)
  56. }
  57. if !bytes.Equal(originalData, decompressedData) {
  58. t.Error("decompressed data doesn't match original data")
  59. }
  60. }
  61. func TestFormatByteCount(t *testing.T) {
  62. testCases := []struct {
  63. n uint64
  64. expectedOutput string
  65. }{
  66. {500, "500B"},
  67. {1024, "1.0K"},
  68. {10000, "9.8K"},
  69. {1024*1024 + 1, "1.0M"},
  70. {100*1024*1024 + 99999, "100.1M"},
  71. }
  72. for _, testCase := range testCases {
  73. t.Run(testCase.expectedOutput, func(t *testing.T) {
  74. output := FormatByteCount(testCase.n)
  75. if output != testCase.expectedOutput {
  76. t.Errorf("unexpected output: %s", output)
  77. }
  78. })
  79. }
  80. }
  81. func TestSafeParseURL(t *testing.T) {
  82. invalidURL := "https://invalid url"
  83. _, err := url.Parse(invalidURL)
  84. if err == nil {
  85. t.Error("unexpected parse success")
  86. }
  87. if strings.Index(err.Error(), invalidURL) == -1 {
  88. t.Error("URL not in error string")
  89. }
  90. _, err = SafeParseURL(invalidURL)
  91. if err == nil {
  92. t.Error("unexpected parse success")
  93. }
  94. if strings.Index(err.Error(), invalidURL) != -1 {
  95. t.Error("URL in error string")
  96. }
  97. }
  98. func TestSafeParseRequestURI(t *testing.T) {
  99. invalidURL := "https://invalid url"
  100. _, err := url.ParseRequestURI(invalidURL)
  101. if err == nil {
  102. t.Error("unexpected parse success")
  103. }
  104. if strings.Index(err.Error(), invalidURL) == -1 {
  105. t.Error("URL not in error string")
  106. }
  107. _, err = SafeParseRequestURI(invalidURL)
  108. if err == nil {
  109. t.Error("unexpected parse success")
  110. }
  111. if strings.Index(err.Error(), invalidURL) != -1 {
  112. t.Error("URL in error string")
  113. }
  114. }
  115. func TestSleepWithContext(t *testing.T) {
  116. start := time.Now()
  117. SleepWithContext(context.Background(), 100*time.Millisecond)
  118. duration := time.Since(start)
  119. // Allows for 100-109ms actual elapsed time.
  120. if duration/time.Millisecond/10 != 10 {
  121. t.Errorf("unexpected duration: %v", duration)
  122. }
  123. start = time.Now()
  124. ctx, cancelFunc := context.WithTimeout(context.Background(), 100*time.Millisecond)
  125. defer cancelFunc()
  126. SleepWithContext(ctx, 50*time.Millisecond)
  127. duration = time.Since(start)
  128. if duration/time.Millisecond/10 != 5 {
  129. t.Errorf("unexpected duration: %v", duration)
  130. }
  131. }
  132. func TestToRandomCasing(t *testing.T) {
  133. s := "test.to.random.ascii.casing.aaaa.bbbb.c" // 32 Unicode letters
  134. seed, err := prng.NewSeed()
  135. if err != nil {
  136. t.Errorf("NewPRNG failed: %s", err)
  137. }
  138. randomized := ToRandomASCIICasing(s, seed)
  139. // Note: there's a (1/2)^32 chance that the randomized string has the same
  140. // casing as the input string.
  141. if strings.Compare(s, randomized) == 0 {
  142. t.Errorf("expected random casing")
  143. }
  144. if strings.Compare(strings.ToLower(s), strings.ToLower(randomized)) != 0 {
  145. t.Errorf("expected strings to be identical minus casing")
  146. }
  147. replaySameSeed := ToRandomASCIICasing(s, seed)
  148. if strings.Compare(randomized, replaySameSeed) != 0 {
  149. t.Errorf("expected randomized string with same seed to be identical")
  150. }
  151. }