Skip to content

Commit

Permalink
added unit tests for pkg packages (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaruGaru authored Oct 22, 2022
1 parent b3f28ab commit 3265b62
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ go 1.18
require github.com/corvus-ch/shamir v1.0.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/auyer/steganography v1.0.1
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.0
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ github.com/auyer/steganography v1.0.1/go.mod h1:Q2qN+f1ixaXnKTCT4xkSDCZ/5NiOpUeT
github.com/corvus-ch/shamir v1.0.1 h1:NaynWw+QQBOYmd/dWmc9xGrUr4cgALhWYJS0252SSnE=
github.com/corvus-ch/shamir v1.0.1/go.mod h1:1v3RBwJf+boj6ol/2QvtT1F1w5MZRZPbh5uys9ZoMnY=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI=
github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
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.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=
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/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=
39 changes: 39 additions & 0 deletions pkg/file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package file

import (
"fmt"
"github.com/stretchr/testify/require"
"os"
"path"
"testing"
)

func Test_WriteFileChecksum(t *testing.T) {
tmpDir := t.TempDir()
original := path.Join(tmpDir, "file")

err := os.WriteFile(original, []byte("content"), 0600)
require.NoError(t, err)

err = WriteFileChecksum(original)
require.NoError(t, err)

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

expectedChecksum := []byte("ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73\tfile")
require.Equal(t, expectedChecksum, checksum)
}

func Test_WriteKey(t *testing.T) {
tmpDir := t.TempDir()
keyFile := path.Join(tmpDir, "file")

expectedKey := []byte("test")
err := WriteKey(expectedKey, keyFile)
require.NoError(t, err)

key, err := ReadKey(keyFile + ".key")
require.NoError(t, err)
require.Equal(t, expectedKey, key)
}
27 changes: 27 additions & 0 deletions pkg/image/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package image

import (
"bytes"
"github.com/stretchr/testify/require"
"image"
"image/jpeg"
"testing"
)

func Test_EncodeDecodeSecret(t *testing.T) {
secret := []byte("test secret")

testImage := image.NewRGBA(image.Rect(0, 0, 256, 256))
var imageBuff bytes.Buffer
err := jpeg.Encode(&imageBuff, testImage, nil)
require.NoError(t, err)

var imageOut bytes.Buffer
err = EncodeSecret(secret, &imageBuff, &imageOut)
require.NoError(t, err)

out, err := DecodeSecret(&imageOut)
require.NoError(t, err)

require.Equal(t, secret, out)
}
65 changes: 65 additions & 0 deletions pkg/stego/aes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package stego

import (
"github.com/stretchr/testify/require"
"testing"
)

func Test_EncryptDecrypt(t *testing.T) {
key, err := GenerateMasterKey()
require.NoError(t, err)

message := []byte("test message")
encrypted, err := Encrypt(key, message)
require.NoError(t, err)

decrypted, err := Decrypt(key, encrypted)
require.NoError(t, err)
require.Equal(t, message, decrypted)
}

func Test_EncryptDecryptEmptyMessage(t *testing.T) {
key, err := GenerateMasterKey()
require.NoError(t, err)

message := []byte{}
encrypted, err := Encrypt(key, message)
require.NoError(t, err)

decrypted, err := Decrypt(key, encrypted)
require.NoError(t, err)
require.Equal(t, message, decrypted)
}

func Benchmark_Encrypt(b *testing.B) {
key, err := GenerateMasterKey()
if err != nil {
b.Fatal(err)
}
message := []byte("test message")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := Encrypt(key, message)
if err != nil {
b.Fatal(err)
}
}
}

func Benchmark_Decrypt(b *testing.B) {
key, err := GenerateMasterKey()
if err != nil {
b.Fatal(err)
}
encr, err := Encrypt(key, []byte("test message"))
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := Decrypt(key, encr)
if err != nil {
b.Fatal(err)
}
}
}

0 comments on commit 3265b62

Please sign in to comment.