utils_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. for _, compression := range []int32{CompressionNone, CompressionZlib} {
  52. originalData := []byte("test data")
  53. compressedData, err := Compress(compression, originalData)
  54. if err != nil {
  55. t.Errorf("Compress failed: %s", err)
  56. }
  57. decompressedData, err := Decompress(compression, compressedData)
  58. if err != nil {
  59. t.Errorf("Decompress failed: %s", err)
  60. }
  61. if !bytes.Equal(originalData, decompressedData) {
  62. t.Error("decompressed data doesn't match original data")
  63. }
  64. }
  65. }
  66. func TestFormatByteCount(t *testing.T) {
  67. testCases := []struct {
  68. n uint64
  69. expectedOutput string
  70. }{
  71. {500, "500B"},
  72. {1024, "1.0K"},
  73. {10000, "9.8K"},
  74. {1024*1024 + 1, "1.0M"},
  75. {100*1024*1024 + 99999, "100.1M"},
  76. }
  77. for _, testCase := range testCases {
  78. t.Run(testCase.expectedOutput, func(t *testing.T) {
  79. output := FormatByteCount(testCase.n)
  80. if output != testCase.expectedOutput {
  81. t.Errorf("unexpected output: %s", output)
  82. }
  83. })
  84. }
  85. }
  86. func TestSafeParseURL(t *testing.T) {
  87. invalidURL := "https://invalid url"
  88. _, err := url.Parse(invalidURL)
  89. if err == nil {
  90. t.Error("unexpected parse success")
  91. }
  92. if strings.Index(err.Error(), invalidURL) == -1 {
  93. t.Error("URL not in error string")
  94. }
  95. _, err = SafeParseURL(invalidURL)
  96. if err == nil {
  97. t.Error("unexpected parse success")
  98. }
  99. if strings.Index(err.Error(), invalidURL) != -1 {
  100. t.Error("URL in error string")
  101. }
  102. }
  103. func TestSafeParseRequestURI(t *testing.T) {
  104. invalidURL := "https://invalid url"
  105. _, err := url.ParseRequestURI(invalidURL)
  106. if err == nil {
  107. t.Error("unexpected parse success")
  108. }
  109. if strings.Index(err.Error(), invalidURL) == -1 {
  110. t.Error("URL not in error string")
  111. }
  112. _, err = SafeParseRequestURI(invalidURL)
  113. if err == nil {
  114. t.Error("unexpected parse success")
  115. }
  116. if strings.Index(err.Error(), invalidURL) != -1 {
  117. t.Error("URL in error string")
  118. }
  119. }
  120. func TestSleepWithContext(t *testing.T) {
  121. start := time.Now()
  122. SleepWithContext(context.Background(), 100*time.Millisecond)
  123. duration := time.Since(start)
  124. // Allows for 100-109ms actual elapsed time.
  125. if duration/time.Millisecond/10 != 10 {
  126. t.Errorf("unexpected duration: %v", duration)
  127. }
  128. start = time.Now()
  129. ctx, cancelFunc := context.WithTimeout(context.Background(), 100*time.Millisecond)
  130. defer cancelFunc()
  131. SleepWithContext(ctx, 50*time.Millisecond)
  132. duration = time.Since(start)
  133. if duration/time.Millisecond/10 != 5 {
  134. t.Errorf("unexpected duration: %v", duration)
  135. }
  136. }
  137. func TestToRandomCasing(t *testing.T) {
  138. s := "test.to.random.ascii.casing.aaaa.bbbb.c" // 32 Unicode letters
  139. seed, err := prng.NewSeed()
  140. if err != nil {
  141. t.Errorf("NewPRNG failed: %s", err)
  142. }
  143. randomized := ToRandomASCIICasing(s, seed)
  144. // Note: there's a (1/2)^32 chance that the randomized string has the same
  145. // casing as the input string.
  146. if strings.Compare(s, randomized) == 0 {
  147. t.Errorf("expected random casing")
  148. }
  149. if strings.Compare(strings.ToLower(s), strings.ToLower(randomized)) != 0 {
  150. t.Errorf("expected strings to be identical minus casing")
  151. }
  152. replaySameSeed := ToRandomASCIICasing(s, seed)
  153. if strings.Compare(randomized, replaySameSeed) != 0 {
  154. t.Errorf("expected randomized string with same seed to be identical")
  155. }
  156. }