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

import (
4
	"math/rand"
Jeromy's avatar
Jeromy committed
5 6
	"testing"

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	inet "github.com/jbenet/go-ipfs/net"
10
	mocknet "github.com/jbenet/go-ipfs/net/mock"
Jeromy's avatar
Jeromy committed
11
	peer "github.com/jbenet/go-ipfs/peer"
12
	routing "github.com/jbenet/go-ipfs/routing"
13
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
14
	u "github.com/jbenet/go-ipfs/util"
15
	testutil "github.com/jbenet/go-ipfs/util/testutil"
Jeromy's avatar
Jeromy committed
16

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
18 19 20
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"

Jeromy's avatar
Jeromy committed
21 22 23
	"time"
)

24
func TestGetFailures(t *testing.T) {
25 26 27
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28

29
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
	peerstore := peer.NewPeerstore()
31
	local := makePeerString(t, "")
32
	peers := []peer.Peer{local, testutil.RandPeer()}
Jeromy's avatar
Jeromy committed
33

34 35 36 37
	nets, err := mocknet.MakeNetworks(ctx, peers)
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38

39 40
	d := NewDHT(ctx, peers[0], peerstore, nets[0], ds.NewMapDatastore())
	d.Update(ctx, peers[1])
41 42

	// This one should time out
43
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
45
	if _, err := d.GetValue(ctx1, u.Key("test")); err != nil {
46 47
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
48 49 50
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
51 52
	}

53 54
	msgs := make(chan *pb.Message, 100)

55
	// u.POut("NotFound Test\n")
56
	// Reply with failures to every message
57 58 59 60 61 62
	nets[1].SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
		defer s.Close()

		pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
		pbw := ggio.NewDelimitedWriter(s)

63
		pmes := new(pb.Message)
64 65
		if err := pbr.ReadMsg(pmes); err != nil {
			panic(err)
66 67
		}

68
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
			Type: pmes.Type,
70
		}
71 72 73 74 75
		if err := pbw.WriteMsg(resp); err != nil {
			panic(err)
		}

		msgs <- resp
76 77 78
	})

	// This one should fail with NotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79 80
	ctx2, _ := context.WithTimeout(context.Background(), time.Second)
	_, err = d.GetValue(ctx2, u.Key("test"))
81
	if err != nil {
82
		if err != routing.ErrNotFound {
83
			t.Fatalf("Expected ErrNotFound, got: %s", err)
84 85 86 87
		}
	} else {
		t.Fatal("expected error, got none.")
	}
88 89

	// Now we test this DHT's handleGetValue failure
90 91 92 93 94 95 96 97 98 99 100 101
	{
		typ := pb.Message_GET_VALUE
		str := "hello"
		rec, err := d.makePutRecord(u.Key(str), []byte("blah"))
		if err != nil {
			t.Fatal(err)
		}
		req := pb.Message{
			Type:   &typ,
			Key:    &str,
			Record: rec,
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102

103 104 105 106 107 108
		// u.POut("handleGetValue Test\n")
		s, err := nets[1].NewStream(inet.ProtocolDHT, peers[0])
		if err != nil {
			t.Fatal(err)
		}
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109

110 111
		pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
		pbw := ggio.NewDelimitedWriter(s)
112

113 114 115
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
116

117 118 119 120 121 122 123 124 125 126 127
		pmes := new(pb.Message)
		if err := pbr.ReadMsg(pmes); err != nil {
			t.Fatal(err)
		}
		if pmes.GetRecord() != nil {
			t.Fatal("shouldnt have value")
		}
		if pmes.GetProviderPeers() != nil {
			t.Fatal("shouldnt have provider peers")
		}
	}
Jeromy's avatar
Jeromy committed
128
}
Jeromy's avatar
Jeromy committed
129 130

// TODO: Maybe put these in some sort of "ipfs_testutil" package
131 132 133
func _randPeer() peer.Peer {
	id := make(peer.ID, 16)
	crand.Read(id)
134
	p := testutil.NewPeerWithID(id)
Jeromy's avatar
Jeromy committed
135 136 137 138
	return p
}

func TestNotFound(t *testing.T) {
139 140 141
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142

143
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144
	peerstore := peer.NewPeerstore()
Jeromy's avatar
Jeromy committed
145

146 147 148 149
	var peers []peer.Peer
	for i := 0; i < 16; i++ {
		peers = append(peers, testutil.RandPeer())
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
150

151 152 153 154 155 156
	nets, err := mocknet.MakeNetworks(ctx, peers)
	if err != nil {
		t.Fatal(err)
	}

	d := NewDHT(ctx, peers[0], peerstore, nets[0], ds.NewMapDatastore())
Jeromy's avatar
Jeromy committed
157

158 159
	for _, p := range peers {
		d.Update(ctx, p)
Jeromy's avatar
Jeromy committed
160 161 162
	}

	// Reply with random peers to every message
163 164 165
	for _, neti := range nets {
		neti.SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
			defer s.Close()
Jeromy's avatar
Jeromy committed
166

167 168
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
169

170 171 172
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
173 174
			}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{Type: pmes.Type}

				ps := []peer.Peer{}
				for i := 0; i < 7; i++ {
					ps = append(ps, peers[rand.Intn(len(peers))])
				}

				resp.CloserPeers = pb.PeersToPBPeers(d.network, peers)
				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}

			default:
				panic("Shouldnt recieve this.")
			}
		})
	}
Jeromy's avatar
Jeromy committed
194

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
196
	v, err := d.GetValue(ctx, u.Key("hello"))
197
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
198 199
	if err != nil {
		switch err {
200
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
201 202 203 204 205 206 207 208 209 210
			//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.")
}
211 212 213 214

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

217
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
218
	peerstore := peer.NewPeerstore()
219

220 221 222 223 224 225 226 227 228
	var peers []peer.Peer
	for i := 0; i < 6; i++ {
		peers = append(peers, testutil.RandPeer())
	}

	nets, err := mocknet.MakeNetworks(ctx, peers)
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229

230
	d := NewDHT(ctx, peers[0], peerstore, nets[0], ds.NewMapDatastore())
231

232 233
	for i := 1; i < 5; i++ {
		d.Update(ctx, peers[i])
234 235 236
	}

	// Reply with random peers to every message
237 238 239 240 241 242
	for _, neti := range nets {
		neti.SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
			defer s.Close()

			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
243

244 245 246
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
247 248
			}

249 250 251 252 253 254 255 256 257 258 259 260
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{
					Type:        pmes.Type,
					CloserPeers: pb.PeersToPBPeers(d.network, []peer.Peer{peers[1]}),
				}

				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}
			default:
				panic("Shouldnt recieve this.")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
261
			}
262

263 264
		})
	}
265

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
266
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
267
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
268
		switch err {
269
		case routing.ErrNotFound:
270 271 272 273 274 275 276 277 278 279
			//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.")
}