ext_test.go 6.07 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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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

17
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
19 20
	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
	mn, err := mocknet.FullMeshConnected(ctx, 2)
31 32 33
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
	nets := mn.Nets()
	peers := mn.Peers()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38
	ps := peer.NewPeerstore()
	d := NewDHT(ctx, peers[0], ps, nets[0], ds.NewMapDatastore())
39
	d.Update(ctx, peers[1])
40 41

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

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

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

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

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

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

		msgs <- resp
75 76 77
	})

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

	// Now we test this DHT's handleGetValue failure
89 90 91 92 93 94 95 96 97 98 99 100
	{
		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
101

102 103 104 105 106 107
		// 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
108

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

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

116 117 118 119 120 121 122 123 124 125 126
		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
127
}
Jeromy's avatar
Jeromy committed
128 129

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

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

142
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143
	mn, err := mocknet.FullMeshConnected(ctx, 16)
144 145 146
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147 148 149
	nets := mn.Nets()
	peers := mn.Peers()
	peerstore := peer.NewPeerstore()
150 151

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

153 154
	for _, p := range peers {
		d.Update(ctx, p)
Jeromy's avatar
Jeromy committed
155 156 157
	}

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

162 163
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
164

165 166 167
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
168 169
			}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
			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
189

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

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

212
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
213
	mn, err := mocknet.FullMeshConnected(ctx, 6)
214 215 216
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217 218 219
	nets := mn.Nets()
	peers := mn.Peers()
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
220

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

223 224
	for i := 1; i < 5; i++ {
		d.Update(ctx, peers[i])
225 226 227
	}

	// Reply with random peers to every message
228 229 230 231 232 233
	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)
234

235 236 237
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
238 239
			}

240 241 242 243 244 245 246 247 248 249 250 251
			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
252
			}
253

254 255
		})
	}
256

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
257
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
258
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
259
		switch err {
260
		case routing.ErrNotFound:
261 262 263 264 265 266 267 268 269 270
			//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.")
}