Exemple #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()
}
Exemple #2
0
func getKey(keyfile string) (*utils.PrivateKey, error) {
	_, err := os.Stat(filepath.Dir(keyfile))

	if _, err := os.Stat(keyfile); err != nil {
		err := os.MkdirAll(filepath.Dir(keyfile), 0755)
		if err != nil {
			return nil, err
		}
		key := utils.GeneratePrivateKey()
		pem, err := key.MarshalText()
		err = ioutil.WriteFile(keyfile, pem, 0644)
		if err != nil {
			return nil, err
		}
		fmt.Printf(" -> Create a new private key: %s\n", keyfile)
	}

	pem, err := ioutil.ReadFile(keyfile)
	if err != nil {
		return nil, err
	}

	var key utils.PrivateKey
	err = key.UnmarshalText(pem)
	if err != nil {
		return nil, err
	}
	return &key, nil
}
Exemple #3
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")
	}
}
Exemple #4
0
func TestPacketSignature(t *testing.T) {
	packet := Packet{
		Dst:     utils.NewRandomNodeID(utils.GlobalNamespace),
		Src:     utils.NewRandomNodeID(utils.GlobalNamespace),
		Type:    "dht",
		Payload: []byte("payload"),
	}

	key := utils.GeneratePrivateKey()
	packet.Sign(key)

	if !packet.Verify(&key.PublicKey) {
		t.Errorf("varification failed")
	}
}
Exemple #5
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")
		}
	}
}
Exemple #6
0
func (s *Session) commandLoop() {
	var chatID *utils.NodeID

	go func() {
		for {
			m, src, err := s.cli.Read()
			if err != nil {
				return
			}
			if msg, ok := m.(murcott.ChatMessage); ok {
				/*
					if chatID == nil {
						chatID = &src
						color.Printf("\n -> Start a chat with @{Wk} %s @{|}\n\n", src.String())
					}
				*/
				str := src.String()
				color.Printf("\r* @{Wk}%s@{|} %s\n", str[len(str)-8:], msg.Text())
				fmt.Print("* ")
			}
		}
	}()

	bio := bufio.NewReader(os.Stdin)
	for {
		if chatID == nil {
			fmt.Print("> ")
		} else {
			fmt.Print("* ")
		}
		line, _, err := bio.ReadLine()
		if err != nil {
			return
		}
		c := strings.Split(string(line), " ")
		if len(c) == 0 || c[0] == "" {
			continue
		}
		switch c[0] {
		case "/chat":
			if len(c) != 2 {
				color.Printf(" -> @{Rk}ERROR:@{|} /chat takes 1 argument\n")
			} else {
				nid, err := utils.NewNodeIDFromString(c[1])
				if err != nil {
					color.Printf(" -> @{Rk}ERROR:@{|} invalid ID\n")
				} else {
					s.cli.Join(nid)
					chatID = &nid
					color.Printf(" -> Start a chat with @{Wk} %s @{|}\n\n", nid.String())
				}
			}
		case "/add":
			if len(c) != 2 {
				color.Printf(" -> @{Rk}ERROR:@{|} /add takes 1 argument\n")
			} else {
				nid, err := utils.NewNodeIDFromString(c[1])
				if err != nil {
					color.Printf(" -> @{Rk}ERROR:@{|} invalid ID\n")
				} else {
					s.cli.Roster.Set(nid, murcott.UserProfile{})
				}
			}
		case "/mkg":
			key := utils.GeneratePrivateKey()
			id := utils.NewNodeID(utils.GroupNamespace, key.Digest())
			color.Printf("Group ID: @{Wk} %s @{|}\n\n", id.String())

		case "/stat":
			color.Printf("  * NumGoroutine() = %d\n", runtime.NumGoroutine())
			nodes := s.cli.ActiveSessions()
			color.Printf("  * active sessions (%d) *\n", len(nodes))
			for _, n := range nodes {
				color.Printf(" %v\n", n)
			}
			list := s.cli.Roster.List()
			color.Printf("  * Roster (%d) *\n", len(list))
			for _, n := range list {
				color.Printf(" %v %s \n", n, s.cli.Roster.Get(n).Nickname)
			}

		case "/end":
			if chatID != nil {
				color.Printf(" -> End current chat\n")
				s.cli.Leave(*chatID)
				chatID = nil
			}
		case "/exit", "/quit":
			color.Printf(" -> See you@{Kg}.@{Kr}.@{Ky}.@{|}\n")
			return
		case "/help":
			showHelp()
		default:
			if chatID == nil {
				color.Printf(" -> @{Rk}ERROR:@{|} unknown command\n")
				showHelp()
			} else {
				s.cli.SendMessage(*chatID, murcott.NewPlainChatMessage(string(line)))
			}
		}
	}
}