@@ -4,11 +4,17 @@ import (
4
4
"context"
5
5
"crypto/rand"
6
6
"testing"
7
+ "time"
7
8
9
+ "github.com/ipfs/boxo/coreiface/path"
10
+ "github.com/ipfs/boxo/ipns"
11
+ ipfspath "github.com/ipfs/boxo/path"
8
12
"github.com/ipfs/boxo/routing/http/types"
9
13
"github.com/ipfs/boxo/routing/http/types/iter"
10
14
"github.com/ipfs/go-cid"
15
+ "github.com/libp2p/go-libp2p/core/crypto"
11
16
"github.com/libp2p/go-libp2p/core/peer"
17
+ "github.com/libp2p/go-libp2p/core/routing"
12
18
"github.com/multiformats/go-multihash"
13
19
"github.com/stretchr/testify/mock"
14
20
"github.com/stretchr/testify/require"
@@ -28,6 +34,15 @@ func (m *mockClient) Ready(ctx context.Context) (bool, error) {
28
34
args := m .Called (ctx )
29
35
return args .Bool (0 ), args .Error (1 )
30
36
}
37
+ func (m * mockClient ) GetIPNSRecord (ctx context.Context , name ipns.Name ) (* ipns.Record , error ) {
38
+ args := m .Called (ctx , name )
39
+ return args .Get (0 ).(* ipns.Record ), args .Error (1 )
40
+ }
41
+ func (m * mockClient ) PutIPNSRecord (ctx context.Context , name ipns.Name , record * ipns.Record ) error {
42
+ args := m .Called (ctx , name , record )
43
+ return args .Error (0 )
44
+ }
45
+
31
46
func makeCID () cid.Cid {
32
47
buf := make ([]byte , 63 )
33
48
_ , err := rand .Read (buf )
@@ -108,3 +123,87 @@ func TestFindPeer(t *testing.T) {
108
123
require .NoError (t , err )
109
124
require .Equal (t , peer .ID , p1 )
110
125
}
126
+
127
+ func makeName (t * testing.T ) (crypto.PrivKey , ipns.Name ) {
128
+ sk , _ , err := crypto .GenerateEd25519Key (rand .Reader )
129
+ require .NoError (t , err )
130
+
131
+ pid , err := peer .IDFromPrivateKey (sk )
132
+ require .NoError (t , err )
133
+
134
+ return sk , ipns .NameFromPeer (pid )
135
+ }
136
+
137
+ func makeIPNSRecord (t * testing.T , sk crypto.PrivKey , opts ... ipns.Option ) (* ipns.Record , []byte ) {
138
+ cid , err := cid .Decode ("bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4" )
139
+ require .NoError (t , err )
140
+
141
+ path := path .IpfsPath (cid )
142
+ eol := time .Now ().Add (time .Hour * 48 )
143
+ ttl := time .Second * 20
144
+
145
+ record , err := ipns .NewRecord (sk , ipfspath .FromString (path .String ()), 1 , eol , ttl , opts ... )
146
+ require .NoError (t , err )
147
+
148
+ rawRecord , err := ipns .MarshalRecord (record )
149
+ require .NoError (t , err )
150
+
151
+ return record , rawRecord
152
+ }
153
+
154
+ func TestGetValue (t * testing.T ) {
155
+ ctx := context .Background ()
156
+ client := & mockClient {}
157
+ crc := NewContentRoutingClient (client )
158
+
159
+ t .Run ("Fail On Unsupported Key" , func (t * testing.T ) {
160
+ v , err := crc .GetValue (ctx , "/something/unsupported" )
161
+ require .Nil (t , v )
162
+ require .ErrorIs (t , err , routing .ErrNotSupported )
163
+ })
164
+
165
+ t .Run ("Fail On Invalid IPNS Name" , func (t * testing.T ) {
166
+ v , err := crc .GetValue (ctx , "/ipns/invalid" )
167
+ require .Nil (t , v )
168
+ require .Error (t , err )
169
+ })
170
+
171
+ t .Run ("Succeeds On Valid IPNS Name" , func (t * testing.T ) {
172
+ sk , name := makeName (t )
173
+ rec , rawRec := makeIPNSRecord (t , sk )
174
+ client .On ("GetIPNSRecord" , ctx , name ).Return (rec , nil )
175
+ v , err := crc .GetValue (ctx , string (name .RoutingKey ()))
176
+ require .NoError (t , err )
177
+ require .Equal (t , rawRec , v )
178
+ })
179
+ }
180
+
181
+ func TestPutValue (t * testing.T ) {
182
+ ctx := context .Background ()
183
+ client := & mockClient {}
184
+ crc := NewContentRoutingClient (client )
185
+
186
+ sk , name := makeName (t )
187
+ _ , rawRec := makeIPNSRecord (t , sk )
188
+
189
+ t .Run ("Fail On Unsupported Key" , func (t * testing.T ) {
190
+ err := crc .PutValue (ctx , "/something/unsupported" , rawRec )
191
+ require .ErrorIs (t , err , routing .ErrNotSupported )
192
+ })
193
+
194
+ t .Run ("Fail On Invalid IPNS Name" , func (t * testing.T ) {
195
+ err := crc .PutValue (ctx , "/ipns/invalid" , rawRec )
196
+ require .Error (t , err )
197
+ })
198
+
199
+ t .Run ("Fail On Invalid IPNS Record" , func (t * testing.T ) {
200
+ err := crc .PutValue (ctx , string (name .RoutingKey ()), []byte ("gibberish" ))
201
+ require .Error (t , err )
202
+ })
203
+
204
+ t .Run ("Succeeds On Valid IPNS Name & Record" , func (t * testing.T ) {
205
+ client .On ("PutIPNSRecord" , ctx , name , mock .Anything ).Return (nil )
206
+ err := crc .PutValue (ctx , string (name .RoutingKey ()), rawRec )
207
+ require .NoError (t , err )
208
+ })
209
+ }
0 commit comments