You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DateKeys/internal/testkit/fixture.go

103 lines
3.6 KiB

package testkit
import (
"encoding/json"
"os"
"path/filepath"
)
// FixtureStanza is the visible part of an age stanza in a fixture.
type FixtureStanza struct {
Type string `json:"type"`
Args []string `json:"args"`
}
// FixtureRelease is the release a fixture opens with.
type FixtureRelease struct {
Round uint64 `json:"round"`
Signature string `json:"signature"`
}
// FixtureStage is the expected result of one step of spec §63.
type FixtureStage struct {
Step int `json:"step"`
Name string `json:"name"`
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}
// DKCFixture holds the expected values of an official .dkc fixture (spec §67).
type DKCFixture struct {
Description string `json:"description"`
Spec string `json:"spec"`
File string `json:"file"`
SHA256 string `json:"sha256"`
Release FixtureRelease `json:"release"`
Prelude string `json:"prelude"`
PublicHeader string `json:"public_header"`
DateKey string `json:"datekey"`
CapsuleID string `json:"capsule_id"`
AccessPolicy string `json:"access_policy"`
Structure string `json:"structure"`
UnlockAt string `json:"unlock_at"`
HeaderBinding string `json:"header_binding"`
OuterStanzas []FixtureStanza `json:"outer_stanzas"`
PayloadStanzas []FixtureStanza `json:"payload_stanzas"`
InnerStanzas []FixtureStanza `json:"inner_stanzas,omitempty"`
AccessKeyFile string `json:"access_key_file,omitempty"`
Identities []string `json:"identities,omitempty"`
ControlCBOR string `json:"control_cbor"`
PayloadIdentity string `json:"payload_identity"`
PlaintextFile string `json:"plaintext_file"`
PlaintextSHA256 string `json:"plaintext_sha256"`
HeaderExtensions []FixtureExt `json:"header_extensions,omitempty"`
ControlExt []FixtureExt `json:"control_extensions,omitempty"`
Stages []FixtureStage `json:"stages"`
}
// FixtureExt is an extension in a fixture.
type FixtureExt struct {
Critical bool `json:"critical"`
ID string `json:"id"`
Version uint64 `json:"version"`
Data string `json:"data,omitempty"` // hex of the CBOR data item
}
// DKKFixture holds the expected values of an official .dkk fixture (spec §68).
type DKKFixture struct {
Description string `json:"description"`
Spec string `json:"spec"`
File string `json:"file"`
SHA256 string `json:"sha256"`
CredentialID string `json:"credential_id"`
CapsuleID string `json:"capsule_id"`
AccessType string `json:"access_type"`
Material string `json:"access_material"`
CapsuleDigest string `json:"capsule_digest,omitempty"`
Extensions []FixtureExt `json:"extensions,omitempty"`
Capsule string `json:"capsule"`
ExpectedResult string `json:"expected_result"`
Stages []FixtureStage `json:"stages,omitempty"`
}
// ReadJSON decodes a JSON file.
func ReadJSON(path string, v any) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
// WriteJSON writes v as indented JSON with a trailing newline.
func WriteJSON(path string, v any) error {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
return os.WriteFile(path, append(b, '\n'), 0o644)
}

Powered by TurnKey Linux.