Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

tool_test.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package base
  2. import (
  3. "net/url"
  4. "os"
  5. "testing"
  6. "time"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/Unknwon/i18n"
  9. macaroni18n "github.com/go-macaron/i18n"
  10. "github.com/stretchr/testify/assert"
  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. const gravatarSource = "https://secure.gravatar.com/avatar/"
  108. func disableGravatar() {
  109. setting.EnableFederatedAvatar = false
  110. setting.LibravatarService = nil
  111. setting.DisableGravatar = true
  112. }
  113. func enableGravatar(t *testing.T) {
  114. setting.DisableGravatar = false
  115. var err error
  116. setting.GravatarSourceURL, err = url.Parse(gravatarSource)
  117. assert.NoError(t, err)
  118. }
  119. func TestSizedAvatarLink(t *testing.T) {
  120. disableGravatar()
  121. assert.Equal(t, "/img/avatar_default.png",
  122. SizedAvatarLink("gitea@example.com", 100))
  123. enableGravatar(t)
  124. assert.Equal(t,
  125. "https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100",
  126. SizedAvatarLink("gitea@example.com", 100),
  127. )
  128. }
  129. func TestAvatarLink(t *testing.T) {
  130. disableGravatar()
  131. assert.Equal(t, "/img/avatar_default.png", AvatarLink("gitea@example.com"))
  132. enableGravatar(t)
  133. assert.Equal(t,
  134. "https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon",
  135. AvatarLink("gitea@example.com"),
  136. )
  137. }
  138. func TestComputeTimeDiff(t *testing.T) {
  139. // test that for each offset in offsets,
  140. // computeTimeDiff(base + offset) == (offset, str)
  141. test := func(base int64, str string, offsets ...int64) {
  142. for _, offset := range offsets {
  143. diff, diffStr := computeTimeDiff(base+offset, "en")
  144. assert.Equal(t, offset, diff)
  145. assert.Equal(t, str, diffStr)
  146. }
  147. }
  148. test(0, "now", 0)
  149. test(1, "1 second", 0)
  150. test(2, "2 seconds", 0)
  151. test(Minute, "1 minute", 0, 1, 30, Minute-1)
  152. test(2*Minute, "2 minutes", 0, Minute-1)
  153. test(Hour, "1 hour", 0, 1, Hour-1)
  154. test(5*Hour, "5 hours", 0, Hour-1)
  155. test(Day, "1 day", 0, 1, Day-1)
  156. test(5*Day, "5 days", 0, Day-1)
  157. test(Week, "1 week", 0, 1, Week-1)
  158. test(3*Week, "3 weeks", 0, 4*Day+25000)
  159. test(Month, "1 month", 0, 1, Month-1)
  160. test(10*Month, "10 months", 0, Month-1)
  161. test(Year, "1 year", 0, Year-1)
  162. test(3*Year, "3 years", 0, Year-1)
  163. }
  164. func TestMinutesToFriendly(t *testing.T) {
  165. // test that a number of minutes yields the expected string
  166. test := func(expected string, minutes int) {
  167. actual := MinutesToFriendly(minutes, "en")
  168. assert.Equal(t, expected, actual)
  169. }
  170. test("1 minute", 1)
  171. test("2 minutes", 2)
  172. test("1 hour", 60)
  173. test("1 hour, 1 minute", 61)
  174. test("1 hour, 2 minutes", 62)
  175. test("2 hours", 120)
  176. }
  177. func TestTimeSince(t *testing.T) {
  178. assert.Equal(t, "now", timeSince(BaseDate, BaseDate, "en"))
  179. // test that each diff in `diffs` yields the expected string
  180. test := func(expected string, diffs ...time.Duration) {
  181. for _, diff := range diffs {
  182. actual := timeSince(BaseDate, BaseDate.Add(diff), "en")
  183. assert.Equal(t, i18n.Tr("en", "tool.ago", expected), actual)
  184. actual = timeSince(BaseDate.Add(diff), BaseDate, "en")
  185. assert.Equal(t, i18n.Tr("en", "tool.from_now", expected), actual)
  186. }
  187. }
  188. test("1 second", time.Second, time.Second+50*time.Millisecond)
  189. test("2 seconds", 2*time.Second, 2*time.Second+50*time.Millisecond)
  190. test("1 minute", time.Minute, time.Minute+30*time.Second)
  191. test("2 minutes", 2*time.Minute, 2*time.Minute+30*time.Second)
  192. test("1 hour", time.Hour, time.Hour+30*time.Minute)
  193. test("2 hours", 2*time.Hour, 2*time.Hour+30*time.Minute)
  194. test("1 day", DayDur, DayDur+12*time.Hour)
  195. test("2 days", 2*DayDur, 2*DayDur+12*time.Hour)
  196. test("1 week", WeekDur, WeekDur+3*DayDur)
  197. test("2 weeks", 2*WeekDur, 2*WeekDur+3*DayDur)
  198. test("1 month", MonthDur, MonthDur+15*DayDur)
  199. test("2 months", 2*MonthDur, 2*MonthDur+15*DayDur)
  200. test("1 year", YearDur, YearDur+6*MonthDur)
  201. test("2 years", 2*YearDur, 2*YearDur+6*MonthDur)
  202. }
  203. func TestTimeSincePro(t *testing.T) {
  204. assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, "en"))
  205. // test that a difference of `diff` yields the expected string
  206. test := func(expected string, diff time.Duration) {
  207. actual := timeSincePro(BaseDate, BaseDate.Add(diff), "en")
  208. assert.Equal(t, expected, actual)
  209. assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate, "en"))
  210. }
  211. test("1 second", time.Second)
  212. test("2 seconds", 2*time.Second)
  213. test("1 minute", time.Minute)
  214. test("1 minute, 1 second", time.Minute+time.Second)
  215. test("1 minute, 59 seconds", time.Minute+59*time.Second)
  216. test("2 minutes", 2*time.Minute)
  217. test("1 hour", time.Hour)
  218. test("1 hour, 1 second", time.Hour+time.Second)
  219. test("1 hour, 59 minutes, 59 seconds", time.Hour+59*time.Minute+59*time.Second)
  220. test("2 hours", 2*time.Hour)
  221. test("1 day", DayDur)
  222. test("1 day, 23 hours, 59 minutes, 59 seconds",
  223. DayDur+23*time.Hour+59*time.Minute+59*time.Second)
  224. test("2 days", 2*DayDur)
  225. test("1 week", WeekDur)
  226. test("2 weeks", 2*WeekDur)
  227. test("1 month", MonthDur)
  228. test("3 months", 3*MonthDur)
  229. test("1 year", YearDur)
  230. test("2 years, 3 months, 1 week, 2 days, 4 hours, 12 minutes, 17 seconds",
  231. 2*YearDur+3*MonthDur+WeekDur+2*DayDur+4*time.Hour+
  232. 12*time.Minute+17*time.Second)
  233. }
  234. func TestHtmlTimeSince(t *testing.T) {
  235. setting.TimeFormat = time.UnixDate
  236. // test that `diff` yields a result containing `expected`
  237. test := func(expected string, diff time.Duration) {
  238. actual := htmlTimeSince(BaseDate, BaseDate.Add(diff), "en")
  239. assert.Contains(t, actual, `title="Sat Jan 1 00:00:00 UTC 2000"`)
  240. assert.Contains(t, actual, expected)
  241. }
  242. test("1 second", time.Second)
  243. test("3 minutes", 3*time.Minute+5*time.Second)
  244. test("1 day", DayDur+18*time.Hour)
  245. test("1 week", WeekDur+6*DayDur)
  246. test("3 months", 3*MonthDur+3*WeekDur)
  247. test("2 years", 2*YearDur)
  248. test("3 years", 3*YearDur+11*MonthDur+4*WeekDur)
  249. }
  250. func TestFileSize(t *testing.T) {
  251. var size int64
  252. size = 512
  253. assert.Equal(t, "512B", FileSize(size))
  254. size = size * 1024
  255. assert.Equal(t, "512KB", FileSize(size))
  256. size = size * 1024
  257. assert.Equal(t, "512MB", FileSize(size))
  258. size = size * 1024
  259. assert.Equal(t, "512GB", FileSize(size))
  260. size = size * 1024
  261. assert.Equal(t, "512TB", FileSize(size))
  262. size = size * 1024
  263. assert.Equal(t, "512PB", FileSize(size))
  264. size = size * 4
  265. assert.Equal(t, "2.0EB", FileSize(size))
  266. }
  267. func TestSubtract(t *testing.T) {
  268. toFloat64 := func(n interface{}) float64 {
  269. switch n.(type) {
  270. case int:
  271. return float64(n.(int))
  272. case int8:
  273. return float64(n.(int8))
  274. case int16:
  275. return float64(n.(int16))
  276. case int32:
  277. return float64(n.(int32))
  278. case int64:
  279. return float64(n.(int64))
  280. case float32:
  281. return float64(n.(float32))
  282. case float64:
  283. return n.(float64)
  284. default:
  285. return 0.0
  286. }
  287. }
  288. values := []interface{}{
  289. int(-3),
  290. int8(14),
  291. int16(81),
  292. int32(-156),
  293. int64(1528),
  294. float32(3.5),
  295. float64(-15.348),
  296. }
  297. for _, left := range values {
  298. for _, right := range values {
  299. expected := toFloat64(left) - toFloat64(right)
  300. sub := Subtract(left, right)
  301. assert.InDelta(t, expected, sub, 1e-3)
  302. }
  303. }
  304. }
  305. func TestEllipsisString(t *testing.T) {
  306. assert.Equal(t, "...", EllipsisString("foobar", 0))
  307. assert.Equal(t, "...", EllipsisString("foobar", 1))
  308. assert.Equal(t, "...", EllipsisString("foobar", 2))
  309. assert.Equal(t, "...", EllipsisString("foobar", 3))
  310. assert.Equal(t, "f...", EllipsisString("foobar", 4))
  311. assert.Equal(t, "fo...", EllipsisString("foobar", 5))
  312. assert.Equal(t, "foobar", EllipsisString("foobar", 6))
  313. assert.Equal(t, "foobar", EllipsisString("foobar", 10))
  314. }
  315. func TestTruncateString(t *testing.T) {
  316. assert.Equal(t, "", TruncateString("foobar", 0))
  317. assert.Equal(t, "f", TruncateString("foobar", 1))
  318. assert.Equal(t, "fo", TruncateString("foobar", 2))
  319. assert.Equal(t, "foo", TruncateString("foobar", 3))
  320. assert.Equal(t, "foob", TruncateString("foobar", 4))
  321. assert.Equal(t, "fooba", TruncateString("foobar", 5))
  322. assert.Equal(t, "foobar", TruncateString("foobar", 6))
  323. assert.Equal(t, "foobar", TruncateString("foobar", 7))
  324. }
  325. func TestStringsToInt64s(t *testing.T) {
  326. testSuccess := func(input []string, expected []int64) {
  327. result, err := StringsToInt64s(input)
  328. assert.NoError(t, err)
  329. assert.Equal(t, expected, result)
  330. }
  331. testSuccess([]string{}, []int64{})
  332. testSuccess([]string{"-1234"}, []int64{-1234})
  333. testSuccess([]string{"1", "4", "16", "64", "256"},
  334. []int64{1, 4, 16, 64, 256})
  335. _, err := StringsToInt64s([]string{"-1", "a", "$"})
  336. assert.Error(t, err)
  337. }
  338. func TestInt64sToStrings(t *testing.T) {
  339. assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
  340. assert.Equal(t,
  341. []string{"1", "4", "16", "64", "256"},
  342. Int64sToStrings([]int64{1, 4, 16, 64, 256}),
  343. )
  344. }
  345. func TestInt64sToMap(t *testing.T) {
  346. assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
  347. assert.Equal(t,
  348. map[int64]bool{1: true, 4: true, 16: true},
  349. Int64sToMap([]int64{1, 4, 16}),
  350. )
  351. }
  352. func TestIsLetter(t *testing.T) {
  353. assert.True(t, IsLetter('a'))
  354. assert.True(t, IsLetter('e'))
  355. assert.True(t, IsLetter('q'))
  356. assert.True(t, IsLetter('z'))
  357. assert.True(t, IsLetter('A'))
  358. assert.True(t, IsLetter('E'))
  359. assert.True(t, IsLetter('Q'))
  360. assert.True(t, IsLetter('Z'))
  361. assert.True(t, IsLetter('_'))
  362. assert.False(t, IsLetter('-'))
  363. assert.False(t, IsLetter('1'))
  364. assert.False(t, IsLetter('$'))
  365. }
  366. func TestIsTextFile(t *testing.T) {
  367. assert.True(t, IsTextFile([]byte{}))
  368. assert.True(t, IsTextFile([]byte("lorem ipsum")))
  369. }
  370. // TODO: IsImageFile(), currently no idea how to test
  371. // TODO: IsPDFFile(), currently no idea how to test