dragonfly/internal/idgen/peer_id_test.go

104 lines
2.4 KiB
Go

/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package idgen
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPeerID(t *testing.T) {
tests := []struct {
name string
ip string
expect func(t *testing.T, d interface{})
}{
{
name: "generate PeerID with ipv4",
ip: "127.0.0.1",
expect: func(t *testing.T, d interface{}) {
assert := assert.New(t)
assert.Regexp("127.0.0.1-.*-.*", d)
},
},
{
name: "generate PeerID with ipv6",
ip: "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b",
expect: func(t *testing.T, d interface{}) {
assert := assert.New(t)
assert.Regexp("2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b-.*-.*", d)
},
},
{
name: "generate PeerID with empty string",
ip: "",
expect: func(t *testing.T, d interface{}) {
assert := assert.New(t)
assert.Regexp("-.*-.*", d)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
data := PeerID(tc.ip)
tc.expect(t, data)
})
}
}
func TestCDNPeerID(t *testing.T) {
tests := []struct {
name string
ip string
expect func(t *testing.T, d interface{})
}{
{
name: "generate CDNPeerID with ipv4",
ip: "127.0.0.1",
expect: func(t *testing.T, d interface{}) {
assert := assert.New(t)
assert.Regexp("127.0.0.1-.*-.*_CDN", d)
},
},
{
name: "generate CDNPeerID with ipv6",
ip: "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b",
expect: func(t *testing.T, d interface{}) {
assert := assert.New(t)
assert.Regexp("2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b-.*-.*_CDN", d)
},
},
{
name: "generate CDNPeerID with empty string",
ip: "",
expect: func(t *testing.T, d interface{}) {
assert := assert.New(t)
assert.Regexp("-.*-.*_CDN", d)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
data := CDNPeerID(tc.ip)
tc.expect(t, data)
})
}
}