|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/MoonSHRD/p2chat/api" |
| 12 | + "github.com/MoonSHRD/p2chat/pkg" |
| 13 | + "github.com/libp2p/go-libp2p" |
| 14 | + "github.com/libp2p/go-libp2p-core/crypto" |
| 15 | + "github.com/libp2p/go-libp2p-core/host" |
| 16 | + "github.com/libp2p/go-libp2p-core/peer" |
| 17 | + "github.com/libp2p/go-libp2p-core/peerstore" |
| 18 | + "github.com/libp2p/go-libp2p-core/protocol" |
| 19 | + pubsub "github.com/libp2p/go-libp2p-pubsub" |
| 20 | + "github.com/phayes/freeport" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + numberOfNodes = 3 |
| 25 | + serviceTag = "moonshard" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + testHosts []host.Host |
| 30 | + testContexts []context.Context |
| 31 | + testHandlers []pkg.Handler |
| 32 | + testPubsubs []*pubsub.PubSub |
| 33 | + testSubscriptions []*pubsub.Subscription |
| 34 | + peerChan chan peer.AddrInfo |
| 35 | +) |
| 36 | + |
| 37 | +// Creates mock host object |
| 38 | +func createHost() (context.Context, host.Host, error) { |
| 39 | + ctx, _ /* cancel */ := context.WithCancel(context.Background()) |
| 40 | + // defer cancel() |
| 41 | + |
| 42 | + prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader) |
| 43 | + if err != nil { |
| 44 | + return nil, nil, err |
| 45 | + } |
| 46 | + |
| 47 | + port, err := freeport.GetFreePort() |
| 48 | + if err != nil { |
| 49 | + return nil, nil, err |
| 50 | + } |
| 51 | + |
| 52 | + host, err := libp2p.New( |
| 53 | + ctx, |
| 54 | + libp2p.Identity(prvKey), |
| 55 | + libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%v", port)), |
| 56 | + ) |
| 57 | + if err != nil { |
| 58 | + return nil, nil, err |
| 59 | + } |
| 60 | + |
| 61 | + return ctx, host, nil |
| 62 | +} |
| 63 | + |
| 64 | +func TestCreateHosts(t *testing.T) { |
| 65 | + for i := 0; i < numberOfNodes; i++ { |
| 66 | + tempCtx, tempHost, err := createHost() |
| 67 | + if err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + |
| 71 | + testHosts = append(testHosts, tempHost) |
| 72 | + testContexts = append(testContexts, tempCtx) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestMDNS(t *testing.T) { |
| 77 | + for i := 0; i < numberOfNodes; i++ { |
| 78 | + pb, err := pubsub.NewFloodsubWithProtocols(context.Background(), testHosts[i], []protocol.ID{protocol.ID("/moonshard/1.0.0")}, pubsub.WithMessageSigning(true), pubsub.WithStrictSignatureVerification(true)) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + testPubsubs = append(testPubsubs, pb) |
| 84 | + testHandlers = append(testHandlers, pkg.NewHandler(pb, serviceTag, &networkTopics)) |
| 85 | + |
| 86 | + peerChan = pkg.InitMDNS(testContexts[i], testHosts[i], serviceTag) |
| 87 | + |
| 88 | + subscription, err := pb.Subscribe(serviceTag) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + testSubscriptions = append(testSubscriptions, subscription) |
| 93 | + |
| 94 | + fmt.Println("Waiting for correct set up of PubSub...") |
| 95 | + time.Sleep(3 * time.Second) |
| 96 | + |
| 97 | + for j := 0; j < i; j++ { |
| 98 | + select { |
| 99 | + case peer := <-peerChan: |
| 100 | + testHosts[i].Peerstore().AddAddr(peer.ID, peer.Addrs[0], peerstore.PermanentAddrTTL) |
| 101 | + |
| 102 | + if err := testHosts[i].Connect(testContexts[i], peer); err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + default: |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Checks whether all nodes are connected to each other |
| 112 | +func TestGetPeers(t *testing.T) { |
| 113 | + for _, handler := range testHandlers { |
| 114 | + if len(handler.GetPeers(serviceTag)) != numberOfNodes-1 { |
| 115 | + t.Fatal("Not all nodes are connected to each other.") |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Sends message to service topic |
| 121 | +func TestSendMessage(t *testing.T) { |
| 122 | + message := &api.BaseMessage{ |
| 123 | + Body: fmt.Sprintf("%s send 'hello test'", testHosts[0].ID()), |
| 124 | + Flag: api.FlagGenericMessage, |
| 125 | + } |
| 126 | + |
| 127 | + sendData, err := json.Marshal(message) |
| 128 | + if err != nil { |
| 129 | + t.Fatal("Error occurred when marshalling message object") |
| 130 | + } |
| 131 | + |
| 132 | + err = testPubsubs[0].Publish(serviceTag, sendData) |
| 133 | + if err != nil { |
| 134 | + t.Fatal("Error occurred when publishing") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// Grabs message from service topic |
| 139 | +func TestGetMessage(t *testing.T) { |
| 140 | + for _, sub := range testSubscriptions[1:] { |
| 141 | + message, err := sub.Next(context.Background()) |
| 142 | + if err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + |
| 146 | + decodedMessage := &api.BaseMessage{} |
| 147 | + json.Unmarshal(message.Data, decodedMessage) |
| 148 | + |
| 149 | + originalMessage := fmt.Sprintf("%s send 'hello test'", testHosts[0].ID()) |
| 150 | + if decodedMessage.Body != originalMessage { |
| 151 | + t.Fatal("Message not does not match") |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestCloseHosts(t *testing.T) { |
| 157 | + for _, host := range testHosts { |
| 158 | + if err := host.Close(); err != nil { |
| 159 | + t.Fatal(fmt.Sprintf("Failed when closing host %v", host.ID())) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments