@ -11,26 +11,62 @@ import (
)
func TestNetworkMapConcise ( t * testing . T ) {
nm := & NetworkMap {
Peers : [ ] * tailcfg . Node {
{
Name : "foo" ,
Endpoints : [ ] string { "192.168.0.100:12" , "192.168.0.100:12354" } ,
nodekey := func ( b byte ) ( ret tailcfg . NodeKey ) {
for i := range ret {
ret [ i ] = b
}
return
}
for _ , tt := range [ ] struct {
name string
nm * NetworkMap
want string
} {
{
name : "basic" ,
nm : & NetworkMap {
NodeKey : nodekey ( 1 ) ,
Peers : [ ] * tailcfg . Node {
{
Key : nodekey ( 2 ) ,
DERP : "127.3.3.40:2" ,
Endpoints : [ ] string { "192.168.0.100:12" , "192.168.0.100:12354" } ,
} ,
{
Key : nodekey ( 3 ) ,
DERP : "127.3.3.40:4" ,
Endpoints : [ ] string { "10.2.0.100:12" , "10.1.0.100:12345" } ,
} ,
} ,
} ,
{
Name : "bar" ,
Endpoints : [ ] string { "10.2.0.100:12" , "10.1.0.100:12345" } ,
want : "netmap: self: [AQEBA] auth=machine-unknown []\n [AgICA] D2 : 192.168.0.100:12 192.168.0.100:12354\n [AwMDA] D4 : 10.2.0.100:12 10.1.0.100:12345\n" ,
} ,
{
name : "debug_non_nil" ,
nm : & NetworkMap {
NodeKey : nodekey ( 1 ) ,
Debug : & tailcfg . Debug { } ,
} ,
want : "netmap: self: [AQEBA] auth=machine-unknown debug={} []\n" ,
} ,
}
var got string
n := int ( testing . AllocsPerRun ( 1000 , func ( ) {
got = nm . Concise ( )
} ) )
t . Logf ( "Allocs = %d" , n )
want := "netmap: self: [AAAAA] auth=machine-unknown :0 []\n" +
" [AAAAA] : 192.168.0.100:12 192.168.0.100:12354\n [AAAAA] : 10.2.0.100:12 10.1.0.100:12345\n"
if got != want {
t . Errorf ( "Wrong output\n Got: %q\nWant: %q\n## Got (unescaped):\n%s\n## Want (unescaped):\n%s\n" , got , want , got , want )
{
name : "debug_values" ,
nm : & NetworkMap {
NodeKey : nodekey ( 1 ) ,
Debug : & tailcfg . Debug { LogHeapPprof : true } ,
} ,
want : "netmap: self: [AQEBA] auth=machine-unknown debug={\"LogHeapPprof\":true} []\n" ,
} ,
} {
t . Run ( tt . name , func ( t * testing . T ) {
var got string
n := int ( testing . AllocsPerRun ( 1000 , func ( ) {
got = tt . nm . Concise ( )
} ) )
t . Logf ( "Allocs = %d" , n )
if got != tt . want {
t . Errorf ( "Wrong output\n Got: %q\nWant: %q\n## Got (unescaped):\n%s\n## Want (unescaped):\n%s\n" , got , tt . want , got , tt . want )
}
} )
}
}