55 "testing"
66 "time"
77
8+ "github.com/gogo/protobuf/proto"
89 ipns_pb "github.com/ipfs/boxo/ipns/pb"
910 "github.com/ipfs/boxo/path"
1011 "github.com/ipfs/boxo/util"
@@ -107,7 +108,7 @@ func TestNewRecord(t *testing.T) {
107108 t .Run ("V1+V2 with option" , func (t * testing.T ) {
108109 t .Parallel ()
109110
110- rec := mustNewRecord (t , sk , testPath , seq , eol , ttl , CompatibleWithV1 (true ))
111+ rec := mustNewRecord (t , sk , testPath , seq , eol , ttl , WithV1Compatibility (true ))
111112 require .NotEmpty (t , rec .pb .SignatureV1 )
112113
113114 _ , err := rec .PubKey ()
@@ -116,51 +117,50 @@ func TestNewRecord(t *testing.T) {
116117 fieldsMatch (t , rec , testPath , seq , eol , ttl )
117118 fieldsMatchV1 (t , rec , testPath , seq , eol , ttl )
118119 })
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
129120
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 ()
131123
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 )
134128
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+ })
137134
138- recPK , err := rec . PubKey ()
139- require . NoError ( t , err )
135+ t . Run ( "Public key not embedded by default for Ed25519 keys" , func ( t * testing. T ) {
136+ t . Parallel ( )
140137
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 )
143142
144- require .Equal (t , pid , recPID )
143+ _ , err := rec .PubKey ()
144+ require .ErrorIs (t , err , ErrPublicKeyNotFound )
145+ }
146+ })
145147}
146148
147149func TestExtractPublicKey (t * testing.T ) {
148150 t .Parallel ()
149151
150152 t .Run ("Returns expected public key when embedded in Peer ID" , func (t * testing.T ) {
151153 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 ) )
153155
154156 pk2 , err := ExtractPublicKey (rec , pid )
155157 require .Nil (t , err )
156158 require .Equal (t , pk , pk2 )
157159 })
158160
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 ) {
160162 sk , pk , pid := mustKeyPair (t , ic .RSA )
161163 rec := mustNewRecord (t , sk , testPath , 0 , time .Now ().Add (time .Hour ), time .Minute * 10 )
162- err := EmbedPublicKey (rec , pk )
163- require .Nil (t , err )
164164
165165 pk2 , err := ExtractPublicKey (rec , pid )
166166 require .Nil (t , err )
@@ -169,7 +169,7 @@ func TestExtractPublicKey(t *testing.T) {
169169
170170 t .Run ("Errors when not embedded in Record or Peer ID" , func (t * testing.T ) {
171171 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 ) )
173173
174174 pk , err := ExtractPublicKey (rec , pid )
175175 require .Error (t , err )
@@ -248,3 +248,52 @@ func TestCBORDataSerialization(t *testing.T) {
248248 assert .Equal (t , expected , f )
249249 }
250250}
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