0
0
mirror of https://github.com/slackhq/nebula.git synced 2024-12-22 12:18:30 +00:00
slackhq_nebula/cmd/nebula-cert/main_test.go
Jack Doan 35603d1c39
add PKCS11 support (#1153)
* add PKCS11 support

* add pkcs11 build option to the makefile, add a stub pkclient to avoid forcing CGO onto people

* don't print the pkcs11 option on nebula-cert keygen if not compiled in

* remove linux-arm64-pkcs11 from the all target to fix CI

* correctly serialize ec keys

* nebula-cert: support PKCS#11 for sign and ca

* fix gofmt lint

* clean up some logic with regard to closing sessions

* pkclient: handle empty correctly for TPM2

* Update Makefile and Actions

---------

Co-authored-by: Morgan Jones <me@numin.it>
Co-authored-by: John Maguire <contact@johnmaguire.me>
2024-09-09 17:51:58 -04:00

94 lines
1.9 KiB
Go

package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
//TODO: all flag parsing continueOnError will print to stderr on its own currently
func Test_help(t *testing.T) {
expected := "Usage of " + os.Args[0] + " <global flags> <mode>:\n" +
" Global flags:\n" +
" -version: Prints the version\n" +
" -h, -help: Prints this help message\n\n" +
" Modes:\n" +
" " + caSummary() + "\n" +
" " + keygenSummary() + "\n" +
" " + signSummary() + "\n" +
" " + printSummary() + "\n" +
" " + verifySummary() + "\n" +
"\n" +
" To see usage for a given mode, use " + os.Args[0] + " <mode> -h\n"
ob := &bytes.Buffer{}
// No error test
help("", ob)
assert.Equal(
t,
expected,
ob.String(),
)
// Error test
ob.Reset()
help("test error", ob)
assert.Equal(
t,
"Error: test error\n\n"+expected,
ob.String(),
)
}
func Test_handleError(t *testing.T) {
ob := &bytes.Buffer{}
// normal error
handleError("", errors.New("test error"), ob)
assert.Equal(t, "Error: test error\n", ob.String())
// unknown mode help error
ob.Reset()
handleError("", newHelpErrorf("test %s", "error"), ob)
assert.Equal(t, "Error: test error\n", ob.String())
// test all modes with help error
modes := map[string]func(io.Writer){"ca": caHelp, "print": printHelp, "sign": signHelp, "verify": verifyHelp}
eb := &bytes.Buffer{}
for mode, fn := range modes {
ob.Reset()
eb.Reset()
fn(eb)
handleError(mode, newHelpErrorf("test %s", "error"), ob)
assert.Equal(t, "Error: test error\n"+eb.String(), ob.String())
}
}
func assertHelpError(t *testing.T, err error, msg string) {
switch err.(type) {
case *helpError:
// good
default:
t.Fatal(fmt.Sprintf("err was not a helpError: %q, expected %q", err, msg))
}
assert.EqualError(t, err, msg)
}
func optionalPkcs11String(msg string) string {
if p11Supported() {
return msg
} else {
return ""
}
}