1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-04-19 07:42:18 +03:00
ssh-vault/create_test.go

70 lines
1.1 KiB
Go

package sshvault
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/ssh-vault/crypto"
"github.com/ssh-vault/crypto/aead"
)
func TestCreate(t *testing.T) {
dir, err := ioutil.TempDir("", "vault")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir) // clean up
tmpfile := filepath.Join(dir, "vault")
vault, err := New("", "test_data/id_rsa.pub", "", "create", tmpfile)
if err != nil {
t.Error(err)
}
PKCS8, err := vault.PKCS8()
if err != nil {
t.Error(err)
}
vault.PublicKey, err = vault.GetRSAPublicKey(PKCS8)
if err != nil {
t.Error(err)
}
vault.Fingerprint, err = vault.GenFingerprint(PKCS8)
if err != nil {
t.Error(err)
}
if vault.Password, err = crypto.GenerateNonce(32); err != nil {
t.Error(err)
}
os.Setenv("EDITOR", "cat")
data, err := vault.Create()
if err != nil {
t.Error(err)
}
out, err := aead.Encrypt(vault.Password, data, []byte(vault.Fingerprint))
if err != nil {
t.Error(err)
}
if err = vault.Close(out); err != nil {
t.Error(err)
}
plaintext, err := vault.View()
if err != nil {
t.Error(err)
}
if len(plaintext) != 0 {
t.Error("Expecting 0")
}
}