-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for absolute path and added images dir flag (#41)
* fix for absolute path, added images dir flag Signed-off-by: Enrico Candino <enrico.candino@gmail.com> * added test Signed-off-by: Enrico Candino <enrico.candino@gmail.com> Signed-off-by: Enrico Candino <enrico.candino@gmail.com>
- Loading branch information
Showing
7 changed files
with
163 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.DS_Store | ||
images/* | ||
out/ | ||
out/* | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
run: | ||
go: "1.19" | ||
|
||
issues: | ||
exclude-rules: | ||
- path: _test.go | ||
linters: | ||
- funlen | ||
|
||
linters: | ||
enable-all: true | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/enrichman/stegosecrets | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/auyer/steganography v1.0.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,119 @@ | ||
package encrypt_test | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/enrichman/stegosecrets/internal/encrypt" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewEncrypter(t *testing.T) { | ||
t.Run("new encrypter with valid options", func(t *testing.T) { | ||
threshold := 3 | ||
parts := 5 | ||
func TestNewEncrypter_WithPartsThreshold(t *testing.T) { | ||
type args struct { | ||
parts int | ||
threshold int | ||
} | ||
|
||
e, err := encrypt.NewEncrypter(encrypt.WithThreshold(threshold), encrypt.WithParts(parts)) | ||
tt := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid options", | ||
args: args{ | ||
parts: 5, | ||
threshold: 3, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid threshold", | ||
args: args{ | ||
threshold: -1, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid parts", | ||
args: args{ | ||
parts: 300, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid threshold and parts", | ||
args: args{ | ||
parts: 3, | ||
threshold: 5, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid output", | ||
args: args{ | ||
parts: 3, | ||
threshold: 5, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
assert.NotNil(t, e) | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("new encrypter with invalid threshold", func(t *testing.T) { | ||
threshold := -1 | ||
for _, tc := range tt { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
encrypter, err := encrypt.NewEncrypter( | ||
encrypt.WithParts(tc.args.parts), | ||
encrypt.WithThreshold(tc.args.threshold), | ||
) | ||
|
||
e, err := encrypt.NewEncrypter(encrypt.WithThreshold(threshold)) | ||
if tc.wantErr { | ||
assert.NotNil(t, err) | ||
assert.Nil(t, encrypter) | ||
} else { | ||
assert.Nil(t, err) | ||
assert.NotNil(t, encrypter) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
assert.Nil(t, e) | ||
assert.EqualError(t, err, "invalid threshold") | ||
}) | ||
t.Run("new encrypter with invalid parts", func(t *testing.T) { | ||
parts := 300 | ||
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)) | ||
|
||
e, err := encrypt.NewEncrypter(encrypt.WithParts(parts)) | ||
_, err := os.Stat(dir) | ||
assert.ErrorIs(t, err, fs.ErrNotExist) | ||
|
||
encrypter, err := encrypt.NewEncrypter( | ||
encrypt.WithOutputDir(dir), | ||
) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, encrypter) | ||
|
||
assert.Nil(t, e) | ||
assert.EqualError(t, err, "invalid parts") | ||
_, err = os.Stat(dir) | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("new encrypter with invalid threshold and parts pair", func(t *testing.T) { | ||
threshold := 5 | ||
parts := 3 | ||
} | ||
|
||
func TestNewEncrypter_WithImagesDir(t *testing.T) { | ||
t.Run("set images dir", func(t *testing.T) { | ||
imagesDir := "random" | ||
|
||
e, err := encrypt.NewEncrypter(encrypt.WithParts(parts), encrypt.WithThreshold(threshold)) | ||
encrypter, err := encrypt.NewEncrypter( | ||
encrypt.WithImagesDir(imagesDir), | ||
) | ||
|
||
assert.Nil(t, e) | ||
assert.EqualError(t, err, fmt.Sprintf("threshold %d cannot exceed the parts %d", threshold, parts)) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, encrypter) | ||
|
||
absoluteImagesDir, err := filepath.Abs(imagesDir) | ||
assert.Nil(t, err) | ||
assert.Equal(t, absoluteImagesDir, encrypter.ImagesDir) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |