Path: blob/dev/pkg/templates/signer/handler_test.go
2070 views
package signer12import (3"bytes"4"os"5"os/exec"6"strings"7"testing"89"github.com/projectdiscovery/gologger"10"github.com/projectdiscovery/gologger/levels"11)1213// This Unit Test generates a new key pair and parses it14// to ensure that the key handler works as expected.15func TestKeyHandler(t *testing.T) {16if val := os.Getenv("KEY_HANDLER_CI"); val != "1" {17cmd := exec.Command(os.Args[0], "-test.run=^TestKeyHandler$", "-test.v")18cmd.Env = append(cmd.Env, "KEY_HANDLER_CI=1")19var buff bytes.Buffer20cmd.Stdin = &buff21buff.WriteString("CIUSER\n")22buff.WriteString("\n")23out, err := cmd.CombinedOutput()24if !strings.Contains(string(out), "PASS\n") || err != nil {25t.Fatalf("%s\n(exit status %v)", string(out), err)26}27return28}29gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)30h := &KeyHandler{}31noUserPassphrase = true32h.GenerateKeyPair()33if h.UserCert == nil {34t.Fatal("no user cert found")35}36if h.PrivateKey == nil {37t.Fatal("no private key found")38}3940// now parse the cert and private key41if err := h.ParseUserCert(); err != nil {42t.Fatal(err)43}44if err := h.ParsePrivateKey(); err != nil {45t.Fatal(err)46}47if h.ecdsaKey == nil {48t.Fatal("no ecdsa key found")49}50if h.ecdsaPubKey == nil {51t.Fatal("no ecdsa public key found")52}53if h.cert == nil {54t.Fatal("no certificate found")55}56if h.cert.Subject.CommonName != "CIUSER" {57t.Fatal("invalid user name found")58}59}606162