Skip to content

Commit

Permalink
More tests for keygen
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Dec 12, 2023
1 parent e9179c9 commit 240921f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
8 changes: 8 additions & 0 deletions internal/keygen/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ import (
)

func TestAes(t *testing.T) {
t.Run("UnknownArg", func(t *testing.T) {
if err := testutil.RunCmd(AesCmd, "-xxx"); err == nil {
t.Fatal("expected error")
}
})

t.Run("NoArgs", func(t *testing.T) {
if err := testutil.RunCmd(AesCmd); err == nil {
t.Fatal("expected error")
}
})

t.Run("InvalidNumBits", func(t *testing.T) {
if err := testutil.RunCmd(AesCmd, "4223"); err == nil {
t.Fatal("expected error")
}
})

t.Run("NumBitsNotInt", func(t *testing.T) {
if err := testutil.RunCmd(AesCmd, "hello"); err == nil {
t.Fatal("expected error")
Expand Down
2 changes: 1 addition & 1 deletion internal/keygen/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func runEd25519(cmd *cmd.Command) error {
}
defer sf.Close()

key, _, err := ed25519.GenerateKey(nil)
_, key, err := ed25519.GenerateKey(nil)
if err != nil {
return err
}
Expand Down
35 changes: 35 additions & 0 deletions internal/keygen/ed25519_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package keygen

import (
"crypto/ed25519"
"os"
"path/filepath"
"testing"

"bandr.me/p/pocryp/internal/testutil"
)

func TestEd25519(t *testing.T) {
t.Run("UnknownArg", func(t *testing.T) {
if err := testutil.RunCmd(Ed25519Cmd, "-xxx"); err == nil {
t.Fatal("expected error")
}
})

tmp := t.TempDir()

t.Run("Ok", func(t *testing.T) {
outPath := filepath.Join(tmp, "out")
if err := testutil.RunCmd(Ed25519Cmd, "-bin", "-out", outPath); err != nil {
t.Fatal(err)
}
result, err := os.ReadFile(outPath)
if err != nil {
t.Fatal(err)
}
if len(result) != ed25519.PrivateKeySize {
t.Fatalf("len: want %d, have %d", ed25519.PrivateKeySize, len(result))
}
})

}
62 changes: 62 additions & 0 deletions internal/keygen/rsa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package keygen

import (
"os"
"path/filepath"
"testing"

"bandr.me/p/pocryp/internal/testutil"

rsautil "bandr.me/p/pocryp/internal/encoding/rsa/util"
)

func TestRsa(t *testing.T) {
t.Run("UnknownArg", func(t *testing.T) {
if err := testutil.RunCmd(RsaCmd, "-xxx"); err == nil {
t.Fatal("expected error")
}
})

t.Run("NoArgs", func(t *testing.T) {
if err := testutil.RunCmd(RsaCmd); err == nil {
t.Fatal("expected error")
}
})

t.Run("InvalidNumBits", func(t *testing.T) {
if err := testutil.RunCmd(RsaCmd, "23"); err == nil {
t.Fatal("expected error")
}
})

t.Run("NumBitsNotInt", func(t *testing.T) {
if err := testutil.RunCmd(RsaCmd, "hello"); err == nil {
t.Fatal("expected error")
}
})

tmp := t.TempDir()

tests := []string{"2048", "3072", "4096"}

for _, numBits := range tests {
t.Run(numBits, func(t *testing.T) {
outPath := filepath.Join(tmp, "out"+numBits)
if err := testutil.RunCmd(RsaCmd, "-out", outPath, numBits); err != nil {
t.Fatalf("%s: %v", numBits, err)
}
result, err := os.ReadFile(outPath)
if err != nil {
t.Fatal(err)
}
key, err := rsautil.PrivateKeyFromPem(result)
if err != nil {
t.Fatal(err)
}
if err := key.Validate(); err != nil {
t.Fatal(err)
}

})
}
}

0 comments on commit 240921f

Please sign in to comment.