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.

185 lines
4.8 KiB

  1. package base
  2. import (
  3. "testing"
  4. "code.gitea.io/gitea/modules/setting"
  5. "github.com/stretchr/testify/assert"
  6. "strk.kbt.io/projects/go/libravatar"
  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 TestShortSha(t *testing.T) {
  21. assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
  22. }
  23. // TODO: Test DetectEncoding()
  24. func TestBasicAuthDecode(t *testing.T) {
  25. _, _, err := BasicAuthDecode("?")
  26. assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
  27. user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
  28. assert.NoError(t, err)
  29. assert.Equal(t, "foo", user)
  30. assert.Equal(t, "bar", pass)
  31. }
  32. func TestBasicAuthEncode(t *testing.T) {
  33. assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
  34. }
  35. func TestGetRandomString(t *testing.T) {
  36. assert.Len(t, GetRandomString(4), 4)
  37. }
  38. // TODO: Test PBKDF2()
  39. // TODO: Test VerifyTimeLimitCode()
  40. // TODO: Test CreateTimeLimitCode()
  41. func TestHashEmail(t *testing.T) {
  42. assert.Equal(t,
  43. "d41d8cd98f00b204e9800998ecf8427e",
  44. HashEmail(""),
  45. )
  46. assert.Equal(t,
  47. "353cbad9b58e69c96154ad99f92bedc7",
  48. HashEmail("gitea@example.com"),
  49. )
  50. }
  51. func TestAvatarLink(t *testing.T) {
  52. setting.EnableFederatedAvatar = false
  53. setting.LibravatarService = nil
  54. setting.DisableGravatar = true
  55. assert.Equal(t, "/img/avatar_default.png", AvatarLink(""))
  56. setting.DisableGravatar = false
  57. assert.Equal(t,
  58. "353cbad9b58e69c96154ad99f92bedc7",
  59. AvatarLink("gitea@example.com"),
  60. )
  61. setting.EnableFederatedAvatar = true
  62. assert.Equal(t,
  63. "353cbad9b58e69c96154ad99f92bedc7",
  64. AvatarLink("gitea@example.com"),
  65. )
  66. setting.LibravatarService = libravatar.New()
  67. assert.Equal(t,
  68. "http://cdn.libravatar.org/avatar/353cbad9b58e69c96154ad99f92bedc7",
  69. AvatarLink("gitea@example.com"),
  70. )
  71. }
  72. // TODO: computeTimeDiff()
  73. // TODO: TimeSincePro()
  74. // TODO: timeSince()
  75. // TODO: RawTimeSince()
  76. // TODO: TimeSince()
  77. func TestFileSize(t *testing.T) {
  78. var size int64
  79. size = 512
  80. assert.Equal(t, "512B", FileSize(size))
  81. size = size * 1024
  82. assert.Equal(t, "512KB", FileSize(size))
  83. size = size * 1024
  84. assert.Equal(t, "512MB", FileSize(size))
  85. size = size * 1024
  86. assert.Equal(t, "512GB", FileSize(size))
  87. size = size * 1024
  88. assert.Equal(t, "512TB", FileSize(size))
  89. size = size * 1024
  90. assert.Equal(t, "512PB", FileSize(size))
  91. //size = size * 1024 TODO: Fix bug for EB
  92. //assert.Equal(t, "512EB", FileSize(size))
  93. }
  94. // TODO: Subtract()
  95. func TestEllipsisString(t *testing.T) {
  96. assert.Equal(t, "...", EllipsisString("foobar", 0))
  97. assert.Equal(t, "...", EllipsisString("foobar", 1))
  98. assert.Equal(t, "...", EllipsisString("foobar", 2))
  99. assert.Equal(t, "...", EllipsisString("foobar", 3))
  100. assert.Equal(t, "f...", EllipsisString("foobar", 4))
  101. assert.Equal(t, "fo...", EllipsisString("foobar", 5))
  102. assert.Equal(t, "foobar", EllipsisString("foobar", 6))
  103. assert.Equal(t, "foobar", EllipsisString("foobar", 10))
  104. }
  105. func TestTruncateString(t *testing.T) {
  106. assert.Equal(t, "", TruncateString("foobar", 0))
  107. assert.Equal(t, "f", TruncateString("foobar", 1))
  108. assert.Equal(t, "fo", TruncateString("foobar", 2))
  109. assert.Equal(t, "foo", TruncateString("foobar", 3))
  110. assert.Equal(t, "foob", TruncateString("foobar", 4))
  111. assert.Equal(t, "fooba", TruncateString("foobar", 5))
  112. assert.Equal(t, "foobar", TruncateString("foobar", 6))
  113. assert.Equal(t, "foobar", TruncateString("foobar", 7))
  114. }
  115. func TestStringsToInt64s(t *testing.T) {
  116. assert.Equal(t, []int64{}, StringsToInt64s([]string{}))
  117. assert.Equal(t,
  118. []int64{1, 4, 16, 64, 256},
  119. StringsToInt64s([]string{"1", "4", "16", "64", "256"}),
  120. )
  121. // TODO: StringsToInt64s should return ([]int64, error)
  122. assert.Equal(t, []int64{-1, 0, 0}, StringsToInt64s([]string{"-1", "a", "$"}))
  123. }
  124. func TestInt64sToStrings(t *testing.T) {
  125. assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
  126. assert.Equal(t,
  127. []string{"1", "4", "16", "64", "256"},
  128. Int64sToStrings([]int64{1, 4, 16, 64, 256}),
  129. )
  130. }
  131. func TestInt64sToMap(t *testing.T) {
  132. assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
  133. assert.Equal(t,
  134. map[int64]bool{1: true, 4: true, 16: true},
  135. Int64sToMap([]int64{1, 4, 16}),
  136. )
  137. }
  138. func TestIsLetter(t *testing.T) {
  139. assert.True(t, IsLetter('a'))
  140. assert.True(t, IsLetter('e'))
  141. assert.True(t, IsLetter('q'))
  142. assert.True(t, IsLetter('z'))
  143. assert.True(t, IsLetter('A'))
  144. assert.True(t, IsLetter('E'))
  145. assert.True(t, IsLetter('Q'))
  146. assert.True(t, IsLetter('Z'))
  147. assert.True(t, IsLetter('_'))
  148. assert.False(t, IsLetter('-'))
  149. assert.False(t, IsLetter('1'))
  150. assert.False(t, IsLetter('$'))
  151. }
  152. func TestIsTextFile(t *testing.T) {
  153. assert.True(t, IsTextFile([]byte{}))
  154. assert.True(t, IsTextFile([]byte("lorem ipsum")))
  155. }
  156. // TODO: IsImageFile(), currently no idea how to test
  157. // TODO: IsPDFFile(), currently no idea how to test