Unverified Commit e4e35047 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #5465 from rob-deutsch/fix/5450

fix behaviour of key rename to same name
parents cbc6e69e b06a0b0e
......@@ -8,10 +8,10 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
crypto "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
)
type KeyAPI CoreAPI
......@@ -159,6 +159,12 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
return nil, false, err
}
// This is important, because future code will delete key `oldName`
// even if it is the same as newName.
if newName == oldName {
return &key{oldName, pid}, false, nil
}
overwrite := false
if options.Force {
exist, err := ks.Has(newName)
......
......@@ -366,6 +366,62 @@ func TestRenameOverwrite(t *testing.T) {
}
}
func TestRenameSameNameNoForce(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
k, overwrote, err := api.Key().Rename(ctx, "foo", "foo")
if err != nil {
t.Fatal(err)
return
}
if overwrote {
t.Error("overwrote should be false")
}
if k.Name() != "foo" {
t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
}
}
func TestRenameSameName(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
k, overwrote, err := api.Key().Rename(ctx, "foo", "foo", opt.Key.Force(true))
if err != nil {
t.Fatal(err)
return
}
if overwrote {
t.Error("overwrote should be false")
}
if k.Name() != "foo" {
t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
}
}
func TestRemove(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment