class TestIconZipDeploy(unittest.TestCase): _ROOT_SCORE_PATH = 'tests/score' _TEST_DB_PATH = 'tests/test_db' _ZERO_SCORE_ID = bytes(DEFAULT_BYTE_SIZE) @classmethod def setUpClass(cls): db_path = os.path.join(TEST_ROOT_PATH, cls._TEST_DB_PATH) ContextDatabaseFactory.open( db_path, ContextDatabaseFactory.Mode.SINGLE_DB) @classmethod def tearDownClass(cls): ContextDatabaseFactory.close() def setUp(self): db_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) score_path = os.path.join(TEST_ROOT_PATH, self._ROOT_SCORE_PATH) self._tx_index = 0 self.__ensure_dir(db_path) self._icx_db = ContextDatabaseFactory.create_by_name('icx_db') self._icx_db.address = ICX_ENGINE_ADDRESS self._icx_storage = IcxStorage(self._icx_db) self._icon_deploy_storage = IconScoreDeployStorage(self._icx_db) self._engine = IconScoreDeployEngine() self._icon_score_loader = IconScoreLoader(score_path, 0) IconScoreMapper.icon_score_loader = self._icon_score_loader IconScoreMapper.deploy_storage = self._icon_deploy_storage self._icon_score_mapper = IconScoreMapper() IconScoreContext.icon_score_manager = Mock(spec=IconScoreManager) self._engine.open( score_root_path=score_path, flag=0, icon_deploy_storage=self._icon_deploy_storage) self.from_address = create_address(AddressPrefix.EOA) self.sample_token_address = create_address(AddressPrefix.CONTRACT) self._factory = IconScoreContextFactory(max_size=1) self.make_context() self._one_icx = 1 * 10 ** 18 self._one_icx_to_token = 1 def tearDown(self): ContextContainer._clear_context() def make_context(self): self._tx_index += 1 self._context = self._factory.create(IconScoreContextType.DIRECT) self._context.msg = Message(self.from_address, 0) tx_hash = create_tx_hash() self._context.new_icon_score_mapper = IconScoreMapper() self._context.tx = Transaction(tx_hash, origin=self.from_address) self._context.block = Block(1, create_block_hash(), 0, None) self._context.icon_score_mapper = self._icon_score_mapper self._context.icx = IcxEngine() self._context.icx.open(self._icx_storage) ContextContainer._push_context(self._context) self._context.validate_deployer = Mock() self._context.validate_score_blacklist = Mock() def tearDown(self): self._engine = None ContextContainer._pop_context() self._icon_score_mapper.close() self._factory.destroy(self._context) remove_path = os.path.join(TEST_ROOT_PATH, 'tests') IconScoreDeployer.remove_existing_score(remove_path) remove_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) IconScoreDeployer.remove_existing_score(remove_path) remove_path = os.path.join( TEST_ROOT_PATH, self.sample_token_address.to_bytes().hex()) IconScoreDeployer.remove_existing_score(remove_path) @staticmethod def __ensure_dir(dir_path): if not os.path.exists(dir_path): os.makedirs(dir_path) @staticmethod def read_zipfile_as_byte(archive_path: str) -> bytes: with open(archive_path, 'rb') as f: byte_data = f.read() return byte_data def test_deploy(self): content: bytes = self.read_zipfile_as_byte( os.path.join(TEST_ROOT_PATH, 'sample', 'valid.zip')) data = { "contentType": "application/zip", "content": f'0x{bytes.hex(content)}' } self._icon_deploy_storage.get_next_tx_hash = Mock(return_value=self._ZERO_SCORE_ID) self._engine.invoke( self._context, ZERO_SCORE_ADDRESS, self.sample_token_address, data) self.assertTrue( os.path.join( TEST_ROOT_PATH, self.sample_token_address.to_bytes().hex()))
class TestIconZipDeploy(unittest.TestCase): _SCORE_ROOT_PATH = 'tests/score' _TEST_DB_PATH = 'tests/test_db' @classmethod def setUpClass(cls): db_path = os.path.join(TEST_ROOT_PATH, cls._TEST_DB_PATH) ContextDatabaseFactory.open(db_path, ContextDatabaseFactory.Mode.SINGLE_DB) @classmethod def tearDownClass(cls): ContextDatabaseFactory.close() def setUp(self): db_path: str = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) # score_root_path: str = os.path.join(TEST_ROOT_PATH, self._SCORE_ROOT_PATH) self._tx_index = 0 self.__ensure_dir(db_path) self._icx_db = ContextDatabaseFactory.create_by_name('icx_db') self._icx_db.address = ICX_ENGINE_ADDRESS self._icx_storage = IcxStorage(self._icx_db) self._icon_deploy_storage = DeployStorage(self._icx_db) self._icon_score_mapper = IconScoreMapper() IconScoreContextUtil.validate_score_blacklist = Mock() IconScoreContextUtil.get_owner = Mock() IconScoreContextUtil.get_icon_score = Mock() IconScoreContextUtil.is_service_flag_on = Mock() self.from_address = create_address(AddressPrefix.EOA) self._engine = DeployEngine() IconScoreContext.storage = ContextStorage( deploy=self._icon_deploy_storage, fee=None, icx=None, iiss=None, prep=None, issue=None, rc=None, meta=None) self.make_context() self._icon_deploy_storage.open(self._context) self._engine.open(self._icon_deploy_storage) self._one_icx = 1 * 10**18 self._one_icx_to_token = 1 def make_context(self): self._tx_index += 1 self._context = IconScoreContext(IconScoreContextType.DIRECT) self._context.msg = Message(self.from_address, 0) tx_hash = create_tx_hash() self._context.new_icon_score_mapper = IconScoreMapper() self._context.tx = Transaction(tx_hash, origin=self.from_address, timestamp=randrange(1, 1000), nonce=randrange(1, 1000)) self._context.block = Block(1, create_block_hash(), 0, None, 0) self._context.icon_score_mapper = self._icon_score_mapper self._context.icx = IcxEngine() self._context.icx.open(self._icx_storage) ContextContainer._push_context(self._context) self._context.validate_score_blacklist = Mock() self._context.is_service_flag_on = Mock(return_value=False) def tearDown(self): self._engine = None ContextContainer._pop_context() self._icon_score_mapper.close() path = os.path.join(TEST_ROOT_PATH, 'tests') remove_path(path) path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) remove_path(path) IconScoreContextUtil.validate_score_blacklist = VALIDATE_SCORE_BLACK_LIST IconScoreContextUtil.get_owner = GET_OWNER IconScoreContextUtil.get_icon_score = GET_ICON_SCORE IconScoreContextUtil.is_service_flag_on = IS_SERVICE_FLAG_ON @staticmethod def __ensure_dir(dir_path): if not os.path.exists(dir_path): os.makedirs(dir_path) @staticmethod def read_zipfile_as_byte(archive_path: str) -> bytes: with open(archive_path, 'rb') as f: byte_data = f.read() return byte_data def test_deploy(self): content: bytes = self.read_zipfile_as_byte( os.path.join(TEST_ROOT_PATH, 'legacy_unittest/sample', 'normal_score.zip')) data = { "contentType": "application/zip", "content": f'0x{bytes.hex(content)}' } self._icon_deploy_storage.get_next_tx_hash = Mock( return_value=ZERO_TX_HASH) self._context.step_counter = Mock() sample_token_address = self._engine.invoke(self._context, SYSTEM_SCORE_ADDRESS, data) deploy_info = self._context.storage.deploy.get_deploy_info( self._context, sample_token_address) self.assertEqual(DeployState.INACTIVE, deploy_info.deploy_state) self.assertEqual(sample_token_address, deploy_info.score_address) self.assertEqual(self._context.tx.origin, deploy_info.owner) self.assertEqual(self._context.tx.hash, deploy_info.next_tx_hash) self.assertEqual(ZERO_TX_HASH, deploy_info.current_tx_hash)