5
5
"testing"
6
6
"time"
7
7
8
+ "github.com/gogo/protobuf/proto"
8
9
ipns_pb "github.com/ipfs/boxo/ipns/pb"
9
10
"github.com/ipfs/boxo/path"
10
11
"github.com/ipfs/boxo/util"
@@ -107,7 +108,7 @@ func TestNewRecord(t *testing.T) {
107
108
t .Run ("V1+V2 with option" , func (t * testing.T ) {
108
109
t .Parallel ()
109
110
110
- rec := mustNewRecord (t , sk , testPath , seq , eol , ttl , CompatibleWithV1 (true ))
111
+ rec := mustNewRecord (t , sk , testPath , seq , eol , ttl , WithV1Compatibility (true ))
111
112
require .NotEmpty (t , rec .pb .SignatureV1 )
112
113
113
114
_ , err := rec .PubKey ()
@@ -116,51 +117,50 @@ func TestNewRecord(t *testing.T) {
116
117
fieldsMatch (t , rec , testPath , seq , eol , ttl )
117
118
fieldsMatchV1 (t , rec , testPath , seq , eol , ttl )
118
119
})
119
- }
120
-
121
- func TestEmbedPublicKey (t * testing.T ) {
122
- t .Parallel ()
123
-
124
- sk , pk , pid := mustKeyPair (t , ic .RSA )
125
-
126
- seq := uint64 (0 )
127
- eol := time .Now ().Add (time .Hour )
128
- ttl := time .Minute * 10
129
120
130
- rec := mustNewRecord (t , sk , testPath , seq , eol , ttl )
121
+ t .Run ("Public key embedded by default for RSA and ECDSA keys" , func (t * testing.T ) {
122
+ t .Parallel ()
131
123
132
- _ , err := rec .PubKey ()
133
- require .ErrorIs (t , err , ErrPublicKeyNotFound )
124
+ for _ , keyType := range []int {ic .RSA , ic .ECDSA } {
125
+ sk , _ , _ := mustKeyPair (t , keyType )
126
+ rec := mustNewRecord (t , sk , testPath , seq , eol , ttl )
127
+ fieldsMatch (t , rec , testPath , seq , eol , ttl )
134
128
135
- err = EmbedPublicKey (rec , pk )
136
- require .NoError (t , err )
129
+ pk , err := rec .PubKey ()
130
+ require .NoError (t , err )
131
+ require .True (t , pk .Equals (sk .GetPublic ()))
132
+ }
133
+ })
137
134
138
- recPK , err := rec . PubKey ()
139
- require . NoError ( t , err )
135
+ t . Run ( "Public key not embedded by default for Ed25519 and Secp256k1 keys" , func ( t * testing. T ) {
136
+ t . Parallel ( )
140
137
141
- recPID , err := peer .IDFromPublicKey (recPK )
142
- require .NoError (t , err )
138
+ for _ , keyType := range []int {ic .Ed25519 , ic .Secp256k1 } {
139
+ sk , _ , _ := mustKeyPair (t , keyType )
140
+ rec := mustNewRecord (t , sk , testPath , seq , eol , ttl )
141
+ fieldsMatch (t , rec , testPath , seq , eol , ttl )
143
142
144
- require .Equal (t , pid , recPID )
143
+ _ , err := rec .PubKey ()
144
+ require .ErrorIs (t , err , ErrPublicKeyNotFound )
145
+ }
146
+ })
145
147
}
146
148
147
149
func TestExtractPublicKey (t * testing.T ) {
148
150
t .Parallel ()
149
151
150
152
t .Run ("Returns expected public key when embedded in Peer ID" , func (t * testing.T ) {
151
153
sk , pk , pid := mustKeyPair (t , ic .Ed25519 )
152
- rec := mustNewRecord (t , sk , testPath , 0 , time .Now ().Add (time .Hour ), time .Minute * 10 )
154
+ rec := mustNewRecord (t , sk , testPath , 0 , time .Now ().Add (time .Hour ), time .Minute * 10 , WithPublicKey ( false ) )
153
155
154
156
pk2 , err := ExtractPublicKey (rec , pid )
155
157
require .Nil (t , err )
156
158
require .Equal (t , pk , pk2 )
157
159
})
158
160
159
- t .Run ("Returns expected public key when embedded in Record" , func (t * testing.T ) {
161
+ t .Run ("Returns expected public key when embedded in Record (by default) " , func (t * testing.T ) {
160
162
sk , pk , pid := mustKeyPair (t , ic .RSA )
161
163
rec := mustNewRecord (t , sk , testPath , 0 , time .Now ().Add (time .Hour ), time .Minute * 10 )
162
- err := EmbedPublicKey (rec , pk )
163
- require .Nil (t , err )
164
164
165
165
pk2 , err := ExtractPublicKey (rec , pid )
166
166
require .Nil (t , err )
@@ -169,7 +169,7 @@ func TestExtractPublicKey(t *testing.T) {
169
169
170
170
t .Run ("Errors when not embedded in Record or Peer ID" , func (t * testing.T ) {
171
171
sk , _ , pid := mustKeyPair (t , ic .RSA )
172
- rec := mustNewRecord (t , sk , testPath , 0 , time .Now ().Add (time .Hour ), time .Minute * 10 )
172
+ rec := mustNewRecord (t , sk , testPath , 0 , time .Now ().Add (time .Hour ), time .Minute * 10 , WithPublicKey ( false ) )
173
173
174
174
pk , err := ExtractPublicKey (rec , pid )
175
175
require .Error (t , err )
@@ -248,3 +248,52 @@ func TestCBORDataSerialization(t *testing.T) {
248
248
assert .Equal (t , expected , f )
249
249
}
250
250
}
251
+
252
+ func TestUnmarshal (t * testing.T ) {
253
+ t .Parallel ()
254
+
255
+ t .Run ("Errors on invalid bytes" , func (t * testing.T ) {
256
+ _ , err := UnmarshalRecord ([]byte ("blah blah blah" ))
257
+ require .ErrorIs (t , err , ErrBadRecord )
258
+ })
259
+
260
+ t .Run ("Errors if record is too long" , func (t * testing.T ) {
261
+ data := make ([]byte , MaxRecordSize + 1 )
262
+ _ , err := UnmarshalRecord (data )
263
+ require .ErrorIs (t , err , ErrRecordSize )
264
+ })
265
+
266
+ t .Run ("Errors with V1-only records" , func (t * testing.T ) {
267
+ pb := ipns_pb.IpnsEntry {}
268
+ data , err := proto .Marshal (& pb )
269
+ require .NoError (t , err )
270
+ _ , err = UnmarshalRecord (data )
271
+ require .ErrorIs (t , err , ErrDataMissing )
272
+ })
273
+
274
+ t .Run ("Errors on bad data" , func (t * testing.T ) {
275
+ pb := ipns_pb.IpnsEntry {
276
+ Data : []byte ("definitely not cbor" ),
277
+ }
278
+ data , err := proto .Marshal (& pb )
279
+ require .NoError (t , err )
280
+ _ , err = UnmarshalRecord (data )
281
+ require .ErrorIs (t , err , ErrBadRecord )
282
+ })
283
+ }
284
+
285
+ func TestKey (t * testing.T ) {
286
+ for _ , v := range [][]string {
287
+ {"RSA" , "QmRp2LvtSQtCkUWCpi92ph5MdQyRtfb9jHbkNgZzGExGuG" , "/ipns/k2k4r8kpauqq30hoj9oktej5btbgz1jeos16d3te36xd78trvak0jcor" },
288
+ {"Ed25519" , "12D3KooWSzRuSFHgLsKr6jJboAPdP7xMga2YBgBspYuErxswcgvt" , "/ipns/k51qzi5uqu5dmjjgoe7s21dncepi970722cn30qlhm9qridas1c9ktkjb6ejux" },
289
+ {"ECDSA" , "QmSBUTocZ9LxE53Br9PDDcPWnR1FJQRv94U96Wkt8eypAw" , "/ipns/k2k4r8ku8cnc1sl2h5xn7i07dma9abfnkqkxi4a6nd1xq0knoxe7b0y4" },
290
+ {"Secp256k1" , "16Uiu2HAmUymv6JpFwNZppdKUMxGJuHsTeicXgHGKbBasu4Ruj3K1" , "/ipns/kzwfwjn5ji4puw3jc1qw4b073j74xvq21iziuqw4rem21pr7f0l4dj8i9yb978s" },
291
+ } {
292
+ t .Run (v [0 ], func (t * testing.T ) {
293
+ pid , err := peer .Decode (v [1 ])
294
+ require .NoError (t , err )
295
+ key := Key (pid )
296
+ require .Equal (t , v [2 ], key )
297
+ })
298
+ }
299
+ }
0 commit comments