Skip to content

Commit

Permalink
check runtime using Python instead of Bash
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tz committed Apr 8, 2024
1 parent aab15fa commit 1e46a08
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
64 changes: 64 additions & 0 deletions .github/check_runtimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (C) 2024 Mandiant, Inc. 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: [package root]/LICENSE.txt
# 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.
"""
Check runtime of testfiles.
"""

import sys
import time
import logging
import argparse
from pathlib import Path

import capa.main

logger = logging.getLogger("capa.tests.data")

THRESHOLD = 60 * 3
TARGET_EXTS = (".exe_", ".dll_", ".elf_", ".sys_", ".raw32", ".raw64", ".BinExport")
IGNORED_DIRS = ("aarch64",)


def main(argv=None):
if argv is None:
argv = sys.argv[1:]

parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+", help="Paths of added/modified files")
args = parser.parse_args(args=argv)

test_failed = False
for file in args.files:
file = Path(file)
# Skip ignored directories
if any((ignored_dir in file.parts) for ignored_dir in IGNORED_DIRS):
continue

if not file.name.endswith(TARGET_EXTS):
continue

time0 = time.time()
capa.main.main(["-q", "-v", str(file)])
diff = time.time() - time0

if diff > THRESHOLD:
logger.info("capa ran for %s seconds, please provide a different sample so we can test more quickly", diff)
test_failed = True
else:
logger.info("all good, capa ran for %s seconds", diff)

if test_failed:
return 1
else:
logger.info("test files look good!")
return 0


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
sys.exit(main())
22 changes: 1 addition & 21 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,4 @@ jobs:
with:
format: 'csv'
- name: Check capa runtime on modified files
run: |
THRESHOLD=180
EXCLUSION_REGEX="aarch64"
FILE_EXTENSION_REGEX=".exe_|.dll_|.elf_|.sys_|.raw32|.raw64"
exitcode=0
cd tests/data
mapfile -d ',' -t added_modified_files < <(printf '%s,' '${{ steps.files.outputs.all }}')
for changed_file in "${added_modified_files[@]}"; do
if [[ $changed_file =~ $FILE_EXTENSION_REGEX && ! $changed_file =~ $EXCLUSION_REGEX ]]; then
time0=$SECONDS
capa -q -v "$changed_file"
diff=$(($SECONDS-time0))
if [[ $diff -gt $THRESHOLD ]]; then
echo "capa ran for $diff seconds, please provide a different sample so we can test more quickly"
exitcode=1
else
echo "all good, capa ran for $diff seconds"
fi
fi
done
exit $exitcode
run: python .github/check_runtimes.py ${{ steps.files.outputs.all }}

0 comments on commit 1e46a08

Please sign in to comment.