internal: add testing for a few libraries

This commit is contained in:
Cadey Ratio 2017-10-01 14:40:48 -07:00
parent 5b4885cf6c
commit cf94f0a59f
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
4 changed files with 124 additions and 0 deletions

View File

@ -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")
}
}

View File

@ -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")
}
}

View File

@ -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")
}
}

View File

@ -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")
}
})
}