def test_status(self): from pysvn import wc_status_kind as wcsk client = self.client # Mock pysvnstatus objects smock = self.mock() smock.path self.setReturn('/some/path/foo') smock.text_status self.setReturn(wcsk.modified) smock2 = self.mock() smock2.path self.setReturn('/some/path/foo2') smock2.text_status self.setReturn(wcsk.conflicted) smock3 = self.mock() smock3.path self.setReturn('/some/path/foo3') smock3.text_status self.setReturn('some_weird_status') status_files = [smock, smock2, smock3] method(client._svnclient, 'status').expects(LOCAL_PATH, recurse=False) self.returns(status_files) ## Stop recording - Replay ## self.replay() expected_res = \ SVNFilesList([ ('/some/path/foo', ST_MODIFIED), ('/some/path/foo2', ST_CONFLICT), ('/some/path/foo3', ST_UNKNOWN)]) self.assertEquals(expected_res, client.status()) ## Verify ## self.verify()
def test_main(): """Hook components together""" args = ["unused_program_name", "u1"] subject = RSReader() subject.aggregate_feed = mock() subject.feed_writer = mock() method(subject.aggregate_feed, 'from_urls').expects(["u1"]) method(subject.feed_writer, 'print_entry_listings').\ expects(subject.aggregate_feed) replay() subject.main(args) verify()
def test_upd(self): client = self.client override(pysvn, "Revision", self.mock()) pysvnhead = pysvn.Revision(pysvn.opt_revision_kind.head) method(client._svnclient, "update").expects(LOCAL_PATH, revision=pysvnhead, depth=INF).returns([self.rev]) override(client, "_filter_files").expects(client.UPD_ACTIONS).returns(self.upd_files) ## Stop recording. Play! self.replay() self.assertEquals(self.upd_files, client.update(rev=None)) ## Verify ## self.verify()
def test_print_agg_feed_listing_is_printed(): """Should print listing of feed entries""" unsorted_entries = [ FeedEntry(xkcd_feed, xkcd_items[1]), FeedEntry(xkcd_feed, xkcd_items[0]) ] aggregate_feed = AggregateFeed() aggregate_feed.entries = unsorted_entries override(sys, 'stdout') method(sys.stdout, 'write').expects(xkcd_output + os.linesep) replay() FeedWriter().print_entry_listings(aggregate_feed) verify()
def test_upd_fail(self): override(pysvn, 'Revision', self.mock()) pysvnhead = pysvn.Revision(pysvn.opt_revision_kind.head) from core.controllers.auto_update.auto_update import SVNUpdateError client = self.client method(client._svnclient, 'update').expects(LOCAL_PATH, revision=pysvnhead) self.raises(pysvn.ClientError('file locked')) ## Stop recording. Play! self.replay() self.assertRaises(SVNUpdateError, client.update) ## Verify ## self.verify()
def test_upd_fail(self): override(pysvn, "Revision", self.mock()) pysvnhead = pysvn.Revision(pysvn.opt_revision_kind.head) client = self.client method(client._svnclient, "update").expects(LOCAL_PATH, revision=pysvnhead, depth=INF).raises( pysvn.ClientError("file locked") ) ## Stop recording. Play! self.replay() self.assertRaises(SVNUpdateError, client.update) ## Verify ## self.verify()
def test_timeout(self): """ Ensure that kah raises 'URLTimeoutError' when timeouts occur and raises 'w3afMustStopException' when the timeout limit is reached. """ kah = self.kahdler host = self.host conn = self.conn req = self.req ## Start recording ## method(req, 'get_host').expects().returns(host) method(req, 'get_host').expects().returns(host) # Override KeepAliveHandler._start_transaction - raises timeout override(kah, '_start_transaction').expects(conn, req).raises(socket.timeout) conn_factory = kah._get_connection; # The connection mgr conn_mgr = self.mock() method(conn_mgr, 'get_available_connection').expects(host, conn_factory).returns(conn) method(conn_mgr, 'remove_connection').expects(conn, host).returns(None) ## Stop Recording.Time to Play! ## self.replay() # Replace with mocked out ConnMgr. kah._cm = conn_mgr self.assertRaises(URLTimeoutError, kah.do_open, req) self.assertRaises(w3afMustStopException, kah.do_open, req) ## Verify ## self.verify()
def test_get_and_remove_conn(self): ''' Each requested connection must be closed by calling 'remove_connection' when the server doesn't support persistent HTTP Connections ''' kah = self.kahdler host = self.host conn = self.conn req = self.req ## Start recording ## method(req, 'get_host').expects().returns(host) method(req, 'get_full_url').expects().returns('test_full_url') # Override KeepAliveHandler._start_transaction override(kah, '_start_transaction').expects(conn, req).returns(None) conn_factory = kah._get_connection # Mock conn's getresponse() resp = HTTPResponse(socket.socket()) resp.will_close = True override(conn, 'getresponse').expects().returns(resp) # The connection mgr conn_mgr_mock = self.mock() method(conn_mgr_mock, 'get_available_connection').expects(host, conn_factory).returns(conn) method(conn_mgr_mock, 'remove_connection').expects(conn, host).returns(None) ## Stop Recording.Time to Play! ## self.replay() # Replace with mocked out ConnMgr. kah._cm = conn_mgr_mock kah.do_open(req) ## Verify ## self.verify()
def test_working_copy(self): cli = self.mock() override(pysvn, 'Client').expects().returns(cli) # NOT A WORKING COPY method(cli, 'status').expects(LOCAL_PATH, recurse=False).raises(Exception()) self.replay() self.assertFalse(w3afSVNClient.is_working_copy(LOCAL_PATH)) # IS A WORKING COPY self.reset() cli = self.mock() override(pysvn, 'Client').expects().returns(cli) method(cli, 'status').expects(LOCAL_PATH, recurse=False).returns(1) self.replay() self.assertTrue(w3afSVNClient.is_working_copy(LOCAL_PATH))
def test_discover_eq_routes(self): # Start recording plugininst = self.plugininst override(plugininst, '_has_permission').expects().returns(True) method(self.fuzz_req, 'getURL').expects().returns(self.test_url) # HTTPS try self._call_traceroute('host.tld', 443, self.tracedict) # HTTP try self._call_traceroute('host.tld', 80, self.tracedict) # Output Manager. It must not be called! ommock = self.mock(hvshsdist.om) ommock.out set_count(exactly=0) ## Stop Recording.Time to Play! ## self.replay() res = plugininst.discover(self.fuzz_req) self.assertEquals(res, []) ## Verify ## self.verify()
def test_discover_override_port(self): plugininst = self.plugininst override(plugininst, '_has_permission').expects().returns(True) method(self.fuzz_req, 'getURL').expects().returns('https://host.tld:4444') # HTTPS try self._call_traceroute('host.tld', 4444, self.tracedict) # HTTP try tracedict = copy.deepcopy(self.tracedict) tracedict['localhost'][3] = ('200.200.0.0', False) # Set diff hop self._call_traceroute('host.tld', 80, tracedict) # Mock output manager. Ensure that is called with the proper desc. override(hvshsdist.om.out, 'information').expects(expr(lambda x: x.find('host.tld:4444') != -1)) set_count(exactly=1) ## Stop Recording.Time to Play! ## self.replay() res = plugininst.discover(self.fuzz_req) self.assertEquals(res, []) ## Verify ## self.verify()
def test_output_plugins_actions(self): '''Call all actions on output plugins''' msg = '<< SOME OUTPUT MESS@GE!! <<' for action in TestOutputManager.OUTPUT_PLUGIN_ACTIONS: plugin = self.mock() self.om._outputPluginList = [plugin] pluginaction = getattr(self.om, action) defvals = inspect.getargspec(pluginaction)[3] method(plugin, action).expects(msg, *defvals) ## Stop recording. Play! self.replay() # Invoke action pluginaction(msg, True) # Verify and reset self.verify() self.reset()
def test_output_plugins_actions_with_unicode_message(self): '''Call all actions on output plugins using a unicode message''' msg = u'<< ÑñçÇyruZZ!! <<' utf8_encoded_msg = msg.encode('utf8') for action in TestOutputManager.OUTPUT_PLUGIN_ACTIONS: plugin = self.mock() self.om._outputPluginList = [plugin] pluginaction = getattr(self.om, action) actiondefvals = inspect.getargspec(pluginaction)[3] method(plugin, action).expects(utf8_encoded_msg, *actiondefvals) ## Stop recording. Play! self.replay() # Invoke action pluginaction(msg, True) # Verify and reset self.verify() self.reset()
def test_get_svnversion_with_non_svn_path(self): os = get_autoupdate_os_module() override(os, "walk").expects(dontcare()).generates(*([("x", "y", "z")] * 3)) cli = self.cli Rev = TestSVNVersion.Rev method(cli, "info").expects(dontcare()).returns({"revision": Rev(22)}) method(cli, "info").expects(dontcare()).returns({"revision": Rev(23)}) # If at least a 2-level depth non svn subdirectory is # found the pysvn client raises an exception method(cli, "info").expects(dontcare()).raises(pysvn.ClientError) ## Stop recording - Replay ## self.replay() self.assertEquals("22:23", get_svnversion(W3AF_LOCAL_PATH)) ## Verify ## self.verify()
def _call_traceroute(self, dest, dport, trace_resp): # Mocks scapy 'traceroute' function https_tracerout_obj = self.mock() method(https_tracerout_obj, 'get_trace').expects().returns(trace_resp) resp_tuple = (https_tracerout_obj, None) override(hvshsdist, 'traceroute').expects(dest, dport=dport).returns(resp_tuple)