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

import (
	"testing"

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

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/routing"
16
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
17
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
18 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 {
31
	handlers []mesHandleFunc
Jeromy's avatar
Jeromy committed
32 33
}

34 35 36
func (f *fauxSender) AddHandler(fn func(msg.NetMessage) msg.NetMessage) {
	f.handlers = append(f.handlers, fn)
}
Jeromy's avatar
Jeromy committed
37

38
func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41 42 43 44
	for _, h := range f.handlers {
		reply := h(m)
		if reply != nil {
			return reply, nil
		}
	}
Jeromy's avatar
Jeromy committed
45

46 47 48 49 50 51
	// no reply? ok force a timeout
	select {
	case <-ctx.Done():
	}

	return nil, ctx.Err()
Jeromy's avatar
Jeromy committed
52 53
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55 56 57 58
func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error {
	for _, h := range f.handlers {
		reply := h(m)
		if reply != nil {
			return nil
Jeromy's avatar
Jeromy committed
59
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
	}
Jeromy's avatar
Jeromy committed
61 62 63
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65 66
// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
type fauxNet struct {
67
}
Jeromy's avatar
Jeromy committed
68

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
// DialPeer attempts to establish a connection to a given peer
70
func (f *fauxNet) DialPeer(peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
	return nil
72 73
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
// ClosePeer connection to peer
75
func (f *fauxNet) ClosePeer(peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	return nil
Jeromy's avatar
Jeromy committed
77 78
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
// IsConnected returns whether a connection to given peer exists.
80
func (f *fauxNet) IsConnected(peer.Peer) (bool, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
	return true, nil
82 83
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85 86 87 88 89
// 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
90 91
}

92
func (f *fauxNet) GetPeerList() []peer.Peer {
93 94 95
	return nil
}

96 97 98 99
func (f *fauxNet) GetBandwidthTotals() (uint64, uint64) {
	return 0, 0
}

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

103
func TestGetFailures(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105
	// t.Skip("skipping test because it makes a lot of output")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107 108
	ctx := context.Background()
	fn := &fauxNet{}
	fs := &fauxSender{}
Jeromy's avatar
Jeromy committed
109

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	peerstore := peer.NewPeerstore()
111
	local := peer.WithIDString("test_peer")
Jeromy's avatar
Jeromy committed
112

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113
	d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
114
	other := peer.WithIDString("other_peer")
115 116 117
	d.Update(other)

	// This one should time out
118
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119 120
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
	_, err := d.GetValue(ctx1, u.Key("test"))
Jeromy's avatar
Jeromy committed
121
	if err != nil {
122 123
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
124 125 126
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
127 128
	}

129
	// u.POut("NotFound Test\n")
130
	// Reply with failures to every message
131
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
132
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133
		err := proto.Unmarshal(mes.Data(), pmes)
134 135 136 137
		if err != nil {
			t.Fatal(err)
		}

138
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139
			Type: pmes.Type,
140
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141 142
		m, err := msg.FromObject(mes.Peer(), resp)
		return m
143 144 145
	})

	// This one should fail with NotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146 147
	ctx2, _ := context.WithTimeout(context.Background(), time.Second)
	_, err = d.GetValue(ctx2, u.Key("test"))
148
	if err != nil {
149
		if err != routing.ErrNotFound {
150
			t.Fatalf("Expected ErrNotFound, got: %s", err)
151 152 153 154
		}
	} else {
		t.Fatal("expected error, got none.")
	}
155

156
	fs.handlers = nil
157
	// Now we test this DHT's handleGetValue failure
158
	typ := pb.Message_GET_VALUE
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159
	str := "hello"
160
	req := pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161 162
		Type:  &typ,
		Key:   &str,
163 164
		Value: []byte{0},
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165

166
	// u.POut("handleGetValue Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
167 168 169 170 171
	mes, err := msg.FromObject(other, &req)
	if err != nil {
		t.Error(err)
	}

172
	mes = d.HandleMessage(ctx, mes)
173

174
	pmes := new(pb.Message)
175 176 177 178 179 180 181 182 183 184 185 186 187 188
	err = proto.Unmarshal(mes.Data(), pmes)
	if err != nil {
		t.Fatal(err)
	}
	if pmes.GetValue() != nil {
		t.Fatal("shouldnt have value")
	}
	if pmes.GetCloserPeers() != nil {
		t.Fatal("shouldnt have closer peers")
	}
	if pmes.GetProviderPeers() != nil {
		t.Fatal("shouldnt have provider peers")
	}

Jeromy's avatar
Jeromy committed
189
}
Jeromy's avatar
Jeromy committed
190 191

// TODO: Maybe put these in some sort of "ipfs_testutil" package
192 193 194 195
func _randPeer() peer.Peer {
	id := make(peer.ID, 16)
	crand.Read(id)
	p := peer.WithID(id)
Jeromy's avatar
Jeromy committed
196 197 198 199
	return p
}

func TestNotFound(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
200 201
	// t.Skip("skipping test because it makes a lot of output")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
203 204
	fn := &fauxNet{}
	fs := &fauxSender{}
Jeromy's avatar
Jeromy committed
205

206
	local := peer.WithIDString("test_peer")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
207
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
208
	peerstore.Add(local)
Jeromy's avatar
Jeromy committed
209

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

212
	var ps []peer.Peer
Jeromy's avatar
Jeromy committed
213 214 215 216 217 218
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
		d.Update(ps[i])
	}

	// Reply with random peers to every message
219
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
220
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
221
		err := proto.Unmarshal(mes.Data(), pmes)
Jeromy's avatar
Jeromy committed
222 223 224 225 226
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
227 228
		case pb.Message_GET_VALUE:
			resp := &pb.Message{Type: pmes.Type}
Jeromy's avatar
Jeromy committed
229

230
			peers := []peer.Peer{}
Jeromy's avatar
Jeromy committed
231
			for i := 0; i < 7; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
232 233
				peers = append(peers, _randPeer())
			}
234
			resp.CloserPeers = pb.PeersToPBPeers(peers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235 236 237
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
Jeromy's avatar
Jeromy committed
238
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239
			return mes
Jeromy's avatar
Jeromy committed
240 241 242 243 244 245
		default:
			panic("Shouldnt recieve this.")
		}

	})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
246
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
247
	v, err := d.GetValue(ctx, u.Key("hello"))
248
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
249 250
	if err != nil {
		switch err {
251
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
252 253 254 255 256 257 258 259 260 261
			//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.")
}
262 263 264 265

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
268
	ctx := context.Background()
269
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
270 271
	fn := &fauxNet{}
	fs := &fauxSender{}
272
	local := peer.WithIDString("test_peer")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
273
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
274
	peerstore.Add(local)
275

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

278
	var ps []peer.Peer
279 280 281 282 283 284 285
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
		d.Update(ps[i])
	}
	other := _randPeer()

	// Reply with random peers to every message
286
	fs.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
287
		pmes := new(pb.Message)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
288
		err := proto.Unmarshal(mes.Data(), pmes)
289 290 291 292 293
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
294 295
		case pb.Message_GET_VALUE:
			resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
296
				Type:        pmes.Type,
297
				CloserPeers: pb.PeersToPBPeers([]peer.Peer{other}),
298 299
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
300 301 302 303 304
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
			}
			return mes
305 306 307 308 309 310
		default:
			panic("Shouldnt recieve this.")
		}

	})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
311
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
312
	_, err := d.GetValue(ctx, u.Key("hello"))
313 314
	if err != nil {
		switch err {
315
		case routing.ErrNotFound:
316 317 318 319 320 321 322 323 324 325
			//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.")
}