Пример #1
0
func TestRouterMessageExchange(t *testing.T) {
	logger := log.NewLogger()
	msg := "The quick brown fox jumps over the lazy dog"

	key1 := utils.GeneratePrivateKey()
	key2 := utils.GeneratePrivateKey()

	router1, err := NewRouter(key1, logger, utils.DefaultConfig)
	if err != nil {
		t.Fatal(err)
	}
	router1.Discover(utils.DefaultConfig.Bootstrap())

	router2, err := NewRouter(key2, logger, utils.DefaultConfig)
	if err != nil {
		t.Fatal(err)
	}
	router2.Discover(utils.DefaultConfig.Bootstrap())

	time.Sleep(100 * time.Millisecond)
	router1.SendMessage(utils.NewNodeID(namespace, key2.Digest()), []byte(msg))

	m, err := router2.RecvMessage()
	if err != nil {
		t.Errorf("router2: recvMessage() returns error")
	}
	if m.Node.Digest.Cmp(router1.key.Digest()) != 0 {
		t.Errorf("router2: wrong source id")
	}
	if string(m.Payload) != msg {
		t.Errorf("router2: wrong message body")
	}

	router2.SendMessage(utils.NewNodeID(namespace, router1.key.Digest()), []byte(msg))
	m, err = router1.RecvMessage()
	if err != nil {
		t.Errorf("router1: recvMessage() returns error")
	}
	if m.Node.Digest.Cmp(router2.key.Digest()) != 0 {
		t.Errorf("router1: wrong source id")
	}
	if string(m.Payload) != msg {
		t.Errorf("router1: wrong message body")
	}

	router1.Close()
	router2.Close()
}
Пример #2
0
func TestRouterRouteExchange(t *testing.T) {
	logger := log.NewLogger()
	msg := "The quick brown fox jumps over the lazy dog"

	key1 := utils.GeneratePrivateKey()
	key2 := utils.GeneratePrivateKey()
	key3 := utils.GeneratePrivateKey()

	router1, err := NewRouter(key1, logger, utils.DefaultConfig)
	if err != nil {
		t.Fatal(err)
	}
	defer router1.Close()
	router1.Discover(utils.DefaultConfig.Bootstrap())

	router2, err := NewRouter(key2, logger, utils.DefaultConfig)
	if err != nil {
		t.Fatal(err)
	}
	defer router2.Close()
	router2.Discover(utils.DefaultConfig.Bootstrap())

	time.Sleep(100 * time.Millisecond)
	router3, err := NewRouter(key3, logger, utils.DefaultConfig)
	if err != nil {
		t.Fatal(err)
	}
	defer router3.Close()
	addr, _ := net.ResolveUDPAddr("udp", router1.listener.Addr().String())
	router3.Discover([]net.UDPAddr{net.UDPAddr{Port: addr.Port, IP: net.ParseIP("127.0.0.1")}})

	time.Sleep(100 * time.Millisecond)
	router3.SendMessage(utils.NewNodeID(namespace, key1.Digest()), []byte(msg))

	m, err := router1.RecvMessage()
	if err != nil {
		t.Errorf("router1: recvMessage() returns error")
	}
	if m.Node.Digest.Cmp(router3.key.Digest()) != 0 {
		t.Errorf("router1: wrong source id")
	}
	if string(m.Payload) != msg {
		t.Errorf("router1: wrong message body")
	}
}
Пример #3
0
// NewClient generates a Client with the given PrivateKey.
func NewClient(key *utils.PrivateKey, config utils.Config) (*Client, error) {
	logger := log.NewLogger()

	r, err := router.NewRouter(key, logger, config)
	if err != nil {
		return nil, err
	}

	c := &Client{
		router: r,
		readch: make(chan router.Message),
		mbuf:   newMessageBuffer(128),
		id:     utils.NewNodeID(utils.GlobalNamespace, key.Digest()),
		config: config,
		Logger: logger,
	}

	return c, nil
}
Пример #4
0
func TestRouterGroup(t *testing.T) {
	var config = utils.Config{
		P: "9200-9300",
		B: []string{
			"localhost:9200-9300",
		},
	}

	logger := log.NewLogger()

	gkey1 := utils.GeneratePrivateKey()
	gkey2 := utils.GeneratePrivateKey()

	router1, err := NewRouter(utils.GeneratePrivateKey(), logger, config)
	if err != nil {
		t.Fatal(err)
	}
	router1.Join(utils.NewNodeID(utils.GroupNamespace, gkey1.Digest()))
	defer router1.Close()

	router2, err := NewRouter(utils.GeneratePrivateKey(), logger, config)
	if err != nil {
		t.Fatal(err)
	}
	router2.Join(utils.NewNodeID(utils.GroupNamespace, gkey1.Digest()))
	defer router2.Close()

	router3, err := NewRouter(utils.GeneratePrivateKey(), logger, config)
	if err != nil {
		t.Fatal(err)
	}
	router3.Join(utils.NewNodeID(utils.GroupNamespace, gkey1.Digest()))
	defer router3.Close()

	router4, err := NewRouter(utils.GeneratePrivateKey(), logger, config)
	if err != nil {
		t.Fatal(err)
	}
	router4.Join(utils.NewNodeID(utils.GroupNamespace, gkey2.Digest()))
	defer router4.Close()

	router5, err := NewRouter(utils.GeneratePrivateKey(), logger, config)
	if err != nil {
		t.Fatal(err)
	}
	router5.Join(utils.NewNodeID(utils.GroupNamespace, gkey2.Digest()))
	defer router5.Close()

	router1.Discover(utils.DefaultConfig.Bootstrap())
	router2.Discover(utils.DefaultConfig.Bootstrap())
	router3.Discover(utils.DefaultConfig.Bootstrap())
	router4.Discover(utils.DefaultConfig.Bootstrap())
	router5.Discover(utils.DefaultConfig.Bootstrap())

	time.Sleep(100 * time.Millisecond)

	msg := "The quick brown fox jumps over the lazy dog"
	router3.SendMessage(utils.NewNodeID(utils.GroupNamespace, gkey1.Digest()), []byte(msg))

	{
		m, err := router1.RecvMessage()
		if err != nil {
			t.Errorf("router1: recvMessage() returns error")
		}
		if m.Node.Digest.Cmp(router3.key.Digest()) != 0 {
			t.Errorf("router1: wrong source id")
		}
		if string(m.Payload) != msg {
			t.Errorf("router1: wrong message body")
		}
	}

	{
		m, err := router2.RecvMessage()
		if err != nil {
			t.Errorf("router2: recvMessage() returns error")
		}
		if m.Node.Digest.Cmp(router3.key.Digest()) != 0 {
			t.Errorf("router2: wrong source id")
		}
		if string(m.Payload) != msg {
			t.Errorf("router2: wrong message body")
		}
	}
}
Пример #5
0
func TestDhtGroup(t *testing.T) {
	logger := log.NewLogger()

	n := 20
	dhtmap := make(map[string]*DHT)
	idary := make([]utils.NodeInfo, n)

	for i := 0; i < n; i++ {
		id := utils.NewRandomNodeID(namespace)
		addr, err := utp.ResolveAddr("utp", ":0")
		if err != nil {
			t.Fatal(err)
		}
		utp, err := utp.Listen("utp", addr)
		uaddr, err := getLoopbackAddr(utp.Addr())
		if err != nil {
			t.Fatal(err)
		}
		node := utils.NodeInfo{ID: id, Addr: uaddr}
		d := NewDHT(10, node.ID, node.ID, utp.RawConn, logger)
		idary[i] = node
		dhtmap[id.String()] = d
		defer d.Close()

		go func() {
			var b [102400]byte
			for {
				l, addr, err := utp.RawConn.ReadFrom(b[:])
				if err != nil {
					return
				}
				d.ProcessPacket(b[:l], addr)
			}
		}()
	}

	rootNode := idary[0]
	rootDht := dhtmap[rootNode.ID.String()]

	for _, d := range dhtmap {
		d.AddNode(rootNode)
		d.FindNearestNode(d.id)
	}

	kvs := map[string]string{}
	for i := 0; i < 20; i++ {
		kvs[fmt.Sprintf("<%d>", i)] = utils.NewRandomNodeID(namespace).String()
	}

	for k, v := range kvs {
		rootDht.StoreValue(k, v)
	}

	for _, d := range dhtmap {
		for k := range kvs {
			val := d.LoadValue(k)
			if val == nil {
				t.Errorf("key not found: %s", k)
			} else if *val != kvs[k] {
				t.Errorf("wrong value for the key: %s : %s; %s expected", k, val, kvs[k])
			}
		}
	}
}
Пример #6
0
func TestDhtPing(t *testing.T) {

	addr, err := utp.ResolveAddr("utp", ":0")
	if err != nil {
		t.Fatal(err)
	}
	utp1, err := utp.Listen("utp", addr)
	if err != nil {
		t.Fatal(err)
	}
	utp2, err := utp.Listen("utp", addr)
	if err != nil {
		t.Fatal(err)
	}

	addr1, err := getLoopbackAddr(utp1.Addr())
	if err != nil {
		t.Fatal(err)
	}
	addr2, err := getLoopbackAddr(utp2.Addr())
	if err != nil {
		t.Fatal(err)
	}

	node1 := utils.NodeInfo{ID: utils.NewRandomNodeID(namespace), Addr: addr1}
	node2 := utils.NodeInfo{ID: utils.NewRandomNodeID(namespace), Addr: addr2}

	dht1 := NewDHT(10, node1.ID, node1.ID, utp1.RawConn, log.NewLogger())
	dht2 := NewDHT(10, node2.ID, node2.ID, utp2.RawConn, log.NewLogger())
	defer dht1.Close()
	defer dht2.Close()

	go func() {
		var b [102400]byte
		for {
			l, addr, err := utp1.RawConn.ReadFrom(b[:])
			if err != nil {
				return
			}
			dht1.ProcessPacket(b[:l], addr)
		}
	}()

	go func() {
		var b [102400]byte
		for {
			l, addr, err := utp2.RawConn.ReadFrom(b[:])
			if err != nil {
				return
			}
			dht2.ProcessPacket(b[:l], addr)
		}
	}()

	dht1.AddNode(node2)

	time.Sleep(time.Millisecond * 100)

	if dht1.GetNodeInfo(node2.ID) == nil {
		t.Errorf("dht1 should know node2")
	}

	if dht2.GetNodeInfo(node1.ID) == nil {
		t.Errorf("dht2 should know node1")
	}
}