You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.6 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "net/url"
  7. "testing"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestEncodeMD5(t *testing.T) {
  12. assert.Equal(t,
  13. "3858f62230ac3c915f300c664312c63f",
  14. EncodeMD5("foobar"),
  15. )
  16. }
  17. func TestEncodeSha1(t *testing.T) {
  18. assert.Equal(t,
  19. "8843d7f92416211de9ebb963ff4ce28125932878",
  20. EncodeSha1("foobar"),
  21. )
  22. }
  23. func TestEncodeSha256(t *testing.T) {
  24. assert.Equal(t,
  25. "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
  26. EncodeSha256("foobar"),
  27. )
  28. }
  29. func TestShortSha(t *testing.T) {
  30. assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
  31. }
  32. func TestBasicAuthDecode(t *testing.T) {
  33. _, _, err := BasicAuthDecode("?")
  34. assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
  35. user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
  36. assert.NoError(t, err)
  37. assert.Equal(t, "foo", user)
  38. assert.Equal(t, "bar", pass)
  39. }
  40. func TestBasicAuthEncode(t *testing.T) {
  41. assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
  42. }
  43. // TODO: Test PBKDF2()
  44. // TODO: Test VerifyTimeLimitCode()
  45. // TODO: Test CreateTimeLimitCode()
  46. func TestHashEmail(t *testing.T) {
  47. assert.Equal(t,
  48. "d41d8cd98f00b204e9800998ecf8427e",
  49. HashEmail(""),
  50. )
  51. assert.Equal(t,
  52. "353cbad9b58e69c96154ad99f92bedc7",
  53. HashEmail("gitea@example.com"),
  54. )
  55. }
  56. const gravatarSource = "https://secure.gravatar.com/avatar/"
  57. func disableGravatar() {
  58. setting.EnableFederatedAvatar = false
  59. setting.LibravatarService = nil
  60. setting.DisableGravatar = true
  61. }
  62. func enableGravatar(t *testing.T) {
  63. setting.DisableGravatar = false
  64. var err error
  65. setting.GravatarSourceURL, err = url.Parse(gravatarSource)
  66. assert.NoError(t, err)
  67. }
  68. func TestSizedAvatarLink(t *testing.T) {
  69. disableGravatar()
  70. assert.Equal(t, "/img/avatar_default.png",
  71. SizedAvatarLink("gitea@example.com", 100))
  72. enableGravatar(t)
  73. assert.Equal(t,
  74. "https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100",
  75. SizedAvatarLink("gitea@example.com", 100),
  76. )
  77. }
  78. func TestFileSize(t *testing.T) {
  79. var size int64 = 512
  80. assert.Equal(t, "512 B", FileSize(size))
  81. size *= 1024
  82. assert.Equal(t, "512 KiB", FileSize(size))
  83. size *= 1024
  84. assert.Equal(t, "512 MiB", FileSize(size))
  85. size *= 1024
  86. assert.Equal(t, "512 GiB", FileSize(size))
  87. size *= 1024
  88. assert.Equal(t, "512 TiB", FileSize(size))
  89. size *= 1024
  90. assert.Equal(t, "512 PiB", FileSize(size))
  91. size *= 4
  92. assert.Equal(t, "2.0 EiB", FileSize(size))
  93. }
  94. func TestSubtract(t *testing.T) {
  95. toFloat64 := func(n interface{}) float64 {
  96. switch v := n.(type) {
  97. case int:
  98. return float64(v)
  99. case int8:
  100. return float64(v)
  101. case int16:
  102. return float64(v)
  103. case int32:
  104. return float64(v)
  105. case int64:
  106. return float64(v)
  107. case float32:
  108. return float64(v)
  109. case float64:
  110. return v
  111. default:
  112. return 0.0
  113. }
  114. }
  115. values := []interface{}{
  116. int(-3),
  117. int8(14),
  118. int16(81),
  119. int32(-156),
  120. int64(1528),
  121. float32(3.5),
  122. float64(-15.348),
  123. }
  124. for _, left := range values {
  125. for _, right := range values {
  126. expected := toFloat64(left) - toFloat64(right)
  127. sub := Subtract(left, right)
  128. assert.InDelta(t, expected, sub, 1e-3)
  129. }
  130. }
  131. }
  132. func TestEllipsisString(t *testing.T) {
  133. assert.Equal(t, "...", EllipsisString("foobar", 0))
  134. assert.Equal(t, "...", EllipsisString("foobar", 1))
  135. assert.Equal(t, "...", EllipsisString("foobar", 2))
  136. assert.Equal(t, "...", EllipsisString("foobar", 3))
  137. assert.Equal(t, "f...", EllipsisString("foobar", 4))
  138. assert.Equal(t, "fo...", EllipsisString("foobar", 5))
  139. assert.Equal(t, "foobar", EllipsisString("foobar", 6))
  140. assert.Equal(t, "foobar", EllipsisString("foobar", 10))
  141. }
  142. func TestTruncateString(t *testing.T) {
  143. assert.Equal(t, "", TruncateString("foobar", 0))
  144. assert.Equal(t, "f", TruncateString("foobar", 1))
  145. assert.Equal(t, "fo", TruncateString("foobar", 2))
  146. assert.Equal(t, "foo", TruncateString("foobar", 3))
  147. assert.Equal(t, "foob", TruncateString("foobar", 4))
  148. assert.Equal(t, "fooba", TruncateString("foobar", 5))
  149. assert.Equal(t, "foobar", TruncateString("foobar", 6))
  150. assert.Equal(t, "foobar", TruncateString("foobar", 7))
  151. }
  152. func TestStringsToInt64s(t *testing.T) {
  153. testSuccess := func(input []string, expected []int64) {
  154. result, err := StringsToInt64s(input)
  155. assert.NoError(t, err)
  156. assert.Equal(t, expected, result)
  157. }
  158. testSuccess([]string{}, []int64{})
  159. testSuccess([]string{"-1234"}, []int64{-1234})
  160. testSuccess([]string{"1", "4", "16", "64", "256"},
  161. []int64{1, 4, 16, 64, 256})
  162. _, err := StringsToInt64s([]string{"-1", "a", "$"})
  163. assert.Error(t, err)
  164. }
  165. func TestInt64sToStrings(t *testing.T) {
  166. assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
  167. assert.Equal(t,
  168. []string{"1", "4", "16", "64", "256"},
  169. Int64sToStrings([]int64{1, 4, 16, 64, 256}),
  170. )
  171. }
  172. func TestInt64sToMap(t *testing.T) {
  173. assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
  174. assert.Equal(t,
  175. map[int64]bool{1: true, 4: true, 16: true},
  176. Int64sToMap([]int64{1, 4, 16}),
  177. )
  178. }
  179. func TestIsLetter(t *testing.T) {
  180. assert.True(t, IsLetter('a'))
  181. assert.True(t, IsLetter('e'))
  182. assert.True(t, IsLetter('q'))
  183. assert.True(t, IsLetter('z'))
  184. assert.True(t, IsLetter('A'))
  185. assert.True(t, IsLetter('E'))
  186. assert.True(t, IsLetter('Q'))
  187. assert.True(t, IsLetter('Z'))
  188. assert.True(t, IsLetter('_'))
  189. assert.False(t, IsLetter('-'))
  190. assert.False(t, IsLetter('1'))
  191. assert.False(t, IsLetter('$'))
  192. }
  193. func TestIsTextFile(t *testing.T) {
  194. assert.True(t, IsTextFile([]byte{}))
  195. assert.True(t, IsTextFile([]byte("lorem ipsum")))
  196. }
  197. // TODO: IsImageFile(), currently no idea how to test
  198. // TODO: IsPDFFile(), currently no idea how to test