|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +""" Smoke test script for Fuzzing |
| 5 | +
|
| 6 | +This script can be used to perform simple calls using `jsshell` |
| 7 | +or whatever other tools you may add. |
| 8 | +
|
| 9 | +The call is done via `taskcluster/ci/fuzzing/kind.yml` and |
| 10 | +files contained in the `target.jsshell.zip` and `target.fuzztest.tests.tar.gz` |
| 11 | +build artifacts are downloaded to run things. |
| 12 | +
|
| 13 | +Everything included in this directory will be added in |
| 14 | +`target.fuzztest.tests.tar.gz` at build time, so you can add more scripts and |
| 15 | +tools if you need. They will be located in `$MOZ_FETCHES_DIR` and follow the |
| 16 | +same directory structure than the source tree. |
| 17 | +""" |
| 18 | +from __future__ import absolute_import |
| 19 | +from distutils.spawn import find_executable |
| 20 | +import os |
| 21 | +import os.path |
| 22 | +import subprocess |
| 23 | +import shlex |
| 24 | +import sys |
| 25 | + |
| 26 | + |
| 27 | +def run_jsshell(command, label=None): |
| 28 | + """Invokes `jsshell` with command. |
| 29 | +
|
| 30 | + This function will use the `JSSHELL` environment variable, |
| 31 | + and fallback to a `js` executable if it finds one |
| 32 | + """ |
| 33 | + shell = os.environ.get("JSSHELL") |
| 34 | + if shell is None: |
| 35 | + shell = find_executable("js") |
| 36 | + if shell is None: |
| 37 | + raise FileNotFoundError(shell) |
| 38 | + else: |
| 39 | + if not os.path.exists(shell) or not os.path.isfile(shell): |
| 40 | + raise FileNotFoundError(shell) |
| 41 | + |
| 42 | + if label is None: |
| 43 | + label = command |
| 44 | + sys.stdout.write(label) |
| 45 | + cmd = [shell] + shlex.split(command) |
| 46 | + sys.stdout.flush() |
| 47 | + try: |
| 48 | + subprocess.check_call(cmd) |
| 49 | + finally: |
| 50 | + sys.stdout.write("\n") |
| 51 | + sys.stdout.flush() |
| 52 | + |
| 53 | + |
| 54 | +def smoke_test(): |
| 55 | + # first, let's make sure it catches crashes so we don't have false |
| 56 | + # positives. |
| 57 | + try: |
| 58 | + run_jsshell("-e 'crash();'", "Testing for crash\n") |
| 59 | + except subprocess.CalledProcessError: |
| 60 | + pass |
| 61 | + else: |
| 62 | + raise Exception("Could not get the process to crash") |
| 63 | + |
| 64 | + # now let's proceed with some tests |
| 65 | + run_jsshell("--fuzzing-safe -e 'print(\"PASSED\")'", "Simple Fuzzing...") |
| 66 | + |
| 67 | + # add more smoke tests here |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + # if this calls raises an error, the job will turn red in the CI. |
| 72 | + smoke_test() |
0 commit comments