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)
    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,
                None
            ) == -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()
        os.close(junk)
        print(do_download(self.client, con, contract, con_info, None))
        con.close()
 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()