[test] Add tests for config.NormalizeGuardPolicy and guard policy helpers#1728
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test] Add tests for config.NormalizeGuardPolicy and guard policy helpers#1728github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
Test coverage improvements for NormalizeGuardPolicy and helpers:
- TestNormalizeGuardPolicy: nil policy, nil AllowOnly, all string types
(all/public/invalid), []interface{}, []string, duplicates, invalid
min-integrity, case-insensitive normalization, whitespace trimming
- TestIsValidRepoScope: all branch paths including exact match, wildcards,
invalid patterns (uppercase, dots, spaces, multiple wildcards, etc.)
- TestIsValidRepoOwner: boundary values (empty, 1 char, 39/40 char limits),
valid/invalid characters
- TestIsValidRepoName: boundary values (empty, 1 char, 100/101 char limits),
valid/invalid characters
- TestNormalizeAndValidateScopeArray: empty array, non-string elements,
empty/whitespace strings, invalid patterns, duplicates, sorted output
- TestGuardPolicyUnmarshalJSON: invalid JSON, unsupported fields, missing
allow-only, canonical and legacy key forms
- TestAllowOnlyPolicyUnmarshalJSON: invalid JSON, unsupported fields,
missing repos/min-integrity, legacy 'integrity' key, whitespace-only
- TestGuardPolicyMarshalJSON: round-trip serialization with canonical keys
- TestAllowOnlyPolicyMarshalJSON: verifies min-integrity key is used
- TestParseGuardPolicyJSONComprehensive: invalid JSON, invalid policy
structures, valid policies with all integrity levels
- TestValidateGuardPolicy: nil, valid, invalid policy delegation
- TestIsScopeTokenChar: all valid and invalid character classes
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Coverage Improvement:
guard_policy.gofunctionsFunction Analyzed
internal/configNormalizeGuardPolicyisValidRepoScope,isValidRepoOwner,isValidRepoName,normalizeAndValidateScopeArray,isScopeTokenChar,ParseGuardPolicyJSON,ValidateGuardPolicy,GuardPolicy.UnmarshalJSON,AllowOnlyPolicy.UnmarshalJSONNormalizeGuardPolicy, 10+ branches inisValidRepoScope, type-switch logic, boundary validationWhy This Function?
guard_policy.gocontains the core security policy validation logic for DIFC guards. Whileconfig_difc_test.goandconfig_guardpolicies_test.goexercise these functions indirectly viaLoadFromStdin()/LoadFromFile(), they do not directly unit-test the low-level helper functions. Many branches and edge cases were completely untested:isScopeTokenChar)normalizeAndValidateScopeArray"integrity"vs canonical"min-integrity"key inAllowOnlyPolicy.UnmarshalJSONNormalizeGuardPolicy[]stringtype path inNormalizeGuardPolicy(separate branch from[]interface{})Tests Added
New file:
internal/config/guard_policy_test.go— 674 lines, 13 test functionsTestNormalizeGuardPolicy— 15 cases: nil policy, nil AllowOnly, string variants (all/public/invalid/casing/whitespace),[]interface{}with sorting,[]stringtype, empty array, duplicates, invalid min-integrity, type mismatchTestIsValidRepoScope— 25 cases: all valid patterns (exact, wildcard, prefix-wildcard, special chars), all invalid patterns (no slash, three parts, leading/trailing slash, uppercase, dots, spaces, multiple wildcards, wildcard-in-middle)TestIsValidRepoOwner— 13 cases: empty, single char, 39/40-char boundaries, valid character classes, invalid chars (uppercase, dot, space, @)TestIsValidRepoName— 13 cases: empty, single char, 100/101-char boundaries, valid/invalid charsTestNormalizeAndValidateScopeArray— 11 cases: empty array, non-string element, empty/whitespace strings, invalid pattern, duplicates, valid single, multiple with sort, wildcard patternsTestGuardPolicyUnmarshalJSON— 5 cases: invalid JSON, unsupported field, missing allow-only, canonical key, legacyallowonlykeyTestAllowOnlyPolicyUnmarshalJSON— 8 cases: invalid JSON, unsupported field, missing repos, missing min-integrity, whitespace-only min-integrity, canonical key, legacyintegritykey, repos as arrayTestGuardPolicyMarshalJSON— round-trip serialization with canonicalallow-onlykeyTestAllowOnlyPolicyMarshalJSON— verifiesmin-integritycanonical key (not legacyintegrity)TestParseGuardPolicyJSONComprehensive— 7 cases: invalid JSON, invalid policy structures, valid policies with all integrity levels and legacy keyTestValidateGuardPolicy— nil, valid, invalid policy delegationTestIsScopeTokenChar— all 36 valid chars + invalid chars (uppercase, dot, slash, space, symbols)Generated by Test Coverage Improver
Target:
internal/config.NormalizeGuardPolicy— highest complexity with incomplete branch coverageWarning
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.