# put this in sulley/fuzz_toed.py

import sys, socket
from sulley import *
from requests import toe

TARGET_ADDR = "127.0.0.1"
TARGET_PORT = 1337
DEBUG = False

def do_fuzz():
    sess = sessions.session(session_filename="audits/toed.session")
    target = sessions.target(TARGET_ADDR, TARGET_PORT)
    
    target.procmon = pedrpc.client(TARGET_ADDR, 26002)
    
    target.procmon_options = \
    {
        "proc_name": "toed",
        "stop_commands": ['killall toed'],
        "start_commands": ["'/Users/rigdern/Documents/School/Semester 6/Secure Software Principles/fuzzing/demos/toed'"]
    }
    
    sess.add_target(target)
    sess.connect(s_get("TOE"))
    sess.fuzz()
    print 'done fuzzing'

def do_single (req, num):
    # connect to the server.
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((TARGET_ADDR, TARGET_PORT))

    # grab the request
    request = s_get(req)

    # mutate the request until we get to the right one
    for i in range(num):
        s_mutate()
    """"while 1:
        if request.names["subs"].value == num:
            break

        s_mutate()"""

    print "xmitting single test case"
    # display the request
    print s_render()
    # send it
    s.send(s_render())
    print "done."

if len(sys.argv) > 1:
    do_single("TOE", int(sys.argv[1]))
else:
    do_fuzz()