Ejemplo n.º 1
0
 def inbound_con_by_ip(ip):
     print("Failure 2")
     global test_no_1_success
     test_no_1_success = 0
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 2
0
 def inbound_con_by_ip(ip):
     print("Failure 2")
     global test_no_1_success
     test_no_1_success = 0
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 3
0
 def add_node_hook(node_ip, node_port, node_type, timeout=5):
     print("Failure 1")
     global test_no_1_success
     test_no_1_success = 0
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 4
0
 def add_node_hook(node_ip, node_port, node_type, timeout=5):
     print("Failure 1")
     global test_no_1_success
     test_no_1_success = 0
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 5
0
 def test_cleanup_cons(self):
     con = Sock()
     con.close()
     self.client.con_info[con] = {}
     self.client.con_info[con]["something"] = 1
     self.client.defers["something"] = defer.Deferred()
     self.client.cons.append(con)
     cleanup_cons(self.client)
Ejemplo n.º 6
0
    def __init__(self,
                 url,
                 expected_hash,
                 file_size,
                 blocking=0,
                 encoding="unicode"):
        """
        Download a file from a HTTP URL and compare it to an MD5 hash.
        Uses the sock.py module for testing.

        :param url: URL to download
        :param expected_hash: MD5 hash of file (md5sum file from term)
        :param file_size: size in bytes of the file to download
        :param blocking: use blocking or non-blocking sockets
        :return:
        """

        url = urlparse(url)
        location = url.netloc.split(":")
        if len(location) == 1:
            port = 80
            host, = location
        else:
            host, port = location

        con = Sock(host, port, blocking=0)
        req = self.build_request(host, url.path)
        con.send(req, send_all=1)
        buf = u""
        eof = u"\r\n\r\n"
        while buf != eof and con.connected:
            ch = con.recv(1)
            if len(ch):
                buf += ch

            eq = 0
            for i in range(0, len(buf)):
                if buf[i] != eof[eq]:
                    eq = 0
                else:
                    eq += 1

            # Reset buf.
            if eq == len(eof):
                break

        fp, path = tempfile.mkstemp()
        os.close(fp)
        remaining = file_size
        total = 0
        self.start_time = timer()
        with open(path, "ab") as fp:
            future = time.time() + 30  # Slow connections are slow.
            while con.connected:
                chunk_size = 2048
                if chunk_size < remaining:
                    chunk_size = remaining
                data = con.recv(chunk_size, encoding=encoding)
                if len(data):
                    remaining -= len(data)
                    total += len(data)

        found_hash = md5sum(path)
        os.remove(path)
        if expected_hash is not None:
            assert (found_hash == expected_hash)

        self.total = total
Ejemplo n.º 7
0
 def test_get_contract_id(self):
     con = Sock("towel.blinkenlights.nl", 23, blocking=1)
     self.client.con_transfer[con] = b""
     contract_id = b""
     assert(get_contract_id(self.client, con, contract_id) == 1)
     con.close()
Ejemplo n.º 8
0
    def test_do_download(self):
        contract = {
            "data_id": hashlib.sha256(b"0").hexdigest()
        }

        con_info = {
            "file_size": 0,
            "file_size_buf": b"x"
        }

        con = Sock("93.184.216.34", 80, blocking=1, timeout=15)
        con.send_line("GET / HTTP/1.1")
        con.send_line("Host: www.example.com\r\n\r\n")

        # Invalid file size.
        self.assertTrue(
            do_download(
                self.client,
                con,
                contract,
                con_info
            ) == -2)
        con.close()

        # Invalid found data hash.
        con = Sock("93.184.216.34", 80, blocking=1, timeout=15)
        con.send_line("GET / HTTP/1.1")
        con.send_line("Host: www.example.com\r\n\r\n")
        data_id = hashlib.sha256(b"0").hexdigest()
        contract = {
            "data_id": data_id
        }
        con_info = {
            "file_size": 2,
            "file_size_buf": b"x",
            "remaining": 1
        }
        junk, self.client.downloading[data_id] = tempfile.mkstemp()
        print(do_download(self.client, con, contract, con_info))
        con.close()
Ejemplo n.º 9
0
 def inbound_con_by_ip(ip):
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 10
0
 def add_node_hook(node_ip, node_port, node_type, timeout=5):
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 11
0
 def inbound_con_by_ip(ip):
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 12
0
 def add_node_hook(node_ip, node_port, node_type, timeout=5):
     s = Sock()
     s.connected = 1
     return s
Ejemplo n.º 13
0
    def __init__(self, url, expected_hash, file_size, blocking=0,
                 encoding="unicode"):
        """
        Download a file from a HTTP URL and compare it to an MD5 hash.
        Uses the sock.py module for testing.

        :param url: URL to download
        :param expected_hash: MD5 hash of file (md5sum file from term)
        :param file_size: size in bytes of the file to download
        :param blocking: use blocking or non-blocking sockets
        :return:
        """

        url = urlparse(url)
        location = url.netloc.split(":")
        if len(location) == 1:
            port = 80
            host, = location
        else:
            host, port = location

        con = Sock(host, port, blocking=0)
        req = self.build_request(host, url.path)
        con.send(req, send_all=1)
        buf = u""
        eof = u"\r\n\r\n"
        while buf != eof and con.connected:
            ch = con.recv(1)
            if len(ch):
                buf += ch

            eq = 0
            for i in range(0, len(buf)):
                if buf[i] != eof[eq]:
                    eq = 0
                else:
                    eq += 1

            # Reset buf.
            if eq == len(eof):
                break

        fp, path = tempfile.mkstemp()
        os.close(fp)
        remaining = file_size
        total = 0
        self.start_time = timer()
        with open(path, "ab") as fp:
            future = time.time() + 30 # Slow connections are slow.
            while con.connected:
                chunk_size = 2048
                if chunk_size < remaining:
                    chunk_size = remaining
                data = con.recv(chunk_size, encoding=encoding)
                if len(data):
                    remaining -= len(data)
                    total += len(data)

        found_hash = md5sum(path)
        os.remove(path)
        if expected_hash is not None:
            assert(found_hash == expected_hash)

        self.total = total