github.com/NebulousLabs/Sia@v1.3.7/modules/gateway/ip_test.go (about) 1 package gateway 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/NebulousLabs/Sia/encoding" 8 "github.com/NebulousLabs/Sia/modules" 9 ) 10 11 // TestIpRPC tests the ip discovery RPC. 12 func TestIpRPC(t *testing.T) { 13 if testing.Short() { 14 t.SkipNow() 15 } 16 t.Parallel() 17 18 // Create gateways for testing. 19 g1 := newNamedTestingGateway(t, "1") 20 defer g1.Close() 21 g2 := newNamedTestingGateway(t, "2") 22 defer g2.Close() 23 24 // Connect gateways. 25 err := g1.Connect(g2.Address()) 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 // Call RPC 31 err = g1.RPC(g2.Address(), "DiscoverIP", func(conn modules.PeerConn) error { 32 var address string 33 err := encoding.ReadObject(conn, &address, 100) 34 if err != nil { 35 t.Error("failed to read object from response", err) 36 } 37 if address != g1.Address().Host() { 38 return fmt.Errorf("ip addresses don't match %v != %v", g1.Address().Host(), address) 39 } 40 return nil 41 }) 42 if err != nil { 43 t.Fatal("RPC failed", err) 44 } 45 } 46 47 // TestIpFromPeers test the functionality of managedIPFromPeers. 48 func TestIPFromPeers(t *testing.T) { 49 if testing.Short() { 50 t.SkipNow() 51 } 52 t.Parallel() 53 54 // Create gateways for testing. 55 g1 := newNamedTestingGateway(t, "1") 56 defer g1.Close() 57 g2 := newNamedTestingGateway(t, "2") 58 defer g2.Close() 59 g3 := newNamedTestingGateway(t, "3") 60 defer g2.Close() 61 62 // Connect gateways. 63 err := g1.Connect(g2.Address()) 64 if err != nil { 65 t.Fatal(err) 66 } 67 err = g1.Connect(g3.Address()) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 // Discover ip using the peers 73 host, err := g1.managedIPFromPeers() 74 if err != nil { 75 t.Fatal("failed to get ip", err) 76 } 77 if host != g1.Address().Host() { 78 t.Fatalf("ip should be %v but was %v", g1.Address().Host(), host) 79 } 80 }