Skip to content

Commit

Permalink
cleanup asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcz committed Jul 29, 2024
1 parent 5b8488f commit 5ab658d
Show file tree
Hide file tree
Showing 88 changed files with 6,812 additions and 22,602 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ require (
github.com/johannesboyne/gofakes3 v0.0.0-20230506070712-04da935ef877
github.com/matryer/moq v0.3.4
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.27.3
gotest.tools/v3 v3.5.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
Expand All @@ -32,5 +31,4 @@ require (
golang.org/x/sys v0.22.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
Expand Down Expand Up @@ -49,9 +51,7 @@ github.com/shabbyrobe/gocovmerge v0.0.0-20190829150210-3e036491d500 h1:WnNuhiq+F
github.com/shabbyrobe/gocovmerge v0.0.0-20190829150210-3e036491d500/go.mod h1:+njLrG5wSeoG4Ds61rFgEzKvenR2UHbjMoDHsczxly0=
github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/urfave/cli/v2 v2.27.3 h1:/POWahRmdh7uztQ3CYnaDddk0Rm90PyOgIxgW2rr41M=
Expand Down Expand Up @@ -111,12 +111,12 @@ golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
51 changes: 26 additions & 25 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package client
import (
"testing"

"github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestGetRemoteFileWithoutDecryption(t *testing.T) {
hash := &HashStub{
VerifyFunc: func(filePath string, expectedChecksum string) error {
assert.Equal(t, "bar.txt", filePath)
assert.Equal(t, "muahahaha", expectedChecksum)
assert.Check(t, is.Equal("bar.txt", filePath))
assert.Check(t, is.Equal("muahahaha", expectedChecksum))
return nil
},
}
Expand All @@ -27,23 +28,23 @@ func TestGetRemoteFileWithoutDecryption(t *testing.T) {
}
},
DownloadFileFunc: func(remotePath string, localPath string) (string, error) {
assert.Equal(t, "s3://foo/bar.txt", remotePath)
assert.Equal(t, "bar.txt", localPath)
assert.Check(t, is.Equal("s3://foo/bar.txt", remotePath))
assert.Check(t, is.Equal("bar.txt", localPath))
return "muahahaha", nil
},
}
c := &Client{
Hash: hash,
Store: store,
}
assert.NoError(t, c.GetRemoteFile("bar.txt", "s3://foo/bar.txt"))
assert.NilError(t, c.GetRemoteFile("bar.txt", "s3://foo/bar.txt"))
}

func TestGetRemoteFileWithDecryption(t *testing.T) {
hash := &HashStub{
VerifyFunc: func(filePath string, expectedChecksum string) error {
assert.Equal(t, "bar.txt.tmp", filePath)
assert.Equal(t, "muahahaha", expectedChecksum)
assert.Check(t, is.Equal("bar.txt.tmp", filePath))
assert.Check(t, is.Equal("muahahaha", expectedChecksum))
return nil
},
}
Expand All @@ -60,15 +61,15 @@ func TestGetRemoteFileWithDecryption(t *testing.T) {
}
},
DownloadFileFunc: func(remotePath string, localPath string) (string, error) {
assert.Equal(t, "s3://foo/bar.txt", remotePath)
assert.Equal(t, "bar.txt.tmp", localPath)
assert.Check(t, is.Equal("s3://foo/bar.txt", remotePath))
assert.Check(t, is.Equal("bar.txt.tmp", localPath))
return "muahahaha", nil
},
}
cipher := &CipherStub{
DecryptFunc: func(cipherTextFile string, plainTextFile string) error {
assert.Equal(t, "bar.txt.tmp", cipherTextFile)
assert.Equal(t, "bar.txt", plainTextFile)
assert.Check(t, is.Equal("bar.txt.tmp", cipherTextFile))
assert.Check(t, is.Equal("bar.txt", plainTextFile))
return nil
},
}
Expand All @@ -77,13 +78,13 @@ func TestGetRemoteFileWithDecryption(t *testing.T) {
Store: store,
Cipher: cipher,
}
assert.NoError(t, c.GetRemoteFile("s3://foo/bar.txt", "bar.txt"))
assert.NilError(t, c.GetRemoteFile("s3://foo/bar.txt", "bar.txt"))
}

func TestPutLocalFileWithoutEncryption(t *testing.T) {
hash := &HashStub{
CalculateFunc: func(filePath string) (string, error) {
assert.Equal(t, "bar.txt", filePath)
assert.Check(t, is.Equal("bar.txt", filePath))
return "woahahaha", nil
},
}
Expand All @@ -100,23 +101,23 @@ func TestPutLocalFileWithoutEncryption(t *testing.T) {
}
},
UploadFileFunc: func(remotePath string, localPath string, checksum string) error {
assert.Equal(t, "s3://foo/bar.txt", remotePath)
assert.Equal(t, "bar.txt", localPath)
assert.Equal(t, "woahahaha", checksum)
assert.Check(t, is.Equal("s3://foo/bar.txt", remotePath))
assert.Check(t, is.Equal("bar.txt", localPath))
assert.Check(t, is.Equal("woahahaha", checksum))
return nil
},
}
c := &Client{
Hash: hash,
Store: store,
}
assert.NoError(t, c.PutLocalFile("bar.txt", "s3://foo/bar.txt"))
assert.NilError(t, c.PutLocalFile("bar.txt", "s3://foo/bar.txt"))
}

func TestPutLocalFileWithEncryption(t *testing.T) {
hash := &HashStub{
CalculateFunc: func(filePath string) (string, error) {
assert.Equal(t, "bar.txt.tmp", filePath)
assert.Check(t, is.Equal("bar.txt.tmp", filePath))
return "woahahaha", nil
},
}
Expand All @@ -133,16 +134,16 @@ func TestPutLocalFileWithEncryption(t *testing.T) {
}
},
UploadFileFunc: func(remotePath string, localPath string, checksum string) error {
assert.Equal(t, "s3://foo/bar.txt", remotePath)
assert.Equal(t, "bar.txt.tmp", localPath)
assert.Equal(t, "woahahaha", checksum)
assert.Check(t, is.Equal("s3://foo/bar.txt", remotePath))
assert.Check(t, is.Equal("bar.txt.tmp", localPath))
assert.Check(t, is.Equal("woahahaha", checksum))
return nil
},
}
cipher := &CipherStub{
EncryptFunc: func(plainTextFile string, cipherTextFile string) error {
assert.Equal(t, "bar.txt", plainTextFile)
assert.Equal(t, "bar.txt.tmp", cipherTextFile)
assert.Check(t, is.Equal("bar.txt", plainTextFile))
assert.Check(t, is.Equal("bar.txt.tmp", cipherTextFile))
return nil
},
}
Expand All @@ -151,5 +152,5 @@ func TestPutLocalFileWithEncryption(t *testing.T) {
Store: store,
Cipher: cipher,
}
assert.NoError(t, c.PutLocalFile("s3://foo/bar.txt", "bar.txt"))
assert.NilError(t, c.PutLocalFile("s3://foo/bar.txt", "bar.txt"))
}
18 changes: 9 additions & 9 deletions internal/client/crypto/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"os"
"testing"

assert "github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"github.com/tomcz/s3backup/internal/utils"
)

func TestRoundTripAESEncryptDecrypt_GeneratedKey(t *testing.T) {
key, err := GenerateAESKeyString()
assert.NoError(t, err, "Cannot generate AES key")
assert.NilError(t, err, "Cannot generate AES key")
testRoundTrip(t, key)
}

Expand All @@ -21,26 +21,26 @@ func TestRoundTripAESEncryptDecrypt_ArbitraryKey(t *testing.T) {

func testRoundTrip(t *testing.T, key string) {
expected, err := utils.Random(1024)
assert.NoError(t, err, "Cannot create file contents")
assert.NilError(t, err, "Cannot create file contents")

file, err := utils.CreateTempFile("aes", expected)
assert.NoError(t, err, "Cannot create file to encrypt")
assert.NilError(t, err, "Cannot create file to encrypt")
defer os.Remove(file)

cipher, err := NewAESCipher(key)
assert.NoError(t, err, "Cannot create AES cipher")
assert.NilError(t, err, "Cannot create AES cipher")

encryptedFile := file + ".enc"
defer os.Remove(encryptedFile)

decryptedFile := file + ".dec"
defer os.Remove(decryptedFile)

assert.NoError(t, cipher.Encrypt(file, encryptedFile), "Cannot encrypt file")
assert.NoError(t, cipher.Decrypt(encryptedFile, decryptedFile), "Cannot decrypt file")
assert.NilError(t, cipher.Encrypt(file, encryptedFile), "Cannot encrypt file")
assert.NilError(t, cipher.Decrypt(encryptedFile, decryptedFile), "Cannot decrypt file")

actual, err := os.ReadFile(decryptedFile)
assert.NoError(t, err, "Cannot read decrypted file")
assert.NilError(t, err, "Cannot read decrypted file")

assert.Equal(t, expected, actual)
assert.DeepEqual(t, expected, actual)
}
20 changes: 10 additions & 10 deletions internal/client/crypto/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@ import (
"os"
"testing"

assert "github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"github.com/tomcz/s3backup/internal/utils"
)

func TestVerifyHashOnSameFile(t *testing.T) {
buf, err := utils.Random(1024)
assert.NoError(t, err, "Cannot create file contents")
assert.NilError(t, err, "Cannot create file contents")

file, err := utils.CreateTempFile("hash", buf)
assert.NoError(t, err, "Cannot create file to hash")
assert.NilError(t, err, "Cannot create file to hash")
defer os.Remove(file)

hash := NewHash()

checksum, err := hash.Calculate(file)
assert.NoError(t, err, "Cannot create checksum")
assert.NilError(t, err, "Cannot create checksum")

assert.NoError(t, hash.Verify(file, checksum), "Unexpected mismatch")
assert.NilError(t, hash.Verify(file, checksum), "Unexpected mismatch")
}

func TestVerifyHashOnDifferentFiles(t *testing.T) {
buf1, err := utils.Random(1024)
assert.NoError(t, err, "Cannot create file contents")
assert.NilError(t, err, "Cannot create file contents")

file1, err := utils.CreateTempFile("hash", buf1)
assert.NoError(t, err, "Cannot create file to hash")
assert.NilError(t, err, "Cannot create file to hash")
defer os.Remove(file1)

buf2, err := utils.Random(1024)
assert.NoError(t, err, "Cannot create file contents")
assert.NilError(t, err, "Cannot create file contents")

file2, err := utils.CreateTempFile("hash", buf2)
assert.NoError(t, err, "Cannot create file to hash")
assert.NilError(t, err, "Cannot create file to hash")
defer os.Remove(file2)

hash := NewHash()

checksum, err := hash.Calculate(file1)
assert.NoError(t, err, "Cannot create checksum")
assert.NilError(t, err, "Cannot create checksum")

assert.ErrorContains(t, hash.Verify(file2, checksum), "checksum mismatch")
}
Expand Down
24 changes: 12 additions & 12 deletions internal/client/crypto/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@ import (
"os"
"testing"

assert "github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"github.com/tomcz/s3backup/internal/utils"
)

func TestRoundTripRSAEncryptDecrypt(t *testing.T) {
expected, err := utils.Random(1024)
assert.NoError(t, err, "Cannot create file contents")
assert.NilError(t, err, "Cannot create file contents")

file, err := utils.CreateTempFile("rsa", expected)
assert.NoError(t, err, "Cannot create file to encrypt")
assert.NilError(t, err, "Cannot create file to encrypt")
defer os.Remove(file)

privFile, err := utils.CreateTempFile("privkey", []byte{})
assert.NoError(t, err, "Cannot create private key file")
assert.NilError(t, err, "Cannot create private key file")
defer os.Remove(privFile)

pubFile, err := utils.CreateTempFile("pubkey", []byte{})
assert.NoError(t, err, "Cannot create public key file")
assert.NilError(t, err, "Cannot create public key file")
defer os.Remove(pubFile)

assert.NoError(t, GenerateRSAKeyPair(privFile, pubFile), "Cannot generate RSA key pair")
assert.NilError(t, GenerateRSAKeyPair(privFile, pubFile), "Cannot generate RSA key pair")

privCipher, err := NewRSACipher(privFile)
assert.NoError(t, err, "Cannot create RSA private cipher")
assert.NilError(t, err, "Cannot create RSA private cipher")

pubCipher, err := NewRSACipher(pubFile)
assert.NoError(t, err, "Cannot create RSA public cipher")
assert.NilError(t, err, "Cannot create RSA public cipher")

encryptedFile := file + ".enc"
defer os.Remove(encryptedFile)

decryptedFile := file + ".dec"
defer os.Remove(decryptedFile)

assert.NoError(t, pubCipher.Encrypt(file, encryptedFile), "Cannot encrypt file")
assert.NoError(t, privCipher.Decrypt(encryptedFile, decryptedFile), "Cannot decrypt file")
assert.NilError(t, pubCipher.Encrypt(file, encryptedFile), "Cannot encrypt file")
assert.NilError(t, privCipher.Decrypt(encryptedFile, decryptedFile), "Cannot decrypt file")

actual, err := os.ReadFile(decryptedFile)
assert.NoError(t, err, "Cannot read decrypted file")
assert.NilError(t, err, "Cannot read decrypted file")

assert.Equal(t, expected, actual)
assert.DeepEqual(t, expected, actual)
}
Loading

0 comments on commit 5ab658d

Please sign in to comment.