From cf94f0a59f4f37347329de31574f42e9ade28fe6 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 1 Oct 2017 14:40:48 -0700 Subject: [PATCH] internal: add testing for a few libraries --- internal/elfs/elfs_test.go | 10 +++++++ internal/middleware/trace_test.go | 33 +++++++++++++++++++++ internal/routecrypto/rsa_test.go | 41 ++++++++++++++++++++++++++ internal/routecrypto/secretbox_test.go | 40 +++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 internal/elfs/elfs_test.go create mode 100644 internal/middleware/trace_test.go create mode 100644 internal/routecrypto/rsa_test.go create mode 100644 internal/routecrypto/secretbox_test.go diff --git a/internal/elfs/elfs_test.go b/internal/elfs/elfs_test.go new file mode 100644 index 0000000..c433477 --- /dev/null +++ b/internal/elfs/elfs_test.go @@ -0,0 +1,10 @@ +package elfs + +import "testing" + +func TestMakeName(t *testing.T) { + n := MakeName() + if len(n) == 0 { + t.Fatalf("MakeName had a zero output") + } +} diff --git a/internal/middleware/trace_test.go b/internal/middleware/trace_test.go new file mode 100644 index 0000000..cc4461a --- /dev/null +++ b/internal/middleware/trace_test.go @@ -0,0 +1,33 @@ +package middleware + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +func TestTrace(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + var executed bool + var handler http.Handler = Trace(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + executed = true + w.WriteHeader(http.StatusOK) + })) + + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatalf("error when creating request: %v", err) + } + req = req.WithContext(ctx) + + rw := httptest.NewRecorder() + + handler.ServeHTTP(rw, req) + + if !executed { + t.Fatal("middleware Trace doesn't pass through to underlying handler") + } +} diff --git a/internal/routecrypto/rsa_test.go b/internal/routecrypto/rsa_test.go new file mode 100644 index 0000000..08e742e --- /dev/null +++ b/internal/routecrypto/rsa_test.go @@ -0,0 +1,41 @@ +package routecrypto + +import "testing" + +var ( + rsaPrivKey = []byte(`-----BEGIN RSA PRIVATE KEY----- +MIICXAIBAAKBgQC6C94euSI3GAbszcTVvuBI4ejM/fugqe/uUyXz2bUIGemkADBh +OOkNWXFi/gnYylHRrFKOH06wxhzZWpsBMacmwx6tD7a7nKktcw7HsVFL8is0PPnp +syhWfW+DF6vMDZxkgI3iKrr9/WY/3/qUg7ga17s1JXb3SmQ2sMDTh5I6DQIET4Bo +LwKBgCBG2EmsLiVPCXwN+Mk8IGck7BHKhVpcm955VDDiuKNMuFK4F9ak3tbsKOza +UDC+JhqhB1U7/J8zABM+qVqHBwse1sJMZUEXPuGbIuw4vmEHFA+scAuwkpmRx4gA +/Ghi9eWr1rDlrRFMEF5vs18GObY7Z07GxTx/nZPx7FZ+6FqZAkEA24zob4NMKGUj +efHggZ4DFiIGDEbfbRS6a/w7VicJwI41pwhbGj7KCPZEwXYhnXR3H9UXSrowsm14 +D0Wbsw4gRwJBANjvAbFVBAW8TWxLCgKx7uyHehygEBl5NY2in/8QHMjJpE7fQX5U +qutOL68A6+8P0lrtoz4VJZSnAxwkaifM8QsCQA37iRRm+Qd64OetQrHj+FhiZlrJ +LAT0CUWmADJ5KYX49B2lfNXDrXOsUG9sZ4tHKRGDt51KC/0KjMgq9BGx41MCQF0y +FxOL0s2EtXz/33V4QA9twe9xUBDY4CMts4Eyq3xlscbBBe4IjwrcKuntJ3POkGPS +Xotb9TDONmrANIqlmbECQCD8Uo0bgt8kR5bShqkbW1e5qVNz5w4+tM7Uh+oQMIGB +bC3xLJD4u2NPTwTdqKxxkeicFMKpuiGvX200M/CcoVc= +-----END RSA PRIVATE KEY-----`) +) + +func TestRSA(t *testing.T) { + pk, err := PemToRSAPrivateKey(rsaPrivKey) + if err != nil { + t.Fatalf("can't parse key: %v", err) + } + + pkd := RSAPrivateKeyToPem(pk) + + pk2, err := PemToRSAPrivateKey(pkd) + if err != nil { + t.Fatalf("can't parse key: %v", err) + } + + pkd2 := RSAPrivateKeyToPem(pk2) + + if string(pkd) != string(pkd2) { + t.Fatalf("functions are not 1:1") + } +} diff --git a/internal/routecrypto/secretbox_test.go b/internal/routecrypto/secretbox_test.go new file mode 100644 index 0000000..05e3d52 --- /dev/null +++ b/internal/routecrypto/secretbox_test.go @@ -0,0 +1,40 @@ +package routecrypto + +import "testing" + +func TestSecretBox(t *testing.T) { + var ( + key *[32]byte + sk string + ) + + t.Run("generate key", func(t *testing.T) { + var err error + key, err = GenerateKey() + if err != nil { + t.Fatalf("can't generate key: %v", err) + } + }) + + if key == nil { + t.Fatal("can't continue") + } + + t.Run("show key", func(t *testing.T) { + sk = ShowKey(key) + if len(sk) == 0 { + t.Fatal("expected output to be a nonzero length string") + } + }) + + t.Run("read key", func(t *testing.T) { + readKey, err := ParseKey(sk) + if err != nil { + t.Fatal(err) + } + + if *key != *readKey { + t.Fatal("key did not parse out correctly") + } + }) +}