mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-01 04:40:54 +08:00
实现基础的DDoS防护
This commit is contained in:
@@ -1,56 +1,67 @@
|
||||
package utils
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBytesToString(t *testing.T) {
|
||||
t.Log(UnsafeBytesToString([]byte("Hello,World")))
|
||||
t.Log(utils.UnsafeBytesToString([]byte("Hello,World")))
|
||||
}
|
||||
|
||||
func TestStringToBytes(t *testing.T) {
|
||||
t.Log(string(UnsafeStringToBytes("Hello,World")))
|
||||
t.Log(string(utils.UnsafeStringToBytes("Hello,World")))
|
||||
}
|
||||
|
||||
func BenchmarkBytesToString(b *testing.B) {
|
||||
data := []byte("Hello,World")
|
||||
var data = []byte("Hello,World")
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = UnsafeBytesToString(data)
|
||||
_ = utils.UnsafeBytesToString(data)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBytesToString2(b *testing.B) {
|
||||
data := []byte("Hello,World")
|
||||
var data = []byte("Hello,World")
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = string(data)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToBytes(b *testing.B) {
|
||||
s := strings.Repeat("Hello,World", 1024)
|
||||
var s = strings.Repeat("Hello,World", 1024)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = UnsafeStringToBytes(s)
|
||||
_ = utils.UnsafeStringToBytes(s)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToBytes2(b *testing.B) {
|
||||
s := strings.Repeat("Hello,World", 1024)
|
||||
var s = strings.Repeat("Hello,World", 1024)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = []byte(s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatAddress(t *testing.T) {
|
||||
t.Log(FormatAddress("127.0.0.1:1234"))
|
||||
t.Log(FormatAddress("127.0.0.1 : 1234"))
|
||||
t.Log(FormatAddress("127.0.0.1:1234"))
|
||||
t.Log(utils.FormatAddress("127.0.0.1:1234"))
|
||||
t.Log(utils.FormatAddress("127.0.0.1 : 1234"))
|
||||
t.Log(utils.FormatAddress("127.0.0.1:1234"))
|
||||
}
|
||||
|
||||
func TestFormatAddressList(t *testing.T) {
|
||||
t.Log(FormatAddressList([]string{
|
||||
t.Log(utils.FormatAddressList([]string{
|
||||
"127.0.0.1:1234",
|
||||
"127.0.0.1 : 1234",
|
||||
"127.0.0.1:1234",
|
||||
}))
|
||||
}
|
||||
|
||||
func TestContainsSameStrings(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
a.IsFalse(utils.ContainsSameStrings([]string{"a"}, []string{"b"}))
|
||||
a.IsFalse(utils.ContainsSameStrings([]string{"a", "b"}, []string{"b"}))
|
||||
a.IsFalse(utils.ContainsSameStrings([]string{"a", "b"}, []string{"a", "b", "c"}))
|
||||
a.IsTrue(utils.ContainsSameStrings([]string{"a", "b"}, []string{"a", "b"}))
|
||||
a.IsTrue(utils.ContainsSameStrings([]string{"a", "b"}, []string{"b", "a"}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user