From: Jelte Jansen Date: Fri, 18 Jan 2013 14:09:37 +0000 (+0100) Subject: [2595] Update __try_login in bindcmd and add test X-Git-Tag: bind10-1.0.0-rc-release~76^2~8^2~4^2~1^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55b05119c73ef4858995efd2377c16e17f8a0f06;p=thirdparty%2Fkea.git [2595] Update __try_login in bindcmd and add test --- diff --git a/src/bin/bindctl/bindcmd.py b/src/bin/bindctl/bindcmd.py index 2bf8f4730c..f6727d7d08 100644 --- a/src/bin/bindctl/bindcmd.py +++ b/src/bin/bindctl/bindcmd.py @@ -209,9 +209,19 @@ WARNING: Python readline module isn't available, so the command line editor return True def __try_login(self, username, password): + ''' + Attempts to log in to bindctl by sending a POST with + the given username and password. + On success of the POST (mind, not the login, only the network + operation), returns a tuple (response, data). + On failure, raises a FailToLogin exception, and prints some + information on the failure. + ''' param = {'username': username, 'password' : password} try: - return self.send_POST('/login', param) + response = self.send_POST('/login', param) + data = response.read().decode() + return (response, data) except socket.error as err: print("Socket error while sending login information: ", err) if err.errno == errno.ECONNRESET: @@ -230,8 +240,8 @@ WARNING: Python readline module isn't available, so the command line editor # Look at existing username/password combinations and try to log in users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME) for row in users: - response = self.__try_login(row[0], row[1]) - data = response.read().decode() + response, data = self.__try_login(row[0], row[1]) + if response.status == http.client.OK: # Is interactive? if sys.stdin.isatty(): @@ -252,8 +262,7 @@ WARNING: Python readline module isn't available, so the command line editor username = input("Username: ") passwd = getpass.getpass() - response = self.__try_login(username, passwd) - data = response.read().decode() + response, data = self.__try_login(username, passwd) print(data) if response.status == http.client.OK: diff --git a/src/bin/bindctl/tests/bindctl_test.py b/src/bin/bindctl/tests/bindctl_test.py index 5c6aeb2804..a7e920552b 100644 --- a/src/bin/bindctl/tests/bindctl_test.py +++ b/src/bin/bindctl/tests/bindctl_test.py @@ -347,6 +347,34 @@ class TestConfigCommands(unittest.TestCase): precmd('EOF') self.assertRaises(socket.error, precmd, 'continue') + def test_try_login(self): + # Make sure __try_login raises the correct exception + # upon failure of either send_POST or the read() on the + # response + + orig_send_POST = self.tool.send_POST + try: + def send_POST_raiseImmediately(self, params): + raise socket.error("test error") + + self.tool.send_POST = send_POST_raiseImmediately + self.assertRaises(FailToLogin, + self.tool._BindCmdInterpreter__try_login, + "foo", "bar") + + def send_POST_raiseOnRead(self, params): + class MyResponse: + def read(self): + raise socket.error("read error") + return MyResponse() + + self.tool.send_POST = send_POST_raiseOnRead + self.assertRaises(FailToLogin, + self.tool._BindCmdInterpreter__try_login, + "foo", "bar") + finally: + self.tool.send_POST = orig_send_POST + def test_run(self): def login_to_cmdctl(): return True