def test_get_contract_with_write_address(self): # Setup util.write_address(self.MOCK_USER, self.VALID_CHALLENGE, self.MOCK_CONTRACT_ADDRESS) contract_helper = util.get_icontract(self.MOCK_USER, self.VALID_CHALLENGE) # Validate assert contract_helper is not None assert contract_helper.contract_address == self.MOCK_CONTRACT_ADDRESS assert contract_helper.user_address == self.MOCK_USER assert contract_helper.web3 is not None # Cleanup shutil.rmtree(constants.DB_PATH + self.MOCK_USER)
def done(address, contract): print("deploying:\t{} {}".format(address, contract)) status = get_deploy_thread_status(address, contract) if status[1] is not None and status[0] == "deployed": print("Status is not none: " + str(status)) global deployers deploy_key = (address, contract) del deployers[deploy_key] # TODO race condition? util.write_address(address, util.get_contract_number(contract), status[1]) if status[0]: return status[0] else: return "Starting deployment process"
def test_get_status(self): ns = ("Not Started", "red") dep = ("Deployed / Unfinished", "black", self.MOCK_CONTRACT_ADDRESS) fin = ("Done!", "green", self.MOCK_CONTRACT_ADDRESS) with run.app.app_context(): stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, ns) util.exists(self.MOCK_USER) stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, ns) util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, dep) util.mark_finished(self.MOCK_USER, "01_naive_programmer") stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, fin)
def test_util_get_status(self): # Setup path = constants.DB_PATH + self.MOCK_USER + "/" + self.MOCK_CHALLENGE if os.path.exists(path): os.remove(path) # Validate assert util.get_status(self.MOCK_USER, self.MOCK_CHALLENGE)[0] == "Not Started" util.write_address(self.MOCK_USER, self.MOCK_CHALLENGE, self.MOCK_CONTRACT_ADDRESS) assert util.get_status(self.MOCK_USER, self.MOCK_CHALLENGE)[0] == "Deployed / Unfinished" util.mark_finished(self.MOCK_USER, self.MOCK_CHALLENGE) assert util.get_status(self.MOCK_USER, self.MOCK_CHALLENGE)[0] == "Done!" # Cleanup if os.path.exists(path): os.remove(path)
def test_write_address(self): # Setup path = constants.DB_PATH + self.MOCK_USER + "/" + self.MOCK_CHALLENGE if os.path.exists(path): os.remove(path) # Validate assert not os.path.exists(path) util.write_address(self.MOCK_USER, self.MOCK_CHALLENGE, self.MOCK_CONTRACT_ADDRESS) assert os.path.exists(path) with open(path) as fd: assert fd.read().strip() == self.MOCK_CONTRACT_ADDRESS # Cleanup if os.path.exists(path): os.remove(path)
def test_mark_in_progress(self): with run.app.app_context(): util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) util.mark_finished(self.MOCK_USER, "01_naive_programmer") util.mark_in_progress(self.MOCK_USER, 1) conn = sqlite3.connect(constants.DB_PATH) cur = conn.cursor() resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] # userid useraddress score c1state c1depoyaddr c2state c2deployaddr c3state c3deployaddr c4state c4deployaddr self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished 1, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, # c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) conn.close()
def test_erase_challenge_deployed_address_from_db(self): with run.app.app_context(): util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) util.erase_challenge_deployed_address_from_db(self.MOCK_USER, 1) conn = sqlite3.connect(constants.DB_PATH) cur = conn.cursor() resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, #userid self.MOCK_USER, #useraddress 0, # score 0, # c0state None, # c0deployaddr 0, # c0finished 0, # c1state None, # c1deployaddr 0, # c1finished 0, #c2state None, # c2deployaddr 0, # c2finished 0, #c3state None, # c3deployaddr 0, # c3finished 0, #c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished )) conn.close()
def test_get_ranking_from_db(self): with run.app.app_context(): util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) util.mark_finished(self.MOCK_USER, "01_naive_programmer") util.write_address(self.VALID_ADDRESSES[0], 2, self.VALID_ADDRESSES[1]) util.mark_finished(self.VALID_ADDRESSES[0], "02_coinflip") util.write_address(self.VALID_ADDRESSES[0], 3, self.VALID_ADDRESSES[2]) util.mark_finished(self.VALID_ADDRESSES[0], "03_ERC20") util.write_address(self.VALID_ADDRESSES[3], 5, self.VALID_ADDRESSES[4]) util.mark_finished(self.VALID_ADDRESSES[3], "05_bad_ico") ranking = util.get_ranking_from_db() self.assertListEqual( [ (self.VALID_ADDRESSES[0], 6), (self.VALID_ADDRESSES[3], 3), (self.MOCK_USER, 1), ], ranking)
def test_malicious_mark_finished(self): with run.app.app_context(): conn = sqlite3.connect(constants.DB_PATH) cur = conn.cursor() util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 0, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_DEPLOYED_IN_PROGRESS, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 0, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, # c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_in_progress(self.MOCK_USER, 1) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 0, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_DEPLOYED_IN_PROGRESS, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 0, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_finished(self.MOCK_USER, "01_naive_programmer") resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_FINISHED, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_finished(self.MOCK_USER, "01_naive_programmer") resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_FINISHED, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.write_address(self.MOCK_USER, 1, self.VALID_ADDRESSES[1]) util.mark_in_progress(self.MOCK_USER, 1) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_DEPLOYED_IN_PROGRESS, # c1state self.VALID_ADDRESSES[1], # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_grading(self.MOCK_USER, 1) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_GRADING, # c1state self.VALID_ADDRESSES[1], # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_finished(self.MOCK_USER, "01_naive_programmer") resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_FINISHED, # c1state self.VALID_ADDRESSES[1], # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) conn.close()
def test_get_deployed_contract_address_for_challenge(self): with run.app.app_context(): util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) self.assertEqual(self.MOCK_CONTRACT_ADDRESS, util.get_deployed_contract_address_for_challenge(self.MOCK_USER, 1))
def done(address, contract): status = get_status(address, contract) if status[1] is not None: util.write_address(address, contract, status[1]) return status[0]