ext_test.go 7.99 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
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	inet "github.com/jbenet/go-ipfs/net"
Jeromy's avatar
Jeromy committed
12
	peer "github.com/jbenet/go-ipfs/peer"
13
	routing "github.com/jbenet/go-ipfs/routing"
14
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
15
	u "github.com/jbenet/go-ipfs/util"
16
	testutil "github.com/jbenet/go-ipfs/util/testutil"
Jeromy's avatar
Jeromy committed
17

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	"sync"
Jeromy's avatar
Jeromy committed
19 20 21
	"time"
)

22 23 24 25 26 27
// 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
28 29
// 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
30
type fauxSender struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	sync.Mutex
32
	handlers []mesHandleFunc
Jeromy's avatar
Jeromy committed
33 34
}

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

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

42
func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43 44 45 46 47 48
	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
49 50 51 52 53
		reply := h(m)
		if reply != nil {
			return reply, nil
		}
	}
Jeromy's avatar
Jeromy committed
54

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

	return nil, ctx.Err()
Jeromy's avatar
Jeromy committed
61 62
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63
func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65 66 67 68 69
	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
70 71 72
		reply := h(m)
		if reply != nil {
			return nil
Jeromy's avatar
Jeromy committed
73
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
	}
Jeromy's avatar
Jeromy committed
75 76 77
	return nil
}

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89 90 91 92
func (f *fauxNet) LocalPeer() peer.Peer {
	return f.local
}

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103 104 105 106 107
// Connectedness returns whether a connection to given peer exists.
func (f *fauxNet) Connectedness(peer.Peer) inet.Connectedness {
	return inet.Connected
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109 110 111 112 113
// 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
114 115
}

116
func (f *fauxNet) GetPeerList() []peer.Peer {
117 118 119
	return nil
}

120 121 122 123
func (f *fauxNet) GetBandwidthTotals() (uint64, uint64) {
	return 0, 0
}

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

127
func TestGetFailures(t *testing.T) {
128 129 130
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
	peerstore := peer.NewPeerstore()
133
	local := makePeerString(t, "")
Jeromy's avatar
Jeromy committed
134

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
135 136 137 138
	ctx := context.Background()
	fn := &fauxNet{local}
	fs := &fauxSender{}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139
	d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
140
	other := makePeerString(t, "")
141
	d.Update(ctx, other)
142 143

	// This one should time out
144
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145 146
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
	_, err := d.GetValue(ctx1, u.Key("test"))
Jeromy's avatar
Jeromy committed
147
	if err != nil {
148 149
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
150 151 152
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
153 154
	}

155
	// u.POut("NotFound Test\n")
156
	// Reply with failures to every message
157
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
158
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159
		err := proto.Unmarshal(mes.Data(), pmes)
160 161 162 163
		if err != nil {
			t.Fatal(err)
		}

164
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165
			Type: pmes.Type,
166
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
167 168
		m, err := msg.FromObject(mes.Peer(), resp)
		return m
169 170 171
	})

	// This one should fail with NotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172 173
	ctx2, _ := context.WithTimeout(context.Background(), time.Second)
	_, err = d.GetValue(ctx2, u.Key("test"))
174
	if err != nil {
175
		if err != routing.ErrNotFound {
176
			t.Fatalf("Expected ErrNotFound, got: %s", err)
177 178 179 180
		}
	} else {
		t.Fatal("expected error, got none.")
	}
181

182
	fs.handlers = nil
183
	// Now we test this DHT's handleGetValue failure
184
	typ := pb.Message_GET_VALUE
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185
	str := "hello"
186 187 188 189
	rec, err := d.makePutRecord(u.Key(str), []byte("blah"))
	if err != nil {
		t.Fatal(err)
	}
190
	req := pb.Message{
191 192 193
		Type:   &typ,
		Key:    &str,
		Record: rec,
194
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195

196
	// u.POut("handleGetValue Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197 198 199 200 201
	mes, err := msg.FromObject(other, &req)
	if err != nil {
		t.Error(err)
	}

202
	mes = d.HandleMessage(ctx, mes)
203

204
	pmes := new(pb.Message)
205 206 207 208
	err = proto.Unmarshal(mes.Data(), pmes)
	if err != nil {
		t.Fatal(err)
	}
209
	if pmes.GetRecord() != nil {
210 211 212 213 214 215
		t.Fatal("shouldnt have value")
	}
	if pmes.GetProviderPeers() != nil {
		t.Fatal("shouldnt have provider peers")
	}

Jeromy's avatar
Jeromy committed
216
}
Jeromy's avatar
Jeromy committed
217 218

// TODO: Maybe put these in some sort of "ipfs_testutil" package
219 220 221
func _randPeer() peer.Peer {
	id := make(peer.ID, 16)
	crand.Read(id)
222
	p := testutil.NewPeerWithID(id)
Jeromy's avatar
Jeromy committed
223 224 225 226
	return p
}

func TestNotFound(t *testing.T) {
227 228 229
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230

231
	local := makePeerString(t, "")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
232
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
233
	peerstore.Add(local)
Jeromy's avatar
Jeromy committed
234

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235 236 237 238
	ctx := context.Background()
	fn := &fauxNet{local}
	fs := &fauxSender{}

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

241
	var ps []peer.Peer
Jeromy's avatar
Jeromy committed
242 243
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
244
		d.Update(ctx, ps[i])
Jeromy's avatar
Jeromy committed
245 246 247
	}

	// Reply with random peers to every message
248
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
249
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
250
		err := proto.Unmarshal(mes.Data(), pmes)
Jeromy's avatar
Jeromy committed
251 252 253 254 255
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
256 257
		case pb.Message_GET_VALUE:
			resp := &pb.Message{Type: pmes.Type}
Jeromy's avatar
Jeromy committed
258

259
			peers := []peer.Peer{}
Jeromy's avatar
Jeromy committed
260
			for i := 0; i < 7; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
261 262
				peers = append(peers, _randPeer())
			}
263
			resp.CloserPeers = pb.PeersToPBPeers(d.dialer, peers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264 265 266
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
Jeromy's avatar
Jeromy committed
267
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
268
			return mes
Jeromy's avatar
Jeromy committed
269 270 271 272 273 274
		default:
			panic("Shouldnt recieve this.")
		}

	})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
275
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
276
	v, err := d.GetValue(ctx, u.Key("hello"))
277
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
278 279
	if err != nil {
		switch err {
280
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
281 282 283 284 285 286 287 288 289 290
			//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.")
}
291 292 293 294

// 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
295 296
	// t.Skip("skipping test because it makes a lot of output")

297
	local := makePeerString(t, "")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
298
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
299
	peerstore.Add(local)
300

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
301 302 303 304 305
	ctx := context.Background()
	u.Debug = false
	fn := &fauxNet{local}
	fs := &fauxSender{}

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

308
	var ps []peer.Peer
309 310
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
311
		d.Update(ctx, ps[i])
312 313 314 315
	}
	other := _randPeer()

	// Reply with random peers to every message
316
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
317
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
318
		err := proto.Unmarshal(mes.Data(), pmes)
319 320 321 322 323
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
324 325
		case pb.Message_GET_VALUE:
			resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
326
				Type:        pmes.Type,
327
				CloserPeers: pb.PeersToPBPeers(d.dialer, []peer.Peer{other}),
328 329
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
330 331 332 333 334
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
			}
			return mes
335 336 337 338 339 340
		default:
			panic("Shouldnt recieve this.")
		}

	})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
341
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
342
	_, err := d.GetValue(ctx, u.Key("hello"))
343 344
	if err != nil {
		switch err {
345
		case routing.ErrNotFound:
346 347 348 349 350 351 352 353 354 355
			//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.")
}