utils_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "encoding/json"
  23. "net/url"
  24. "reflect"
  25. "strings"
  26. "testing"
  27. )
  28. func TestGetStringSlice(t *testing.T) {
  29. originalSlice := []string{"a", "b", "c"}
  30. j, err := json.Marshal(originalSlice)
  31. if err != nil {
  32. t.Errorf("json.Marshal failed: %s", err)
  33. }
  34. var value interface{}
  35. err = json.Unmarshal(j, &value)
  36. if err != nil {
  37. t.Errorf("json.Unmarshal failed: %s", err)
  38. }
  39. newSlice, ok := GetStringSlice(value)
  40. if !ok {
  41. t.Errorf("GetStringSlice failed")
  42. }
  43. if !reflect.DeepEqual(originalSlice, newSlice) {
  44. t.Errorf("unexpected GetStringSlice output")
  45. }
  46. }
  47. func TestCompress(t *testing.T) {
  48. originalData := []byte("test data")
  49. compressedData := Compress(originalData)
  50. decompressedData, err := Decompress(compressedData)
  51. if err != nil {
  52. t.Errorf("Uncompress failed: %s", err)
  53. }
  54. if !bytes.Equal(originalData, decompressedData) {
  55. t.Error("decompressed data doesn't match original data")
  56. }
  57. }
  58. func TestFormatByteCount(t *testing.T) {
  59. testCases := []struct {
  60. n uint64
  61. expectedOutput string
  62. }{
  63. {500, "500B"},
  64. {1024, "1.0K"},
  65. {10000, "9.8K"},
  66. {1024*1024 + 1, "1.0M"},
  67. {100*1024*1024 + 99999, "100.1M"},
  68. }
  69. for _, testCase := range testCases {
  70. t.Run(testCase.expectedOutput, func(t *testing.T) {
  71. output := FormatByteCount(testCase.n)
  72. if output != testCase.expectedOutput {
  73. t.Errorf("unexpected output: %s", output)
  74. }
  75. })
  76. }
  77. }
  78. func TestSafeParseURL(t *testing.T) {
  79. invalidURL := "https://invalid url"
  80. _, err := url.Parse(invalidURL)
  81. if err == nil {
  82. t.Error("unexpected parse success")
  83. }
  84. if strings.Index(err.Error(), invalidURL) == -1 {
  85. t.Error("URL not in error string")
  86. }
  87. _, err = SafeParseURL(invalidURL)
  88. if err == nil {
  89. t.Error("unexpected parse success")
  90. }
  91. if strings.Index(err.Error(), invalidURL) != -1 {
  92. t.Error("URL in error string")
  93. }
  94. }
  95. func TestSafeParseRequestURI(t *testing.T) {
  96. invalidURL := "https://invalid url"
  97. _, err := url.ParseRequestURI(invalidURL)
  98. if err == nil {
  99. t.Error("unexpected parse success")
  100. }
  101. if strings.Index(err.Error(), invalidURL) == -1 {
  102. t.Error("URL not in error string")
  103. }
  104. _, err = SafeParseRequestURI(invalidURL)
  105. if err == nil {
  106. t.Error("unexpected parse success")
  107. }
  108. if strings.Index(err.Error(), invalidURL) != -1 {
  109. t.Error("URL in error string")
  110. }
  111. }