utils_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. )
  30. func TestGetStringSlice(t *testing.T) {
  31. originalSlice := []string{"a", "b", "c"}
  32. j, err := json.Marshal(originalSlice)
  33. if err != nil {
  34. t.Errorf("json.Marshal failed: %s", err)
  35. }
  36. var value interface{}
  37. err = json.Unmarshal(j, &value)
  38. if err != nil {
  39. t.Errorf("json.Unmarshal failed: %s", err)
  40. }
  41. newSlice, ok := GetStringSlice(value)
  42. if !ok {
  43. t.Errorf("GetStringSlice failed")
  44. }
  45. if !reflect.DeepEqual(originalSlice, newSlice) {
  46. t.Errorf("unexpected GetStringSlice output")
  47. }
  48. }
  49. func TestCompress(t *testing.T) {
  50. originalData := []byte("test data")
  51. compressedData := Compress(originalData)
  52. decompressedData, err := Decompress(compressedData)
  53. if err != nil {
  54. t.Errorf("Uncompress failed: %s", err)
  55. }
  56. if !bytes.Equal(originalData, decompressedData) {
  57. t.Error("decompressed data doesn't match original data")
  58. }
  59. }
  60. func TestFormatByteCount(t *testing.T) {
  61. testCases := []struct {
  62. n uint64
  63. expectedOutput string
  64. }{
  65. {500, "500B"},
  66. {1024, "1.0K"},
  67. {10000, "9.8K"},
  68. {1024*1024 + 1, "1.0M"},
  69. {100*1024*1024 + 99999, "100.1M"},
  70. }
  71. for _, testCase := range testCases {
  72. t.Run(testCase.expectedOutput, func(t *testing.T) {
  73. output := FormatByteCount(testCase.n)
  74. if output != testCase.expectedOutput {
  75. t.Errorf("unexpected output: %s", output)
  76. }
  77. })
  78. }
  79. }
  80. func TestSafeParseURL(t *testing.T) {
  81. invalidURL := "https://invalid url"
  82. _, err := url.Parse(invalidURL)
  83. if err == nil {
  84. t.Error("unexpected parse success")
  85. }
  86. if strings.Index(err.Error(), invalidURL) == -1 {
  87. t.Error("URL not in error string")
  88. }
  89. _, err = SafeParseURL(invalidURL)
  90. if err == nil {
  91. t.Error("unexpected parse success")
  92. }
  93. if strings.Index(err.Error(), invalidURL) != -1 {
  94. t.Error("URL in error string")
  95. }
  96. }
  97. func TestSafeParseRequestURI(t *testing.T) {
  98. invalidURL := "https://invalid url"
  99. _, err := url.ParseRequestURI(invalidURL)
  100. if err == nil {
  101. t.Error("unexpected parse success")
  102. }
  103. if strings.Index(err.Error(), invalidURL) == -1 {
  104. t.Error("URL not in error string")
  105. }
  106. _, err = SafeParseRequestURI(invalidURL)
  107. if err == nil {
  108. t.Error("unexpected parse success")
  109. }
  110. if strings.Index(err.Error(), invalidURL) != -1 {
  111. t.Error("URL in error string")
  112. }
  113. }
  114. func TestSleepWithContext(t *testing.T) {
  115. start := time.Now()
  116. SleepWithContext(context.Background(), 100*time.Millisecond)
  117. duration := time.Since(start)
  118. // Allows for 100-109ms actual elapsed time.
  119. if duration/time.Millisecond/10 != 10 {
  120. t.Errorf("unexpected duration: %v", duration)
  121. }
  122. start = time.Now()
  123. ctx, cancelFunc := context.WithTimeout(context.Background(), 100*time.Millisecond)
  124. defer cancelFunc()
  125. SleepWithContext(ctx, 50*time.Millisecond)
  126. duration = time.Since(start)
  127. if duration/time.Millisecond/10 != 5 {
  128. t.Errorf("unexpected duration: %v", duration)
  129. }
  130. }