Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test #5

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions aes/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,11 @@ func TestUnsupportedMethod(t *testing.T) {
_, err = key.PublicKey()
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "PublicKey failed")

err = nil
_, err = key.Sign("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Sign failed")

err = nil
_, err = key.Verify("hello world", "signature")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Verify failed")

}
}

Expand Down
2 changes: 1 addition & 1 deletion argon2/argon2.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (k *KeyImpl[T]) Algorithm() types.Algorithm {
}

func (k *KeyImpl[T]) Export() (key T, err error) {
return T(""), nil
return T(""), ErrUnsupportedMethod
}

func (k *KeyImpl[T]) SKI() T {
Expand Down
68 changes: 68 additions & 0 deletions argon2/argon2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,74 @@ import (
"github.com/yakumioto/go-crypto-suite/types"
)

func TestAlgorithm(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.Argon2,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)

assert.Equal(t, tc.algorithm, key.Algorithm(), "Algorithm failed")
}
}

func TestSKI(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.Argon2,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)

assert.Equal(t, "", key.SKI(), "SKI failed")
}
}

func TestUnsupportedMethod(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.Argon2,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)

_, err = key.Export()
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Export failed")

_, err = key.PublicKey()
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "PublicKey failed")

_, err = key.Encrypt("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Sign failed")

_, err = key.Decrypt("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Verify failed")

}
}

func TestSignAndVerify(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
Expand Down
2 changes: 0 additions & 2 deletions chacha20/chacha20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ func TestUnsupportedMethod(t *testing.T) {
_, err = key.PublicKey()
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "PublicKey failed")

err = nil
_, err = key.Sign("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Sign failed")

err = nil
_, err = key.Verify("hello world", "signature")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Verify failed")

Expand Down
5 changes: 3 additions & 2 deletions ecdsa/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/subtle"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
Expand Down Expand Up @@ -110,11 +111,11 @@ func (e *PublicKey[T]) SKI() T {

hash := sha256.New()
hash.Write(raw)
return T(hash.Sum(nil))
return T(hex.EncodeToString(hash.Sum(nil)))
}

func (e *PublicKey[T]) PublicKey() (key.Key[T], error) {
return e, nil
return e, ErrUnsupportedMethod
}

func (e *PublicKey[T]) Sign(_ T) (T, error) {
Expand Down
164 changes: 164 additions & 0 deletions ecdsa/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,170 @@ import (
"github.com/yakumioto/go-crypto-suite/types"
)

func TestAlgorithm(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.EcdsaP256,
},
{
algorithm: types.EcdsaP384,
},
{
algorithm: types.EcdsaP521,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)
assert.Equal(t, tc.algorithm, key.Algorithm(), "Algorithm failed")

pk, err := key.PublicKey()
assert.NoErrorf(t, err, "PublicKey failed: %s", err)
assert.Equal(t, tc.algorithm, pk.Algorithm(), "Algorithm failed")
}
}

func TestExport(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.EcdsaP256,
},
{
algorithm: types.EcdsaP384,
},
{
algorithm: types.EcdsaP521,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)

password, err := key.Export()
assert.NoErrorf(t, err, "Export failed: %s", err)
assert.NotEmptyf(t, password, "Export failed")

pk, err := key.PublicKey()
assert.NoErrorf(t, err, "PublicKey failed: %s", err)

password, err = pk.Export()
assert.NoErrorf(t, err, "Export failed: %s", err)
assert.NotEmptyf(t, password, "Export failed")
}
}

func TestSKI(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.EcdsaP256,
},
{
algorithm: types.EcdsaP384,
},
{
algorithm: types.EcdsaP521,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)
assert.NotEmptyf(t, key.SKI(), "SKI failed")

pk, err := key.PublicKey()
assert.NoErrorf(t, err, "PublicKey failed: %s", err)
assert.NotEmptyf(t, pk.SKI(), "SKI failed")
}
}

func TestKeyPubicKey(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.EcdsaP256,
},
{
algorithm: types.EcdsaP384,
},
{
algorithm: types.EcdsaP521,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

privKey, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)

pubKey, err := privKey.PublicKey()
assert.NoErrorf(t, err, "PublicKey failed: %s", err)
assert.NotNil(t, pubKey, "PublicKey failed")

_, err = pubKey.PublicKey()
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "PublicKey failed")
}
}

func TestUnsupportedMethod(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
}{
{
algorithm: types.EcdsaP256,
},
{
algorithm: types.EcdsaP384,
},
{
algorithm: types.EcdsaP521,
},
}

for _, tc := range tcs {
ki := new(KeyGeneratorImpl[string])

key, err := ki.KeyGen(tc.algorithm)
assert.NoErrorf(t, err, "KeyGen failed: %s", err)

_, err = key.Encrypt("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Sign failed")

_, err = key.Decrypt("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Verify failed")

_, err = key.Verify("", "")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Verify failed")

pk, err := key.PublicKey()
assert.NoErrorf(t, err, "PublicKey failed: %s", err)

_, err = pk.Encrypt("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Encrypt failed")

_, err = pk.Decrypt("hello world")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Decrypt failed")

_, err = pk.Sign("")
assert.EqualError(t, err, ErrUnsupportedMethod.Error(), "Sign failed")
}
}

func TestSignAndVerify(t *testing.T) {
tcs := []struct {
algorithm types.Algorithm
Expand Down
Loading
Loading