From 240921f6be25134380f2ffebafbe99394518e605 Mon Sep 17 00:00:00 2001 From: Andrei Burdulescu Date: Tue, 12 Dec 2023 20:25:35 +0200 Subject: [PATCH] More tests for keygen --- internal/keygen/aes_test.go | 8 +++++ internal/keygen/ed25519.go | 2 +- internal/keygen/ed25519_test.go | 35 +++++++++++++++++++ internal/keygen/rsa_test.go | 62 +++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 internal/keygen/ed25519_test.go create mode 100644 internal/keygen/rsa_test.go diff --git a/internal/keygen/aes_test.go b/internal/keygen/aes_test.go index 467f1e0..d005bac 100644 --- a/internal/keygen/aes_test.go +++ b/internal/keygen/aes_test.go @@ -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") diff --git a/internal/keygen/ed25519.go b/internal/keygen/ed25519.go index a996260..63b78ba 100644 --- a/internal/keygen/ed25519.go +++ b/internal/keygen/ed25519.go @@ -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 } diff --git a/internal/keygen/ed25519_test.go b/internal/keygen/ed25519_test.go new file mode 100644 index 0000000..5db0dbe --- /dev/null +++ b/internal/keygen/ed25519_test.go @@ -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)) + } + }) + +} diff --git a/internal/keygen/rsa_test.go b/internal/keygen/rsa_test.go new file mode 100644 index 0000000..76501e4 --- /dev/null +++ b/internal/keygen/rsa_test.go @@ -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) + } + + }) + } +}