<commit_before>#!/usr/bin/python
import os, sys, socket, random, time
from test_common import *

# "I am a string" -> ["I a", "m a s", "trin", "g"]
def rand_split(string, nsub_strings):
    cutoffs = random.sample(range(len(string)), nsub_strings);
    cutoffs.sort();
    cutoffs.insert(0, 0)
    cutoffs.append(len(string))
    strings = []
    for (start, end) in zip(cutoffs[0:len(cutoffs) - 1], cutoffs[1:len(cutoffs)]):
        strings.append(string[start:end])

    return strings

meaningful_words = ['set', 'get', 'delete', 'gets', 'add', 'replace', 'append', 'prepend', 'cas', 'incr', 'decr', 'stat', 'rethinkdbctl', 'noreply'] 

def word():
    return random.choice(meaningful_words)

delims = ['\t', ' ', '\n', '\r']

def delim():
    return random.choice(delims)

def number():
    return str(random.randint(0, 999999999999))

def garbage():
    chars=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','!','@','#','$','%','^','&','*','(',')','_','+',',','.','/',';','[',']','<','>',':','{','}','?','\r\n','\r','\n','\t',' ']
    len = random.randint(1, 1000)
    res = ''
    for i in range(len):
        res += random.choice(chars)
    return res

def funny():
    return "Yo dawg"

def test_function(opts, port):

    sent_log = open('fuzz_sent', 'w')
    recv_log = open('fuzz_recv', 'w')
    
    start_time = time.time()

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("localhost", port))

    time.sleep(2)

    while (time.time() - start_time < opts["duration"]):
        time.sleep(.05)
        str = ''
        for i in range(20):
            choice = random.random()
            if choice < .5:
                str += word()
            elif choice < .75:
                str += number()
            elif choice < .85:
                str += garbage()
            elif choice < .99:
                str += delim()
            else:
                str += funny()

        for substr in rand_split(str, random.randint(10, 40)):
            s.send(substr)
            sent_log.write(substr)

        s.settimeout(0)
        try:
            server_str = s.recv(100000)
            recv_log.write(server_str)
        except:
            pass

    s.send('quit\r\n')

    s.close()

if __name__ == "__main__":
    op = make_option_parser()
    op["duration"] = IntFlag("--duration", 1000)
    auto_server_test_main(test_function, op.parse(sys.argv), timeout = 120)<commit_msg>Change fuzz-test timeout to be greater than fuzz-test duration.<commit_after>#!/usr/bin/python
import os, sys, socket, random, time
from test_common import *

# "I am a string" -> ["I a", "m a s", "trin", "g"]
def rand_split(string, nsub_strings):
    cutoffs = random.sample(range(len(string)), nsub_strings);
    cutoffs.sort();
    cutoffs.insert(0, 0)
    cutoffs.append(len(string))
    strings = []
    for (start, end) in zip(cutoffs[0:len(cutoffs) - 1], cutoffs[1:len(cutoffs)]):
        strings.append(string[start:end])

    return strings

meaningful_words = ['set', 'get', 'delete', 'gets', 'add', 'replace', 'append', 'prepend', 'cas', 'incr', 'decr', 'stat', 'rethinkdbctl', 'noreply'] 

def word():
    return random.choice(meaningful_words)

delims = ['\t', ' ', '\n', '\r']

def delim():
    return random.choice(delims)

def number():
    return str(random.randint(0, 999999999999))

def garbage():
    chars=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','!','@','#','$','%','^','&','*','(',')','_','+',',','.','/',';','[',']','<','>',':','{','}','?','\r\n','\r','\n','\t',' ']
    len = random.randint(1, 1000)
    res = ''
    for i in range(len):
        res += random.choice(chars)
    return res

def funny():
    return "Yo dawg"

def test_function(opts, port):

    sent_log = open('fuzz_sent', 'w')
    recv_log = open('fuzz_recv', 'w')
    
    start_time = time.time()

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("localhost", port))

    time.sleep(2)

    while (time.time() - start_time < opts["duration"]):
        time.sleep(.05)
        str = ''
        for i in range(20):
            choice = random.random()
            if choice < .5:
                str += word()
            elif choice < .75:
                str += number()
            elif choice < .85:
                str += garbage()
            elif choice < .99:
                str += delim()
            else:
                str += funny()

        for substr in rand_split(str, random.randint(10, 40)):
            s.send(substr)
            sent_log.write(substr)

        s.settimeout(0)
        try:
            server_str = s.recv(100000)
            recv_log.write(server_str)
        except:
            pass

    s.send('quit\r\n')

    s.close()

if __name__ == "__main__":
    op = make_option_parser()
    op["duration"] = IntFlag("--duration", 1000)
    opts = op.parse(sys.argv)
    auto_server_test_main(test_function, opts, timeout = opts["duration"] + 30)
{'fraction_non_alphanumeric': 0.24962434259954921, 'fraction_numerical': 0.023478587528174305, 'mean_word_length': 2.8117394416607016, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1845546', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1878, 'n_words': 360}