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.

395 lines
11 KiB

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