def test_splunk_connect_errors(splunk_client): """Check connect failure errors.""" splunk_client.connect = cli_connect sp_driver = SplunkDriver() check.is_true(sp_driver.loaded) print("connected", sp_driver.connected) with pytest.raises(MsticpyConnectionError) as mp_ex: # [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Test code")] sp_driver.connect( host="AuthError", username="******", password=_FAKE_STRING ) # nosec print("connected", sp_driver.connected) check.is_false(sp_driver.connected) check.is_in("Splunk connection", mp_ex.value.args) sp_driver = SplunkDriver() print("connected", sp_driver.connected) with pytest.raises(MsticpyConnectionError) as mp_ex: # [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Test code")] sp_driver.connect( host="HTTPError", username="******", password=_FAKE_STRING ) # nosec print("connected", sp_driver.connected) check.is_false(sp_driver.connected) check.is_in("Splunk connection", mp_ex.value.args)
def test_methodId_invalid(api, db, case_data): """业务Id不存在""" url = '/gasStation/process' data_source_id = case_data.get('data_source_id') card_number = case_data.get('card_number') # 环境检查 if db.check_card(card_number): print(f'卡号: {card_number} 已存在') db.del_card(card_number) json_data = { "dataSourceId": data_source_id, "methodId": "05A", "CardInfo": { "cardNumber": card_number } } res_dict = api.post(url, json=json_data).json() # 响应断言 ck.equal(199, res_dict.get("code")) ck.equal("业务ID无效", res_dict.get("msg")) ck.is_false(res_dict.get('success')) # 数据库断言 ck.is_false(db.check_card(card_number)) # if __name__ == '__main__': # pytest.main(['test_add_fuel_card.py'])
def test_add_fuel_card_normal(api, db, case_data): """正常添加加油卡""" url = '/gasStation/process' data_source_id = case_data.get('data_source_id') card_number = case_data.get('card_number') # 环境检查 if db.check_card(card_number): pytest.skip(f'卡号: {card_number} 已存在') json_data = { "dataSourceId": data_source_id, "methodId": "00A", "CardInfo": { "cardNumber": card_number } } res_dict = api.post(url, json=json_data).json() # 响应断言 ck.equal(200, res_dict.get("code")) ck.equal("添加卡成功", res_dict.get("msg")) ck.is_false(res_dict.get('success')) # 数据库断言 ck.is_true(db.check_card(card_number)) # 环境清理 db.del_card(card_number)
def test_save_clips(): timestamps1 = [0, 5] timestamps2 = [4, 5] timestamps3 = [-1, 5] timestamps4 = [10, 5] timestamps5 = [1, -5] timestamps6 = [1, 1] check.is_false(example_job1.save_clips([])) with pytest.raises(Exception): example_job1.save_clips([timestamps3]) example_job1.save_clips([timestamps4]) example_job1.save_clips([timestamps5]) example_job1.save_clips([timestamps6]) check.is_true(example_job1.save_clips([timestamps1])) check.is_true(example_job1.save_clips([timestamps1, timestamps2])) path = os.path.splitext(example_job1.video_path) # form of filenames updated to match implementation check.is_true( os.path.isfile( path[0] + '_subclip(' + str(timestamps1[0]) + ',' + str(timestamps1[1]) + ')' + path[1])) check.is_true( os.path.isfile( path[0] + '_subclip(' + str(timestamps2[0]) + ',' + str(timestamps2[1]) + ')' + path[1]))
def test_dataSourceId_noAuthority(api, db, case_data): """第三方机构无权限""" url = '/gasStation/process' data_source_id = case_data.get('data_source_id') card_number = case_data.get('card_number') # 环境检查 if db.check_card(card_number): print(f'卡号: {card_number} 已存在') db.del_card(card_number) json_data = { "dataSourceId": data_source_id, "methodId": "00A", "CardInfo": { "cardNumber": card_number } } res_dict = api.post(url, json=json_data).json() # 响应断言 ck.equal(100, res_dict.get("code")) ck.equal("对不起,您的第三方机构无权限访问该接口", res_dict.get("msg")) ck.is_false(res_dict.get('success')) # 数据库断言 ck.is_false(db.check_card(card_number))
def test_mordor_load(mdr_driver: MordorDriver): """Check basic load of driver.""" check.is_true(mdr_driver.loaded) check.is_true(mdr_driver.connected) check.is_false(mdr_driver.use_query_paths) check.is_true(mdr_driver.has_driver_queries) check.is_instance(mdr_driver.mitre_techniques, pd.DataFrame) check.is_instance(mdr_driver.mitre_tactics, pd.DataFrame) check.is_in("T1078", mdr_driver.mitre_techniques.index) check.is_in("TA0001", mdr_driver.mitre_tactics.index) check.is_true(len(mdr_driver.mordor_data) > 50) _, first_item = next(iter(mdr_driver.mordor_data.items())) check.is_instance(first_item.title, str) check.is_instance(first_item.id, str) check.is_instance(first_item.author, str) check.is_instance(first_item.creation_date, datetime) check.is_instance(first_item.files, list) check.is_true(len(first_item.files) > 0) check.is_instance(first_item.attack_mappings, list) for attack in first_item.attack_mappings: check.is_in("technique", attack) check.is_in("tactics", attack)
def test_simple_flat(inplace): expect = np.ones((20, 20))*3 expect[0:5, 0:5] = 3/0.5 # Checking flat division: frame1 = FrameData(np.ones((20, 20))*3, unit=u.adu) master_flat_dimless = FrameData(np.ones((20, 20)), unit=None) master_flat_dimless.data[0:5, 0:5] = 0.5 res1 = flat_correct(frame1, master_flat_dimless, inplace=inplace) check.is_true(isinstance(res1, FrameData)) npt.assert_array_equal(res1.data, expect) check.equal(res1.header['hierarch astropop flat_corrected'], True) # # Checking flat-corrected frame unit: # check.equal(res1.unit, u.Unit('adu')) # Check inplace statement: if inplace: check.is_true(res1.data is frame1.data) else: check.is_false(res1.data is frame1.data)
def test_reset(self, mocker): """ should reset attr if reset is True. """ logging_callback_path = 'learnrl.callbacks.logging_callback.LoggingCallback' mocker.patch(logging_callback_path + '._get_attr_name', return_value="target_name") mocker.patch(logging_callback_path + '._reset_attr') mocker.patch(logging_callback_path + '._get_value', return_value="value") mocker.patch(logging_callback_path + '._update_attr') self.logging_callback._update_metrics(self.metric_list, 'target_prefix', source_prefix='source_prefix', logs='logs', agent_id='agent_id', reset=True) for args, _ in self.logging_callback._get_attr_name.call_args_list: check.equal(args[0], 'target_prefix') check.equal(args[2], 'agent_id') for args, _ in self.logging_callback._get_value.call_args_list: check.equal(args[1], 'source_prefix') check.equal(args[2], 'agent_id') check.equal(args[3], 'logs') for args, _ in self.logging_callback._reset_attr.call_args_list: check.equal(args[0], 'target_name') check.is_false(self.logging_callback._update_attr.called)
def test_InsufficientBalance(api, db, case_data): """加油卡消费,余额不足""" url = '/gasStation/process' data_source_id = case_data.get('data_source_id') card_number = case_data.get('card_number') user_id = case_data.get('user_id') cardBalance = case_data.get('card_balance') # 环境检查 balance = db.check_cardBalance(card_number) if balance > cardBalance: balance = db.set_cardBalance(card_number, cardBalance) json_data = { "dataSourceId": data_source_id, "methodId": "04A", "CardUser": { "userId": user_id }, "CardInfo": { "cardNumber": card_number, "cardBalance": cardBalance } } res_dict = api.post(url, json=json_data).json() # 响应断言 ck.equal(200, res_dict.get("code")) ck.equal("对不起,您的余额不足,请充值!", res_dict.get("msg")) ck.is_false(res_dict.get('success'))
def test_azure_sentinel_editor(mp_conf_ctrl): """Items edit controls.""" edit_comp = CEAzureSentinel(mp_controls=mp_conf_ctrl) n_opts = len(edit_comp.select_item.options) edit_comp.edit_buttons.btn_add.click() check.equal(n_opts + 1, len(edit_comp.select_item.options)) new_ws = edit_comp.current_workspace result, _ = _validate_ws(new_ws, mp_conf_ctrl, edit_comp._COMP_PATH) check.is_false(result) edit_comp.edit_ctrls.children[1].value = "40dcc8bf-0478-4f3b-b275-ed0a94f2c013" edit_comp.edit_ctrls.children[2].value = "40dcc8bf-0478-4f3b-b275-ed0a94f2c013" edit_comp.edit_buttons.btn_save.click() result, _ = _validate_ws(new_ws, mp_conf_ctrl, edit_comp._COMP_PATH) check.is_true(result) # Save the current item edit_comp.edit_buttons.btn_save.click() check.is_not_none(mp_conf_ctrl.get_value(f"{edit_comp._COMP_PATH}.{new_ws}")) # Rename edit_comp.edit_ctrls.children[0].value = "TestWS" edit_comp.edit_buttons.btn_save.click() ren_workspace_settings = mp_conf_ctrl.get_value(f"{edit_comp._COMP_PATH}.TestWS") check.is_not_none(ren_workspace_settings) edit_comp.btn_set_default.click() def_ws = mp_conf_ctrl.get_value(f"{edit_comp._COMP_PATH}.Default") check.equal(def_ws, ren_workspace_settings)
def test_reward_shaping_all_useful(self, mocker: MockerFixture): """should give achivement value for every item in solving option unrolled graph.""" TaskObtainItem( world=self.dummy_world, item=self.dummy_item, reward_shaping=RewardShaping.ALL_USEFUL, shaping_value=self.shaping_value, ) is_called = {item.item_id: False for item in self.dummy_items} expected_called_items = [item.item_id for item in self.dummy_items[2:]] check.equal( self.add_achivement_mocker.call_args_list[0].args, (self.dummy_item, 10), ) check.equal( self.add_achivement_mocker.call_args_list[0].kwargs, {"end_task": True} ) for call in self.add_achivement_mocker.call_args_list[1:]: item_called: Item = call.args[0] is_called[item_called.item_id] = True check.equal(call.args[1], self.shaping_value) for item in self.dummy_items: item_is_called = is_called[item.item_id] if item.item_id in expected_called_items: check.is_true(item_is_called, f"{item} was not called when expected.") else: check.is_false(item_is_called, f"{item} was called when not expected.")
def test_enable_disable_memmap(tmpdir): f = os.path.join(tmpdir, 'npn_empty.npy') arr = [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]] a = MemMapArray(arr, filename=f, dtype=None, memmap=False) check.is_false(a.memmap) check.is_false(os.path.exists(f)) a.enable_memmap() check.is_true(a.memmap) check.is_true(os.path.exists(f)) check.is_instance(a._contained, np.memmap) # First keep the file a.disable_memmap(remove=False) check.is_false(a.memmap) check.is_true(os.path.exists(f)) check.is_not_instance(a._contained, np.memmap) a.enable_memmap() check.is_true(a.memmap) check.is_true(os.path.exists(f)) check.is_instance(a._contained, np.memmap) # Remove the file a.disable_memmap(remove=True) check.is_false(a.memmap) check.is_false(os.path.exists(f)) check.is_not_instance(a._contained, np.memmap) with pytest.raises(ValueError): # raises error if name is locked a.enable_memmap('not_the_same_name.npy')
def test_reset_data(tmpdir): d1 = np.array([[1, 2], [3, 4]]).astype('float32') m = MemMapArray(d1, os.path.join(tmpdir, 'reset.npy'), dtype='float64', memmap=True) check.is_true(np.issubdtype(m.dtype, np.float64)) npt.assert_array_equal(m, d1) check.is_false(m.empty) check.is_true(m.memmap) m.reset_data(d1) check.is_true(np.issubdtype(m.dtype, np.float32)) npt.assert_array_equal(m, d1) check.is_false(m.empty) check.is_true(m.memmap) m.reset_data(d1.astype('int16')) check.is_true(np.issubdtype(m.dtype, np.int16)) npt.assert_array_equal(m, d1) check.is_false(m.empty) check.is_true(m.memmap) m.reset_data(None) check.is_true(m.empty) check.is_true(m._contained is None) check.is_true(m.memmap) m.disable_memmap() m.reset_data(np.ones((10, 10)), dtype='float32') check.is_true(np.issubdtype(m.dtype, np.float32)) npt.assert_array_equal(m, np.ones((10, 10))) check.is_false(m.empty) check.is_false(m.memmap)
def test_splunk_saved_searches(splunk_client): """Check saved searches.""" splunk_client.connect = cli_connect sp_driver = SplunkDriver() # trying to get these before connecting should throw with pytest.raises(MsticpyNotConnectedError) as mp_ex: sp_driver._get_saved_searches() check.is_false(sp_driver.connected) check.is_none(sp_driver._saved_searches) check.is_in("not connected to Splunk.", mp_ex.value.args) # [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Test code")] sp_driver.connect(host="localhost", username="******", password=_FAKE_STRING) # nosec check.is_true(sp_driver.connected) check.is_instance(sp_driver._saved_searches, pd.DataFrame) for _, search in sp_driver._saved_searches.iterrows(): check.is_true(search["name"].startswith("query")) check.equal(search["query"], "get stuff from somewhere") queries, name = sp_driver.service_queries check.equal(name, "SavedSearches") check.is_instance(queries, dict) for name, query in queries.items(): check.is_true(name.startswith("query")) check.equal(query, "search get stuff from somewhere")
def test_live_connect(): """Use this to do live testing.""" sp_driver = SplunkDriver() www = "splunk-mstic.westus2.cloudapp.azure.com" sp_driver.connect(host=www, port=8089, username="******", password="******") # nosec query = """index="botsv2" earliest=08/25/2017:00:00:00 latest=08/26/2017:00:00:00 source="WinEventLog:Microsoft-Windows-Sysmon/Operational" | table TimeCreated, host, EventID, EventDescription, User, process | head 10 """ res_df = sp_driver.query(query) check.is_not_none(res_df) query0 = """index="botsv2" earliest=08/25/2020:00:00:00 + 'source="WinEventLog:Microsoft-Windows-Sysmon/Operational" | table TimeCreated, host, EventID, EventDescription, User, process | head 10 """ res_df = sp_driver.query(query0) check.is_instance(res_df, list) check.is_false(res_df) query1 = """ index=blackhat sourcetype=network earliest=0 | table TimeGenerated, TotalBytesSent """ res_df = sp_driver.query(query1) check.is_not_none(res_df)
def test_noCardConsumption(api, db, case_data): """加油卡号不存在""" url = '/gasStation/process' data_source_id = case_data.get('data_source_id') card_number = case_data.get('card_number') user_id = case_data.get('user_id') cardBalance = case_data.get('card_balance') # 环境检查 if db.check_card(card_number): print(f'卡号: {card_number} 已存在') db.del_card(card_number) json_data = { "dataSourceId": data_source_id, "methodId": "04A", "CardUser": { "userId": user_id }, "CardInfo": { "cardNumber": card_number, "cardBalance": cardBalance } } res_dict = api.post(url, json=json_data).json() # 响应断言 ck.equal(5013, res_dict.get("code")) ck.equal("根据用户ID没有查询到卡号!", res_dict.get("msg")) ck.is_false(res_dict.get('success')) # if __name__ == '__main__': # pytest.main(['test_consumption_fuel_card.py'])
def test_bind_fuel_card_norma9(api, data, db): """每个用户只能绑定两张卡""" request_data = data.get('test_bind_fuel_card_norma9') #环境检查 card_number1 = '8448646' card_number2 = '98893' card_name = 'zhangchao1' bind_card = [(card_number1, card_name), (card_number2, card_name)] card_number = request_data.get('json').get('CardInfo').get('cardNumber') for bind_data in bind_card: db.bind_card(*bind_data) if not db.check_card(card_number): db.add_card(card_number) else: db.reset_card(card_number) res_dict = api.request_all(request_data).json() print(f'响应数据{res_dict}') # 响应断言 ck.equal(5014, res_dict.get("code")) ck.equal("每个用户只能绑定两张卡", res_dict.get("msg")) ck.is_false(res_dict.get('success')) #环境清理 db.del_card(card_number1) db.del_card(card_number1)
def test_noCardRecharge(api, db, case_data): """卡不存在,充值""" url = '/gasStation/process' data_source_id = case_data.get('data_source_id') card_number = case_data.get('card_number') cardBalance = case_data.get('card_balance') # 环境检查 if db.check_card(card_number): print(f'卡号: {card_number} 已存在') db.del_card(card_number) json_data = { "dataSourceId": data_source_id, "methodId": "03A", "CardInfo": { "cardNumber": card_number, "cardBalance": cardBalance } } res_dict = api.post(url, json=json_data).json() # 响应断言 ck.equal(5013, res_dict.get("code")) ck.equal("加油卡号不存在", res_dict.get("msg")) ck.is_false(res_dict.get("success"))
def test_extract_header_nowcs(): header = fits.Header.fromstring(_base_header, sep='\n') h, wcs = extract_header_wcs(header) check.is_none(wcs) check.is_instance(h, fits.Header) check.equal(h, header) check.is_false(h is header)
def test_missing_pkgs_req(): """Check for packages used in code but not in requirements.txt.""" mod_imports = analyze_imports(package_root=PKG_ROOT, package_name=PKG_NAME, req_file=REQS_FILE) import_errs = {v for s in mod_imports.values() for v in s.unknown} print("re module path:", re.__file__) print("Import errors:\n", import_errs) paths = {str(Path(p).resolve()) for p in sys.path} stdlib_paths = { p for p in paths if p.lower().startswith(sys.prefix.lower()) and "site-packages" not in p } print("sys.path", sys.path) print("paths", paths) print("sys.prefix", sys.prefix) print("Stdlib paths:\b", stdlib_paths) missing_req_mod = { f"{req}:{mod}" for mod, reqs in mod_imports.items() for req in reqs.missing_reqs } missing_reqs = { req.strip() for reqs in mod_imports.values() for req in reqs.missing_reqs } missing_reqs = missing_reqs - EXTRAS_EXCEPTIONS if missing_reqs: print( "Missing packages:\n", "\n".join(req for req in missing_req_mod if req.split(":")[0] in missing_reqs), ) check.is_false(missing_reqs)
def test_splunk_saved_searches(splunk_client): """Check saved searches.""" splunk_client.connect = cli_connect sp_driver = SplunkDriver() # trying to get these before connecting should throw with pytest.raises(MsticpyNotConnectedError) as mp_ex: sp_driver._get_saved_searches() check.is_false(sp_driver.connected) check.is_none(sp_driver._saved_searches) check.is_in("not connected to Splunk.", mp_ex.value.args) sp_driver.connect(host="localhost", username="******", password="******") # nosec check.is_true(sp_driver.connected) check.is_instance(sp_driver._saved_searches, pd.DataFrame) for _, search in sp_driver._saved_searches.iterrows(): check.is_true(search["name"].startswith("query")) check.equal(search["query"], "get stuff from somewhere") queries, name = sp_driver.service_queries check.equal(name, "SavedSearches") check.is_instance(queries, dict) for name, query in queries.items(): check.is_true(name.startswith("query")) check.equal(query, "search get stuff from somewhere")
def test_query_fuel_card_user_not_exist(db, api, data): user_id = data['USER_ID_NOT_EXIST'] card_number = 'hzc_123456' res = api.query_fuel_card(user_id, card_number) check.equal(400, res['code']) check.equal('无查询信息', res['msg']) check.is_false(res['success'])
def test_test(self, mocker): """ test should call run with learn=False and render=True. """ mocker.patch('learnrl.playground.Playground.run') playground = Playground(self.env, self.agents) playground.test(10) _, kwargs = playground.run.call_args check.is_false(kwargs.get('learn')) check.is_true(kwargs.get('render'))
def test_bind_fuel_card_3times(db, api, data): user_name, id_number = data['USER_WITH_TWO_CARDS'] # 用户已绑定2张卡 card_number = data['FREE_CARD'] res = api.bind_fuel_card(user_name, id_number, card_number) check.equal(5014, res['code']) check.equal('每个用户只能绑定两张卡', res['msg']) check.is_false(res['success'])
def test_extract_invalid_wcs_header(): # It should no raise, just return empty wcs # No header change too header = fits.Header.fromstring(_base_header + _invalid_wcs, sep='\n') h, wcs = extract_header_wcs(header) check.is_none(wcs) check.is_instance(h, fits.Header) check.equal(h, header) check.is_false(h is header)
def test_process_list(): def dummy_func(i): return 1 a = np.zeros(20) b = np.ones(20) c = process_list(dummy_func, a) npt.assert_array_equal(b, c) check.is_false(np.array_equal(a, c))
def test_add_fuel_card_exist(db, api, data): card_number = data['FREE_CARD_NUMBERS'][0] db.add_card_if_not_exist(card_number) res = api.add_fuel_card(card_number) check.equal(5000, res['code']) check.equal('该卡已添加', res['msg']) check.is_false(res['success']) # bug
def test_query_fuel_card_norma2(api,data): '''无查询信息''' request_data = data.get('test_query_fuel_card_norma2') res_dict = api.request_all(request_data).json() print(f'响应数据{res_dict}') # 响应断言 ck.equal(400, res_dict.get("code")) ck.equal("无查询信息", res_dict.get("msg")) ck.is_false(res_dict.get('success'))
def test_kql_query_not_connected(get_ipython): """Check loaded true.""" get_ipython.return_value = _MockIPython() kql_driver = KqlDriver() with pytest.raises(MsticpyNotConnectedError) as mp_ex: kql_driver.query("test") check.is_in("not connected to a workspace.", mp_ex.value.args) check.is_false(kql_driver.connected)
def test_kql_connect_authn_exceptions(get_ipython): """Check loaded true.""" get_ipython.return_value = _MockIPython() kql_driver = KqlDriver() with pytest.raises(MsticpyKqlConnectionError) as mp_ex: kql_driver.connect(connection_str="la://connection+AuthenticationError") check.is_in("authentication failed", mp_ex.value.args) check.is_false(kql_driver.connected)