github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/protocol/relay/relay_test.go (about) 1 package relay_test 2 3 import ( 4 "io" 5 "testing" 6 7 inet "github.com/ipfs/go-ipfs/p2p/net" 8 protocol "github.com/ipfs/go-ipfs/p2p/protocol" 9 relay "github.com/ipfs/go-ipfs/p2p/protocol/relay" 10 testutil "github.com/ipfs/go-ipfs/p2p/test/util" 11 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog" 12 13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 14 ) 15 16 var log = eventlog.Logger("relay_test") 17 18 func TestRelaySimple(t *testing.T) { 19 20 ctx := context.Background() 21 22 // these networks have the relay service wired in already. 23 n1 := testutil.GenHostSwarm(t, ctx) 24 n2 := testutil.GenHostSwarm(t, ctx) 25 n3 := testutil.GenHostSwarm(t, ctx) 26 27 n1p := n1.ID() 28 n2p := n2.ID() 29 n3p := n3.ID() 30 31 n2pi := n2.Peerstore().PeerInfo(n2p) 32 if err := n1.Connect(ctx, n2pi); err != nil { 33 t.Fatal("Failed to connect:", err) 34 } 35 if err := n3.Connect(ctx, n2pi); err != nil { 36 t.Fatal("Failed to connect:", err) 37 } 38 39 // setup handler on n3 to copy everything over to the pipe. 40 piper, pipew := io.Pipe() 41 n3.SetStreamHandler(protocol.TestingID, func(s inet.Stream) { 42 log.Debug("relay stream opened to n3!") 43 log.Debug("piping and echoing everything") 44 w := io.MultiWriter(s, pipew) 45 io.Copy(w, s) 46 log.Debug("closing stream") 47 s.Close() 48 }) 49 50 // ok, now we can try to relay n1--->n2--->n3. 51 log.Debug("open relay stream") 52 s, err := n1.NewStream(relay.ID, n2p) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 // ok first thing we write the relay header n1->n3 58 log.Debug("write relay header") 59 if err := relay.WriteHeader(s, n1p, n3p); err != nil { 60 t.Fatal(err) 61 } 62 63 // ok now the header's there, we can write the next protocol header. 64 log.Debug("write testing header") 65 if err := protocol.WriteHeader(s, protocol.TestingID); err != nil { 66 t.Fatal(err) 67 } 68 69 // okay, now we should be able to write text, and read it out. 70 buf1 := []byte("abcdefghij") 71 buf2 := make([]byte, 10) 72 buf3 := make([]byte, 10) 73 log.Debug("write in some text.") 74 if _, err := s.Write(buf1); err != nil { 75 t.Fatal(err) 76 } 77 78 // read it out from the pipe. 79 log.Debug("read it out from the pipe.") 80 if _, err := io.ReadFull(piper, buf2); err != nil { 81 t.Fatal(err) 82 } 83 if string(buf1) != string(buf2) { 84 t.Fatal("should've gotten that text out of the pipe") 85 } 86 87 // read it out from the stream (echoed) 88 log.Debug("read it out from the stream (echoed).") 89 if _, err := io.ReadFull(s, buf3); err != nil { 90 t.Fatal(err) 91 } 92 if string(buf1) != string(buf3) { 93 t.Fatal("should've gotten that text out of the stream") 94 } 95 96 // sweet. relay works. 97 log.Debug("sweet, relay works.") 98 s.Close() 99 } 100 101 func TestRelayAcrossFour(t *testing.T) { 102 103 ctx := context.Background() 104 105 // these networks have the relay service wired in already. 106 n1 := testutil.GenHostSwarm(t, ctx) 107 n2 := testutil.GenHostSwarm(t, ctx) 108 n3 := testutil.GenHostSwarm(t, ctx) 109 n4 := testutil.GenHostSwarm(t, ctx) 110 n5 := testutil.GenHostSwarm(t, ctx) 111 112 n1p := n1.ID() 113 n2p := n2.ID() 114 n3p := n3.ID() 115 n4p := n4.ID() 116 n5p := n5.ID() 117 118 n2pi := n2.Peerstore().PeerInfo(n2p) 119 n4pi := n4.Peerstore().PeerInfo(n4p) 120 121 if err := n1.Connect(ctx, n2pi); err != nil { 122 t.Fatalf("Failed to dial:", err) 123 } 124 if err := n3.Connect(ctx, n2pi); err != nil { 125 t.Fatalf("Failed to dial:", err) 126 } 127 if err := n3.Connect(ctx, n4pi); err != nil { 128 t.Fatalf("Failed to dial:", err) 129 } 130 if err := n5.Connect(ctx, n4pi); err != nil { 131 t.Fatalf("Failed to dial:", err) 132 } 133 134 // setup handler on n5 to copy everything over to the pipe. 135 piper, pipew := io.Pipe() 136 n5.SetStreamHandler(protocol.TestingID, func(s inet.Stream) { 137 log.Debug("relay stream opened to n5!") 138 log.Debug("piping and echoing everything") 139 w := io.MultiWriter(s, pipew) 140 io.Copy(w, s) 141 log.Debug("closing stream") 142 s.Close() 143 }) 144 145 // ok, now we can try to relay n1--->n2--->n3--->n4--->n5 146 log.Debug("open relay stream") 147 s, err := n1.NewStream(relay.ID, n2p) 148 if err != nil { 149 t.Fatal(err) 150 } 151 152 log.Debugf("write relay header n1->n3 (%s -> %s)", n1p, n3p) 153 if err := relay.WriteHeader(s, n1p, n3p); err != nil { 154 t.Fatal(err) 155 } 156 157 log.Debugf("write relay header n1->n4 (%s -> %s)", n1p, n4p) 158 if err := protocol.WriteHeader(s, relay.ID); err != nil { 159 t.Fatal(err) 160 } 161 if err := relay.WriteHeader(s, n1p, n4p); err != nil { 162 t.Fatal(err) 163 } 164 165 log.Debugf("write relay header n1->n5 (%s -> %s)", n1p, n5p) 166 if err := protocol.WriteHeader(s, relay.ID); err != nil { 167 t.Fatal(err) 168 } 169 if err := relay.WriteHeader(s, n1p, n5p); err != nil { 170 t.Fatal(err) 171 } 172 173 // ok now the header's there, we can write the next protocol header. 174 log.Debug("write testing header") 175 if err := protocol.WriteHeader(s, protocol.TestingID); err != nil { 176 t.Fatal(err) 177 } 178 179 // okay, now we should be able to write text, and read it out. 180 buf1 := []byte("abcdefghij") 181 buf2 := make([]byte, 10) 182 buf3 := make([]byte, 10) 183 log.Debug("write in some text.") 184 if _, err := s.Write(buf1); err != nil { 185 t.Fatal(err) 186 } 187 188 // read it out from the pipe. 189 log.Debug("read it out from the pipe.") 190 if _, err := io.ReadFull(piper, buf2); err != nil { 191 t.Fatal(err) 192 } 193 if string(buf1) != string(buf2) { 194 t.Fatal("should've gotten that text out of the pipe") 195 } 196 197 // read it out from the stream (echoed) 198 log.Debug("read it out from the stream (echoed).") 199 if _, err := io.ReadFull(s, buf3); err != nil { 200 t.Fatal(err) 201 } 202 if string(buf1) != string(buf3) { 203 t.Fatal("should've gotten that text out of the stream") 204 } 205 206 // sweet. relay works. 207 log.Debug("sweet, relaying across 4 works.") 208 s.Close() 209 } 210 211 func TestRelayStress(t *testing.T) { 212 buflen := 1 << 18 213 iterations := 10 214 215 ctx := context.Background() 216 217 // these networks have the relay service wired in already. 218 n1 := testutil.GenHostSwarm(t, ctx) 219 n2 := testutil.GenHostSwarm(t, ctx) 220 n3 := testutil.GenHostSwarm(t, ctx) 221 222 n1p := n1.ID() 223 n2p := n2.ID() 224 n3p := n3.ID() 225 226 n2pi := n2.Peerstore().PeerInfo(n2p) 227 if err := n1.Connect(ctx, n2pi); err != nil { 228 t.Fatalf("Failed to dial:", err) 229 } 230 if err := n3.Connect(ctx, n2pi); err != nil { 231 t.Fatalf("Failed to dial:", err) 232 } 233 234 // setup handler on n3 to copy everything over to the pipe. 235 piper, pipew := io.Pipe() 236 n3.SetStreamHandler(protocol.TestingID, func(s inet.Stream) { 237 log.Debug("relay stream opened to n3!") 238 log.Debug("piping and echoing everything") 239 w := io.MultiWriter(s, pipew) 240 io.Copy(w, s) 241 log.Debug("closing stream") 242 s.Close() 243 }) 244 245 // ok, now we can try to relay n1--->n2--->n3. 246 log.Debug("open relay stream") 247 s, err := n1.NewStream(relay.ID, n2p) 248 if err != nil { 249 t.Fatal(err) 250 } 251 252 // ok first thing we write the relay header n1->n3 253 log.Debug("write relay header") 254 if err := relay.WriteHeader(s, n1p, n3p); err != nil { 255 t.Fatal(err) 256 } 257 258 // ok now the header's there, we can write the next protocol header. 259 log.Debug("write testing header") 260 if err := protocol.WriteHeader(s, protocol.TestingID); err != nil { 261 t.Fatal(err) 262 } 263 264 // okay, now write lots of text and read it back out from both 265 // the pipe and the stream. 266 buf1 := make([]byte, buflen) 267 buf2 := make([]byte, len(buf1)) 268 buf3 := make([]byte, len(buf1)) 269 270 fillbuf := func(buf []byte, b byte) { 271 for i := range buf { 272 buf[i] = b 273 } 274 } 275 276 for i := 0; i < iterations; i++ { 277 fillbuf(buf1, byte(int('a')+i)) 278 log.Debugf("writing %d bytes (%d/%d)", len(buf1), i, iterations) 279 if _, err := s.Write(buf1); err != nil { 280 t.Fatal(err) 281 } 282 283 log.Debug("read it out from the pipe.") 284 if _, err := io.ReadFull(piper, buf2); err != nil { 285 t.Fatal(err) 286 } 287 if string(buf1) != string(buf2) { 288 t.Fatal("should've gotten that text out of the pipe") 289 } 290 291 // read it out from the stream (echoed) 292 log.Debug("read it out from the stream (echoed).") 293 if _, err := io.ReadFull(s, buf3); err != nil { 294 t.Fatal(err) 295 } 296 if string(buf1) != string(buf3) { 297 t.Fatal("should've gotten that text out of the stream") 298 } 299 } 300 301 log.Debug("sweet, relay works under stress.") 302 s.Close() 303 }