Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
enrichman committed Feb 26, 2024
1 parent 25b115a commit 4d6cb70
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion internal/cli/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ If provided keys or images will be ignored`)
return decryptCmd
}

func runDecryptCmd(cmd *cobra.Command, args []string) error {
func runDecryptCmd(cmd *cobra.Command, _ []string) error {
if encryptedFile == "" {
return errors.New("missing file to decrypt. Use -f/--file flag")
}
Expand Down
11 changes: 6 additions & 5 deletions internal/cli/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/enrichman/stegosecrets/internal/cli"
"github.com/enrichman/stegosecrets/pkg/file"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const testAssetsDir = "../../test/assets/p5t3/"
Expand All @@ -19,7 +20,7 @@ func TestDecryptCmd_NoInput(t *testing.T) {
rootCmd.SetArgs([]string{"decrypt"})

err := rootCmd.Execute()
assert.NotNil(t, err)
assert.Error(t, err)
}

func TestDecryptCmd(t *testing.T) {
Expand Down Expand Up @@ -79,16 +80,16 @@ func TestDecryptCmd(t *testing.T) {

err := rootCmd.Execute()
if tc.wantExecuteErr {
assert.NotNil(t, err, outAndErr)
require.Error(t, err, outAndErr)
} else {
assert.Nil(t, err, outAndErr)
require.NoError(t, err, outAndErr)
}

err = file.Check(testAssetsDir+"secret", testAssetsDir+"secret.checksum")
if tc.wantCheckErr {
assert.NotNil(t, err, outAndErr)
require.Error(t, err, outAndErr)
} else {
assert.Nil(t, err, outAndErr)
require.NoError(t, err, outAndErr)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ If empty no images will be generated.`)
return encryptCmd
}

func runEncryptCmd(cmd *cobra.Command, args []string) error {
func runEncryptCmd(cmd *cobra.Command, _ []string) error {
if keyThreshold > keyParts {
return errors.Errorf("threshold %d cannot exceed the parts %d", keyThreshold, keyParts)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/cli/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/enrichman/stegosecrets/internal/cli"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func setupTest(tb testing.TB) func(tb testing.TB) {
Expand All @@ -19,7 +20,7 @@ func setupTest(tb testing.TB) func(tb testing.TB) {
tb.Log("teardown test")

assert.DirExists(tb, "out")
assert.Nil(tb, os.RemoveAll("out"))
assert.NoError(tb, os.RemoveAll("out"))
}
}

Expand All @@ -33,7 +34,7 @@ func TestEncryptCmd_NoInput(t *testing.T) {
assert.NoDirExists(t, "out")

err := rootCmd.Execute()
assert.NotNil(t, err)
require.Error(t, err)
assert.NoDirExists(t, "out")
}

Expand All @@ -54,7 +55,7 @@ func TestEncryptCmd_Stdin(t *testing.T) {
assert.NoDirExists(t, "out")

err := rootCmd.Execute()
assert.Nil(t, err, outAndErr)
require.NoError(t, err, outAndErr)

assert.DirExists(t, "out")
assert.FileExists(t, "out/secret.enc")
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
errInvalidNumOfImages = errors.New("number of images must be at least 1")
)

func runImagesCmd(cmd *cobra.Command, args []string) error {
func runImagesCmd(_ *cobra.Command, _ []string) error {
if imagesNum == 0 {
return errInvalidNumOfImages
}
Expand Down
1 change: 1 addition & 0 deletions internal/decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (d *Decrypter) Decrypt(filename string) error {
key = d.MasterKey
} else {
d.Logger.Print("Decrypting with partial keys")

key, err = sss.Combine(d.Parts)
if err != nil {
return errors.Wrap(err, "failed combining parts")
Expand Down
4 changes: 2 additions & 2 deletions internal/encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (e *Encrypter) generateAndSaveMasterKey(filename string) ([]byte, error) {
return nil, errors.Wrap(err, "failed generating master key")
}

encFilename := filepath.Join(e.OutputDir, fmt.Sprintf("%s.enc", filename))
encFilename := filepath.Join(e.OutputDir, filename+".enc")

err = file.WriteKey(e.Logger, masterKey, encFilename)
if err != nil {
Expand All @@ -163,7 +163,7 @@ func (e *Encrypter) encryptAndSaveMessage(masterKey []byte, reader io.Reader, fi
return errors.Wrap(err, "failed encrypting message")
}

encryptedFilename := filepath.Join(e.OutputDir, fmt.Sprintf("%s.enc", filename))
encryptedFilename := filepath.Join(e.OutputDir, filename+".enc")

err = file.WriteFile(e.Logger, encryptedMessage, encryptedFilename)
if err != nil {
Expand Down
25 changes: 13 additions & 12 deletions internal/encrypt/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/enrichman/stegosecrets/internal/encrypt"
"github.com/enrichman/stegosecrets/internal/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewEncrypter_WithPartsThreshold(t *testing.T) {
Expand Down Expand Up @@ -59,10 +60,10 @@ func TestNewEncrypter_WithPartsThreshold(t *testing.T) {
)

if tc.wantErr {
assert.NotNil(t, err)
require.Error(t, err)
assert.Nil(t, encrypter)
} else {
assert.Nil(t, err)
require.NoError(t, err)
assert.NotNil(t, encrypter)
}
})
Expand All @@ -73,20 +74,20 @@ func TestNewEncrypter_WithOutputDir(t *testing.T) {
// it should create the output dir
t.Run("non existing dir", func(t *testing.T) {
dir := filepath.Join(os.TempDir(), "non-existing-dir")
assert.Nil(t, os.RemoveAll(dir))
require.NoError(t, os.RemoveAll(dir))

_, err := os.Stat(dir)
assert.ErrorIs(t, err, fs.ErrNotExist)
require.ErrorIs(t, err, fs.ErrNotExist)

encrypter, err := encrypt.NewEncrypter(
encrypt.WithOutputDir(dir),
)

assert.Nil(t, err)
require.NoError(t, err)
assert.NotNil(t, encrypter)

_, err = os.Stat(dir)
assert.Nil(t, err)
assert.NoError(t, err)
})
}

Expand All @@ -98,18 +99,18 @@ func TestNewEncrypter_WithImagesDir(t *testing.T) {
encrypt.WithImagesDir(imagesDir),
)

assert.Nil(t, err)
require.NoError(t, err)
assert.NotNil(t, encrypter)

absoluteImagesDir, err := filepath.Abs(imagesDir)
assert.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, absoluteImagesDir, encrypter.ImagesDir)
})
}

func TestEncrypt(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "out-*")
assert.Nil(t, err)
require.NoError(t, err)
assert.DirExists(t, tmpDir)

encrypter, err := encrypt.NewEncrypter(
Expand All @@ -118,10 +119,10 @@ func TestEncrypt(t *testing.T) {
encrypt.WithImagesDir("../../test/assets/p5t3"),
encrypt.WithLogger(log.NewSimpleLogger(io.Discard, log.None)),
)
assert.Nil(t, err)
require.NoError(t, err)

err = encrypter.Encrypt(strings.NewReader("hello world!"), "secret")
assert.Nil(t, err)
require.NoError(t, err)

assert.DirExists(t, tmpDir)
assert.FileExists(t, tmpDir+"/secret.enc")
Expand All @@ -136,5 +137,5 @@ func TestEncrypt(t *testing.T) {
}

err = os.RemoveAll(tmpDir)
assert.Nil(t, err)
require.NoError(t, err)
}
7 changes: 4 additions & 3 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -38,13 +39,13 @@ func WriteChecksum(logger log.Logger, content []byte, filename string) error {

checksum := fmt.Sprintf("%x\t%s", h.Sum(nil), filepath.Base(filename))

return WriteFile(logger, []byte(checksum), fmt.Sprintf("%s.checksum", filename))
return WriteFile(logger, []byte(checksum), filename+".checksum")
}

func WriteKey(logger log.Logger, key []byte, filename string) error {
base64EncodedKey := base64.StdEncoding.EncodeToString(key)

return WriteFile(logger, []byte(base64EncodedKey), fmt.Sprintf("%s.key", filename))
return WriteFile(logger, []byte(base64EncodedKey), filename+".key")
}

func WriteFile(logger log.Logger, content []byte, filename string) error {
Expand Down Expand Up @@ -118,7 +119,7 @@ func Check(filename, checksumFilename string) error {
}

checksumToVerify := strings.Split(string(checksumFileContent), "\t")[0]
checksumContent := fmt.Sprintf("%x", h.Sum(nil))
checksumContent := hex.EncodeToString(h.Sum(nil))

if checksumToVerify != checksumContent {
return errors.New("failed checksum verification")
Expand Down
3 changes: 1 addition & 2 deletions pkg/file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package file_test

import (
"fmt"
"os"
"path"
"testing"
Expand All @@ -20,7 +19,7 @@ func Test_WriteFileChecksum(t *testing.T) {
err = file.WriteFileChecksum(nil, original)
require.NoError(t, err)

checksum, err := os.ReadFile(fmt.Sprintf("%s.checksum", original))
checksum, err := os.ReadFile(original + ".checksum")
require.NoError(t, err)

expectedChecksum := []byte("ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73\tfile")
Expand Down
12 changes: 6 additions & 6 deletions pkg/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import (

func TestEncodeSecretFromFile(t *testing.T) {
inJpeg, err := os.CreateTemp(os.TempDir(), "in.jpg")
assert.Nil(t, err)
require.NoError(t, err)

defer os.Remove(filepath.Join(os.TempDir(), "in.jpg"))

testImage := image.NewRGBA(image.Rect(0, 0, 256, 256))

var imageBuff bytes.Buffer
err = jpeg.Encode(&imageBuff, testImage, nil)
assert.Nil(t, err)
require.NoError(t, err)

_, err = inJpeg.Write(imageBuff.Bytes())
assert.Nil(t, err)
require.NoError(t, err)

inJpeg.Close()
assert.FileExists(t, inJpeg.Name())
Expand All @@ -36,16 +36,16 @@ func TestEncodeSecretFromFile(t *testing.T) {

secret := []byte("test secret")
err = stegoimage.EncodeSecretFromFile(secret, inJpeg.Name(), outJpeg)
assert.Nil(t, err)
require.NoError(t, err)
assert.FileExists(t, outJpeg)

defer os.Remove(filepath.Join(os.TempDir(), "out.jpg"))

outJpegFile, err := os.Open(outJpeg)
assert.Nil(t, err)
require.NoError(t, err)

decodedSecret, err := stegoimage.DecodeSecret(outJpegFile)
assert.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, secret, decodedSecret)
}

Expand Down

0 comments on commit 4d6cb70

Please sign in to comment.