-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add OSS-Fuzz on Demand build steps to upload testcases #13296
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8c33ac8
Standardize build step type to dict
decoNR 0faa6cf
Add python script to upload OSS-Fuzz on Demand testcases
decoNR 5473323
Add upload testcase build steps for OSS-Fuzz on Demand
decoNR 395ee04
Lint
decoNR ac9e846
Remove clusterfuzz-config url comment
decoNR 188fa0a
Use single quotes instead of double quotes
decoNR 765ba23
Use raise_for_status() on ood_upload_testcase.py
decoNR cdb0c1d
Lint
decoNR 1f17419
Ensure testcases are opened in 'rb' mode
decoNR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,100 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2025 Google LLC | ||
# | ||
# 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. | ||
# | ||
################################################################################ | ||
"""Upload OSS-Fuzz on Demand testcases.""" | ||
|
||
import json | ||
import logging | ||
import os | ||
import sys | ||
import subprocess | ||
|
||
try: | ||
import requests | ||
except ImportError: | ||
print("requests library not found. Installing...") | ||
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"]) | ||
import requests | ||
|
||
POST_URL = 'https://oss-fuzz.com/upload-testcase/upload-oauth' | ||
|
||
|
||
def get_access_token(access_token_path): | ||
"""Returns the ACCESS_TOKEN for upload testcase requests""" | ||
with open(access_token_path, 'r') as f: | ||
line = f.readline() | ||
return line.strip() | ||
|
||
|
||
def get_headers(access_token_path): | ||
"""Returns the headers required to upload testcase requests""" | ||
access_token = get_access_token(access_token_path) | ||
return { | ||
'Authorization': 'Bearer ' + access_token, | ||
} | ||
|
||
|
||
def upload_testcase(upload_url, testcase_path, job, target, access_token_path): | ||
"""Make an upload testcase request.""" | ||
files = { | ||
'file': open(testcase_path, 'rb'), | ||
} | ||
data = { | ||
'job': job, | ||
'target': target, | ||
} | ||
try: | ||
resp = requests.post(upload_url, | ||
files=files, | ||
data=data, | ||
headers=get_headers(access_token_path)) | ||
resp.raise_for_status() | ||
result = json.loads(resp.text) | ||
print('Upload succeeded. Testcase ID is', result['id']) | ||
except: | ||
print('Failed to upload with status', resp.status_code) | ||
print(resp.text) | ||
|
||
|
||
def get_file_path(dir_path): | ||
"""Returns the path of a file inside 'dir_path'. Returns None if there are no | ||
files inside the the given directory.""" | ||
files = [] | ||
for entry in os.scandir(dir_path): | ||
if entry.is_file(): | ||
return f'{dir_path}/{entry.name}' | ||
decoNR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return None | ||
|
||
|
||
def main(): | ||
"""Upload an OSS-Fuzz on Demand testcase.""" | ||
testcase_dir_path = sys.argv[1] | ||
job = sys.argv[2] | ||
target = sys.argv[3] | ||
access_token_path = sys.argv[4] | ||
testcase_path = get_file_path(testcase_dir_path) | ||
|
||
if not testcase_path: | ||
print('OSS-Fuzz on Demand did not find any crashes.') | ||
else: | ||
upload_testcase(POST_URL, testcase_path, job, target, access_token_path) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains hidden or 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,69 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# 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. | ||
# | ||
################################################################################ | ||
"""Tests for ood_upload_testcase_test.py.""" | ||
|
||
import os | ||
import shutil | ||
import tempfile | ||
import unittest | ||
from unittest import mock | ||
|
||
import ood_upload_testcase | ||
|
||
|
||
class GetFilePath(unittest.TestCase): | ||
"""Tests for get_file_path.""" | ||
|
||
def setUp(self): | ||
"""Set tem_dir attribute""" | ||
self.temp_dir = tempfile.mkdtemp() | ||
|
||
def tearDown(self): | ||
"""Remove temp_dir after tests""" | ||
shutil.rmtree(self.temp_dir) | ||
|
||
def test_no_files(self): | ||
"""Test for empty directory""" | ||
self.assertIsNone(ood_upload_testcase.get_file_path(self.temp_dir)) | ||
|
||
def test_single_file(self): | ||
"""Test for single file""" | ||
file_name = 'test_file.txt' | ||
file_path = os.path.join(self.temp_dir, file_name) | ||
open(file_path, 'w').close() | ||
self.assertEqual(ood_upload_testcase.get_file_path(self.temp_dir), | ||
file_path) | ||
|
||
def test_multiple_files(self): | ||
"""Test for multiple files""" | ||
file_names = ['file1.txt', 'file2.csv', 'data.json'] | ||
file_paths = [] | ||
for name in file_names: | ||
file_path = os.path.join(self.temp_dir, name) | ||
file_paths.append(file_path) | ||
open(file_path, 'w').close() | ||
self.assertIn(ood_upload_testcase.get_file_path(self.temp_dir), file_paths) | ||
|
||
def test_with_subdirectory(self): | ||
"""Test for directory with subdirectory""" | ||
os.makedirs(os.path.join(self.temp_dir, 'subdir')) | ||
self.assertIsNone(ood_upload_testcase.get_file_path(self.temp_dir)) | ||
|
||
file_name = 'test_file.txt' | ||
file_path = os.path.join(self.temp_dir, file_name) | ||
open(file_path, 'w').close() | ||
self.assertEqual(ood_upload_testcase.get_file_path(self.temp_dir), | ||
file_path) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.