37baabe134
* EE-384: add endpoint to set auto backup (#224) * EE-383: add endpoint to fetch backup settings (#231) * add get backup settings handler * add api docs desc * EE-382: restore from s3 (#233) * EE-381: add GET backup status handler (#234) * EE-385: Add S3 backup execute handler (#237) * add s3 backup execute handler * refactories inside `./api/backup/backup_scheduler.go` and `./api/backup/backup_scheduler.go` * fix tests * EE-375: added backup to S3 form * EE-376: added restore from S3 form * EE-377: Update Home screen to display last backup run status * update backup service with back end endpoints. * restart admin monitor during s3 restores * use go 1.13 * go 1.13 compatibility * EE-375: added cron-validator lib * EE-375: using enum to compare form types * EE-375: validate cron rule field * try fix windows build * EE-375 EE-376 backup and restore forms validation changes * fix(autobackup): update autobackup settings validation rules (#260) * fix(autobackup): automate backup to s3 fe update (#261) * EE-292: fixed typo in property. * EE-292: updated auto backup front end validation. * EE-292: updated lib to validate cron rule in front end * fix dependencies * bumped libcompose version Co-authored-by: Hui <arris_li@hotmail.com> Co-authored-by: Felix Han <felix.han@portainer.io> Co-authored-by: fhanportainer <79428273+fhanportainer@users.noreply.github.com>
132 lines
4.2 KiB
Go
132 lines
4.2 KiB
Go
package crypto
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
|
|
tmpdir, _ := ioutil.TempDir("", "encrypt")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
var (
|
|
originFilePath = filepath.Join(tmpdir, "origin")
|
|
encryptedFilePath = filepath.Join(tmpdir, "encrypted")
|
|
decryptedFilePath = filepath.Join(tmpdir, "decrypted")
|
|
)
|
|
|
|
content := []byte("content")
|
|
ioutil.WriteFile(originFilePath, content, 0600)
|
|
|
|
originFile, _ := os.Open(originFilePath)
|
|
defer originFile.Close()
|
|
|
|
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
|
defer encryptedFileWriter.Close()
|
|
|
|
err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase"))
|
|
assert.Nil(t, err, "Failed to encrypt a file")
|
|
encryptedContent, err := ioutil.ReadFile(encryptedFilePath)
|
|
assert.Nil(t, err, "Couldn't read encrypted file")
|
|
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
|
|
|
|
encryptedFileReader, _ := os.Open(encryptedFilePath)
|
|
defer encryptedFileReader.Close()
|
|
|
|
decryptedFileWriter, _ := os.Create(decryptedFilePath)
|
|
defer decryptedFileWriter.Close()
|
|
|
|
decryptedReader, err := AesDecrypt(encryptedFileReader, []byte("passphrase"))
|
|
assert.Nil(t, err, "Failed to decrypt file")
|
|
|
|
io.Copy(decryptedFileWriter, decryptedReader)
|
|
|
|
decryptedContent, _ := ioutil.ReadFile(decryptedFilePath)
|
|
assert.Equal(t, content, decryptedContent, "Original and decrypted content should match")
|
|
}
|
|
|
|
func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
|
|
tmpdir, _ := ioutil.TempDir("", "encrypt")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
var (
|
|
originFilePath = filepath.Join(tmpdir, "origin")
|
|
encryptedFilePath = filepath.Join(tmpdir, "encrypted")
|
|
decryptedFilePath = filepath.Join(tmpdir, "decrypted")
|
|
)
|
|
|
|
content := []byte("content")
|
|
ioutil.WriteFile(originFilePath, content, 0600)
|
|
|
|
originFile, _ := os.Open(originFilePath)
|
|
defer originFile.Close()
|
|
|
|
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
|
defer encryptedFileWriter.Close()
|
|
|
|
err := AesEncrypt(originFile, encryptedFileWriter, []byte(""))
|
|
assert.Nil(t, err, "Failed to encrypt a file")
|
|
encryptedContent, err := ioutil.ReadFile(encryptedFilePath)
|
|
assert.Nil(t, err, "Couldn't read encrypted file")
|
|
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
|
|
|
|
encryptedFileReader, _ := os.Open(encryptedFilePath)
|
|
defer encryptedFileReader.Close()
|
|
|
|
decryptedFileWriter, _ := os.Create(decryptedFilePath)
|
|
defer decryptedFileWriter.Close()
|
|
|
|
decryptedReader, err := AesDecrypt(encryptedFileReader, []byte(""))
|
|
assert.Nil(t, err, "Failed to decrypt file")
|
|
|
|
io.Copy(decryptedFileWriter, decryptedReader)
|
|
|
|
decryptedContent, _ := ioutil.ReadFile(decryptedFilePath)
|
|
assert.Equal(t, content, decryptedContent, "Original and decrypted content should match")
|
|
}
|
|
|
|
func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T) {
|
|
tmpdir, _ := ioutil.TempDir("", "encrypt")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
var (
|
|
originFilePath = filepath.Join(tmpdir, "origin")
|
|
encryptedFilePath = filepath.Join(tmpdir, "encrypted")
|
|
decryptedFilePath = filepath.Join(tmpdir, "decrypted")
|
|
)
|
|
|
|
content := []byte("content")
|
|
ioutil.WriteFile(originFilePath, content, 0600)
|
|
|
|
originFile, _ := os.Open(originFilePath)
|
|
defer originFile.Close()
|
|
|
|
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
|
defer encryptedFileWriter.Close()
|
|
|
|
err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase"))
|
|
assert.Nil(t, err, "Failed to encrypt a file")
|
|
encryptedContent, err := ioutil.ReadFile(encryptedFilePath)
|
|
assert.Nil(t, err, "Couldn't read encrypted file")
|
|
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
|
|
|
|
encryptedFileReader, _ := os.Open(encryptedFilePath)
|
|
defer encryptedFileReader.Close()
|
|
|
|
decryptedFileWriter, _ := os.Create(decryptedFilePath)
|
|
defer decryptedFileWriter.Close()
|
|
|
|
decryptedReader, err := AesDecrypt(encryptedFileReader, []byte("garbage"))
|
|
assert.Nil(t, err, "Should allow to decrypt with wrong passphrase")
|
|
|
|
io.Copy(decryptedFileWriter, decryptedReader)
|
|
|
|
decryptedContent, _ := ioutil.ReadFile(decryptedFilePath)
|
|
assert.NotEqual(t, content, decryptedContent, "Original and decrypted content should NOT match")
|
|
}
|