Skip to content

Commit

Permalink
test.ReadTestCert breaks for downstream consumers dependent on the pr…
Browse files Browse the repository at this point in the history
…evious relative certificate path building behavior (#695)

* util: gtld_map autopull updates for 2022-10-06T19:22:06 UTC

* Trigger GHA

* revert change

* fixing our own tests

Co-authored-by: GitHub <noreply@github.com>
  • Loading branch information
christopher-henderson and web-flow committed Nov 3, 2022
1 parent 6292ca4 commit 8a9f61e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
17 changes: 17 additions & 0 deletions v3/test/configuration_test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package test
import (
"fmt"
"math/rand"
"os"
"strconv"
"sync"
"testing"
Expand All @@ -27,6 +28,22 @@ import (
"github.com/zmap/zlint/v3/lint"
)

func init() {
// This is a complication caused https://github.com/zmap/zlint/issues/696
//
// This test package required access to the test certificate directory, however
// the ReadTestCert testing helper function assumes that your PWD is one of the
// lint genre directories.
//
// ReadTestCert was changed to operate from the root of the repo to accommodate this
// test package, however that broke downstream consumers who were dependent on the
// relative path building behavior.
err := os.Chdir("../lints/rfc")
if err != nil {
panic(err)
}
}

type caCommonNameMissing struct {
BeerHall string
Working *lint.CABFBaselineRequirementsConfig
Expand Down
18 changes: 3 additions & 15 deletions v3/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ package test
// Contains resources necessary to the Unit Test Cases

import (
"bytes"
"encoding/pem"
"fmt"
"os"

"os/exec"
"path"
"strings"

"github.com/zmap/zcrypto/x509"
Expand All @@ -37,6 +34,7 @@ import (
// Important: TestLint is only appropriate for unit tests. It will panic if the
// lintName is not known or if the testCertFilename can not be loaded, or if the
// lint result is nil.
//
//nolint:revive
func TestLint(lintName string, testCertFilename string) *lint.LintResult {
return TestLintWithConfig(lintName, testCertFilename, "")
Expand All @@ -56,6 +54,7 @@ func TestLintWithConfig(lintName string, testCertFilename string, configuration
//
// Important: TestLintCert is only appropriate for unit tests. It will panic if
// the lintName is not known or if the lint result is nil.
//
//nolint:revive
func TestLintCert(lintName string, cert *x509.Certificate, ctx lint.Configuration) *lint.LintResult {
l := lint.GlobalRegistry().ByName(lintName)
Expand All @@ -75,24 +74,13 @@ func TestLintCert(lintName string, cert *x509.Certificate, ctx lint.Configuratio
return res
}

var testDir = ""

// ReadTestCert loads a x509.Certificate from the given inPath which is assumed
// to be relative to `testdata/`.
//
// Important: ReadTestCert is only appropriate for unit tests. It will panic if
// the inPath file can not be loaded.
func ReadTestCert(inPath string) *x509.Certificate {
if testDir == "" {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
out, err := cmd.CombinedOutput()
if err != nil {
panic(fmt.Sprintf("error when attempting to find the root directory of the repository: %v, output: '%s'", err, out))
}
testDir = path.Join(string(bytes.TrimSpace(out)), "v3", "testdata")
}
fullPath := path.Join(testDir, inPath)

fullPath := fmt.Sprintf("../../testdata/%s", inPath)
data, err := os.ReadFile(fullPath)
if err != nil {
panic(fmt.Sprintf(
Expand Down

0 comments on commit 8a9f61e

Please sign in to comment.