ext_test.go 6.74 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 12
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
	msg "github.com/jbenet/go-ipfs/net/message"
	mux "github.com/jbenet/go-ipfs/net/mux"
Jeromy's avatar
Jeromy committed
15
	peer "github.com/jbenet/go-ipfs/peer"
16
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
17 18 19 20 21 22

	"time"
)

// 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
23
type fauxSender struct {
24
	handlers []mesHandleFunc
Jeromy's avatar
Jeromy committed
25 26
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
Jeromy's avatar
Jeromy committed
28

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31 32 33 34
	for _, h := range f.handlers {
		reply := h(m)
		if reply != nil {
			return reply, nil
		}
	}
Jeromy's avatar
Jeromy committed
35

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
	return nil, nil
Jeromy's avatar
Jeromy committed
37 38
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41 42 43
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
44
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	}
Jeromy's avatar
Jeromy committed
46 47 48
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50 51 52
// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
type fauxNet struct {
	handlers []mesHandleFunc
Jeromy's avatar
Jeromy committed
53 54
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57 58 59 60 61 62
// 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

func (f *fauxNet) AddHandler(fn func(msg.NetMessage) msg.NetMessage) {
	f.handlers = append(f.handlers, fn)
63
}
Jeromy's avatar
Jeromy committed
64

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65 66 67
// DialPeer attempts to establish a connection to a given peer
func (f *fauxNet) DialPeer(*peer.Peer) error {
	return nil
68 69
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71 72
// ClosePeer connection to peer
func (f *fauxNet) ClosePeer(*peer.Peer) error {
	return nil
Jeromy's avatar
Jeromy committed
73 74
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85
// 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
86 87
}

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

91
func TestGetFailures(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94
	ctx := context.Background()
	fn := &fauxNet{}
	fs := &fauxSender{}
Jeromy's avatar
Jeromy committed
95

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
	peerstore := peer.NewPeerstore()
Jeromy's avatar
Jeromy committed
97
	local := new(peer.Peer)
98
	local.ID = peer.ID("test_peer")
Jeromy's avatar
Jeromy committed
99

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

102 103
	other := &peer.Peer{ID: peer.ID("other_peer")}

Jeromy's avatar
Jeromy committed
104 105
	d.Start()

106 107 108
	d.Update(other)

	// This one should time out
109
	_, err := d.GetValue(u.Key("test"), time.Millisecond*10)
Jeromy's avatar
Jeromy committed
110
	if err != nil {
111
		if err != u.ErrTimeout {
112 113 114 115
			t.Fatal("Got different error than we expected.")
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
116 117
	}

118
	// Reply with failures to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119 120 121
	fn.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
		pmes := new(Message)
		err := proto.Unmarshal(mes.Data(), pmes)
122 123 124 125
		if err != nil {
			t.Fatal(err)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126 127
		resp := &Message{
			Type: pmes.Type,
128
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129 130
		m, err := msg.FromObject(mes.Peer(), resp)
		return m
131 132 133
	})

	// This one should fail with NotFound
134
	_, err = d.GetValue(u.Key("test"), time.Millisecond*1000)
135 136
	if err != nil {
		if err != u.ErrNotFound {
137
			t.Fatalf("Expected ErrNotFound, got: %s", err)
138 139 140 141
		}
	} else {
		t.Fatal("expected error, got none.")
	}
142 143 144

	success := make(chan struct{})
	fn.handlers = nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145 146 147
	fn.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
		resp := new(Message)
		err := proto.Unmarshal(mes.Data(), resp)
148 149 150 151 152 153 154 155
		if err != nil {
			t.Fatal(err)
		}
		success <- struct{}{}
		return nil
	})

	// Now we test this DHT's handleGetValue failure
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156 157
	typ := Message_GET_VALUE
	str := "hello"
Chas Leichner's avatar
Chas Leichner committed
158
	req := Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159 160
		Type:  &typ,
		Key:   &str,
161 162
		Value: []byte{0},
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
163 164 165 166 167 168 169 170 171 172

	mes, err := msg.FromObject(other, &req)
	if err != nil {
		t.Error(err)
	}

	mes, err = fs.SendRequest(ctx, mes)
	if err != nil {
		t.Error(err)
	}
173 174

	<-success
Jeromy's avatar
Jeromy committed
175
}
Jeromy's avatar
Jeromy committed
176 177 178 179 180 181 182 183 184 185 186

// TODO: Maybe put these in some sort of "ipfs_testutil" package
func _randPeer() *peer.Peer {
	p := new(peer.Peer)
	p.ID = make(peer.ID, 16)
	p.Addresses = []*ma.Multiaddr{nil}
	crand.Read(p.ID)
	return p
}

func TestNotFound(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187 188
	fn := &fauxNet{}
	fs := &fauxSender{}
Jeromy's avatar
Jeromy committed
189 190 191

	local := new(peer.Peer)
	local.ID = peer.ID("test_peer")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192
	peerstore := peer.NewPeerstore()
Jeromy's avatar
Jeromy committed
193

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194
	d := NewDHT(local, peerstore, fn, fs, ds.NewMapDatastore())
Jeromy's avatar
Jeromy committed
195 196 197 198 199 200 201 202 203
	d.Start()

	var ps []*peer.Peer
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
		d.Update(ps[i])
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204 205 206
	fn.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
		pmes := new(Message)
		err := proto.Unmarshal(mes.Data(), pmes)
Jeromy's avatar
Jeromy committed
207 208 209 210 211
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212 213
		case Message_GET_VALUE:
			resp := &Message{Type: pmes.Type}
Jeromy's avatar
Jeromy committed
214

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
215
			peers := []*peer.Peer{}
Jeromy's avatar
Jeromy committed
216
			for i := 0; i < 7; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217 218 219 220 221 222
				peers = append(peers, _randPeer())
			}
			resp.CloserPeers = peersToPBPeers(peers)
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
Jeromy's avatar
Jeromy committed
223
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
224
			return mes
Jeromy's avatar
Jeromy committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		default:
			panic("Shouldnt recieve this.")
		}

	})

	_, err := d.GetValue(u.Key("hello"), time.Second*30)
	if err != nil {
		switch err {
		case u.ErrNotFound:
			//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.")
}
245 246 247 248 249

// 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) {
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
250 251 252
	fn := &fauxNet{}
	fs := &fauxSender{}
	peerstore := peer.NewPeerstore()
253 254 255
	local := new(peer.Peer)
	local.ID = peer.ID("test_peer")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
256
	d := NewDHT(local, peerstore, fn, fs, ds.NewMapDatastore())
257 258 259 260 261 262 263 264 265 266
	d.Start()

	var ps []*peer.Peer
	for i := 0; i < 5; i++ {
		ps = append(ps, _randPeer())
		d.Update(ps[i])
	}
	other := _randPeer()

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
267 268 269
	fn.AddHandler(func(mes msg.NetMessage) msg.NetMessage {
		pmes := new(Message)
		err := proto.Unmarshal(mes.Data(), pmes)
270 271 272 273 274
		if err != nil {
			t.Fatal(err)
		}

		switch pmes.GetType() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
275 276 277 278
		case Message_GET_VALUE:
			resp := &Message{
				Type:        pmes.Type,
				CloserPeers: peersToPBPeers([]*peer.Peer{other}),
279 280
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
281 282 283 284 285
			mes, err := msg.FromObject(mes.Peer(), resp)
			if err != nil {
				t.Error(err)
			}
			return mes
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		default:
			panic("Shouldnt recieve this.")
		}

	})

	_, err := d.GetValue(u.Key("hello"), time.Second*30)
	if err != nil {
		switch err {
		case u.ErrNotFound:
			//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.")
}