2020-04-05 11:50:50 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-05 11:50:50 +05:30
|
|
|
|
2016-11-06 06:23:11 +05:30
|
|
|
package base
|
|
|
|
|
2016-11-07 05:02:32 +05:30
|
|
|
import (
|
2024-05-20 20:42:50 +05:30
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
2021-01-28 23:38:11 +05:30
|
|
|
"os"
|
2016-11-07 05:02:32 +05:30
|
|
|
"testing"
|
2021-01-28 23:38:11 +05:30
|
|
|
"time"
|
2016-11-08 01:43:38 +05:30
|
|
|
|
2024-05-20 20:42:50 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/test"
|
|
|
|
|
2016-11-08 01:43:38 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
2016-11-07 05:02:32 +05:30
|
|
|
)
|
2016-11-06 06:23:11 +05:30
|
|
|
|
2019-05-04 21:15:34 +05:30
|
|
|
func TestEncodeSha256(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
|
|
|
|
EncodeSha256("foobar"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-06 06:23:11 +05:30
|
|
|
func TestShortSha(t *testing.T) {
|
2016-11-08 00:58:03 +05:30
|
|
|
assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
|
2016-11-06 06:23:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthDecode(t *testing.T) {
|
2016-11-08 00:58:03 +05:30
|
|
|
_, _, err := BasicAuthDecode("?")
|
|
|
|
assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
|
2016-11-06 06:23:11 +05:30
|
|
|
|
|
|
|
user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
|
2016-11-08 00:58:03 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", user)
|
|
|
|
assert.Equal(t, "bar", pass)
|
2020-12-18 07:21:28 +05:30
|
|
|
|
|
|
|
_, _, err = BasicAuthDecode("aW52YWxpZA==")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
_, _, err = BasicAuthDecode("invalid")
|
|
|
|
assert.Error(t, err)
|
2016-11-06 06:23:11 +05:30
|
|
|
}
|
|
|
|
|
2021-01-28 23:38:11 +05:30
|
|
|
func TestVerifyTimeLimitCode(t *testing.T) {
|
2024-05-20 20:42:50 +05:30
|
|
|
defer test.MockVariableValue(&setting.InstallLock, true)()
|
|
|
|
initGeneralSecret := func(secret string) {
|
|
|
|
setting.InstallLock = true
|
|
|
|
setting.CfgProvider, _ = setting.NewConfigProviderFromData(fmt.Sprintf(`
|
|
|
|
[oauth2]
|
|
|
|
JWT_SECRET = %s
|
|
|
|
`, secret))
|
|
|
|
setting.LoadCommonSettings()
|
2021-01-28 23:38:11 +05:30
|
|
|
}
|
|
|
|
|
2024-05-20 20:42:50 +05:30
|
|
|
initGeneralSecret("KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko")
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
t.Run("TestGenericParameter", func(t *testing.T) {
|
|
|
|
time2000 := time.Date(2000, 1, 2, 3, 4, 5, 0, time.Local)
|
|
|
|
assert.Equal(t, "2000010203040000026fa5221b2731b7cf80b1b506f5e39e38c115fee5", CreateTimeLimitCode("test-sha1", 2, time2000, sha1.New()))
|
|
|
|
assert.Equal(t, "2000010203040000026fa5221b2731b7cf80b1b506f5e39e38c115fee5", CreateTimeLimitCode("test-sha1", 2, "200001020304", sha1.New()))
|
|
|
|
assert.Equal(t, "2000010203040000024842227a2f87041ff82025199c0187410a9297bf", CreateTimeLimitCode("test-hmac", 2, time2000, nil))
|
|
|
|
assert.Equal(t, "2000010203040000024842227a2f87041ff82025199c0187410a9297bf", CreateTimeLimitCode("test-hmac", 2, "200001020304", nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestInvalidCode", func(t *testing.T) {
|
|
|
|
assert.False(t, VerifyTimeLimitCode(now, "data", 2, ""))
|
|
|
|
assert.False(t, VerifyTimeLimitCode(now, "data", 2, "invalid code"))
|
|
|
|
})
|
2021-01-28 23:38:11 +05:30
|
|
|
|
2024-05-20 20:42:50 +05:30
|
|
|
t.Run("TestCreateAndVerify", func(t *testing.T) {
|
|
|
|
code := CreateTimeLimitCode("data", 2, now, nil)
|
|
|
|
assert.False(t, VerifyTimeLimitCode(now.Add(-time.Minute), "data", 2, code)) // not started yet
|
|
|
|
assert.True(t, VerifyTimeLimitCode(now, "data", 2, code))
|
|
|
|
assert.True(t, VerifyTimeLimitCode(now.Add(time.Minute), "data", 2, code))
|
|
|
|
assert.False(t, VerifyTimeLimitCode(now.Add(time.Minute), "DATA", 2, code)) // invalid data
|
|
|
|
assert.False(t, VerifyTimeLimitCode(now.Add(2*time.Minute), "data", 2, code)) // expired
|
|
|
|
})
|
2021-01-28 23:38:11 +05:30
|
|
|
|
2024-05-20 20:42:50 +05:30
|
|
|
t.Run("TestDifferentSecret", func(t *testing.T) {
|
|
|
|
// use another secret to ensure the code is invalid for different secret
|
|
|
|
verifyDataCode := func(c string) bool {
|
|
|
|
return VerifyTimeLimitCode(now, "data", 2, c)
|
|
|
|
}
|
|
|
|
code1 := CreateTimeLimitCode("data", 2, now, sha1.New())
|
|
|
|
code2 := CreateTimeLimitCode("data", 2, now, nil)
|
|
|
|
assert.True(t, verifyDataCode(code1))
|
|
|
|
assert.True(t, verifyDataCode(code2))
|
|
|
|
initGeneralSecret("000_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko")
|
|
|
|
assert.False(t, verifyDataCode(code1))
|
|
|
|
assert.False(t, verifyDataCode(code2))
|
|
|
|
})
|
2021-01-28 23:38:11 +05:30
|
|
|
}
|
2016-11-06 06:23:11 +05:30
|
|
|
|
2016-11-07 05:02:32 +05:30
|
|
|
func TestFileSize(t *testing.T) {
|
2019-06-13 01:11:28 +05:30
|
|
|
var size int64 = 512
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "512 B", FileSize(size))
|
2019-06-13 01:11:28 +05:30
|
|
|
size *= 1024
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "512 KiB", FileSize(size))
|
2019-06-13 01:11:28 +05:30
|
|
|
size *= 1024
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "512 MiB", FileSize(size))
|
2019-06-13 01:11:28 +05:30
|
|
|
size *= 1024
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "512 GiB", FileSize(size))
|
2019-06-13 01:11:28 +05:30
|
|
|
size *= 1024
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "512 TiB", FileSize(size))
|
2019-06-13 01:11:28 +05:30
|
|
|
size *= 1024
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "512 PiB", FileSize(size))
|
2019-06-13 01:11:28 +05:30
|
|
|
size *= 4
|
2020-02-04 01:20:37 +05:30
|
|
|
assert.Equal(t, "2.0 EiB", FileSize(size))
|
2016-11-07 05:02:32 +05:30
|
|
|
}
|
|
|
|
|
2016-11-08 01:57:14 +05:30
|
|
|
func TestEllipsisString(t *testing.T) {
|
|
|
|
assert.Equal(t, "...", EllipsisString("foobar", 0))
|
|
|
|
assert.Equal(t, "...", EllipsisString("foobar", 1))
|
|
|
|
assert.Equal(t, "...", EllipsisString("foobar", 2))
|
|
|
|
assert.Equal(t, "...", EllipsisString("foobar", 3))
|
|
|
|
assert.Equal(t, "f...", EllipsisString("foobar", 4))
|
|
|
|
assert.Equal(t, "fo...", EllipsisString("foobar", 5))
|
|
|
|
assert.Equal(t, "foobar", EllipsisString("foobar", 6))
|
|
|
|
assert.Equal(t, "foobar", EllipsisString("foobar", 10))
|
2021-05-13 13:20:57 +05:30
|
|
|
assert.Equal(t, "测...", EllipsisString("测试文本一二三四", 4))
|
|
|
|
assert.Equal(t, "测试...", EllipsisString("测试文本一二三四", 5))
|
|
|
|
assert.Equal(t, "测试文...", EllipsisString("测试文本一二三四", 6))
|
|
|
|
assert.Equal(t, "测试文本一二三四", EllipsisString("测试文本一二三四", 10))
|
2016-11-08 01:57:14 +05:30
|
|
|
}
|
|
|
|
|
2016-11-08 02:07:08 +05:30
|
|
|
func TestTruncateString(t *testing.T) {
|
|
|
|
assert.Equal(t, "", TruncateString("foobar", 0))
|
|
|
|
assert.Equal(t, "f", TruncateString("foobar", 1))
|
|
|
|
assert.Equal(t, "fo", TruncateString("foobar", 2))
|
|
|
|
assert.Equal(t, "foo", TruncateString("foobar", 3))
|
|
|
|
assert.Equal(t, "foob", TruncateString("foobar", 4))
|
|
|
|
assert.Equal(t, "fooba", TruncateString("foobar", 5))
|
|
|
|
assert.Equal(t, "foobar", TruncateString("foobar", 6))
|
|
|
|
assert.Equal(t, "foobar", TruncateString("foobar", 7))
|
2021-05-13 13:20:57 +05:30
|
|
|
assert.Equal(t, "测试文本", TruncateString("测试文本一二三四", 4))
|
|
|
|
assert.Equal(t, "测试文本一", TruncateString("测试文本一二三四", 5))
|
|
|
|
assert.Equal(t, "测试文本一二", TruncateString("测试文本一二三四", 6))
|
|
|
|
assert.Equal(t, "测试文本一二三", TruncateString("测试文本一二三四", 7))
|
2016-11-08 02:07:08 +05:30
|
|
|
}
|
|
|
|
|
2016-11-08 02:19:50 +05:30
|
|
|
func TestStringsToInt64s(t *testing.T) {
|
2016-12-22 14:28:04 +05:30
|
|
|
testSuccess := func(input []string, expected []int64) {
|
|
|
|
result, err := StringsToInt64s(input)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
}
|
2024-03-21 20:37:35 +05:30
|
|
|
testSuccess(nil, nil)
|
2016-12-22 14:28:04 +05:30
|
|
|
testSuccess([]string{}, []int64{})
|
|
|
|
testSuccess([]string{"-1234"}, []int64{-1234})
|
2024-03-21 20:37:35 +05:30
|
|
|
testSuccess([]string{"1", "4", "16", "64", "256"}, []int64{1, 4, 16, 64, 256})
|
2016-11-08 02:19:50 +05:30
|
|
|
|
2024-03-21 20:37:35 +05:30
|
|
|
ints, err := StringsToInt64s([]string{"-1", "a"})
|
|
|
|
assert.Len(t, ints, 0)
|
2016-12-22 14:28:04 +05:30
|
|
|
assert.Error(t, err)
|
2016-11-08 02:19:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestInt64sToStrings(t *testing.T) {
|
|
|
|
assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"1", "4", "16", "64", "256"},
|
|
|
|
Int64sToStrings([]int64{1, 4, 16, 64, 256}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-28 23:38:11 +05:30
|
|
|
// TODO: Test EntryIcon
|
|
|
|
|
|
|
|
func TestSetupGiteaRoot(t *testing.T) {
|
|
|
|
_ = os.Setenv("GITEA_ROOT", "test")
|
2021-06-07 10:57:09 +05:30
|
|
|
assert.Equal(t, "test", SetupGiteaRoot())
|
2021-01-28 23:38:11 +05:30
|
|
|
_ = os.Setenv("GITEA_ROOT", "")
|
|
|
|
assert.NotEqual(t, "test", SetupGiteaRoot())
|
|
|
|
}
|
|
|
|
|
2020-09-16 09:37:18 +05:30
|
|
|
func TestFormatNumberSI(t *testing.T) {
|
|
|
|
assert.Equal(t, "125", FormatNumberSI(int(125)))
|
|
|
|
assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
|
|
|
|
assert.Equal(t, "21.3M", FormatNumberSI(21317675))
|
|
|
|
assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
|
|
|
|
assert.Equal(t, "", FormatNumberSI("test"))
|
|
|
|
}
|