diff options
author | 6543 <6543@obermui.de> | 2021-01-28 19:08:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-28 19:08:11 +0100 |
commit | 99b7af6fc89687ee74ca938b9a3f07bf8acc79dd (patch) | |
tree | 50cc0c817c23fc54c66ca50b1d0eafea960bbebc /modules/base | |
parent | d1353e1f7c9bf648cd72bd9731b4f843b0dc9a86 (diff) | |
download | gitea-99b7af6fc89687ee74ca938b9a3f07bf8acc79dd.tar.gz gitea-99b7af6fc89687ee74ca938b9a3f07bf8acc79dd.zip |
Add some Unit-Tests (#14500)
* fix url
* modules/auth/pa: coverage: 40#.0%
* modules/base coverage: 67.6% -> 89.9%
* modules/cache coverage: 0% -> 12.0%
* modules/convert coverage: 27.1% -> 29.7%
Diffstat (limited to 'modules/base')
-rw-r--r-- | modules/base/natural_sort_test.go | 4 | ||||
-rw-r--r-- | modules/base/tool.go | 2 | ||||
-rw-r--r-- | modules/base/tool_test.go | 99 |
3 files changed, 96 insertions, 9 deletions
diff --git a/modules/base/natural_sort_test.go b/modules/base/natural_sort_test.go index b8d45e4ec0..2faa5acf38 100644 --- a/modules/base/natural_sort_test.go +++ b/modules/base/natural_sort_test.go @@ -17,8 +17,8 @@ func TestNaturalSortLess(t *testing.T) { test("v1.20.0", "v1.2.0", false) test("v1.20.0", "v1.29.0", true) test("v1.20.0", "v1.20.0", false) - test("abc", "bcd", "abc" < "bcd") + test("abc", "bcd", true) test("a-1-a", "a-1-b", true) test("2", "12", true) - test("a", "ab", "a" < "ab") + test("a", "ab", true) } diff --git a/modules/base/tool.go b/modules/base/tool.go index 53339d6449..d721d47e9d 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -270,7 +270,7 @@ func Int64sContains(intsSlice []int64, a int64) bool { } // IsLetter reports whether the rune is a letter (category L). -// https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257 +// https://github.com/golang/go/blob/c3b4918/src/go/scanner/scanner.go#L342 func IsLetter(ch rune) bool { return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch) } diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go index a2a989b31f..b6baeb8c3c 100644 --- a/modules/base/tool_test.go +++ b/modules/base/tool_test.go @@ -5,7 +5,10 @@ package base import ( + "encoding/base64" + "os" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -53,11 +56,48 @@ func TestBasicAuthDecode(t *testing.T) { func TestBasicAuthEncode(t *testing.T) { assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar")) + assert.Equal(t, "MjM6IjotLS0t", BasicAuthEncode("23:\"", "----")) } -// TODO: Test PBKDF2() -// TODO: Test VerifyTimeLimitCode() -// TODO: Test CreateTimeLimitCode() +func TestVerifyTimeLimitCode(t *testing.T) { + tc := []struct { + data string + minutes int + code string + valid bool + }{{ + data: "data", + minutes: 2, + code: testCreateTimeLimitCode(t, "data", 2), + valid: true, + }, { + data: "abc123-ß", + minutes: 1, + code: testCreateTimeLimitCode(t, "abc123-ß", 1), + valid: true, + }, { + data: "data", + minutes: 2, + code: "2021012723240000005928251dac409d2c33a6eb82c63410aaad569bed", + valid: false, + }} + for _, test := range tc { + actualValid := VerifyTimeLimitCode(test.data, test.minutes, test.code) + assert.Equal(t, test.valid, actualValid, "data: '%s' code: '%s' should be valid: %t", test.data, test.code, test.valid) + } +} + +func testCreateTimeLimitCode(t *testing.T, data string, m int) string { + result0 := CreateTimeLimitCode(data, m, nil) + result1 := CreateTimeLimitCode(data, m, time.Now().Format("200601021504")) + result2 := CreateTimeLimitCode(data, m, time.Unix(time.Now().Unix()+int64(time.Minute)*int64(m), 0).Format("200601021504")) + + assert.Equal(t, result0, result1) + assert.NotEqual(t, result0, result2) + + assert.True(t, len(result0) != 0) + return result0 +} func TestFileSize(t *testing.T) { var size int64 = 512 @@ -76,6 +116,12 @@ func TestFileSize(t *testing.T) { assert.Equal(t, "2.0 EiB", FileSize(size)) } +func TestPrettyNumber(t *testing.T) { + assert.Equal(t, "23,342,432", PrettyNumber(23342432)) + assert.Equal(t, "0", PrettyNumber(0)) + assert.Equal(t, "-100,000", PrettyNumber(-100000)) +} + func TestSubtract(t *testing.T) { toFloat64 := func(n interface{}) float64 { switch v := n.(type) { @@ -168,6 +214,13 @@ func TestInt64sToMap(t *testing.T) { ) } +func TestInt64sContains(t *testing.T) { + assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{})) + assert.Equal(t, true, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 1)) + assert.Equal(t, true, Int64sContains([]int64{2323}, 2323)) + assert.Equal(t, false, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 232)) +} + func TestIsLetter(t *testing.T) { assert.True(t, IsLetter('a')) assert.True(t, IsLetter('e')) @@ -181,6 +234,8 @@ func TestIsLetter(t *testing.T) { assert.False(t, IsLetter('-')) assert.False(t, IsLetter('1')) assert.False(t, IsLetter('$')) + assert.False(t, IsLetter(0x00)) + assert.False(t, IsLetter(0x93)) } func TestDetectContentTypeLongerThanSniffLen(t *testing.T) { @@ -197,11 +252,19 @@ Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment --><svg></svg>`))) } +// IsRepresentableAsText + func TestIsTextFile(t *testing.T) { assert.True(t, IsTextFile([]byte{})) assert.True(t, IsTextFile([]byte("lorem ipsum"))) } +func TestIsImageFile(t *testing.T) { + png, _ := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC") + assert.True(t, IsImageFile(png)) + assert.False(t, IsImageFile([]byte("plain text"))) +} + func TestIsSVGImageFile(t *testing.T) { assert.True(t, IsSVGImageFile([]byte("<svg></svg>"))) assert.True(t, IsSVGImageFile([]byte(" <svg></svg>"))) @@ -248,6 +311,33 @@ func TestIsSVGImageFile(t *testing.T) { <foo></foo>`))) } +func TestIsPDFFile(t *testing.T) { + pdf, _ := base64.StdEncoding.DecodeString("JVBERi0xLjYKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nF3NPwsCMQwF8D2f4s2CNYk1baF0EHRwOwg4iJt/NsFb/PpevUE4Mjwe") + assert.True(t, IsPDFFile(pdf)) + assert.False(t, IsPDFFile([]byte("plain text"))) +} + +func TestIsVideoFile(t *testing.T) { + mp4, _ := base64.StdEncoding.DecodeString("AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAEI721vb3YAAABsbXZoZAAAAADaBlwX2gZcFwAAA+gA") + assert.True(t, IsVideoFile(mp4)) + assert.False(t, IsVideoFile([]byte("plain text"))) +} + +func TestIsAudioFile(t *testing.T) { + mp3, _ := base64.StdEncoding.DecodeString("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl") + assert.True(t, IsAudioFile(mp3)) + assert.False(t, IsAudioFile([]byte("plain text"))) +} + +// TODO: Test EntryIcon + +func TestSetupGiteaRoot(t *testing.T) { + _ = os.Setenv("GITEA_ROOT", "test") + assert.EqualValues(t, "test", SetupGiteaRoot()) + _ = os.Setenv("GITEA_ROOT", "") + assert.NotEqual(t, "test", SetupGiteaRoot()) +} + func TestFormatNumberSI(t *testing.T) { assert.Equal(t, "125", FormatNumberSI(int(125))) assert.Equal(t, "1.3k", FormatNumberSI(int64(1317))) @@ -255,6 +345,3 @@ func TestFormatNumberSI(t *testing.T) { assert.Equal(t, "45.7G", FormatNumberSI(45721317675)) assert.Equal(t, "", FormatNumberSI("test")) } - -// TODO: IsImageFile(), currently no idea how to test -// TODO: IsPDFFile(), currently no idea how to test |