forked from TrueCloudLab/neoneo-go
wallet: truncate file when writing
If wallet size decreases, we need to remove trailing garbage if it exists. This can happen when removing account or reading pretty-printed wallet. It doesn't affect our CLI (we decode only file prefix), but it is nice to always have a valid JSON file. Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
8f196c8222
commit
a429aa3e68
2 changed files with 42 additions and 1 deletions
|
@ -19,6 +19,38 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWalletAccountRemove(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
os.RemoveAll(tmpDir)
|
||||
})
|
||||
|
||||
e := newExecutor(t, false)
|
||||
|
||||
walletPath := path.Join(tmpDir, "wallet.json")
|
||||
e.In.WriteString("acc1\r")
|
||||
e.In.WriteString("pass\r")
|
||||
e.In.WriteString("pass\r")
|
||||
e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath, "--account")
|
||||
|
||||
e.In.WriteString("acc2\r")
|
||||
e.In.WriteString("pass\r")
|
||||
e.In.WriteString("pass\r")
|
||||
e.Run(t, "neo-go", "wallet", "create", "--wallet", walletPath)
|
||||
|
||||
w, err := wallet.NewWalletFromFile(walletPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := w.Accounts[0].Address
|
||||
e.Run(t, "neo-go", "wallet", "remove", "--wallet", walletPath,
|
||||
"--address", addr, "--force")
|
||||
|
||||
rawWallet, err := ioutil.ReadFile(walletPath)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, json.Unmarshal(rawWallet, new(wallet.Wallet)))
|
||||
}
|
||||
|
||||
func TestWalletInit(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit")
|
||||
require.NoError(t, err)
|
||||
|
|
|
@ -167,7 +167,16 @@ func (w *Wallet) writeRaw(data []byte) error {
|
|||
}
|
||||
|
||||
_, err := w.rw.Write(data)
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if f, ok := w.rw.(*os.File); ok {
|
||||
if err := f.Truncate(int64(len(data))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Wallet) rewind() error {
|
||||
|
|
Loading…
Reference in a new issue