76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
package connectors
|
|
|
|
import (
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"testing"
|
|
)
|
|
|
|
// TestDigestIgnoresHowJSONIsSpelled guards the rule that makes an approval
|
|
// mean one action: an input that has been through a step record or a decision
|
|
// body comes back re-encoded, and the digest over it must match the digest of
|
|
// what the agent wrote. A digest that changed would refuse a judged action as
|
|
// altered, so an approved action would never run.
|
|
func TestDigestIgnoresHowJSONIsSpelled(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
a jsontext.Value
|
|
b jsontext.Value
|
|
same bool
|
|
}{
|
|
{
|
|
name: "spacing doesn't change the action",
|
|
a: jsontext.Value(`{"clean_dir": "/tmp", "host": "web-1"}`),
|
|
b: jsontext.Value(`{"clean_dir":"/tmp","host":"web-1"}`),
|
|
same: true,
|
|
},
|
|
{
|
|
name: "key order doesn't change the action",
|
|
a: jsontext.Value(`{"host":"web-1","clean_dir":"/tmp"}`),
|
|
b: jsontext.Value(`{"clean_dir":"/tmp","host":"web-1"}`),
|
|
same: true,
|
|
},
|
|
{
|
|
name: "two large ids are two actions",
|
|
a: jsontext.Value(`{"id":9007199254740993}`),
|
|
b: jsontext.Value(`{"id":9007199254740992}`),
|
|
same: false,
|
|
},
|
|
{
|
|
name: "a different value is a different action",
|
|
a: jsontext.Value(`{"host":"web-1","clean_dir":"/tmp"}`),
|
|
b: jsontext.Value(`{"host":"web-1","clean_dir":"/var/tmp"}`),
|
|
same: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := Digest("clean", tt.a) == Digest("clean", tt.b)
|
|
if got != tt.same {
|
|
t.Fatalf("same digest = %v, want %v", got, tt.same)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestDigestSurvivesAStepRecord is the same rule through the encoding a run
|
|
// actually puts an input through: a value marshalled into a step and read back.
|
|
func TestDigestSurvivesAStepRecord(t *testing.T) {
|
|
judged := jsontext.Value(`{"host": "web-1", "older_than_hours": "24"}`)
|
|
recorded, err := json.Marshal(struct {
|
|
Input jsontext.Value `json:"input"`
|
|
}{Input: judged})
|
|
if err != nil {
|
|
t.Fatalf("encode: %v", err)
|
|
}
|
|
var back struct {
|
|
Input jsontext.Value `json:"input"`
|
|
}
|
|
if err := json.Unmarshal(recorded, &back); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if Digest("clean", back.Input) != Digest("clean", judged) {
|
|
t.Fatalf("digest changed through a step record: %s vs %s", back.Input, judged)
|
|
}
|
|
}
|