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
class TestIconScoreMapper(unittest.TestCase): def setUp(self): IconScoreMapper.icon_score_loader = Mock(spec=IconScoreLoader) IconScoreMapper.deploy_storage = Mock(spec=IconScoreDeployStorage) self.context = Mock(spec=IconScoreContext) self.icon_score_mapper = IconScoreMapper() def tearDown(self): pass def test_get_icon_score_score_success(self): tx_hash = create_tx_hash() self.icon_score_mapper.load_score = Mock(return_value=TestScore()) self.icon_score_mapper.get_icon_score( create_address(AddressPrefix.CONTRACT), tx_hash)
def test_get_score_api(self, mocked_icon_score_engine_validate_score_blacklist, mocked_score_context_util_get_icon_score): context = IconScoreContext(IconScoreContextType.INVOKE) context.new_icon_score_mapper = IconScoreMapper() # failure case: should raise error if there is no SCORE score_address = create_address(AddressPrefix.CONTRACT) mocked_score_context_util_get_icon_score.return_value = None self.assertRaises(ScoreNotFoundException, IconScoreEngine.get_score_api, context, score_address) mocked_icon_score_engine_validate_score_blacklist.assert_called_with( context, score_address) # reset mock mocked_icon_score_engine_validate_score_blacklist.reset_mock( return_value=None) # success case: if SCORE exists, getattr(score, "__get_api") method should be called score_object = Mock(spec=IconScoreBase) mocked_score_context_util_get_icon_score.return_value = score_object IconScoreEngine.get_score_api(context, score_address) mocked_icon_score_engine_validate_score_blacklist.assert_called_with( context, score_address) get_api = getattr(score_object, ATTR_SCORE_GET_API) get_api.assert_called()
def setUp(self): db_path = os.path.join(PROJECT_ROOT_PATH, self._TEST_DB_PATH) score_path = os.path.join(PROJECT_ROOT_PATH, self._ROOT_SCORE_PATH) self.score_path = score_path self._tx_index = 0 self.__ensure_dir(db_path) self._icx_db = ContextDatabaseFactory.create_by_name(ICON_DEX_DB_NAME) self._icx_db.address = ICX_ENGINE_ADDRESS self._icx_storage = IcxStorage(self._icx_db) self._score_deploy_engine = isde.Engine() self._deploy_storage = DeployStorage(self._icx_db) self._icon_score_mapper = IconScoreMapper() self.addr1 = create_address(AddressPrefix.EOA) self.addr2 = create_address(AddressPrefix.EOA) self.score_address = create_address(AddressPrefix.CONTRACT) self._score_deploy_engine.open() IconScoreContext.storage = ContextStorage( deploy=self._deploy_storage, fee=None, icx=None, iiss=None, prep=None, issue=None, rc=None, meta=None, ) self.make_context()
def test_call(self, mocked_score_engine_get_icon_score, mocked_score_engine_convert_score_params_by_annotations): context = IconScoreContext(IconScoreContextType.INVOKE) context.new_icon_score_mapper = IconScoreMapper() def intercept_score_base_call(func_name: str, kw_params: dict): self.assertEqual(func_name, 'score_method') # should be equal to converted_params self.assertEqual(kw_params, converted_params) return "__call method called" score_address = Mock(spec=Address) score_object = Mock(spec=IconScoreBase) mocked_score_engine_get_icon_score.return_value = score_object primitive_params = {"address": str(create_address(AddressPrefix.EOA)), "integer": "0x10"} converted_params = {"address": create_address(AddressPrefix.EOA), "integer": 10} mocked_score_engine_convert_score_params_by_annotations.return_value = converted_params context.set_func_type_by_icon_score = Mock() setattr(score_object, ATTR_SCORE_CALL, Mock(side_effect=intercept_score_base_call)) # set method, and params, method cannot be None as pre-validate it data = {'method': 'score_method', 'params': primitive_params} result = IconScoreEngine._call(context, score_address, data) self.assertEqual(result, "__call method called") IconScoreEngine._get_icon_score.assert_called() IconScoreEngine._convert_score_params_by_annotations.assert_called() context.set_func_type_by_icon_score.assert_called() call = getattr(score_object, ATTR_SCORE_CALL) call.assert_called()
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.sample_token_address = create_address(AddressPrefix.CONTRACT) self.make_context() self._engine = DeployEngine() self._engine.open(self._icon_deploy_storage) IconScoreContext.storage = ContextStorage( deploy=Mock(DeployStorage), fee=None, icx=None, iiss=None, prep=None, issue=None, rc=None, meta=None ) self._one_icx = 1 * 10 ** 18 self._one_icx_to_token = 1
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 setUp(self): rmtree(self._ROOT_SCORE_PATH) rmtree(self._TEST_DB_PATH) archive_path = 'tests/sample/valid.zip' archive_path = os.path.join(TEST_ROOT_PATH, archive_path) zip_bytes = self.read_zipfile_as_byte(archive_path) install_path = os.path.join(TEST_ROOT_PATH, self._ROOT_SCORE_PATH) self.__unpack_zip_file(install_path, zip_bytes) db_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) ContextDatabaseFactory.open( db_path, ContextDatabaseFactory.Mode.SINGLE_DB) self.__ensure_dir(db_path) icx_db = ContextDatabaseFactory.create_by_name('icx_db') self.icx_storage = IcxStorage(icx_db) deploy_storage = IconScoreDeployStorage(self.icx_storage.db) deploy_engine = IconScoreDeployEngine() deploy_engine.open(self._ROOT_SCORE_PATH, 0, deploy_storage) IconScoreContext.icon_score_manager = IconScoreManager(deploy_engine) self.icon_score_loader = IconScoreLoader(self._ROOT_SCORE_PATH, 0) IconScoreMapper.icon_score_loader = self.icon_score_loader IconScoreMapper.deploy_storage = deploy_storage self.icon_score_mapper = IconScoreMapper() self.engine = IconScoreEngine() self.engine.open( self.icx_storage, self.icon_score_mapper) self._from = create_address(AddressPrefix.EOA) self._icon_score_address = create_address(AddressPrefix.CONTRACT) self.factory = IconScoreContextFactory(max_size=1) IconScoreContext.icon_score_mapper = self.icon_score_mapper self._context = self.factory.create(IconScoreContextType.DIRECT) self._context.msg = Message(self._from, 0) tx_hash = create_tx_hash() self._context.tx = Transaction( tx_hash, origin=create_address(AddressPrefix.EOA)) block_hash = create_block_hash() self._context.block = Block(1, block_hash, 0, None)
def context(): ctx = IconScoreContext(IconScoreContextType.DIRECT) ctx.tx = Transaction(tx_hash=create_tx_hash(), origin=EOA1) ctx.block = Block(block_height=0, block_hash=create_block_hash(), timestamp=0, prev_hash=None) ctx.msg = Message(sender=EOA1, value=0) ctx.icon_score_mapper = IconScoreMapper() ctx.new_icon_score_mapper = {} ctx.step_counter = IconScoreStepCounter(1, {}, 1000, False) ctx.event_logs = [] ctx.traces = [] ctx.current_address = EOA1 IconScoreContext.storage = ContextStorage(deploy=Mock(spec=Storage), fee=None, icx=None, iiss=None, prep=None, issue=None, meta=None, rc=None, inv=None) ContextContainer._push_context(ctx) yield ctx ContextContainer._pop_context()
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 setUp(self): rmtree(self._SCORE_ROOT_PATH) rmtree(self._TEST_DB_PATH) archive_path = 'tests/sample/normal_score.zip' archive_path = os.path.join(TEST_ROOT_PATH, archive_path) zip_bytes = self.read_zipfile_as_byte(archive_path) install_path = os.path.join(TEST_ROOT_PATH, self._SCORE_ROOT_PATH) self.__unpack_zip_file(install_path, zip_bytes) db_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) ContextDatabaseFactory.open(db_path, ContextDatabaseFactory.Mode.SINGLE_DB) self.__ensure_dir(db_path) icx_db = ContextDatabaseFactory.create_by_name('icx_db') self.icx_storage = IcxStorage(icx_db) deploy_storage = IconScoreDeployStorage(self.icx_storage.db) deploy_engine = IconScoreDeployEngine() deploy_engine.open(deploy_storage) IconScoreClassLoader.init(self._SCORE_ROOT_PATH) IconScoreMapper.deploy_storage = deploy_storage self.icon_score_mapper = IconScoreMapper() self.engine = IconScoreEngine() # Use mock to prevent an exception from IconScoreEngine._validate_score_blacklist(). IconScoreEngine._validate_score_blacklist = mock.Mock() self._from = create_address(AddressPrefix.EOA) self._icon_score_address = create_address(AddressPrefix.CONTRACT) IconScoreContext.icon_score_deploy_engine = deploy_engine self._context = IconScoreContext(IconScoreContextType.DIRECT) self._context.msg = Message(self._from, 0) tx_hash = create_tx_hash() self._context.tx = Transaction(tx_hash, origin=create_address( AddressPrefix.EOA)) block_hash = create_block_hash() self._context.block = Block(1, block_hash, 0, None)
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(ICON_DEX_DB_NAME) self._icx_db.address = ICX_ENGINE_ADDRESS self._icx_storage = IcxStorage(self._icx_db) self._score_deploy_engine = IconScoreDeployEngine() self._deploy_storage = IconScoreDeployStorage(self._icx_db) self._icon_score_loader = IconScoreClassLoader(score_path) self._icon_score_mapper = IconScoreMapper() self._addr1 = create_address(AddressPrefix.EOA) self._score_deploy_engine.open( score_root_path=score_path, score_deploy_storage=self._deploy_storage) self.make_context()
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()))
def setUp(self): IconScoreMapper.icon_score_class_loader = Mock( spec=IconScoreClassLoader) IconScoreMapper.deploy_storage = Mock(spec=DeployStorage) self.context = Mock(spec=IconScoreContext) self.icon_score_mapper = IconScoreMapper()
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)