ext_test.go 7.71 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5
package dht

import (
	"testing"

Jeromy's avatar
Jeromy committed
6
	crand "crypto/rand"
Jeromy's avatar
Jeromy committed
7

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
10

11
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
	msg "github.com/jbenet/go-ipfs/net/message"
	mux "github.com/jbenet/go-ipfs/net/mux"
Jeromy's avatar
Jeromy committed
14
	peer "github.com/jbenet/go-ipfs/peer"
15
	"github.com/jbenet/go-ipfs/peer/mock"
16
	"github.com/jbenet/go-ipfs/routing"
17
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
18
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
19

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
	"sync"
Jeromy's avatar
Jeromy committed
21 22 23
	"time"
)

24 25 26 27 28 29
// mesHandleFunc is a function that takes in outgoing messages
// and can respond to them, simulating other peers on the network.
// returning nil will chose not to respond and pass the message onto the
// next registered handler
type mesHandleFunc func(msg.NetMessage) msg.NetMessage

Jeromy's avatar
Jeromy committed
30 31
// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
type fauxSender struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
	sync.Mutex
34
	handlers []mesHandleFunc
Jeromy's avatar
Jeromy committed
35 36
}

37
func (f *fauxSender) AddHandler(fn func(msg.NetMessage) msg.NetMessage) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40
	f.Lock()
	defer f.Unlock()

41 42
	f.handlers = append(f.handlers, fn)
}
Jeromy's avatar
Jeromy committed
43

44
func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47 48 49 50
	f.Lock()
	handlers := make([]mesHandleFunc, len(f.handlers))
	copy(handlers, f.handlers)
	f.Unlock()

	for _, h := range handlers {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52 53 54 55
		reply := h(m)
		if reply != nil {
			return reply, nil
		}
	}
Jeromy's avatar
Jeromy committed
56

57 58 59 60 61 62
	// no reply? ok force a timeout
	select {
	case <-ctx.Done():
	}

	return nil, ctx.Err()
Jeromy's avatar
Jeromy committed
63 64
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66 67 68 69 70 71
	f.Lock()
	handlers := make([]mesHandleFunc, len(f.handlers))
	copy(handlers, f.handlers)
	f.Unlock()

	for _, h := range handlers {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73 74
		reply := h(m)
		if reply != nil {
			return nil
Jeromy's avatar
Jeromy committed
75
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	}
Jeromy's avatar
Jeromy committed
77 78 79
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82
// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
type fauxNet struct {
83
}
Jeromy's avatar
Jeromy committed
84

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
// DialPeer attempts to establish a connection to a given peer
86
func (f *fauxNet) DialPeer(context.Context, peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
	return nil
88 89
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
// ClosePeer connection to peer
91
func (f *fauxNet) ClosePeer(peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
	return nil
Jeromy's avatar
Jeromy committed
93 94
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95
// IsConnected returns whether a connection to given peer exists.
96
func (f *fauxNet) IsConnected(peer.Peer) (bool, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97
	return true, nil
98 99
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101 102 103 104 105
// GetProtocols returns the protocols registered in the network.
func (f *fauxNet) GetProtocols() *mux.ProtocolMap { return nil }

// SendMessage sends given Message out
func (f *fauxNet) SendMessage(msg.NetMessage) error {
	return nil
Jeromy's avatar
Jeromy committed
106 107
}

108
func (f *fauxNet) GetPeerList() []peer.Peer {
109 110 111
	return nil
}

112 113 114 115
func (f *fauxNet) GetBandwidthTotals() (uint64, uint64) {
	return 0, 0
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116 117 118
// Close terminates all network operation
func (f *fauxNet) Close() error { return nil }

119
func TestGetFailures(t *testing.T) {
120 121 122
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
123

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124 125 126
	ctx := context.Background()
	fn := &fauxNet{}
	fs := &fauxSender{}
Jeromy's avatar
Jeromy committed
127

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
128
	peerstore := peer.NewPeerstore()
129
	local := makePeer(nil)
Jeromy's avatar
Jeromy committed
130

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131
	d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
132
	other := makePeer(nil)
133
	d.Update(ctx, other)
134 135

	// This one should time out
136
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137 138
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
	_, err := d.GetValue(ctx1, u.Key("test"))
Jeromy's avatar
Jeromy committed
139
	if err != nil {
140 141
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
142 143 144
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
145 146
	}

147
	// u.POut("NotFound Test\n")
148
	// Reply with failures to every message
149
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
150
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151
		err := proto.Unmarshal(mes.Data(), pmes)
152 153 154 155
		if err != nil {
			t.Fatal(err)
		}

156
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157
			Type: pmes.Type,
158
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159 160
		m, err := msg.FromObject(mes.Peer(), resp)
		return m
161 162 163
	})

	// This one should fail with NotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164 165
	ctx2, _ := context.WithTimeout(context.Background(), time.Second)
	_, err = d.GetValue(ctx2, u.Key("test"))
166
	if err != nil {
167
		if err != routing.ErrNotFound {
168
			t.Fatalf("Expected ErrNotFound, got: %s", err)
169 170 171 172
		}
	} else {
		t.Fatal("expected error, got none.")
	}
173

174
	fs.handlers = nil
175
	// Now we test this DHT's handleGetValue failure
176
	typ := pb.Message_GET_VALUE
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
177
	str := "hello"
178 179 180 181
	rec, err := d.makePutRecord(u.Key(str), []byte("blah"))
	if err != nil {
		t.Fatal(err)
	}
182
	req := pb.Message{
183 184 185
		Type:   &typ,
		Key:    &str,
		Record: rec,
186
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187

188
	// u.POut("handleGetValue Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
189 190 191 192 193
	mes, err := msg.FromObject(other, &req)
	if err != nil {
		t.Error(err)
	}

194
	mes = d.HandleMessage(ctx, mes)
195

196
	pmes := new(pb.Message)
197 198 199 200
	err = proto.Unmarshal(mes.Data(), pmes)
	if err != nil {
		t.Fatal(err)
	}
201
	if pmes.GetRecord() != nil {
202 203 204 205 206 207
		t.Fatal("shouldnt have value")
	}
	if pmes.GetProviderPeers() != nil {
		t.Fatal("shouldnt have provider peers")
	}

Jeromy's avatar
Jeromy committed
208
}
Jeromy's avatar
Jeromy committed
209 210

// TODO: Maybe put these in some sort of "ipfs_testutil" package
211 212 213
func _randPeer() peer.Peer {
	id := make(peer.ID, 16)
	crand.Read(id)
214
	p := mockpeer.WithID(id)
Jeromy's avatar
Jeromy committed
215 216 217 218
	return p
}

func TestNotFound(t *testing.T) {
219 220 221
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
223
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
224 225
	fn := &fauxNet{}
	fs := &fauxSender{}
Jeromy's avatar
Jeromy committed
226

227
	local := makePeer(nil)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
228
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
	peerstore.Add(local)
Jeromy's avatar
Jeromy committed
230

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
231
	d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
Jeromy's avatar
Jeromy committed
232

233
	var ps []peer.Peer
Jeromy's avatar
Jeromy committed
234 235
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
236
		d.Update(ctx, ps[i])
Jeromy's avatar
Jeromy committed
237 238 239
	}

	// Reply with random peers to every message
240
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
241
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
242
		err := proto.Unmarshal(mes.Data(), pmes)
Jeromy's avatar
Jeromy committed
243 244 245 246 247
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
248 249
		case pb.Message_GET_VALUE:
			resp := &pb.Message{Type: pmes.Type}
Jeromy's avatar
Jeromy committed
250

251
			peers := []peer.Peer{}
Jeromy's avatar
Jeromy committed
252
			for i := 0; i < 7; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
253 254
				peers = append(peers, _randPeer())
			}
255
			resp.CloserPeers = pb.PeersToPBPeers(peers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
256 257 258
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
Jeromy's avatar
Jeromy committed
259
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
260
			return mes
Jeromy's avatar
Jeromy committed
261 262 263 264 265 266
		default:
			panic("Shouldnt recieve this.")
		}

	})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
267
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
268
	v, err := d.GetValue(ctx, u.Key("hello"))
269
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
270 271
	if err != nil {
		switch err {
272
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
273 274 275 276 277 278 279 280 281 282
			//Success!
			return
		case u.ErrTimeout:
			t.Fatal("Should not have gotten timeout!")
		default:
			t.Fatalf("Got unexpected error: %s", err)
		}
	}
	t.Fatal("Expected to recieve an error.")
}
283 284 285 286

// If less than K nodes are in the entire network, it should fail when we make
// a GET rpc and nobody has the value
func TestLessThanKResponses(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
287 288
	// t.Skip("skipping test because it makes a lot of output")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
289
	ctx := context.Background()
290
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
291 292
	fn := &fauxNet{}
	fs := &fauxSender{}
293
	local := makePeer(nil)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
294
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
295
	peerstore.Add(local)
296

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
297
	d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
298

299
	var ps []peer.Peer
300 301
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
302
		d.Update(ctx, ps[i])
303 304 305 306
	}
	other := _randPeer()

	// Reply with random peers to every message
307
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
308
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
309
		err := proto.Unmarshal(mes.Data(), pmes)
310 311 312 313 314
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
315 316
		case pb.Message_GET_VALUE:
			resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
317
				Type:        pmes.Type,
318
				CloserPeers: pb.PeersToPBPeers([]peer.Peer{other}),
319 320
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
321 322 323 324 325
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
			}
			return mes
326 327 328 329 330 331
		default:
			panic("Shouldnt recieve this.")
		}

	})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
332
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
333
	_, err := d.GetValue(ctx, u.Key("hello"))
334 335
	if err != nil {
		switch err {
336
		case routing.ErrNotFound:
337 338 339 340 341 342 343 344 345 346
			//Success!
			return
		case u.ErrTimeout:
			t.Fatal("Should not have gotten timeout!")
		default:
			t.Fatalf("Got unexpected error: %s", err)
		}
	}
	t.Fatal("Expected to recieve an error.")
}