1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
"""Test the pyzor.account module"""
import io
import os
import time
import email
import hashlib
import unittest
import pyzor
import pyzor.config
import pyzor.account
class AccountTest(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
self.timestamp = 1381219396
self.msg = email.message_from_string("")
self.msg["Op"] = "ping"
self.msg["Thread"] = "14941"
self.msg["PV"] = "2.1"
self.msg["User"] = "anonymous"
self.msg["Time"] = str(self.timestamp)
def tearDown(self):
unittest.TestCase.tearDown(self)
def test_sign_msg(self):
"""Test the sign message function"""
hashed_key = hashlib.sha1(b"test_key").hexdigest()
expected = "2ab1bad2aae6fd80c656a896c82eef0ec1ec38a0"
result = pyzor.account.sign_msg(hashed_key, self.timestamp, self.msg)
self.assertEqual(result, expected)
def test_hash_key(self):
"""Test the hash key function"""
user = "testuser"
key = "testkey"
expected = "0957bd79b58263657127a39762879098286d8477"
result = pyzor.account.hash_key(key, user)
self.assertEqual(result, expected)
def test_verify_signature(self):
"""Test the verify signature function"""
def mock_sm(h, t, m):
return "testsig"
real_sm = pyzor.account.sign_msg
pyzor.account.sign_msg = mock_sm
try:
self.msg["Sig"] = "testsig"
del self.msg["Time"]
self.msg["Time"] = str(int(time.time()))
pyzor.account.verify_signature(self.msg, "testkey")
finally:
pyzor.account.sign_msg = real_sm
def test_verify_signature_old_timestamp(self):
"""Test the verify signature with old timestamp"""
def mock_sm(h, t, m):
return "testsig"
real_sm = pyzor.account.sign_msg
pyzor.account.sign_msg = mock_sm
try:
self.msg["Sig"] = "testsig"
self.assertRaises(
pyzor.SignatureError,
pyzor.account.verify_signature,
self.msg,
"testkey",
)
finally:
pyzor.account.sign_msg = real_sm
def test_verify_signature_bad_signature(self):
"""Test the verify signature with invalid signature"""
def mock_sm(h, t, m):
return "testsig"
real_sm = pyzor.account.sign_msg
pyzor.account.sign_msg = mock_sm
try:
self.msg["Sig"] = "testsig-bad"
del self.msg["Time"]
self.msg["Time"] = str(int(time.time()))
self.assertRaises(
pyzor.SignatureError,
pyzor.account.verify_signature,
self.msg,
"testkey",
)
finally:
pyzor.account.sign_msg = real_sm
class LoadAccountTest(unittest.TestCase):
"""Tests for the load_accounts function"""
filepath = "test_file"
def setUp(self):
unittest.TestCase.setUp(self)
self.real_exists = os.path.exists
os.path.exists = lambda p: True if p == self.filepath else self.real_exists(p)
self.mock_file = io.StringIO()
try:
self.real_open = pyzor.account.__builtins__.open
except AttributeError:
self.real_open = pyzor.account.__builtins__["open"]
def mock_open(path, mode="r", buffering=-1):
if path == self.filepath:
self.mock_file.seek(0)
return self.mock_file
else:
return self.real_open(path, mode, buffering)
try:
pyzor.account.__builtins__.open = mock_open
except AttributeError:
pyzor.account.__builtins__["open"] = mock_open
def tearDown(self):
unittest.TestCase.tearDown(self)
os.path.exists = self.real_exists
try:
pyzor.account.__builtins__.open = self.real_open
except AttributeError:
pyzor.account.__builtins__["open"] = self.real_open
def test_load_accounts_nothing(self):
try:
with self.assertLogs("pyzor", level="WARNING") as logs:
result = pyzor.config.load_accounts("foobar")
self.assertEqual(
[
"WARNING:pyzor:No accounts are setup. All commands will be executed by the anonymous user."
],
logs.output,
)
except AttributeError as e:
# Python 2 backwards compatibility.
self.assertEqual(
e.message, "'LoadAccountTest' object has no attribute 'assertLogs'"
)
result = pyzor.config.load_accounts("foobar")
self.assertEqual(result, {})
def test_load_accounts(self):
"""Test loading the account file"""
self.mock_file.write(
"public.pyzor.org : 24441 : test : 123abc,cba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321"
)
result = pyzor.config.load_accounts(self.filepath)
self.assertIn(("public.pyzor.org", 24441), result)
self.assertIn(("public2.pyzor.org", 24441), result)
account = result[("public.pyzor.org", 24441)]
self.assertEqual(
(account.username, account.salt, account.key), ("test", "123abc", "cba321")
)
account = result[("public2.pyzor.org", 24441)]
self.assertEqual(
(account.username, account.salt, account.key), ("test2", "123abc", "cba321")
)
def test_load_accounts_invalid_line(self):
"""Test loading the account file"""
self.mock_file.write(
"public.pyzor.org : 24441 ; test : 123abc,cba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321"
)
try:
with self.assertLogs("pyzor", level="WARNING") as logs:
result = pyzor.config.load_accounts(self.filepath)
self.assertEqual(
[
"WARNING:pyzor:account file: invalid line 0: wrong number of parts"
],
logs.output,
)
except AttributeError as e:
# Python 2 backwards compatibility.
self.assertEqual(
e.message, "'LoadAccountTest' object has no attribute 'assertLogs'"
)
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEqual(len(result), 1)
self.assertIn(("public2.pyzor.org", 24441), result)
account = result[("public2.pyzor.org", 24441)]
self.assertEqual(
(account.username, account.salt, account.key), ("test2", "123abc", "cba321")
)
def test_load_accounts_invalid_port(self):
"""Test loading the account file"""
self.mock_file.write(
"public.pyzor.org : a4441 : test : 123abc,cba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321"
)
try:
with self.assertLogs("pyzor", level="WARNING") as logs:
result = pyzor.config.load_accounts(self.filepath)
self.assertEqual(
[
"WARNING:pyzor:account file: invalid line 0: invalid literal for int() with base 10: 'a4441'"
],
logs.output,
)
except AttributeError as e:
# Python 2 backwards compatibility.
self.assertEqual(
e.message, "'LoadAccountTest' object has no attribute 'assertLogs'"
)
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEqual(len(result), 1)
self.assertIn(("public2.pyzor.org", 24441), result)
account = result[("public2.pyzor.org", 24441)]
self.assertEqual(
(account.username, account.salt, account.key), ("test2", "123abc", "cba321")
)
def test_load_accounts_invalid_key(self):
"""Test loading the account file"""
self.mock_file.write(
"public.pyzor.org : 24441 : test : ,\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321"
)
try:
with self.assertLogs("pyzor", level="WARNING") as logs:
result = pyzor.config.load_accounts(self.filepath)
self.assertEqual(
[
"WARNING:pyzor:account file: invalid line 0: keystuff can't be all None's"
],
logs.output,
)
except AttributeError as e:
# Python 2 backwards compatibility.
self.assertEqual(
e.message, "'LoadAccountTest' object has no attribute 'assertLogs'"
)
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEqual(len(result), 1)
self.assertIn(("public2.pyzor.org", 24441), result)
account = result[("public2.pyzor.org", 24441)]
self.assertEqual(
(account.username, account.salt, account.key), ("test2", "123abc", "cba321")
)
def test_load_accounts_invalid_missing_comma(self):
"""Test loading the account file"""
self.mock_file.write(
"public.pyzor.org : 24441 : test : 123abccba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321"
)
try:
with self.assertLogs("pyzor", level="WARNING") as logs:
result = pyzor.config.load_accounts(self.filepath)
self.assertEqual(
[
"WARNING:pyzor:account file: invalid line 0: Invalid number of parts for key; perhaps you forgot the comma at the beginning for the salt divider?"
],
logs.output,
)
except AttributeError as e:
# Python 2 backwards compatibility.
self.assertEqual(
e.message, "'LoadAccountTest' object has no attribute 'assertLogs'"
)
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEqual(len(result), 1)
self.assertIn(("public2.pyzor.org", 24441), result)
account = result[("public2.pyzor.org", 24441)]
self.assertEqual(
(account.username, account.salt, account.key), ("test2", "123abc", "cba321")
)
def test_load_accounts_comment(self):
"""Test skipping commented lines"""
self.mock_file.write("#public1.pyzor.org : 24441 : test : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertFalse(result)
def suite():
"""Gather all the tests from this module in a test suite."""
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(AccountTest))
test_suite.addTest(unittest.makeSuite(LoadAccountTest))
return test_suite
if __name__ == "__main__":
unittest.main()
|