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.

393 lines
11 KiB

  1. package base
  2. import (
  3. "testing"
  4. "code.gitea.io/gitea/modules/setting"
  5. "github.com/Unknwon/i18n"
  6. macaroni18n "github.com/go-macaron/i18n"
  7. "github.com/stretchr/testify/assert"
  8. "os"
  9. "strk.kbt.io/projects/go/libravatar"
  10. "time"
  11. )
  12. var BaseDate time.Time
  13. // time durations
  14. const (
  15. DayDur = 24 * time.Hour
  16. WeekDur = 7 * DayDur
  17. MonthDur = 30 * DayDur
  18. YearDur = 12 * MonthDur
  19. )
  20. func TestMain(m *testing.M) {
  21. // setup
  22. macaroni18n.I18n(macaroni18n.Options{
  23. Directory: "../../options/locale/",
  24. DefaultLang: "en-US",
  25. Langs: []string{"en-US"},
  26. Names: []string{"english"},
  27. })
  28. BaseDate = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
  29. // run the tests
  30. retVal := m.Run()
  31. os.Exit(retVal)
  32. }
  33. func TestEncodeMD5(t *testing.T) {
  34. assert.Equal(t,
  35. "3858f62230ac3c915f300c664312c63f",
  36. EncodeMD5("foobar"),
  37. )
  38. }
  39. func TestEncodeSha1(t *testing.T) {
  40. assert.Equal(t,
  41. "8843d7f92416211de9ebb963ff4ce28125932878",
  42. EncodeSha1("foobar"),
  43. )
  44. }
  45. func TestShortSha(t *testing.T) {
  46. assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
  47. }
  48. func TestDetectEncoding(t *testing.T) {
  49. testSuccess := func(b []byte, expected string) {
  50. encoding, err := DetectEncoding(b)
  51. assert.NoError(t, err)
  52. assert.Equal(t, expected, encoding)
  53. }
  54. // utf-8
  55. b := []byte("just some ascii")
  56. testSuccess(b, "UTF-8")
  57. // utf-8-sig: "hey" (with BOM)
  58. b = []byte{0xef, 0xbb, 0xbf, 0x68, 0x65, 0x79}
  59. testSuccess(b, "UTF-8")
  60. // utf-16: "hey<accented G>"
  61. b = []byte{0xff, 0xfe, 0x68, 0x00, 0x65, 0x00, 0x79, 0x00, 0xf4, 0x01}
  62. testSuccess(b, "UTF-16LE")
  63. // iso-8859-1: d<accented e>cor<newline>
  64. b = []byte{0x44, 0xe9, 0x63, 0x6f, 0x72, 0x0a}
  65. encoding, err := DetectEncoding(b)
  66. assert.NoError(t, err)
  67. // due to a race condition in `chardet` library, it could either detect
  68. // "ISO-8859-1" or "IS0-8859-2" here. Technically either is correct, so
  69. // we accept either.
  70. assert.Contains(t, encoding, "ISO-8859")
  71. setting.Repository.AnsiCharset = "placeholder"
  72. testSuccess(b, "placeholder")
  73. // invalid bytes
  74. b = []byte{0xfa}
  75. _, err = DetectEncoding(b)
  76. assert.Error(t, err)
  77. }
  78. func TestBasicAuthDecode(t *testing.T) {
  79. _, _, err := BasicAuthDecode("?")
  80. assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
  81. user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
  82. assert.NoError(t, err)
  83. assert.Equal(t, "foo", user)
  84. assert.Equal(t, "bar", pass)
  85. }
  86. func TestBasicAuthEncode(t *testing.T) {
  87. assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
  88. }
  89. func TestGetRandomString(t *testing.T) {
  90. randomString, err := GetRandomString(4)
  91. assert.NoError(t, err)
  92. assert.Len(t, randomString, 4)
  93. }
  94. // TODO: Test PBKDF2()
  95. // TODO: Test VerifyTimeLimitCode()
  96. // TODO: Test CreateTimeLimitCode()
  97. func TestHashEmail(t *testing.T) {
  98. assert.Equal(t,
  99. "d41d8cd98f00b204e9800998ecf8427e",
  100. HashEmail(""),
  101. )
  102. assert.Equal(t,
  103. "353cbad9b58e69c96154ad99f92bedc7",
  104. HashEmail("gitea@example.com"),
  105. )
  106. }
  107. func TestAvatarLink(t *testing.T) {
  108. setting.EnableFederatedAvatar = false
  109. setting.LibravatarService = nil
  110. setting.DisableGravatar = true
  111. assert.Equal(t, "/img/avatar_default.png", AvatarLink(""))
  112. setting.DisableGravatar = false
  113. assert.Equal(t,
  114. "353cbad9b58e69c96154ad99f92bedc7",
  115. AvatarLink("gitea@example.com"),
  116. )
  117. setting.EnableFederatedAvatar = true
  118. assert.Equal(t,
  119. "353cbad9b58e69c96154ad99f92bedc7",
  120. AvatarLink("gitea@example.com"),
  121. )
  122. setting.LibravatarService = libravatar.New()
  123. assert.Equal(t,
  124. "http://cdn.libravatar.org/avatar/353cbad9b58e69c96154ad99f92bedc7",
  125. AvatarLink("gitea@example.com"),
  126. )
  127. }
  128. func TestComputeTimeDiff(t *testing.T) {
  129. // test that for each offset in offsets,
  130. // computeTimeDiff(base + offset) == (offset, str)
  131. test := func(base int64, str string, offsets ...int64) {
  132. for _, offset := range offsets {
  133. diff, diffStr := computeTimeDiff(base + offset)
  134. assert.Equal(t, offset, diff)
  135. assert.Equal(t, str, diffStr)
  136. }
  137. }
  138. test(0, "now", 0)
  139. test(1, "1 second", 0)
  140. test(2, "2 seconds", 0)
  141. test(Minute, "1 minute", 0, 1, 30, Minute-1)
  142. test(2*Minute, "2 minutes", 0, Minute-1)
  143. test(Hour, "1 hour", 0, 1, Hour-1)
  144. test(5*Hour, "5 hours", 0, Hour-1)
  145. test(Day, "1 day", 0, 1, Day-1)
  146. test(5*Day, "5 days", 0, Day-1)
  147. test(Week, "1 week", 0, 1, Week-1)
  148. test(3*Week, "3 weeks", 0, 4*Day+25000)
  149. test(Month, "1 month", 0, 1, Month-1)
  150. test(10*Month, "10 months", 0, Month-1)
  151. test(Year, "1 year", 0, Year-1)
  152. test(3*Year, "3 years", 0, Year-1)
  153. }
  154. func TestTimeSince(t *testing.T) {
  155. assert.Equal(t, "now", timeSince(BaseDate, BaseDate, "en"))
  156. // test that each diff in `diffs` yields the expected string
  157. test := func(expected string, diffs ...time.Duration) {
  158. ago := i18n.Tr("en", "tool.ago")
  159. fromNow := i18n.Tr("en", "tool.from_now")
  160. for _, diff := range diffs {
  161. actual := timeSince(BaseDate, BaseDate.Add(diff), "en")
  162. assert.Equal(t, expected+" "+ago, actual)
  163. actual = timeSince(BaseDate.Add(diff), BaseDate, "en")
  164. assert.Equal(t, expected+" "+fromNow, actual)
  165. }
  166. }
  167. test("1 second", time.Second, time.Second+50*time.Millisecond)
  168. test("2 seconds", 2*time.Second, 2*time.Second+50*time.Millisecond)
  169. test("1 minute", time.Minute, time.Minute+30*time.Second)
  170. test("2 minutes", 2*time.Minute, 2*time.Minute+30*time.Second)
  171. test("1 hour", time.Hour, time.Hour+30*time.Minute)
  172. test("2 hours", 2*time.Hour, 2*time.Hour+30*time.Minute)
  173. test("1 day", DayDur, DayDur+12*time.Hour)
  174. test("2 days", 2*DayDur, 2*DayDur+12*time.Hour)
  175. test("1 week", WeekDur, WeekDur+3*DayDur)
  176. test("2 weeks", 2*WeekDur, 2*WeekDur+3*DayDur)
  177. test("1 month", MonthDur, MonthDur+15*DayDur)
  178. test("2 months", 2*MonthDur, 2*MonthDur+15*DayDur)
  179. test("1 year", YearDur, YearDur+6*MonthDur)
  180. test("2 years", 2*YearDur, 2*YearDur+6*MonthDur)
  181. }
  182. func TestTimeSincePro(t *testing.T) {
  183. assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate))
  184. // test that a difference of `diff` yields the expected string
  185. test := func(expected string, diff time.Duration) {
  186. actual := timeSincePro(BaseDate, BaseDate.Add(diff))
  187. assert.Equal(t, expected, actual)
  188. assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate))
  189. }
  190. test("1 second", time.Second)
  191. test("2 seconds", 2*time.Second)
  192. test("1 minute", time.Minute)
  193. test("1 minute, 1 second", time.Minute+time.Second)
  194. test("1 minute, 59 seconds", time.Minute+59*time.Second)
  195. test("2 minutes", 2*time.Minute)
  196. test("1 hour", time.Hour)
  197. test("1 hour, 1 second", time.Hour+time.Second)
  198. test("1 hour, 59 minutes, 59 seconds", time.Hour+59*time.Minute+59*time.Second)
  199. test("2 hours", 2*time.Hour)
  200. test("1 day", DayDur)
  201. test("1 day, 23 hours, 59 minutes, 59 seconds",
  202. DayDur+23*time.Hour+59*time.Minute+59*time.Second)
  203. test("2 days", 2*DayDur)
  204. test("1 week", WeekDur)
  205. test("2 weeks", 2*WeekDur)
  206. test("1 month", MonthDur)
  207. test("3 months", 3*MonthDur)
  208. test("1 year", YearDur)
  209. test("2 years, 3 months, 1 week, 2 days, 4 hours, 12 minutes, 17 seconds",
  210. 2*YearDur+3*MonthDur+WeekDur+2*DayDur+4*time.Hour+
  211. 12*time.Minute+17*time.Second)
  212. }
  213. func TestHtmlTimeSince(t *testing.T) {
  214. setting.TimeFormat = time.UnixDate
  215. // test that `diff` yields a result containing `expected`
  216. test := func(expected string, diff time.Duration) {
  217. actual := htmlTimeSince(BaseDate, BaseDate.Add(diff), "en")
  218. assert.Contains(t, actual, `title="Sat Jan 1 00:00:00 UTC 2000"`)
  219. assert.Contains(t, actual, expected)
  220. }
  221. test("1 second", time.Second)
  222. test("3 minutes", 3*time.Minute+5*time.Second)
  223. test("1 day", DayDur+18*time.Hour)
  224. test("1 week", WeekDur+6*DayDur)
  225. test("3 months", 3*MonthDur+3*WeekDur)
  226. test("2 years", 2*YearDur)
  227. test("3 years", 3*YearDur+11*MonthDur+4*WeekDur)
  228. }
  229. func TestFileSize(t *testing.T) {
  230. var size int64
  231. size = 512
  232. assert.Equal(t, "512B", FileSize(size))
  233. size = size * 1024
  234. assert.Equal(t, "512KB", FileSize(size))
  235. size = size * 1024
  236. assert.Equal(t, "512MB", FileSize(size))
  237. size = size * 1024
  238. assert.Equal(t, "512GB", FileSize(size))
  239. size = size * 1024
  240. assert.Equal(t, "512TB", FileSize(size))
  241. size = size * 1024
  242. assert.Equal(t, "512PB", FileSize(size))
  243. size = size * 4
  244. assert.Equal(t, "2.0EB", FileSize(size))
  245. }
  246. func TestSubtract(t *testing.T) {
  247. toFloat64 := func(n interface{}) float64 {
  248. switch n.(type) {
  249. case int:
  250. return float64(n.(int))
  251. case int8:
  252. return float64(n.(int8))
  253. case int16:
  254. return float64(n.(int16))
  255. case int32:
  256. return float64(n.(int32))
  257. case int64:
  258. return float64(n.(int64))
  259. case float32:
  260. return float64(n.(float32))
  261. case float64:
  262. return n.(float64)
  263. default:
  264. return 0.0
  265. }
  266. }
  267. values := []interface{}{
  268. int(-3),
  269. int8(14),
  270. int16(81),
  271. int32(-156),
  272. int64(1528),
  273. float32(3.5),
  274. float64(-15.348),
  275. }
  276. for _, left := range values {
  277. for _, right := range values {
  278. expected := toFloat64(left) - toFloat64(right)
  279. sub := Subtract(left, right)
  280. assert.InDelta(t, expected, sub, 1e-3)
  281. }
  282. }
  283. }
  284. func TestEllipsisString(t *testing.T) {
  285. assert.Equal(t, "...", EllipsisString("foobar", 0))
  286. assert.Equal(t, "...", EllipsisString("foobar", 1))
  287. assert.Equal(t, "...", EllipsisString("foobar", 2))
  288. assert.Equal(t, "...", EllipsisString("foobar", 3))
  289. assert.Equal(t, "f...", EllipsisString("foobar", 4))
  290. assert.Equal(t, "fo...", EllipsisString("foobar", 5))
  291. assert.Equal(t, "foobar", EllipsisString("foobar", 6))
  292. assert.Equal(t, "foobar", EllipsisString("foobar", 10))
  293. }
  294. func TestTruncateString(t *testing.T) {
  295. assert.Equal(t, "", TruncateString("foobar", 0))
  296. assert.Equal(t, "f", TruncateString("foobar", 1))
  297. assert.Equal(t, "fo", TruncateString("foobar", 2))
  298. assert.Equal(t, "foo", TruncateString("foobar", 3))
  299. assert.Equal(t, "foob", TruncateString("foobar", 4))
  300. assert.Equal(t, "fooba", TruncateString("foobar", 5))
  301. assert.Equal(t, "foobar", TruncateString("foobar", 6))
  302. assert.Equal(t, "foobar", TruncateString("foobar", 7))
  303. }
  304. func TestStringsToInt64s(t *testing.T) {
  305. testSuccess := func(input []string, expected []int64) {
  306. result, err := StringsToInt64s(input)
  307. assert.NoError(t, err)
  308. assert.Equal(t, expected, result)
  309. }
  310. testSuccess([]string{}, []int64{})
  311. testSuccess([]string{"-1234"}, []int64{-1234})
  312. testSuccess([]string{"1", "4", "16", "64", "256"},
  313. []int64{1, 4, 16, 64, 256})
  314. _, err := StringsToInt64s([]string{"-1", "a", "$"})
  315. assert.Error(t, err)
  316. }
  317. func TestInt64sToStrings(t *testing.T) {
  318. assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
  319. assert.Equal(t,
  320. []string{"1", "4", "16", "64", "256"},
  321. Int64sToStrings([]int64{1, 4, 16, 64, 256}),
  322. )
  323. }
  324. func TestInt64sToMap(t *testing.T) {
  325. assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
  326. assert.Equal(t,
  327. map[int64]bool{1: true, 4: true, 16: true},
  328. Int64sToMap([]int64{1, 4, 16}),
  329. )
  330. }
  331. func TestIsLetter(t *testing.T) {
  332. assert.True(t, IsLetter('a'))
  333. assert.True(t, IsLetter('e'))
  334. assert.True(t, IsLetter('q'))
  335. assert.True(t, IsLetter('z'))
  336. assert.True(t, IsLetter('A'))
  337. assert.True(t, IsLetter('E'))
  338. assert.True(t, IsLetter('Q'))
  339. assert.True(t, IsLetter('Z'))
  340. assert.True(t, IsLetter('_'))
  341. assert.False(t, IsLetter('-'))
  342. assert.False(t, IsLetter('1'))
  343. assert.False(t, IsLetter('$'))
  344. }
  345. func TestIsTextFile(t *testing.T) {
  346. assert.True(t, IsTextFile([]byte{}))
  347. assert.True(t, IsTextFile([]byte("lorem ipsum")))
  348. }
  349. // TODO: IsImageFile(), currently no idea how to test
  350. // TODO: IsPDFFile(), currently no idea how to test