Skip to content

test: add unit tests for plugin payload attribute validation#581

Open
arpitjain099 wants to merge 1 commit into
notaryproject:mainfrom
arpitjain099:test/payload-descriptor-validation
Open

test: add unit tests for plugin payload attribute validation#581
arpitjain099 wants to merge 1 commit into
notaryproject:mainfrom
arpitjain099:test/payload-descriptor-validation

Conversation

@arpitjain099
Copy link
Copy Markdown

What this does

Adds unit tests for the plugin payload attribute validation that #178 introduced. As noted in #194, those checks shipped without unit coverage because they were hard to exercise through the full signing flow without refactoring business logic. This PR tests the two helper functions directly instead, so no production code changes are needed.

Closes #194

Tests added

In a new signer/payload_test.go:

TestIsPayloadDescriptorValid is table-driven and covers a plugin returning the same subject descriptor it was given (valid), appending an annotation (valid), and the tamper cases that the function exists to catch: a changed digest, a changed size, a changed mediaType, and an existing annotation being overridden. These map to the spec requirement that a plugin must not alter the payload subject during signing.

TestAreUnknownAttributesAdded covers a payload where every key is an allowed descriptor field (nothing reported), an unknown attribute injected inside the targetArtifact descriptor, an unknown attribute added at the payload top level, and both at once. This matches the rule from the plugin signing workflow that no extra top-level attributes may be added to the subject.

Verification

  • go test ./signer/... passes; statement coverage of isPayloadDescriptorValid and areUnknownAttributesAdded goes from 0 percent to full.
  • gofmt -l and go vet ./signer/... are clean.
  • I confirmed the tests are real by temporarily weakening each function (dropping the descriptor equality check, and returning no unknown attributes); the relevant subtests failed, then passed again once the code was restored. The diff in this PR touches only the new test file.

Adds table-driven coverage for the payload descriptor checks introduced
in notaryproject#178, which previously had no unit tests (tracked in notaryproject#194).

isPayloadDescriptorValid is exercised for a matching descriptor, an
appended annotation, and rejection on a tampered digest, size, or
mediaType, plus rejection when a plugin overrides an existing
annotation. areUnknownAttributesAdded is exercised for the all-known
case and for unknown attributes added inside the descriptor and at the
payload top level.

Coverage of both functions goes from 0 percent to full statement
coverage. No production code changes.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Copilot AI review requested due to automatic review settings June 3, 2026 00:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds unit tests for payload validation logic in the signer package to detect descriptor tampering and unexpected JSON fields during envelope generation.

Changes:

  • Introduces TestIsPayloadDescriptorValid covering allowed vs. rejected descriptor mutations.
  • Introduces TestAreUnknownAttributesAdded covering detection of unknown JSON attributes at multiple levels.
  • Adds a shared baseDescriptor() helper to reduce duplication across cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread signer/payload_test.go
Comment on lines +93 to +99
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
if got := isPayloadDescriptorValid(original, tc.newDesc); got != tc.want {
t.Fatalf("isPayloadDescriptorValid() = %v, want %v", got, tc.want)
}
})
}
Comment thread signer/payload_test.go
Comment on lines +129 to +139
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := areUnknownAttributesAdded([]byte(tc.content))
sort.Strings(got)
want := append([]string{}, tc.want...)
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Fatalf("areUnknownAttributesAdded() = %+q, want %+q", got, want)
}
})
}
Comment thread signer/payload_test.go
}
}

func TestAreUnknownAttributesAdded(t *testing.T) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit test payload attribute validation

2 participants