2020-04-05 09:20:50 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-05 09:20:50 +03:00
|
|
|
|
2016-11-06 03:53:11 +03:00
|
|
|
package base
|
|
|
|
|
2016-11-07 02:32:32 +03:00
|
|
|
import (
|
|
|
|
"testing"
|
2024-05-20 18:12:50 +03:00
|
|
|
|
2016-11-07 23:13:38 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-11-07 02:32:32 +03:00
|
|
|
)
|
2016-11-06 03:53:11 +03:00
|
|
|
|
2019-05-04 18:45:34 +03:00
|
|
|
func TestEncodeSha256(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
|
|
|
|
EncodeSha256("foobar"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-06 03:53:11 +03:00
|
|
|
func TestShortSha(t *testing.T) {
|
2016-11-07 22:28:03 +03:00
|
|
|
assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
|
2016-11-06 03:53:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthDecode(t *testing.T) {
|
2016-11-07 22:28:03 +03:00
|
|
|
_, _, err := BasicAuthDecode("?")
|
|
|
|
assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
|
2016-11-06 03:53:11 +03:00
|
|
|
|
|
|
|
user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2016-11-07 22:28:03 +03:00
|
|
|
assert.Equal(t, "foo", user)
|
|
|
|
assert.Equal(t, "bar", pass)
|
2020-12-18 04:51:28 +03:00
|
|
|
|
|
|
|
_, _, err = BasicAuthDecode("aW52YWxpZA==")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2020-12-18 04:51:28 +03:00
|
|
|
|
|
|
|
_, _, err = BasicAuthDecode("invalid")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2024-08-08 09:32:14 +03:00
|
|
|
|
|
|
|
_, _, err = BasicAuthDecode("YWxpY2U=") // "alice", no colon
|
|
|
|
require.Error(t, err)
|
2016-11-06 03:53:11 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 02:32:32 +03:00
|
|
|
func TestFileSize(t *testing.T) {
|
2019-06-12 22:41:28 +03:00
|
|
|
var size int64 = 512
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "512 B", FileSize(size))
|
2019-06-12 22:41:28 +03:00
|
|
|
size *= 1024
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "512 KiB", FileSize(size))
|
2019-06-12 22:41:28 +03:00
|
|
|
size *= 1024
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "512 MiB", FileSize(size))
|
2019-06-12 22:41:28 +03:00
|
|
|
size *= 1024
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "512 GiB", FileSize(size))
|
2019-06-12 22:41:28 +03:00
|
|
|
size *= 1024
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "512 TiB", FileSize(size))
|
2019-06-12 22:41:28 +03:00
|
|
|
size *= 1024
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "512 PiB", FileSize(size))
|
2019-06-12 22:41:28 +03:00
|
|
|
size *= 4
|
2020-02-03 22:50:37 +03:00
|
|
|
assert.Equal(t, "2.0 EiB", FileSize(size))
|
2016-11-07 02:32:32 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:27:14 +03: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))
|
2021-05-13 10:50:57 +03:00
|
|
|
assert.Equal(t, "测...", EllipsisString("测试文本一二三四", 4))
|
|
|
|
assert.Equal(t, "测试...", EllipsisString("测试文本一二三四", 5))
|
|
|
|
assert.Equal(t, "测试文...", EllipsisString("测试文本一二三四", 6))
|
|
|
|
assert.Equal(t, "测试文本一二三四", EllipsisString("测试文本一二三四", 10))
|
2016-11-07 23:27:14 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:37:08 +03: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))
|
2021-05-13 10:50:57 +03:00
|
|
|
assert.Equal(t, "测试文本", TruncateString("测试文本一二三四", 4))
|
|
|
|
assert.Equal(t, "测试文本一", TruncateString("测试文本一二三四", 5))
|
|
|
|
assert.Equal(t, "测试文本一二", TruncateString("测试文本一二三四", 6))
|
|
|
|
assert.Equal(t, "测试文本一二三", TruncateString("测试文本一二三四", 7))
|
2016-11-07 23:37:08 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:49:50 +03:00
|
|
|
func TestStringsToInt64s(t *testing.T) {
|
2016-12-22 11:58:04 +03:00
|
|
|
testSuccess := func(input []string, expected []int64) {
|
|
|
|
result, err := StringsToInt64s(input)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2016-12-22 11:58:04 +03:00
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
}
|
2024-03-21 18:07:35 +03:00
|
|
|
testSuccess(nil, nil)
|
2016-12-22 11:58:04 +03:00
|
|
|
testSuccess([]string{}, []int64{})
|
|
|
|
testSuccess([]string{"-1234"}, []int64{-1234})
|
2024-03-21 18:07:35 +03:00
|
|
|
testSuccess([]string{"1", "4", "16", "64", "256"}, []int64{1, 4, 16, 64, 256})
|
2016-11-07 23:49:50 +03:00
|
|
|
|
2024-03-21 18:07:35 +03:00
|
|
|
ints, err := StringsToInt64s([]string{"-1", "a"})
|
2024-07-30 22:41:10 +03:00
|
|
|
assert.Empty(t, ints)
|
|
|
|
require.Error(t, err)
|
2016-11-07 23:49:50 +03: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}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:08:11 +03:00
|
|
|
// TODO: Test EntryIcon
|
|
|
|
|
|
|
|
func TestSetupGiteaRoot(t *testing.T) {
|
2024-07-18 00:07:41 +03:00
|
|
|
t.Setenv("GITEA_ROOT", "test")
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.Equal(t, "test", SetupGiteaRoot())
|
2024-07-18 00:07:41 +03:00
|
|
|
t.Setenv("GITEA_ROOT", "")
|
2021-01-28 21:08:11 +03:00
|
|
|
assert.NotEqual(t, "test", SetupGiteaRoot())
|
|
|
|
}
|
|
|
|
|
2020-09-16 07:07:18 +03:00
|
|
|
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"))
|
|
|
|
}
|