Files
shared/backend/coreconfig/contract_test.go
T

53 lines
1.6 KiB
Go

package coreconfig
import (
"encoding/json"
"os"
"testing"
)
// contractFile holds the fixtures the Go, Node and Python internal-secret checks must agree on. The
// Node and Python suites load the same file; see the "description" field in it.
const contractFile = "../../contract/auth-contract.json"
type internalSecretContract struct {
InternalSecret struct {
MinLength int `json:"minLength"`
Placeholders []string `json:"placeholders"`
Cases []struct {
Name string `json:"name"`
Secret string `json:"secret"`
Provided string `json:"provided"`
Matches bool `json:"matches"`
} `json:"cases"`
} `json:"internalSecret"`
}
func TestContractInternalSecret(t *testing.T) {
data, err := os.ReadFile(contractFile)
if os.IsNotExist(err) {
t.Skipf("%s is not part of this checkout", contractFile)
}
if err != nil {
t.Fatal(err)
}
var contract internalSecretContract
if err := json.Unmarshal(data, &contract); err != nil {
t.Fatalf("decode %s: %v", contractFile, err)
}
rule := contract.InternalSecret
if MinInternalSecretLength != rule.MinLength {
t.Fatalf("MinInternalSecretLength = %d, contract says %d", MinInternalSecretLength, rule.MinLength)
}
for _, placeholder := range rule.Placeholders {
if InternalSecretUsable(placeholder) {
t.Errorf("placeholder %q is usable, contract says it never gates a route", placeholder)
}
}
for _, entry := range rule.Cases {
if got := InternalSecretMatches(entry.Provided, entry.Secret); got != entry.Matches {
t.Errorf("%s: InternalSecretMatches = %v, contract says %v", entry.Name, got, entry.Matches)
}
}
}