You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.2 KiB
72 lines
1.2 KiB
package hashid
|
|
|
|
import (
|
|
"github.com/HFO4/cloudreve/bootstrap/constant"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
func TestHashEncode(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
|
|
{
|
|
res, err := HashEncode([]int{1, 2, 3})
|
|
asserts.NoError(err)
|
|
asserts.NotEmpty(res)
|
|
}
|
|
|
|
{
|
|
res, err := HashEncode([]int{})
|
|
asserts.Error(err)
|
|
asserts.Empty(res)
|
|
}
|
|
|
|
}
|
|
|
|
func TestHashID(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
|
|
{
|
|
res := HashID(1, ShareID)
|
|
asserts.NotEmpty(res)
|
|
}
|
|
}
|
|
|
|
func TestHashDecode(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
|
|
// 正常
|
|
{
|
|
res, _ := HashEncode([]int{1, 2, 3})
|
|
decodeRes, err := HashDecode(res)
|
|
asserts.NoError(err)
|
|
asserts.Equal([]int{1, 2, 3}, decodeRes)
|
|
}
|
|
|
|
// 出错
|
|
{
|
|
decodeRes, err := HashDecode("233")
|
|
asserts.Error(err)
|
|
asserts.Len(decodeRes, 0)
|
|
}
|
|
}
|
|
|
|
func TestDecodeHashID(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
constant.HashIDTable = []int{0, 1, 2, 3, 4, 5}
|
|
|
|
// 成功
|
|
{
|
|
uid, err := DecodeHashID(HashID(1, ShareID), ShareID)
|
|
asserts.NoError(err)
|
|
asserts.EqualValues(1, uid)
|
|
}
|
|
|
|
// 类型不匹配
|
|
{
|
|
uid, err := DecodeHashID(HashID(1, ShareID), UserID)
|
|
asserts.Error(err)
|
|
asserts.EqualValues(0, uid)
|
|
}
|
|
}
|