This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes a panic which occurs if an input which is a valid URL doesn't have either 2 or 3 parts to the opaque once split. The only valid lengths for PURL opaques once split are 2 and 3, so this PR returns an empty PURL if the PURL being parsed is invalid.
- Loading branch information
Showing
2 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright © 2022 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package analyzer | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func Test_purlStringToStruct(t *testing.T) { | ||
type args struct { | ||
purlInput string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want purl | ||
}{ | ||
{ | ||
name: "valid purl", | ||
args: args{ | ||
purlInput: "pkg:maven/org.apache.xmlgraphics/[email protected]?packaging=sources", | ||
}, | ||
want: purl{ | ||
scheme: "pkg", | ||
typ: "maven", | ||
namespace: "org.apache.xmlgraphics", | ||
name: "batik-anim", | ||
version: "1.9.1", | ||
qualifers: url.Values{ | ||
"packaging": []string{"sources"}, | ||
}, | ||
subpath: "", | ||
}, | ||
}, | ||
{ | ||
name: "empty purl", | ||
args: args{ | ||
purlInput: "", | ||
}, | ||
want: newPurl(), | ||
}, | ||
{ | ||
name: "junk", | ||
args: args{ | ||
purlInput: "asdkjnasdkhjbasdhb", | ||
}, | ||
want: newPurl(), | ||
}, | ||
{ | ||
name: "no namespace purl", | ||
args: args{ | ||
purlInput: "pkg:pypi/[email protected]", | ||
}, | ||
want: purl{ | ||
scheme: "pkg", | ||
typ: "pypi", | ||
namespace: "", | ||
name: "Babel", | ||
version: "2.9.1", | ||
qualifers: url.Values{}, | ||
subpath: "", | ||
}, | ||
}, | ||
{ | ||
name: "no type or namespace", | ||
args: args{ | ||
purlInput: "pkg:[email protected]", | ||
}, | ||
want: newPurl(), | ||
}, | ||
{ | ||
name: "alpine package missing type", | ||
args: args{ | ||
purlInput: "pkg:alpine/[email protected]?arch=x86", | ||
}, | ||
want: purl{ | ||
scheme: "pkg", | ||
typ: "apk", | ||
namespace: "alpine", | ||
name: "apk", | ||
version: "2.12.9-r3", | ||
qualifers: url.Values{ | ||
"arch": []string{"x86"}, | ||
}, | ||
subpath: "", | ||
}, | ||
}, | ||
{ | ||
name: "no namespace, unknown type", | ||
args: args{ | ||
purlInput: "pkg:debi/[email protected]?arch=i386&distro=jessie", | ||
}, | ||
want: newPurl(), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := purlStringToStruct(tt.args.purlInput) | ||
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(purl{})); diff != "" { | ||
t.Errorf("purlStringToStruct() mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |