arnaucube
arnaucube/go-dhtPublic Self-hosted repository
go-dht/rpc-test/test.go
rpc-test/test.goRaw
  1package main
  2
  3import (
  4	"errors"
  5	"flag"
  6	"fmt"
  7	"go-dht/kademlia"
  8	"go-dht/node"
  9	"log"
 10	"net/rpc"
 11	"strconv"
 12)
 13
 14// Utility to test the Node RPC methods
 15
 16func main() {
 17	pingFlag := flag.Bool("ping", false, "test Ping")
 18	findnodeFlag := flag.Bool("findnode", false, "test FindNode")
 19	findvalueFlag := flag.Bool("findvalue", false, "test FindValue")
 20	storeFlag := flag.Bool("store", false, "test Store")
 21	flag.Parse()
 22
 23	if *pingFlag {
 24		testPing()
 25	}
 26	if *findnodeFlag {
 27		testFindNode()
 28	}
 29	if *findvalueFlag {
 30		testFindValue()
 31	}
 32	if *storeFlag {
 33		testStore()
 34	}
 35}
 36
 37func testPing() {
 38	lns := prepareTestListedNodes()
 39	client, err := rpc.DialHTTP("tcp", "127.0.0.1:5000")
 40	if err != nil {
 41		log.Fatal("Connection error: ", err)
 42	}
 43
 44	var reply kademlia.ListedNode
 45	for _, ln := range lns {
 46		err = client.Call("Node.Ping", ln, &reply)
 47		if err != nil {
 48			panic(err)
 49		}
 50		fmt.Println(reply)
 51	}
 52}
 53
 54func testFindNode() {
 55	// existing node
 56	id, err := kademlia.IDFromString("1ff734fb9897600ca54a9c55ace2d22a51afb610")
 57	if err != nil {
 58		panic(err)
 59	}
 60	ln := kademlia.ListedNode{
 61		ID:   id,
 62		Addr: "",
 63		Port: "",
 64	}
 65	client, err := rpc.DialHTTP("tcp", "127.0.0.1:5000")
 66	if err != nil {
 67		log.Fatal("Connection error: ", err)
 68	}
 69
 70	var reply []kademlia.ListedNode
 71	err = client.Call("Node.FindNode", ln, &reply)
 72	if err != nil {
 73		panic(err)
 74	}
 75	fmt.Println(reply)
 76
 77	// find non existing node, to get a closer one
 78	id, err = kademlia.IDFromString("1ff734fb9897600ca54a9c55ace2d22a51aaaaaa")
 79	if err != nil {
 80		panic(err)
 81	}
 82	ln = kademlia.ListedNode{
 83		ID:   id,
 84		Addr: "",
 85		Port: "",
 86	}
 87
 88	var reply2 []kademlia.ListedNode
 89	err = client.Call("Node.FindNode", ln, &reply2)
 90	if err != nil {
 91		panic(err)
 92	}
 93	fmt.Println(reply2)
 94}
 95
 96func testFindValue() {
 97	client, err := rpc.DialHTTP("tcp", "127.0.0.1:5002")
 98	if err != nil {
 99		log.Fatal("Connection error: ", err)
100	}
101	// first store the value
102	data := []byte("test data0")
103	h := kademlia.HashData(data)
104	fmt.Println(h)
105	var reply bool
106	err = client.Call("Node.Store", data, &reply)
107	if err != nil {
108		panic(err)
109	}
110	fmt.Println(reply)
111
112	// now call FIND_VALUE
113	id, err := kademlia.IDFromString("1ff734fb9897600ca54a9c55ace2d22a51afb610")
114	if err != nil {
115		panic(err)
116	}
117	var reply2 node.FindValueResp
118	err = client.Call("Node.FindValue", id, &reply2)
119	if err != nil {
120		panic(err)
121	}
122	if len(reply2.Value) == 0 {
123		panic(errors.New("expected value response on FIND_VALUE"))
124	}
125	fmt.Println("FIND_VALUE response:", string(reply2.Value))
126}
127
128func testStore() {
129	client, err := rpc.DialHTTP("tcp", "127.0.0.1:5000")
130	if err != nil {
131		log.Fatal("Connection error: ", err)
132	}
133	var reply bool
134	for i := 0; i < 10; i++ {
135		err = client.Call("Node.Store", []byte("test data"+strconv.Itoa(i)), &reply)
136		if err != nil {
137			panic(err)
138		}
139		fmt.Println(reply)
140	}
141
142}
143
144func prepareTestListedNodes() []kademlia.ListedNode {
145	lnIDs := []string{
146		"c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b",
147		"420bfebd44fc62615253224328f40f29c9b225fa",
148		"6272bb67b1661abfa07d1d32cd9b810e531d42cd",
149		"07db608db033384895e48098a1bbe25266387463",
150		"c19c3285ab9ada4b420050ae1a204640b2bed508",
151		"f8971d5da24517c8cc5a316fb0658de8906c4155",
152		"04122a5f2dec42284147b1847ec1bc41ecd78626",
153		"407a90870d7b482a271446c85fda940ce78a4c7a",
154		"5ebe4539e7a33721a8623f7ebfab62600aa503e7",
155		"fc44a42879ef3a74d6bd8303bc3e4e205a92acf9",
156		"fc44a42879ef3a74d6bd8303bc3e4e205a92acf9",
157		"10c86f96ebaa1685a46a6417e6faa2ef34a68976",
158		"243c81515196a5b0e2d4675e73f0da3eead12793",
159		"0fd85ddddf15aeec2d5d8b01b013dbca030a18d7",
160		"0fd85ddddf15aeec2d5d8b01b013dbca030a18d5",
161		"0fd85ddddf15aeec2d5d8b01b013dbca030a18d0",
162		"0fd85ddddf15aeec2d5d8b01b013dbca030a1800",
163		"0750931c40a52a2facd220a02851f7d34f95e1fa",
164		"ebfba615ac50bcd0f5c2420741d032e885abf576",
165	}
166	var lns []kademlia.ListedNode
167	for i := 0; i < len(lnIDs); i++ {
168		idI, err := kademlia.IDFromString(lnIDs[i])
169		if err != nil {
170			panic(err)
171		}
172		lnI := kademlia.ListedNode{
173			ID:   idI,
174			Addr: "",
175		}
176		lns = append(lns, lnI)
177	}
178	return lns
179}