test: final version of snippet script

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
This commit is contained in:
Christopher Phillips 2023-12-19 13:37:38 -05:00
parent 48cfc60631
commit 6d89a2bfcc
No known key found for this signature in database
GPG Key ID: 17F8FF2EA7EF0AA0
3 changed files with 15 additions and 6 deletions

View File

@ -146,7 +146,7 @@ func Test_Cataloger_DefaultClassifiers_PositiveCases(t *testing.T) {
},
{
name: "positive-httpd-2.4.54",
fixtureDir: "test-fixtures/classifiers/positive/httpd-2.4.54-3e11fbb6fa371cc9c3e513f49f0ab247b482773cd312248eb86e51251e36fafd-546892-100",
fixtureDir: "test-fixtures/classifiers/positive/httpd-2.4.54",
expected: pkg.Package{
Name: "httpd",
Version: "2.4.54",

View File

@ -3,10 +3,11 @@
# Default values for length and prefix length
LENGTH=100
PREFIX_LENGTH=10
SEARCH_FOR=''
# Function to show usage
usage() {
echo "Usage: $0 <path-to-binary> <search-pattern> [--length <length>] [--prefix-length <prefix_length>]"
echo "Usage: $0 <path-to-binary> <version> [--search-for <pattern>] [--length <length>] [--prefix-length <prefix_length>]"
exit 1
}
@ -15,6 +16,11 @@ while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--search-for)
SEARCH_FOR="$2"
shift # past argument
shift # past value
;;
--length)
LENGTH="$2"
shift # past argument
@ -28,8 +34,8 @@ while [[ $# -gt 0 ]]; do
*)
if [ -z "$BINARY_FILE" ]; then
BINARY_FILE="$1"
elif [ -z "$PATTERN" ]; then
PATTERN="$1"
elif [ -z "$VERSION" ]; then
VERSION="$1"
else
echo "Unknown option: $1"
usage
@ -40,7 +46,7 @@ while [[ $# -gt 0 ]]; do
done
# check if binary file and pattern are provided
if [ -z "$BINARY_FILE" ] || [ -z "$PATTERN" ]; then
if [ -z "$BINARY_FILE" ] || [ -z "$VERSION" ]; then
usage
fi
@ -50,6 +56,9 @@ if ! command -v xxd &> /dev/null; then
exit 1
fi
PATTERN=${SEARCH_FOR:-$VERSION}
PATTERN_RESULTS=$(strings -a -t d "$BINARY_FILE" | grep "$PATTERN")
# if there are multiple matches, prompt the user to select one
@ -113,7 +122,7 @@ DATE=$(date)
BASE64_PATTERN=$(echo -n "$PATTERN" | base64)
FILENAME=$(basename "$BINARY_FILE")
INFO=$(file -b "$BINARY_FILE")
OUTPUT_DIRECTORY="classifiers/positive/$FILENAME-$PATTERN-$SHA256-$OFFSET-$LENGTH"
OUTPUT_DIRECTORY="classifiers/positive/$FILENAME-$VERSION"
mkdir "$OUTPUT_DIRECTORY"
OUTPUT_FILE="$OUTPUT_DIRECTORY/$FILENAME"