| 1 | package integrationtest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | |
| 7 | blocks "github.com/ipfs/go-block-format" |
| 8 | "github.com/ipfs/go-cid" |
| 9 | "github.com/ipfs/kubo/core" |
| 10 | coremock "github.com/ipfs/kubo/core/mock" |
| 11 | "github.com/ipfs/kubo/core/node/libp2p" |
| 12 | mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" |
| 13 | ) |
| 14 | |
| 15 | func TestBitswapWithoutRouting(t *testing.T) { |
| 16 | ctx := t.Context() |
| 17 | const numPeers = 4 |
| 18 | |
| 19 | // create network |
| 20 | mn := mocknet.New() |
| 21 | |
| 22 | var nodes []*core.IpfsNode |
| 23 | for range numPeers { |
| 24 | n, err := core.NewNode(ctx, &core.BuildCfg{ |
| 25 | Online: true, |
| 26 | Host: coremock.MockHostOption(mn), |
| 27 | Routing: libp2p.NilRouterOption, // no routing |
| 28 | }) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | defer n.Close() |
| 33 | nodes = append(nodes, n) |
| 34 | } |
| 35 | |
| 36 | err := mn.LinkAll() |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | |
| 41 | // connect them |
| 42 | for _, n1 := range nodes { |
| 43 | for _, n2 := range nodes { |
| 44 | if n1 == n2 { |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | log.Debug("connecting to other hosts") |
| 49 | p2 := n2.PeerHost.Peerstore().PeerInfo(n2.PeerHost.ID()) |
| 50 | if err := n1.PeerHost.Connect(ctx, p2); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // add blocks to each before |
| 57 | log.Debug("adding block.") |
| 58 | block0 := blocks.NewBlock([]byte("block0")) |
| 59 | block1 := blocks.NewBlock([]byte("block1")) |
| 60 | |
| 61 | // put 1 before |
| 62 | if err := nodes[0].Blockstore.Put(ctx, block0); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | // get it out. |
| 67 | for i, n := range nodes { |
| 68 | // skip first because block not in its exchange. will hang. |
| 69 | if i == 0 { |
| 70 | continue |
| 71 | } |
| 72 | |
| 73 | log.Debugf("%d %s get block.", i, n.Identity) |
| 74 | b, err := n.Blocks.GetBlock(ctx, cid.NewCidV0(block0.Multihash())) |
| 75 | if err != nil { |
| 76 | t.Error(err) |
| 77 | } else if !bytes.Equal(b.RawData(), block0.RawData()) { |
| 78 | t.Error("byte comparison fail") |
| 79 | } else { |
| 80 | log.Debug("got block: %s", b.Cid()) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // put 1 after |
| 85 | if err := nodes[1].Blockstore.Put(ctx, block1); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | |
| 89 | // get it out. |
| 90 | for _, n := range nodes { |
| 91 | b, err := n.Blocks.GetBlock(ctx, cid.NewCidV0(block1.Multihash())) |
| 92 | if err != nil { |
| 93 | t.Error(err) |
| 94 | } else if !bytes.Equal(b.RawData(), block1.RawData()) { |
| 95 | t.Error("byte comparison fail") |
| 96 | } else { |
| 97 | log.Debug("got block: %s", b.Cid()) |
| 98 | } |
| 99 | } |
| 100 | } |