Wrote ClusterFuzz test for WebRTC GetUserMedia.

This initial test is very simple since we are just releasing GetUserMedia in the next release.

BUG=
TEST=

Review URL: https://webrtc-codereview.appspot.com/639006

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2476 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
phoglund@webrtc.org 2012-07-02 11:39:22 +00:00
parent b358bd8f87
commit ef8ca6a801
3 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<!--
Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
Use of this source code is governed by a BSD-style license
that can be found in the LICENSE file in the root of the source
tree. An additional intellectual property rights grant can be found
in the file PATENTS. All contributing project authors may
be found in the AUTHORS file in the root of the source tree.
-->
<html>
<head>
<title>WebRTC Fuzz Test Template</title>
<script type="text/javascript">
function requestVideo() {
navigator.webkitGetUserMedia(FUZZ_USER_MEDIA_INPUT,
FUZZ_OK_CALLBACK,
FUZZ_FAIL_CALLBACK);
}
function getUserMediaFailedCallback(error) {
console.log(error.code)
}
function getUserMediaOkCallback(stream) {
var streamUrl = webkitURL.createObjectURL(stream);
document.getElementById("view1").src = streamUrl;
stream.stop()
}
</script>
</head>
<body onload="requestVideo();">
<table border="0">
<tr>
<td>Local Preview</td>
</tr>
<tr>
<td><video width="320" height="240" id="view1"
autoplay="autoplay"></video></td>
</tr>
</table>
</body>
</html>

58
src/test/fuzz/fuzz_main_run.py Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env python
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
# Based on the ClusterFuzz simple fuzzer template.
import getopt
import os
import sys
import tempfile
import time
import get_user_media_fuzz
def GenerateData(input_dir):
template = open(os.path.join(input_dir, 'template.html'))
file_data = template.read()
template.close()
file_extension = 'html'
file_data = get_user_media_fuzz.Fuzz(file_data)
return file_data, file_extension
if __name__ == '__main__':
start_time = time.time()
no_of_files = None
input_dir = None
output_dir = None
optlist, args = getopt.getopt(sys.argv[1:], '', \
['no_of_files=', 'output_dir=', 'input_dir='])
for option, value in optlist:
if option == '--no_of_files': no_of_files = int(value)
elif option == '--output_dir': output_dir = value
elif option == '--input_dir': input_dir = value
assert no_of_files is not None, 'Missing "--no_of_files" argument'
assert output_dir is not None, 'Missing "--output_dir" argument'
assert input_dir is not None, 'Missing "--input_dir" argument'
for file_no in range(no_of_files):
file_data, file_extension = GenerateData(input_dir)
file_descriptor, file_path = tempfile.mkstemp(
prefix='fuzz-%d-%d' % (start_time, file_no),
suffix='.' + file_extension,
dir=output_dir)
file = os.fdopen(file_descriptor, 'wb')
print 'Writing %d bytes to "%s"' % (len(file_data), file_path)
print file_data
file.write(file_data)
file.close()

View File

@ -0,0 +1,91 @@
#!/usr/bin/env python
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
import random
import string
class MissingParameterException(Exception):
pass
def FillInParameter(parameter, value, template):
if parameter not in template:
raise MissingParameterException('Did not find parameter %s in template.' %
parameter)
return template.replace(parameter, value)
def RandomIdentifier():
length = random.randint(1, 25)
return (random.choice(string.letters) +
''.join(random.choice(string.letters + string.digits)
for i in xrange(length)))
def GenerateRandomJavascriptAttributes(with_num_attributes):
return ['%s: %s' % (RandomIdentifier(), GenerateRandomJavascriptValue())
for i in xrange(with_num_attributes)]
def MakeJavascriptObject(attributes):
return '{ ' + ', '.join(attributes) + ' }'
def GenerateRandomJavascriptFunction():
num_parameters = random.randint(0, 10)
parameter_list = ', '.join(RandomIdentifier() for i in xrange(num_parameters))
return 'function ' + RandomIdentifier() + '(' + parameter_list + ')' + '{ }'
def GenerateRandomJavascriptValue():
roll = random.random()
if roll < 0.3:
return '"' + RandomIdentifier() + '"'
elif roll < 0.6:
return str(random.randint(-10000000, 10000000))
elif roll < 0.9:
# Functions are first-class objects.
return GenerateRandomJavascriptFunction()
else:
return 'true' if random.random() < 0.5 else 'false'
def Fuzz(template):
"""Generates a single random HTML page which tries to mess with getUserMedia.
We require a template which has certain placeholders defined in it (such
as FUZZ_USER_MEDIA_INPUT). We then replace these placeholders with random
identifiers and data in certain patterns. For instance, since the getUserMedia
function accepts an object, we try to pass in everything from {video:true,
audio:false} (which is a correct value) to {sdjkjsjh34sd:455, video:'yxuhsd'}
and other strange things.
See the template at corpus/template.html for an example of how a template
looks like.
"""
random.seed()
attributes = GenerateRandomJavascriptAttributes(random.randint(0, 10))
if (random.random() < 0.8):
attributes.append('video: %s' % GenerateRandomJavascriptValue())
if (random.random() < 0.8):
attributes.append('audio: %s' % GenerateRandomJavascriptValue())
input_object = MakeJavascriptObject(attributes)
template = FillInParameter('FUZZ_USER_MEDIA_INPUT', input_object, template)
ok_callback = (GenerateRandomJavascriptValue()
if random.random() < 0.5 else 'getUserMediaOkCallback')
template = FillInParameter('FUZZ_OK_CALLBACK', ok_callback, template)
fail_callback = (GenerateRandomJavascriptValue()
if random.random() < 0.5 else 'getUserMediaFailedCallback')
template = FillInParameter('FUZZ_FAIL_CALLBACK', fail_callback, template)
return template