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.

234 lines
5.8 KiB

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