2022-01-19 18:30:05 +01:00
|
|
|
package server_test
|
2021-11-24 22:03:42 +01:00
|
|
|
|
|
|
|
import (
|
2021-11-25 00:24:06 +01:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-01-19 18:30:05 +01:00
|
|
|
|
|
|
|
server "github.com/lbryio/hub/server"
|
2021-11-24 22:03:42 +01:00
|
|
|
)
|
|
|
|
|
2021-12-02 01:32:23 +01:00
|
|
|
// TestUDPPing tests UDPPing correctness against prod server.
|
2021-11-24 22:03:42 +01:00
|
|
|
func TestUDPPing(t *testing.T) {
|
2021-11-25 00:24:06 +01:00
|
|
|
args := makeDefaultArgs()
|
2021-11-25 00:56:34 +01:00
|
|
|
args.DisableStartUDP = true
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
wantIP string
|
|
|
|
wantCountry string
|
|
|
|
wantProtocolVersion int
|
|
|
|
wantHeightMin int
|
|
|
|
wantFlags byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Correctly parse information from production server.",
|
|
|
|
wantIP: "SETME",
|
|
|
|
wantCountry: "US",
|
|
|
|
wantProtocolVersion: 1,
|
|
|
|
wantHeightMin: 1060000,
|
|
|
|
wantFlags: 1,
|
|
|
|
},
|
|
|
|
}
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2022-02-08 18:50:37 +01:00
|
|
|
toAddr := "spv17.lbry.com"
|
2021-11-25 00:24:06 +01:00
|
|
|
toPort := "50001"
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
pong, err := server.UDPPing(toAddr, toPort)
|
2021-11-25 00:24:06 +01:00
|
|
|
gotCountry := pong.DecodeCountry()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
res, err := exec.Command("dig", "@resolver4.opendns.com", "myip.opendns.com", "+short").Output()
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
digIP := strings.TrimSpace(string(res))
|
|
|
|
udpIP := pong.DecodeAddress().String()
|
|
|
|
tt.wantIP = digIP
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
log.Println("Height:", pong.DecodeHeight())
|
|
|
|
log.Printf("Flags: %x\n", pong.DecodeFlags())
|
|
|
|
log.Println("ProtocolVersion:", pong.DecodeProtocolVersion())
|
|
|
|
log.Printf("Tip: %x\n", pong.DecodeTip())
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
gotHeight := pong.DecodeHeight()
|
|
|
|
gotProtocolVersion := pong.DecodeProtocolVersion()
|
|
|
|
gotFlags := pong.DecodeFlags()
|
|
|
|
gotIP := udpIP
|
2021-11-24 23:58:05 +01:00
|
|
|
|
2021-11-25 00:24:06 +01:00
|
|
|
if gotIP != tt.wantIP {
|
|
|
|
t.Errorf("ip: got: '%s', want: '%s'\n", gotIP, tt.wantIP)
|
|
|
|
}
|
|
|
|
if gotCountry != tt.wantCountry {
|
|
|
|
t.Errorf("country: got: '%s', want: '%s'\n", gotCountry, tt.wantCountry)
|
|
|
|
}
|
|
|
|
if gotHeight < tt.wantHeightMin {
|
|
|
|
t.Errorf("height: got: %d, want >=: %d\n", gotHeight, tt.wantHeightMin)
|
|
|
|
}
|
|
|
|
if gotProtocolVersion != tt.wantProtocolVersion {
|
|
|
|
t.Errorf("protocolVersion: got: %d, want: %d\n", gotProtocolVersion, tt.wantProtocolVersion)
|
|
|
|
}
|
|
|
|
if gotFlags != tt.wantFlags {
|
|
|
|
t.Errorf("flags: got: %d, want: %d\n", gotFlags, tt.wantFlags)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-11-24 22:03:42 +01:00
|
|
|
|
|
|
|
}
|