master
go 173 lines 3.74 KB
Raw
1 package integrationtest
2
3 import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "io"
9 "math"
10 "os"
11 "testing"
12 "time"
13
14 "github.com/ipfs/boxo/bootstrap"
15 "github.com/ipfs/boxo/files"
16 logging "github.com/ipfs/go-log/v2"
17 "github.com/ipfs/go-test/random"
18 "github.com/ipfs/kubo/core"
19 "github.com/ipfs/kubo/core/coreapi"
20 mock "github.com/ipfs/kubo/core/mock"
21 "github.com/ipfs/kubo/thirdparty/unit"
22 testutil "github.com/libp2p/go-libp2p-testing/net"
23 "github.com/libp2p/go-libp2p/core/peer"
24 mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
25 )
26
27 var log = logging.Logger("epictest")
28
29 const kSeed = 1
30
31 func Test1KBInstantaneous(t *testing.T) {
32 conf := testutil.LatencyConfig{
33 NetworkLatency: 0,
34 RoutingLatency: 0,
35 BlockstoreLatency: 0,
36 }
37
38 if err := DirectAddCat(RandomBytes(1*unit.KB), conf); err != nil {
39 t.Fatal(err)
40 }
41 }
42
43 func TestDegenerateSlowBlockstore(t *testing.T) {
44 SkipUnlessEpic(t)
45 conf := testutil.LatencyConfig{BlockstoreLatency: 50 * time.Millisecond}
46 if err := AddCatPowers(conf, 128); err != nil {
47 t.Fatal(err)
48 }
49 }
50
51 func TestDegenerateSlowNetwork(t *testing.T) {
52 SkipUnlessEpic(t)
53 conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond}
54 if err := AddCatPowers(conf, 128); err != nil {
55 t.Fatal(err)
56 }
57 }
58
59 func TestDegenerateSlowRouting(t *testing.T) {
60 SkipUnlessEpic(t)
61 conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond}
62 if err := AddCatPowers(conf, 128); err != nil {
63 t.Fatal(err)
64 }
65 }
66
67 func Test100MBMacbookCoastToCoast(t *testing.T) {
68 SkipUnlessEpic(t)
69 conf := testutil.LatencyConfig{}.NetworkNYtoSF().BlockstoreSlowSSD2014().RoutingSlow()
70 if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil {
71 t.Fatal(err)
72 }
73 }
74
75 func AddCatPowers(conf testutil.LatencyConfig, megabytesMax int64) error {
76 var i int64
77 for i = 1; i < megabytesMax; i = i * 2 {
78 fmt.Printf("%d MB\n", i)
79 if err := DirectAddCat(RandomBytes(i*1024*1024), conf); err != nil {
80 return err
81 }
82 }
83 return nil
84 }
85
86 func RandomBytes(n int64) []byte {
87 random.SetSeed(kSeed)
88 return random.Bytes(int(n))
89 }
90
91 func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
92 ctx, cancel := context.WithCancel(context.Background())
93 defer cancel()
94
95 // create network
96 mn := mocknet.New()
97 mn.SetLinkDefaults(mocknet.LinkOptions{
98 Latency: conf.NetworkLatency,
99 // TODO add to conf. This is tricky because we want 0 values to be functional.
100 Bandwidth: math.MaxInt32,
101 })
102
103 adder, err := core.NewNode(ctx, &core.BuildCfg{
104 Online: true,
105 Host: mock.MockHostOption(mn),
106 })
107 if err != nil {
108 return err
109 }
110 defer adder.Close()
111
112 catter, err := core.NewNode(ctx, &core.BuildCfg{
113 Online: true,
114 Host: mock.MockHostOption(mn),
115 })
116 if err != nil {
117 return err
118 }
119 defer catter.Close()
120
121 adderAPI, err := coreapi.NewCoreAPI(adder)
122 if err != nil {
123 return err
124 }
125
126 catterAPI, err := coreapi.NewCoreAPI(catter)
127 if err != nil {
128 return err
129 }
130
131 err = mn.LinkAll()
132 if err != nil {
133 return err
134 }
135
136 bs1 := []peer.AddrInfo{adder.Peerstore.PeerInfo(adder.Identity)}
137 bs2 := []peer.AddrInfo{catter.Peerstore.PeerInfo(catter.Identity)}
138
139 if err := catter.Bootstrap(bootstrap.BootstrapConfigWithPeers(bs1)); err != nil {
140 return err
141 }
142 if err := adder.Bootstrap(bootstrap.BootstrapConfigWithPeers(bs2)); err != nil {
143 return err
144 }
145
146 added, err := adderAPI.Unixfs().Add(ctx, files.NewBytesFile(data))
147 if err != nil {
148 return err
149 }
150
151 readerCatted, err := catterAPI.Unixfs().Get(ctx, added)
152 if err != nil {
153 return err
154 }
155
156 // verify
157 var bufout bytes.Buffer
158 _, err = io.Copy(&bufout, readerCatted.(io.Reader))
159 if err != nil {
160 return err
161 }
162 if !bytes.Equal(bufout.Bytes(), data) {
163 return errors.New("catted data does not match added data")
164 }
165
166 return nil
167 }
168
169 func SkipUnlessEpic(t *testing.T) {
170 if os.Getenv("IPFS_EPIC_TEST") == "" {
171 t.SkipNow()
172 }
173 }