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