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.

util_test.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "regexp"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestURLJoin(t *testing.T) {
  11. type test struct {
  12. Expected string
  13. Base string
  14. Elements []string
  15. }
  16. newTest := func(expected, base string, elements ...string) test {
  17. return test{Expected: expected, Base: base, Elements: elements}
  18. }
  19. for _, test := range []test{
  20. newTest("https://try.gitea.io/a/b/c",
  21. "https://try.gitea.io", "a/b", "c"),
  22. newTest("https://try.gitea.io/a/b/c",
  23. "https://try.gitea.io/", "/a/b/", "/c/"),
  24. newTest("https://try.gitea.io/a/c",
  25. "https://try.gitea.io/", "/a/./b/", "../c/"),
  26. newTest("a/b/c",
  27. "a", "b/c/"),
  28. newTest("a/b/d",
  29. "a/", "b/c/", "/../d/"),
  30. newTest("https://try.gitea.io/a/b/c#d",
  31. "https://try.gitea.io", "a/b", "c#d"),
  32. newTest("/a/b/d",
  33. "/a/", "b/c/", "/../d/"),
  34. newTest("/a/b/c",
  35. "/a", "b/c/"),
  36. newTest("/a/b/c#hash",
  37. "/a", "b/c#hash"),
  38. } {
  39. assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
  40. }
  41. }
  42. func TestIsEmptyString(t *testing.T) {
  43. cases := []struct {
  44. s string
  45. expected bool
  46. }{
  47. {"", true},
  48. {" ", true},
  49. {" ", true},
  50. {" a", false},
  51. }
  52. for _, v := range cases {
  53. assert.Equal(t, v.expected, IsEmptyString(v.s))
  54. }
  55. }
  56. func Test_NormalizeEOL(t *testing.T) {
  57. data1 := []string{
  58. "",
  59. "This text starts with empty lines",
  60. "another",
  61. "",
  62. "",
  63. "",
  64. "Some other empty lines in the middle",
  65. "more.",
  66. "And more.",
  67. "Ends with empty lines too.",
  68. "",
  69. "",
  70. "",
  71. }
  72. data2 := []string{
  73. "This text does not start with empty lines",
  74. "another",
  75. "",
  76. "",
  77. "",
  78. "Some other empty lines in the middle",
  79. "more.",
  80. "And more.",
  81. "Ends without EOLtoo.",
  82. }
  83. buildEOLData := func(data []string, eol string) []byte {
  84. return []byte(strings.Join(data, eol))
  85. }
  86. dos := buildEOLData(data1, "\r\n")
  87. unix := buildEOLData(data1, "\n")
  88. mac := buildEOLData(data1, "\r")
  89. assert.Equal(t, unix, NormalizeEOL(dos))
  90. assert.Equal(t, unix, NormalizeEOL(mac))
  91. assert.Equal(t, unix, NormalizeEOL(unix))
  92. dos = buildEOLData(data2, "\r\n")
  93. unix = buildEOLData(data2, "\n")
  94. mac = buildEOLData(data2, "\r")
  95. assert.Equal(t, unix, NormalizeEOL(dos))
  96. assert.Equal(t, unix, NormalizeEOL(mac))
  97. assert.Equal(t, unix, NormalizeEOL(unix))
  98. assert.Equal(t, []byte("one liner"), NormalizeEOL([]byte("one liner")))
  99. assert.Equal(t, []byte("\n"), NormalizeEOL([]byte("\n")))
  100. assert.Equal(t, []byte("\ntwo liner"), NormalizeEOL([]byte("\ntwo liner")))
  101. assert.Equal(t, []byte("two liner\n"), NormalizeEOL([]byte("two liner\n")))
  102. assert.Equal(t, []byte{}, NormalizeEOL([]byte{}))
  103. assert.Equal(t, []byte("mix\nand\nmatch\n."), NormalizeEOL([]byte("mix\r\nand\rmatch\n.")))
  104. }
  105. func Test_RandomInt(t *testing.T) {
  106. int, err := CryptoRandomInt(255)
  107. assert.True(t, int >= 0)
  108. assert.True(t, int <= 255)
  109. assert.NoError(t, err)
  110. }
  111. func Test_RandomString(t *testing.T) {
  112. str1, err := CryptoRandomString(32)
  113. assert.NoError(t, err)
  114. matches, err := regexp.MatchString(`^[a-zA-Z0-9]{32}$`, str1)
  115. assert.NoError(t, err)
  116. assert.True(t, matches)
  117. str2, err := CryptoRandomString(32)
  118. assert.NoError(t, err)
  119. matches, err = regexp.MatchString(`^[a-zA-Z0-9]{32}$`, str1)
  120. assert.NoError(t, err)
  121. assert.True(t, matches)
  122. assert.NotEqual(t, str1, str2)
  123. str3, err := CryptoRandomString(256)
  124. assert.NoError(t, err)
  125. matches, err = regexp.MatchString(`^[a-zA-Z0-9]{256}$`, str3)
  126. assert.NoError(t, err)
  127. assert.True(t, matches)
  128. str4, err := CryptoRandomString(256)
  129. assert.NoError(t, err)
  130. matches, err = regexp.MatchString(`^[a-zA-Z0-9]{256}$`, str4)
  131. assert.NoError(t, err)
  132. assert.True(t, matches)
  133. assert.NotEqual(t, str3, str4)
  134. }
  135. func Test_RandomBytes(t *testing.T) {
  136. bytes1, err := CryptoRandomBytes(32)
  137. assert.NoError(t, err)
  138. bytes2, err := CryptoRandomBytes(32)
  139. assert.NoError(t, err)
  140. assert.NotEqual(t, bytes1, bytes2)
  141. bytes3, err := CryptoRandomBytes(256)
  142. assert.NoError(t, err)
  143. bytes4, err := CryptoRandomBytes(256)
  144. assert.NoError(t, err)
  145. assert.NotEqual(t, bytes3, bytes4)
  146. }
  147. func Test_OptionalBool(t *testing.T) {
  148. assert.Equal(t, OptionalBoolNone, OptionalBoolParse(""))
  149. assert.Equal(t, OptionalBoolNone, OptionalBoolParse("x"))
  150. assert.Equal(t, OptionalBoolFalse, OptionalBoolParse("0"))
  151. assert.Equal(t, OptionalBoolFalse, OptionalBoolParse("f"))
  152. assert.Equal(t, OptionalBoolFalse, OptionalBoolParse("False"))
  153. assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("1"))
  154. assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("t"))
  155. assert.Equal(t, OptionalBoolTrue, OptionalBoolParse("True"))
  156. }
  157. // Test case for any function which accepts and returns a single string.
  158. type StringTest struct {
  159. in, out string
  160. }
  161. var upperTests = []StringTest{
  162. {"", ""},
  163. {"ONLYUPPER", "ONLYUPPER"},
  164. {"abc", "ABC"},
  165. {"AbC123", "ABC123"},
  166. {"azAZ09_", "AZAZ09_"},
  167. {"longStrinGwitHmixofsmaLLandcAps", "LONGSTRINGWITHMIXOFSMALLANDCAPS"},
  168. {"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", "LONG\u0250STRING\u0250WITH\u0250NONASCII\u2C6FCHARS"},
  169. {"\u0250\u0250\u0250\u0250\u0250", "\u0250\u0250\u0250\u0250\u0250"},
  170. {"a\u0080\U0010FFFF", "A\u0080\U0010FFFF"},
  171. {"lél", "LéL"},
  172. }
  173. func TestToUpperASCII(t *testing.T) {
  174. for _, tc := range upperTests {
  175. assert.Equal(t, ToUpperASCII(tc.in), tc.out)
  176. }
  177. }
  178. func BenchmarkToUpper(b *testing.B) {
  179. for _, tc := range upperTests {
  180. b.Run(tc.in, func(b *testing.B) {
  181. for i := 0; i < b.N; i++ {
  182. ToUpperASCII(tc.in)
  183. }
  184. })
  185. }
  186. }
  187. func TestToTitleCase(t *testing.T) {
  188. assert.Equal(t, ToTitleCase(`foo bar baz`), `Foo Bar Baz`)
  189. assert.Equal(t, ToTitleCase(`FOO BAR BAZ`), `Foo Bar Baz`)
  190. }
  191. func TestDedent(t *testing.T) {
  192. assert.Equal(t, Dedent(`
  193. foo
  194. bar
  195. `), "foo\n\tbar")
  196. }