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