def init_hs():
    """Set up a hidden service -- configure Tor, and start an HTTP
    server that we return a handle to."""
    options = ctl.get_option("HiddenServiceOptions")
    if (options[0][1] == None or options[1][1] == None):
        ctl.set_options((("HiddenServiceDir", get_repo_root() + '/examples/tor/data/hs'), 
                         ("HiddenServicePort", str(8888))))
    port = int(ctl.get_option("HiddenServiceOptions")[1][1])
    serve_path = get_repo_root() + '/examples/tor/test/tests/'
    os.chdir(serve_path)
    httpd = SocketServer.TCPServer(("", port), 
                                   SimpleHTTPServer.SimpleHTTPRequestHandler)
    return (httpd, port)
def run_test():
    # set up
    (httpd, port) = init_hs()
    thread = HSTestThread(httpd)
    thread.daemon = True
    thread.start()

    # get the hidden service hostname
    f = open(get_repo_root() + '/examples/tor/data/hs/hostname', "r")
    hs_hostname = f.read().rstrip()
    f.close()

    # download the file without Tor
    print "%s: Downloading test file without Tor" % __file__
    file_hash = hashlib.sha512(fetch_file_clear(TESTFILE, "localhost", port)).hexdigest()
    
    # download the image over Tor
    print "%s: Downloading test file over Tor" % __file__
    tor_hash = hashlib.sha512(fetch_file_from_hs(TESTFILE, hs_hostname, port)).hexdigest()

    # test
    assert(tor_hash == file_hash)
    # shut down
    thread.shutdown()
    thread.join()
    return 0