@@ -2,13 +2,17 @@ package identify
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"testing"
6
7
"time"
7
8
8
9
"github.com/libp2p/go-libp2p/core/network"
9
10
"github.com/libp2p/go-libp2p/core/peer"
11
+ "github.com/libp2p/go-libp2p/core/peerstore"
12
+ recordPb "github.com/libp2p/go-libp2p/core/record/pb"
10
13
blhost "github.com/libp2p/go-libp2p/p2p/host/blank"
11
14
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
15
+ "google.golang.org/protobuf/proto"
12
16
13
17
"github.com/stretchr/testify/assert"
14
18
"github.com/stretchr/testify/require"
@@ -82,3 +86,90 @@ func TestFastDisconnect(t *testing.T) {
82
86
// double-check to make sure we didn't actually timeout somewhere.
83
87
require .NoError (t , ctx .Err ())
84
88
}
89
+
90
+ func TestWrongSignedPeerRecord (t * testing.T ) {
91
+ h1 := blhost .NewBlankHost (swarmt .GenSwarm (t ))
92
+ defer h1 .Close ()
93
+ ids , err := NewIDService (h1 )
94
+ require .NoError (t , err )
95
+ ids .Start ()
96
+ defer ids .Close ()
97
+
98
+ h2 := blhost .NewBlankHost (swarmt .GenSwarm (t ))
99
+ defer h2 .Close ()
100
+ ids2 , err := NewIDService (h2 )
101
+ require .NoError (t , err )
102
+ ids2 .Start ()
103
+ defer ids2 .Close ()
104
+
105
+ h3 := blhost .NewBlankHost (swarmt .GenSwarm (t ))
106
+ defer h2 .Close ()
107
+ ids3 , err := NewIDService (h3 )
108
+ require .NoError (t , err )
109
+ ids3 .Start ()
110
+ defer ids3 .Close ()
111
+
112
+ h2 .Connect (context .Background (), peer.AddrInfo {ID : h1 .ID (), Addrs : h1 .Addrs ()})
113
+ s , err := h2 .NewStream (context .Background (), h1 .ID (), IDPush )
114
+ require .NoError (t , err )
115
+
116
+ err = ids3 .sendIdentifyResp (s , true )
117
+ // This should fail because the peer record is signed by h3, not h2
118
+ require .NoError (t , err )
119
+ time .Sleep (time .Second )
120
+
121
+ require .Empty (t , h1 .Peerstore ().Addrs (h3 .ID ()), "h1 should not know about h3 since it was relayed over h2" )
122
+ }
123
+
124
+ func TestInvalidSignedPeerRecord (t * testing.T ) {
125
+ h1 := blhost .NewBlankHost (swarmt .GenSwarm (t ))
126
+ defer h1 .Close ()
127
+ ids , err := NewIDService (h1 )
128
+ require .NoError (t , err )
129
+ ids .Start ()
130
+ defer ids .Close ()
131
+
132
+ h2 := blhost .NewBlankHost (swarmt .GenSwarm (t ))
133
+ defer h2 .Close ()
134
+ ids2 , err := NewIDService (h2 )
135
+ require .NoError (t , err )
136
+ // We don't want to start the identify service, we'll manage the messages h2
137
+ // sends manually so we can tweak it
138
+ // ids2.Start()
139
+
140
+ h2 .Connect (context .Background (), peer.AddrInfo {ID : h1 .ID (), Addrs : h1 .Addrs ()})
141
+ require .Empty (t , h1 .Peerstore ().Addrs (h2 .ID ()))
142
+
143
+ s , err := h2 .NewStream (context .Background (), h1 .ID (), IDPush )
144
+ require .NoError (t , err )
145
+
146
+ ids2 .updateSnapshot ()
147
+ ids2 .currentSnapshot .Lock ()
148
+ snapshot := ids2 .currentSnapshot .snapshot
149
+ ids2 .currentSnapshot .Unlock ()
150
+ mes := ids2 .createBaseIdentifyResponse (s .Conn (), & snapshot )
151
+ fmt .Println ("Signed record is" , snapshot .record )
152
+ marshalled , err := snapshot .record .Marshal ()
153
+ require .NoError (t , err )
154
+
155
+ var envPb recordPb.Envelope
156
+ err = proto .Unmarshal (marshalled , & envPb )
157
+ require .NoError (t , err )
158
+
159
+ envPb .Signature = []byte ("invalid" )
160
+
161
+ mes .SignedPeerRecord , err = proto .Marshal (& envPb )
162
+ require .NoError (t , err )
163
+
164
+ err = ids2 .writeChunkedIdentifyMsg (s , mes )
165
+ require .NoError (t , err )
166
+ fmt .Println ("Done sending msg" )
167
+ s .Close ()
168
+
169
+ // Wait a bit for h1 to process the message
170
+ time .Sleep (1 * time .Second )
171
+
172
+ cab , ok := h1 .Peerstore ().(peerstore.CertifiedAddrBook )
173
+ require .True (t , ok )
174
+ require .Nil (t , cab .GetPeerRecord (h2 .ID ()))
175
+ }
0 commit comments