fix behaviour of key rename to same name
Renaming a key to the same name always succeeds regardless of force flag. Fixed bug whereby renaming to same name (with --force) would delete the key by accident fixed #5450 License: MIT Signed-off-by: Rob Deutsch <rdeutschob@gmail.com>
rob-deutsch committed
Sep 14, 2018 at 12:24 UTC
b06a0b0e3244b624631ae090b9a5b59c2bd6520c
2 files changed
+63
-1
core/coreapi/key.go
+7
-1
@@ -8,10 +8,10 @@ import (
8
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11
- ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
11
12
crypto "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
13
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
14
+ ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
15
)
16
17
type KeyAPI CoreAPI
@@ -159,6 +159,12 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
159
return nil, false, err
160
}
161
162
+ // This is important, because future code will delete key `oldName`
163
+ // even if it is the same as newName.
164
+ if newName == oldName {
165
+ return &key{oldName, pid}, false, nil
166
+ }
167
+
168
overwrite := false
169
if options.Force {
170
exist, err := ks.Has(newName)
core/coreapi/key_test.go
+56
@@ -366,6 +366,62 @@ func TestRenameOverwrite(t *testing.T) {
366
}
367
}
368
369
+func TestRenameSameNameNoForce(t *testing.T) {
370
+ ctx := context.Background()
371
+ _, api, err := makeAPI(ctx)
372
+ if err != nil {
373
+ t.Error(err)
374
+ }
375
+
376
+ _, err = api.Key().Generate(ctx, "foo")
377
+ if err != nil {
378
+ t.Fatal(err)
379
+ return
380
+ }
381
+
382
+ k, overwrote, err := api.Key().Rename(ctx, "foo", "foo")
383
+ if err != nil {
384
+ t.Fatal(err)
385
+ return
386
+ }
387
+
388
+ if overwrote {
389
+ t.Error("overwrote should be false")
390
+ }
391
+
392
+ if k.Name() != "foo" {
393
+ t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
394
+ }
395
+}
396
+
397
+func TestRenameSameName(t *testing.T) {
398
+ ctx := context.Background()
399
+ _, api, err := makeAPI(ctx)
400
+ if err != nil {
401
+ t.Error(err)
402
+ }
403
+
404
+ _, err = api.Key().Generate(ctx, "foo")
405
+ if err != nil {
406
+ t.Fatal(err)
407
+ return
408
+ }
409
+
410
+ k, overwrote, err := api.Key().Rename(ctx, "foo", "foo", opt.Key.Force(true))
411
+ if err != nil {
412
+ t.Fatal(err)
413
+ return
414
+ }
415
+
416
+ if overwrote {
417
+ t.Error("overwrote should be false")
418
+ }
419
+
420
+ if k.Name() != "foo" {
421
+ t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
422
+ }
423
+}
424
+
425
func TestRemove(t *testing.T) {
426
ctx := context.Background()
427
_, api, err := makeAPI(ctx)