mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +08:00
Merge pull request #5465 from rob-deutsch/fix/5450
fix behaviour of key rename to same name
This commit is contained in:
@ -8,10 +8,10 @@ import (
|
|||||||
|
|
||||||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
||||||
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
|
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
|
||||||
ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
|
|
||||||
|
|
||||||
crypto "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
|
crypto "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
|
||||||
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
|
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
|
||||||
|
ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
|
||||||
)
|
)
|
||||||
|
|
||||||
type KeyAPI CoreAPI
|
type KeyAPI CoreAPI
|
||||||
@ -159,6 +159,12 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
|
|||||||
return nil, false, err
|
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
|
overwrite := false
|
||||||
if options.Force {
|
if options.Force {
|
||||||
exist, err := ks.Has(newName)
|
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) {
|
func TestRemove(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_, api, err := makeAPI(ctx)
|
_, api, err := makeAPI(ctx)
|
||||||
|
Reference in New Issue
Block a user