2016-11-05 17:53:11 -07:00
|
|
|
package base
|
|
|
|
|
2016-11-06 16:32:32 -07:00
|
|
|
import (
|
2017-12-03 04:55:13 -07:00
|
|
|
"net/url"
|
2016-11-06 16:32:32 -07:00
|
|
|
"testing"
|
2016-11-07 13:13:38 -07:00
|
|
|
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-12-03 04:55:13 -07:00
|
|
|
|
2016-11-07 13:13:38 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-11-06 16:32:32 -07:00
|
|
|
)
|
2016-11-05 17:53:11 -07:00
|
|
|
|
|
|
|
func TestEncodeMD5(t *testing.T) {
|
2016-11-07 13:49:50 -07:00
|
|
|
assert.Equal(t,
|
|
|
|
"3858f62230ac3c915f300c664312c63f",
|
|
|
|
EncodeMD5("foobar"),
|
|
|
|
)
|
2016-11-05 17:53:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncodeSha1(t *testing.T) {
|
2016-11-07 13:49:50 -07:00
|
|
|
assert.Equal(t,
|
|
|
|
"8843d7f92416211de9ebb963ff4ce28125932878",
|
|
|
|
EncodeSha1("foobar"),
|
|
|
|
)
|
2016-11-05 17:53:11 -07:00
|
|
|
}
|
|
|
|
|
2019-05-04 08:45:34 -07:00
|
|
|
func TestEncodeSha256(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
|
|
|
|
EncodeSha256("foobar"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:53:11 -07:00
|
|
|
func TestShortSha(t *testing.T) {
|
2016-11-07 12:28:03 -07:00
|
|
|
assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
|
2016-11-05 17:53:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthDecode(t *testing.T) {
|
2016-11-07 12:28:03 -07:00
|
|
|
_, _, err := BasicAuthDecode("?")
|
|
|
|
assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
|
2016-11-05 17:53:11 -07:00
|
|
|
|
|
|
|
user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
|
2016-11-07 12:28:03 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", user)
|
|
|
|
assert.Equal(t, "bar", pass)
|
2016-11-05 17:53:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthEncode(t *testing.T) {
|
2016-11-07 12:28:03 -07:00
|
|
|
assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
|
2016-11-05 17:53:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Test PBKDF2()
|
|
|
|
// TODO: Test VerifyTimeLimitCode()
|
|
|
|
// TODO: Test CreateTimeLimitCode()
|
|
|
|
|
|
|
|
func TestHashEmail(t *testing.T) {
|
2016-11-07 13:49:50 -07:00
|
|
|
assert.Equal(t,
|
|
|
|
"d41d8cd98f00b204e9800998ecf8427e",
|
|
|
|
HashEmail(""),
|
|
|
|
)
|
|
|
|
assert.Equal(t,
|
|
|
|
"353cbad9b58e69c96154ad99f92bedc7",
|
|
|
|
HashEmail("gitea@example.com"),
|
|
|
|
)
|
2016-11-05 17:53:11 -07:00
|
|
|
}
|
|
|
|
|
2017-12-03 04:55:13 -07:00
|
|
|
const gravatarSource = "https://secure.gravatar.com/avatar/"
|
|
|
|
|
|
|
|
func disableGravatar() {
|
2016-11-07 13:13:38 -07:00
|
|
|
setting.EnableFederatedAvatar = false
|
|
|
|
setting.LibravatarService = nil
|
|
|
|
setting.DisableGravatar = true
|
2017-12-03 04:55:13 -07:00
|
|
|
}
|
2016-11-07 13:13:38 -07:00
|
|
|
|
2017-12-03 04:55:13 -07:00
|
|
|
func enableGravatar(t *testing.T) {
|
2016-11-07 13:13:38 -07:00
|
|
|
setting.DisableGravatar = false
|
2017-12-03 04:55:13 -07:00
|
|
|
var err error
|
|
|
|
setting.GravatarSourceURL, err = url.Parse(gravatarSource)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSizedAvatarLink(t *testing.T) {
|
|
|
|
disableGravatar()
|
|
|
|
assert.Equal(t, "/img/avatar_default.png",
|
|
|
|
SizedAvatarLink("gitea@example.com", 100))
|
|
|
|
|
|
|
|
enableGravatar(t)
|
|
|
|
assert.Equal(t,
|
|
|
|
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100",
|
|
|
|
SizedAvatarLink("gitea@example.com", 100),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAvatarLink(t *testing.T) {
|
|
|
|
disableGravatar()
|
|
|
|
assert.Equal(t, "/img/avatar_default.png", AvatarLink("gitea@example.com"))
|
|
|
|
|
|
|
|
enableGravatar(t)
|
2016-11-07 13:49:50 -07:00
|
|
|
assert.Equal(t,
|
2017-12-03 04:55:13 -07:00
|
|
|
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon",
|
2016-11-07 13:49:50 -07:00
|
|
|
AvatarLink("gitea@example.com"),
|
|
|
|
)
|
2016-11-07 13:13:38 -07:00
|
|
|
}
|
|
|
|
|
2016-11-06 16:32:32 -07:00
|
|
|
func TestFileSize(t *testing.T) {
|
2019-06-12 12:41:28 -07:00
|
|
|
var size int64 = 512
|
2016-11-06 16:32:32 -07:00
|
|
|
assert.Equal(t, "512B", FileSize(size))
|
2019-06-12 12:41:28 -07:00
|
|
|
size *= 1024
|
2016-11-06 16:32:32 -07:00
|
|
|
assert.Equal(t, "512KB", FileSize(size))
|
2019-06-12 12:41:28 -07:00
|
|
|
size *= 1024
|
2016-11-06 16:32:32 -07:00
|
|
|
assert.Equal(t, "512MB", FileSize(size))
|
2019-06-12 12:41:28 -07:00
|
|
|
size *= 1024
|
2016-11-06 16:32:32 -07:00
|
|
|
assert.Equal(t, "512GB", FileSize(size))
|
2019-06-12 12:41:28 -07:00
|
|
|
size *= 1024
|
2016-11-06 16:32:32 -07:00
|
|
|
assert.Equal(t, "512TB", FileSize(size))
|
2019-06-12 12:41:28 -07:00
|
|
|
size *= 1024
|
2016-11-06 16:32:32 -07:00
|
|
|
assert.Equal(t, "512PB", FileSize(size))
|
2019-06-12 12:41:28 -07:00
|
|
|
size *= 4
|
2016-12-22 01:58:04 -07:00
|
|
|
assert.Equal(t, "2.0EB", FileSize(size))
|
2016-11-06 16:32:32 -07:00
|
|
|
}
|
|
|
|
|
2016-12-22 01:58:04 -07:00
|
|
|
func TestSubtract(t *testing.T) {
|
|
|
|
toFloat64 := func(n interface{}) float64 {
|
2019-05-28 08:45:54 -07:00
|
|
|
switch v := n.(type) {
|
2016-12-22 01:58:04 -07:00
|
|
|
case int:
|
2019-05-28 08:45:54 -07:00
|
|
|
return float64(v)
|
2016-12-22 01:58:04 -07:00
|
|
|
case int8:
|
2019-05-28 08:45:54 -07:00
|
|
|
return float64(v)
|
2016-12-22 01:58:04 -07:00
|
|
|
case int16:
|
2019-05-28 08:45:54 -07:00
|
|
|
return float64(v)
|
2016-12-22 01:58:04 -07:00
|
|
|
case int32:
|
2019-05-28 08:45:54 -07:00
|
|
|
return float64(v)
|
2016-12-22 01:58:04 -07:00
|
|
|
case int64:
|
2019-05-28 08:45:54 -07:00
|
|
|
return float64(v)
|
2016-12-22 01:58:04 -07:00
|
|
|
case float32:
|
2019-05-28 08:45:54 -07:00
|
|
|
return float64(v)
|
2016-12-22 01:58:04 -07:00
|
|
|
case float64:
|
2019-05-28 08:45:54 -07:00
|
|
|
return v
|
2016-12-22 01:58:04 -07:00
|
|
|
default:
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
values := []interface{}{
|
|
|
|
int(-3),
|
|
|
|
int8(14),
|
|
|
|
int16(81),
|
|
|
|
int32(-156),
|
|
|
|
int64(1528),
|
|
|
|
float32(3.5),
|
|
|
|
float64(-15.348),
|
|
|
|
}
|
|
|
|
for _, left := range values {
|
|
|
|
for _, right := range values {
|
|
|
|
expected := toFloat64(left) - toFloat64(right)
|
|
|
|
sub := Subtract(left, right)
|
|
|
|
assert.InDelta(t, expected, sub, 1e-3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 13:27:14 -07:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:37:08 -07:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:49:50 -07:00
|
|
|
func TestStringsToInt64s(t *testing.T) {
|
2016-12-22 01:58:04 -07:00
|
|
|
testSuccess := func(input []string, expected []int64) {
|
|
|
|
result, err := StringsToInt64s(input)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
}
|
|
|
|
testSuccess([]string{}, []int64{})
|
|
|
|
testSuccess([]string{"-1234"}, []int64{-1234})
|
|
|
|
testSuccess([]string{"1", "4", "16", "64", "256"},
|
|
|
|
[]int64{1, 4, 16, 64, 256})
|
2016-11-07 13:49:50 -07:00
|
|
|
|
2016-12-22 01:58:04 -07:00
|
|
|
_, err := StringsToInt64s([]string{"-1", "a", "$"})
|
|
|
|
assert.Error(t, err)
|
2016-11-07 13:49:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:08:50 -07:00
|
|
|
func TestInt64sToMap(t *testing.T) {
|
|
|
|
assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
|
|
|
|
assert.Equal(t,
|
|
|
|
map[int64]bool{1: true, 4: true, 16: true},
|
|
|
|
Int64sToMap([]int64{1, 4, 16}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:44:25 -07:00
|
|
|
func TestIsLetter(t *testing.T) {
|
|
|
|
assert.True(t, IsLetter('a'))
|
|
|
|
assert.True(t, IsLetter('e'))
|
|
|
|
assert.True(t, IsLetter('q'))
|
|
|
|
assert.True(t, IsLetter('z'))
|
|
|
|
assert.True(t, IsLetter('A'))
|
|
|
|
assert.True(t, IsLetter('E'))
|
|
|
|
assert.True(t, IsLetter('Q'))
|
|
|
|
assert.True(t, IsLetter('Z'))
|
|
|
|
assert.True(t, IsLetter('_'))
|
|
|
|
assert.False(t, IsLetter('-'))
|
|
|
|
assert.False(t, IsLetter('1'))
|
|
|
|
assert.False(t, IsLetter('$'))
|
|
|
|
}
|
|
|
|
|
2016-11-07 15:15:02 -07:00
|
|
|
func TestIsTextFile(t *testing.T) {
|
|
|
|
assert.True(t, IsTextFile([]byte{}))
|
|
|
|
assert.True(t, IsTextFile([]byte("lorem ipsum")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: IsImageFile(), currently no idea how to test
|
|
|
|
// TODO: IsPDFFile(), currently no idea how to test
|