-
Notifications
You must be signed in to change notification settings - Fork 49
update code to honor the canonical purl encoding #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
update code to honor the canonical purl encoding #83
Conversation
Makefile
Outdated
|
|
||
| test: | ||
| curl -Ls https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json | ||
| curl -Ls https://raw.githubusercontent.com/package-url/purl-spec/de752b1160c7dc309482aaa80fd31884005aaec5/test-suite-data.json -o testdata/test-suite-data.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be entirely replaced if/when #80 is merged.
Pull Request Test Coverage Report for Build 17266694404Details
💛 - Coveralls |
| for _, qq := range q { | ||
| v.Add(qq.Key, qq.Value) | ||
| } | ||
| return v.Encode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot use Values.Encode() since it uses https://pkg.go.dev/net/url#QueryEscape under the hood to encode the value. This works in many cases but has caveats (does not work according to spec for whitespace and colon :).
| u := &url.URL{ | ||
| Scheme: "pkg", | ||
| RawQuery: p.Qualifiers.urlQuery(), | ||
| Fragment: p.Subpath, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: we cannot use url.Fragment since url.String will call u.EscapedFragment() and we end up with an incorrect escaping (similar to url.QueryEscape I believe).
Instead we "manually" add any fragment part at the end.
packageurl.go
Outdated
| qualifierKeys := slices.Collect(maps.Keys(p.Qualifiers.Map())) | ||
| hasNamespace := p.Namespace != "" | ||
| hasChannel := slices.Contains(qualifierKeys, "channel") | ||
| if hasNamespace && !hasChannel { | ||
| return errors.New("channel qualifier is required if namespace is present") | ||
| } | ||
| if hasChannel && !hasNamespace { | ||
| return errors.New("namespace is required if channel qualifier is present") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An attempt to make the code more readable.
| import "testing" | ||
|
|
||
| // Verifies that qualifier values are properly percent-encoded. | ||
| func TestQualifierValueEncoding(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some explicit testing of qualifier encoding (outside of purl-spec test suite) since this was a problematic spot which had poor coverage in the upstream tests.
|
@shibumi @mcombuechen ? |
|
Barring some outstanding issues with the purl spec test suite (see below) this code now passes the entire purl-spec suite. We should first merge #80 which changes the code to run against the new test suite in the purl-spec repo. When that is merged we can merge this PR. Until these issues/PRs are fixed the code in this PR will fail to complete the entire test suite (it will fail on about 10 out of 500). |
|
|
||
| test: | ||
| curl -Ls https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json | ||
| go test -v -cover ./... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: relies on #80 being merged.
| func validCustomRules(p PackageURL) error { | ||
| q := p.Qualifiers.Map() | ||
| switch p.Type { | ||
| case TypeConan: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conan spec has been relaxed.
| return errors.New("channel qualifier does not exist") | ||
| } | ||
| } else { | ||
| if val, ok := q["channel"]; ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conan spec has been relaxed.
| if p.Version == "" { | ||
| return errors.New("version is required") | ||
| } | ||
| case TypeCran: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @@ -1,572 +0,0 @@ | |||
| /* | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should be replaced with packageurl_test.go from #80
|
Anyone up for reviewing? |
This PR is an attempt to make the code honor the spec rules on producing a canonical purl when a
PackageURLis turned into its string representation (ToString()andString()).The old test suite (
test-suite-data.json) is no longer present upstream. For now I updated theMakefileto run against the last commit wheretest-suite-data.jsonwas still present in the repo (it was removed in 8a699a).This should be removed as soon as we can migrate to the new test suite (see separate PR).
It fails on the following tests:
is_invalidfor these cases was swapped fromtruetofalsein this commit without much of an explanation. These test cases appear to have been removed from the new test set though.For the new test suite (a pull request to make use of it is up here) it currently fails on 45 and passes on 468 test cases. For the failed ones:
pkg:conanI cannot figure out why we are failing. I've opened a ticket since I suspect that the test cases might be incorrect purl-spec#643.