|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_readAuthorizedKeys(t *testing.T) { |
| 13 | + v := Vault{} |
| 14 | + v.ReadAuthorizedKeys("../test/authorized_keys") |
| 15 | + assert.Equal(t, uint32(0x752098b2), crc32sum(v.publicKeys[0])) |
| 16 | + assert.Equal(t, uint32(0xbc02a9b3), crc32sum(v.publicKeys[1])) |
| 17 | +} |
| 18 | + |
| 19 | +func Test_readPrivateKey(t *testing.T) { |
| 20 | + v := Vault{} |
| 21 | + v.readPrivateKey("../test/t1_id_rsa") |
| 22 | + assert.Equal(t, uint32(0xaa0afb16), crc32sum(v.privateKey)) |
| 23 | +} |
| 24 | + |
| 25 | +func Test_storeAndReadVault(t *testing.T) { |
| 26 | + tmpfile, _ := ioutil.TempFile("", "ssh-crypt-test") |
| 27 | + defer os.Remove(tmpfile.Name()) |
| 28 | + v1 := Vault{Plaintext: []byte("my secret")} |
| 29 | + v1.ReadAuthorizedKeys("../test/authorized_keys") |
| 30 | + v1.StoreSecuredVault(tmpfile.Name()) |
| 31 | + |
| 32 | + v2 := Vault{} |
| 33 | + v2.DecryptVaultWithKey(tmpfile.Name(), "../test/t1_id_rsa") |
| 34 | + v3 := Vault{} |
| 35 | + v3.DecryptVaultWithKey(tmpfile.Name(), "../test/t2_id_rsa") |
| 36 | + assert.Equal(t, v2.Plaintext, v3.Plaintext) |
| 37 | +} |
| 38 | + |
| 39 | +func Test_storeAndReadVaultWithNotUsedKey(t *testing.T) { |
| 40 | + tmpfile, _ := ioutil.TempFile("", "ssh-crypt-test") |
| 41 | + defer os.Remove(tmpfile.Name()) |
| 42 | + v1 := Vault{Plaintext: []byte("my secret")} |
| 43 | + v1.ReadAuthorizedKeys("../test/authorized_keys") |
| 44 | + v1.StoreSecuredVault(tmpfile.Name()) |
| 45 | + |
| 46 | + v3 := Vault{} |
| 47 | + err := v3.DecryptVaultWithKey(tmpfile.Name(), "../test/t3_id_rsa") |
| 48 | + assert.Equal(t, "Unable to read vault "+tmpfile.Name()+" with key ../test/t3_id_rsa", err.Error()) |
| 49 | +} |
| 50 | + |
| 51 | +func Test_EncodingVaultSecured(t *testing.T) { |
| 52 | + v := vaultSecured{} |
| 53 | + v1, err := fromBase64(toBase64(v)) |
| 54 | + assert.Equal(t, v, v1) |
| 55 | + assert.Nil(t, err) |
| 56 | +} |
| 57 | + |
| 58 | +func Test_ReadWithKeyEmptyVault(t *testing.T) { |
| 59 | + v := Vault{} |
| 60 | + v.DecryptVaultWithKey("/tmp/newvault123123", "../test/t1_id_rsa") |
| 61 | + assert.Equal(t, []byte(""), v.Plaintext) |
| 62 | +} |
| 63 | + |
| 64 | +func Test_ReadWithKeyNotBase64(t *testing.T) { |
| 65 | + v := Vault{} |
| 66 | + assert.Equal( |
| 67 | + t, |
| 68 | + "../test/authorized_keys content does not look like base64", |
| 69 | + v.DecryptVaultWithKey("../test/authorized_keys", "../test/t1_id_rsa").Error(), |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +func Test_ReadWithKeyNoKey(t *testing.T) { |
| 74 | + v := Vault{} |
| 75 | + assert.Equal( |
| 76 | + t, |
| 77 | + "no such file", |
| 78 | + v.DecryptVaultWithKey("../test/authorized_keys", "no such file").(*os.PathError).Path, |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +func Test_readPrivateKeyNoKey(t *testing.T) { |
| 83 | + v := Vault{} |
| 84 | + assert.Equal(t, "no such file", v.readPrivateKey("no such file").(*os.PathError).Path) |
| 85 | +} |
| 86 | + |
| 87 | +func Test_readPrivateKeyFail2(t *testing.T) { |
| 88 | + v := Vault{} |
| 89 | + assert.Equal(t, errors.New("Unable to decode private key"), v.readPrivateKey("../test/authorized_keys")) |
| 90 | +} |
0 commit comments