@ -8,6 +8,7 @@ package portlist
import (
import (
"bufio"
"bufio"
"encoding/json"
"encoding/json"
"fmt"
"strings"
"strings"
"testing"
"testing"
@ -52,30 +53,40 @@ udp46 0 0 *.146 *.*
`
`
func TestParsePortsNetstat ( t * testing . T ) {
func TestParsePortsNetstat ( t * testing . T ) {
want := List {
for _ , loopBack := range [ ... ] bool { false , true } {
Port { "tcp" , 23 , "" } ,
t . Run ( fmt . Sprintf ( "loopback_%v" , loopBack ) , func ( t * testing . T ) {
Port { "tcp" , 24 , "" } ,
want := List {
Port { "udp" , 104 , "" } ,
{ "tcp" , 23 , "" , 0 } ,
Port { "udp" , 106 , "" } ,
{ "tcp" , 24 , "" , 0 } ,
Port { "udp" , 146 , "" } ,
{ "udp" , 104 , "" , 0 } ,
Port { "tcp" , 8185 , "" } , // but not 8186, 8187, 8188 on localhost
{ "udp" , 106 , "" , 0 } ,
}
{ "udp" , 146 , "" , 0 } ,
{ "tcp" , 8185 , "" , 0 } , // but not 8186, 8187, 8188 on localhost, when loopback is false
pl , err := appendParsePortsNetstat ( nil , bufio . NewReader ( strings . NewReader ( netstatOutput ) ) )
}
if err != nil {
if loopBack {
t . Fatal ( err )
want = append ( want ,
}
Port { "tcp" , 8186 , "" , 0 } ,
pl = sortAndDedup ( pl )
Port { "tcp" , 8187 , "" , 0 } ,
jgot , _ := json . MarshalIndent ( pl , "" , "\t" )
Port { "tcp" , 8188 , "" , 0 } ,
jwant , _ := json . MarshalIndent ( want , "" , "\t" )
)
if len ( pl ) != len ( want ) {
}
t . Fatalf ( "Got:\n%s\n\nWant:\n%s\n" , jgot , jwant )
pl , err := appendParsePortsNetstat ( nil , bufio . NewReader ( strings . NewReader ( netstatOutput ) ) , loopBack )
}
if err != nil {
for i := range pl {
t . Fatal ( err )
if pl [ i ] != want [ i ] {
}
t . Errorf ( "row#%d\n got: %+v\n\nwant: %+v\n" ,
pl = sortAndDedup ( pl )
i , pl [ i ] , want [ i ] )
jgot , _ := json . MarshalIndent ( pl , "" , "\t" )
t . Fatalf ( "Got:\n%s\n\nWant:\n%s\n" , jgot , jwant )
jwant , _ := json . MarshalIndent ( want , "" , "\t" )
}
if len ( pl ) != len ( want ) {
t . Fatalf ( "Got:\n%s\n\nWant:\n%s\n" , jgot , jwant )
}
for i := range pl {
if pl [ i ] != want [ i ] {
t . Errorf ( "row#%d\n got: %+v\n\nwant: %+v\n" ,
i , pl [ i ] , want [ i ] )
t . Fatalf ( "Got:\n%s\n\nWant:\n%s\n" , jgot , jwant )
}
}
} )
}
}
}
}