Skip to content
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

Upgrade binary test fixtures management #2444

Merged
merged 31 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
59eef37
test: strip fixtures of any execution permissions
spiffcs Dec 18, 2023
f194de3
chore: add lint check for large files
spiffcs Dec 18, 2023
de87abf
add helper script to capture binary snippets
wagoodman Dec 18, 2023
2bf9971
chore: update scripts and add new dir output for snippets
spiffcs Dec 19, 2023
9099b98
test: update erlang test to new generated format
spiffcs Dec 19, 2023
a782fd2
test: update memcached to new generator pattern
spiffcs Dec 19, 2023
d289325
test: update openjdk to named version
spiffcs Dec 19, 2023
b945968
test: move openjdk lts to versioned folder
spiffcs Dec 19, 2023
5e55530
test: rename unversioned java to versioned folders
spiffcs Dec 19, 2023
d1f3d11
test: migrate bash fixture to new snippet workflow
spiffcs Dec 19, 2023
0b4f9ad
test: update script to size 600 bytes
spiffcs Dec 19, 2023
b92ccf8
test: update go classifier to new snippet workflow
spiffcs Dec 19, 2023
fd26fc1
test: move haproxy new new snippet
spiffcs Dec 19, 2023
7b86f23
test: add flatter haproxy example
spiffcs Dec 19, 2023
48cfc60
test: update tests to new pattern
spiffcs Dec 19, 2023
6d89a2b
test: final version of snippet script
spiffcs Dec 19, 2023
bab4142
[wip] download bin helpers
wagoodman Dec 19, 2023
e516eb4
add manager for binary cataloger test fixtures
wagoodman Dec 21, 2023
d849719
add remaining binary cataloger patterns and snippets
wagoodman Dec 22, 2023
8006e9d
adjust gitignore to be more permissive to snippets
wagoodman Dec 22, 2023
a8b0f23
Merge remote-tracking branch 'origin/main' into least-permissive-snip…
wagoodman Dec 22, 2023
76619b2
add rust darwin snippets
wagoodman Dec 22, 2023
8d9f1a8
skip tests that are missing full binaries
wagoodman Dec 22, 2023
f422fc9
Merge remote-tracking branch 'origin/main' into least-permissive-snip…
wagoodman Jan 5, 2024
ac1c35e
address PR feedback
wagoodman Jan 5, 2024
1860b35
add tests for binary test fixture manager
wagoodman Jan 5, 2024
28aea2b
highlight rows that do not have binaries or snippets
wagoodman Jan 5, 2024
6d1a72f
bump fixture limit to 1K (found exceptions when adding snippets)
wagoodman Jan 5, 2024
4f0a5ae
add redis and postgres snippets
wagoodman Jan 5, 2024
83109d0
improve formating of fixture listing
wagoodman Jan 5, 2024
42c5308
Merge remote-tracking branch 'origin/main' into least-permissive-snip…
wagoodman Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/scripts/check_binary_fixture_size.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Check if a directory is provided as an argument
if [ $# -eq 0 ]; then
echo "Usage: $0 <directory>"
exit 1
fi

directory="$1"

# Check if the directory exists
if [ ! -d "$directory" ]; then
echo "Directory not found: $directory"
exit 1
fi

# Use find to locate all files in the directory and its subdirectories
found_large_files=0
while IFS= read -r -d '' file; do
# Check if the file size is greater than 100 bytes
if [ $(wc -c < "$file") -gt 100 ]; then
echo "File $file is greater than 100 bytes."
found_large_files=1
fi
done < <(find "$directory" -type f -print0)

# Check if any large files were found
if [ "$found_large_files" -eq 1 ]; then
echo "Script failed: Some files are greater than 100 bytes."
exit 1
else
echo "All files in $directory and its subdirectories are 100 bytes or smaller. Script passed."
exit 0
fi

6 changes: 6 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tasks:
- task: check-licenses
- task: lint
- task: check-json-schema-drift
- task: check-binary-fixture-size

test:
desc: Run all levels of test
Expand Down Expand Up @@ -169,6 +170,11 @@ tasks:
cmds:
- .github/scripts/json-schema-drift-check.sh

check-binary-fixture-size:
desc: Ensure that the binary test fixtures are not too large
cmds:
- .github/scripts/check_binary_fixture_size.sh syft/pkg/cataloger/binary/test-fixtures/classifiers/positive


## Testing tasks #################################

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
130 changes: 130 additions & 0 deletions syft/pkg/cataloger/binary/test-fixtures/get-fixture-snippet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash

# Default values for length and prefix length
LENGTH=100
PREFIX_LENGTH=10

# Function to show usage
usage() {
echo "Usage: $0 <path-to-binary> <search-pattern> [--length <length>] [--prefix-length <prefix_length>]"
exit 1
}

# Parse arguments
while [[ $# -gt 0 ]]; do
key="$1"

case $key in
--length)
LENGTH="$2"
shift # past argument
shift # past value
;;
--prefix-length)
PREFIX_LENGTH="$2"
shift # past argument
shift # past value
;;
*)
if [ -z "$BINARY_FILE" ]; then
BINARY_FILE="$1"
elif [ -z "$PATTERN" ]; then
PATTERN="$1"
else
echo "Unknown option: $1"
usage
fi
shift # past argument
;;
esac
done

# check if binary file and pattern are provided
if [ -z "$BINARY_FILE" ] || [ -z "$PATTERN" ]; then
usage
fi

# check if xxd is even installed
if ! command -v xxd &> /dev/null; then
echo "xxd not found. Please install xxd."
exit 1
fi

PATTERN_RESULTS=$(strings -a -t d "$BINARY_FILE" | grep "$PATTERN")

# if there are multiple matches, prompt the user to select one
if [ $(echo "$PATTERN_RESULTS" | wc -l) -gt 1 ]; then
echo "Multiple string matches found in the binary:"
echo ""

# show result lines one at a time (in a numbered list)
# but only show everything after the first field (not the offset)
echo "$PATTERN_RESULTS" | cut -d ' ' -f 2- | nl -w 1 -s ') '


echo ""
read -p "Please select a match: " SELECTION

# if the selection is not a number, exit
if ! [[ "$SELECTION" =~ ^[0-9]+$ ]]; then
echo "Invalid selection."
exit 1
fi

# if the selection is out of bounds, exit
if [ "$SELECTION" -gt $(echo "$PATTERN_RESULTS" | wc -l) ]; then
echo "Invalid selection."
exit 1
fi

# select the line from the results
PATTERN_RESULTS=$(echo "$PATTERN_RESULTS" | sed -n "${SELECTION}p")
fi

# search for the pattern in the binary file and capture the offset
OFFSET=$(echo "${PATTERN_RESULTS}" | cut -d ' ' -f 1)

if [ -z "$OFFSET" ]; then
echo "Pattern not found."
exit 1
fi

# adjust the offset to capture prefix length before the match
OFFSET=$(expr "$OFFSET" - "$PREFIX_LENGTH")

# use xxd to capture the specified length from the calculated offset
SNIPPET=$(xxd -l "$LENGTH" -s "$OFFSET" "$BINARY_FILE")

# display the output and prompt the user
echo ""
echo "$SNIPPET"
echo ""
read -p "Does this snippet capture what you need? (Y/n) " RESPONSE
RESPONSE=${RESPONSE:-y}

if [ "$RESPONSE" != "y" ]; then
echo "Exiting with no action taken."
exit 1
fi

# generate a text file with metadata and the binary snippet
SHA256=$(sha256sum "$BINARY_FILE" | cut -d ' ' -f 1)
DATE=$(date)
BASE64_PATTERN=$(echo -n "$PATTERN" | base64)
FILENAME=$(basename "$BINARY_FILE")
INFO=$(file -b "$BINARY_FILE")
OUTPUT_FILE="classifiers/positive/$FILENAME-$SHA256-$OFFSET-$LENGTH.snippet"

cat > "$OUTPUT_FILE" <<EOF
### generated by script $(basename $0) at $DATE ###
# filename: $FILENAME
# sha256: $SHA256
# file info: $INFO
# base64(search): $BASE64_PATTERN
# start offset: $OFFSET
# length: $LENGTH
### start of binary snippet ###
EOF
echo "$SNIPPET" | xxd -r -s -"$OFFSET" >> "$OUTPUT_FILE"

echo "Snippet written to $OUTPUT_FILE"
Loading