Fix: rand seeding error / Test: conf/version and util

pull/247/head
HFO4 5 years ago
parent 3ed1a5dc43
commit f453b3e918

@ -5,12 +5,16 @@ import (
"cloudreve/pkg/conf"
"cloudreve/routers"
"github.com/gin-gonic/gin"
"math/rand"
"time"
)
func init() {
conf.Init("conf/conf.ini")
model.Init()
rand.Seed(time.Now().UnixNano())
// Debug 关闭时,切换为生产模式
if !conf.SystemConfig.Debug {
gin.SetMode(gin.ReleaseMode)

@ -0,0 +1,27 @@
package conf
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
)
func TestWriteVersionLock(t *testing.T) {
asserts := assert.New(t)
// 清理残余文件
if _, err := os.Stat("version.lock"); !os.IsNotExist(err) {
err = os.Remove("version.lock")
asserts.NoError(err)
}
err := WriteVersionLock()
defer func() { err = os.Remove("version.lock") }()
writtenVersion, err := ioutil.ReadFile("version.lock")
// 写入的版本应与当前版本相同
asserts.NoError(err)
asserts.Equal(string(writtenVersion), BackendVersion)
}

@ -2,14 +2,12 @@ package util
import (
"math/rand"
"time"
)
// RandStringRunes 返回随机字符串
func RandStringRunes(n int) string {
var letterRunes = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]

@ -0,0 +1,27 @@
package util
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestRandStringRunes(t *testing.T) {
asserts := assert.New(t)
// 0 长度字符
randStr := RandStringRunes(0)
asserts.Len(randStr, 0)
// 16 长度字符
randStr = RandStringRunes(16)
asserts.Len(randStr, 16)
// 32 长度字符
randStr = RandStringRunes(32)
asserts.Len(randStr, 32)
//相同长度字符
sameLenStr1 := RandStringRunes(32)
sameLenStr2 := RandStringRunes(32)
asserts.NotEqual(sameLenStr1, sameLenStr2)
}
Loading…
Cancel
Save