2018-04-16 20:19:30 +02:00
|
|
|
"""
|
|
|
|
Tests using API.
|
|
|
|
"""
|
|
|
|
|
2018-04-10 17:31:44 +02:00
|
|
|
import base64
|
|
|
|
import json
|
2019-07-22 10:06:19 +02:00
|
|
|
import time
|
2018-05-08 16:39:07 +02:00
|
|
|
import xmlrpclib # nosec
|
2018-04-10 17:31:44 +02:00
|
|
|
|
2019-07-22 10:06:19 +02:00
|
|
|
from test_process import TestProcessProto, TestProcessShutdown
|
2018-04-12 16:29:22 +02:00
|
|
|
|
|
|
|
|
2019-07-22 10:06:19 +02:00
|
|
|
class TestAPIProto(TestProcessProto):
|
|
|
|
"""Test case logic for testing API"""
|
2018-04-12 16:29:22 +02:00
|
|
|
_process_cmd = ['pybitmessage', '-t']
|
2018-04-10 17:31:44 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2018-04-16 20:19:30 +02:00
|
|
|
"""Setup XMLRPC proxy for pybitmessage API"""
|
2019-07-22 10:06:19 +02:00
|
|
|
super(TestAPIProto, cls).setUpClass()
|
2018-04-10 17:31:44 +02:00
|
|
|
cls.addresses = []
|
|
|
|
cls.api = xmlrpclib.ServerProxy(
|
|
|
|
"http://username:password@127.0.0.1:8442/")
|
2019-07-22 10:06:19 +02:00
|
|
|
for _ in range(5):
|
2018-04-12 16:29:22 +02:00
|
|
|
if cls._get_readline('.api_started'):
|
2018-04-10 17:31:44 +02:00
|
|
|
return
|
2019-07-22 10:06:19 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
class TestAPIShutdown(TestAPIProto, TestProcessShutdown):
|
|
|
|
"""Separate test case for API command 'shutdown'"""
|
|
|
|
def test_shutdown(self):
|
|
|
|
"""Shutdown the pybitmessage"""
|
2019-09-26 13:26:55 +02:00
|
|
|
self.assertEqual(self.api.shutdown(), 'done')
|
2019-07-22 10:06:19 +02:00
|
|
|
for _ in range(5):
|
|
|
|
if not self.process.is_running():
|
|
|
|
break
|
|
|
|
time.sleep(2)
|
|
|
|
else:
|
|
|
|
self.fail(
|
|
|
|
'%s has not stopped in 10 sec' % ' '.join(self._process_cmd))
|
|
|
|
|
|
|
|
|
2019-11-11 16:31:16 +01:00
|
|
|
# TODO: uncovered API commands
|
|
|
|
# getAllInboxMessages
|
|
|
|
# getAllInboxMessageIds
|
|
|
|
# getInboxMessageById
|
|
|
|
# getInboxMessagesByReceiver
|
|
|
|
# trashMessage
|
|
|
|
# trashInboxMessage
|
|
|
|
# addSubscription
|
|
|
|
# disseminatePreEncryptedMsg
|
|
|
|
# disseminatePubkey
|
|
|
|
# getMessageDataByDestinationHash
|
|
|
|
# statusBar
|
|
|
|
|
2019-07-22 10:06:19 +02:00
|
|
|
class TestAPI(TestAPIProto):
|
|
|
|
"""Main API test case"""
|
|
|
|
_seed = base64.encodestring(
|
|
|
|
'TIGER, tiger, burning bright. In the forests of the night'
|
|
|
|
)
|
2018-04-10 17:31:44 +02:00
|
|
|
|
2018-04-11 17:55:43 +02:00
|
|
|
def _add_random_address(self, label):
|
|
|
|
return self.api.createRandomAddress(base64.encodestring(label))
|
|
|
|
|
2018-04-10 17:31:44 +02:00
|
|
|
def test_user_password(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""Trying to connect with wrong username/password"""
|
2018-04-10 17:31:44 +02:00
|
|
|
api_wrong = xmlrpclib.ServerProxy("http://test:wrong@127.0.0.1:8442/")
|
2018-10-29 17:18:35 +01:00
|
|
|
with self.assertRaises(xmlrpclib.ProtocolError):
|
|
|
|
api_wrong.clientStatus()
|
2018-04-10 17:31:44 +02:00
|
|
|
|
|
|
|
def test_connection(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""API command 'helloWorld'"""
|
2018-04-10 17:31:44 +02:00
|
|
|
self.assertEqual(
|
|
|
|
self.api.helloWorld('hello', 'world'),
|
|
|
|
'hello-world'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_arithmetic(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""API command 'add'"""
|
2018-04-10 17:31:44 +02:00
|
|
|
self.assertEqual(self.api.add(69, 42), 111)
|
|
|
|
|
|
|
|
def test_invalid_method(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""Issuing nonexistent command 'test'"""
|
2018-04-10 17:31:44 +02:00
|
|
|
self.assertEqual(
|
|
|
|
self.api.test(),
|
|
|
|
'API Error 0020: Invalid method: test'
|
|
|
|
)
|
|
|
|
|
2018-11-01 15:05:37 +01:00
|
|
|
def test_clientstatus_consistency(self):
|
|
|
|
"""If networkStatus is notConnected networkConnections should be 0"""
|
|
|
|
status = json.loads(self.api.clientStatus())
|
|
|
|
if status["networkStatus"] == "notConnected":
|
|
|
|
self.assertEqual(status["networkConnections"], 0)
|
|
|
|
else:
|
|
|
|
self.assertGreater(status["networkConnections"], 0)
|
|
|
|
|
2018-04-10 17:31:44 +02:00
|
|
|
def test_list_addresses(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""Checking the return of API command 'listAddresses'"""
|
2018-04-10 17:31:44 +02:00
|
|
|
self.assertEqual(
|
|
|
|
json.loads(self.api.listAddresses()).get('addresses'),
|
|
|
|
self.addresses
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_decode_address(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""Checking the return of API command 'decodeAddress'"""
|
2018-04-10 17:31:44 +02:00
|
|
|
result = json.loads(
|
|
|
|
self.api.decodeAddress('BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'))
|
|
|
|
self.assertEqual(result.get('status'), 'success')
|
|
|
|
self.assertEqual(result['addressVersion'], 4)
|
|
|
|
self.assertEqual(result['streamNumber'], 1)
|
|
|
|
|
|
|
|
def test_create_deterministic_addresses(self):
|
2019-11-11 16:31:16 +01:00
|
|
|
"""Test creation of deterministic addresses"""
|
2018-04-10 17:31:44 +02:00
|
|
|
self.assertEqual(
|
2018-04-12 17:49:20 +02:00
|
|
|
self.api.getDeterministicAddress(self._seed, 4, 1),
|
2019-11-11 16:31:16 +01:00
|
|
|
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
|
2018-04-10 17:31:44 +02:00
|
|
|
self.assertEqual(
|
2018-04-12 17:49:20 +02:00
|
|
|
self.api.getDeterministicAddress(self._seed, 3, 1),
|
2019-11-11 16:31:16 +01:00
|
|
|
'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN')
|
2018-04-12 12:34:37 +02:00
|
|
|
self.assertRegexpMatches(
|
2018-04-12 17:49:20 +02:00
|
|
|
self.api.getDeterministicAddress(self._seed, 2, 1),
|
2019-11-11 16:31:16 +01:00
|
|
|
r'^API Error 0002:')
|
|
|
|
|
2018-04-12 12:34:37 +02:00
|
|
|
# This is here until the streams will be implemented
|
|
|
|
self.assertRegexpMatches(
|
2018-04-12 17:49:20 +02:00
|
|
|
self.api.getDeterministicAddress(self._seed, 3, 2),
|
2019-11-11 16:31:16 +01:00
|
|
|
r'API Error 0003:')
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
self.api.createDeterministicAddresses(self._seed, 1, 4, 2),
|
|
|
|
r'API Error 0003:')
|
|
|
|
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
self.api.createDeterministicAddresses('', 1),
|
|
|
|
r'API Error 0001:')
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
self.api.createDeterministicAddresses(self._seed, 1, 2),
|
|
|
|
r'API Error 0002:')
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
self.api.createDeterministicAddresses(self._seed, 0),
|
|
|
|
r'API Error 0004:')
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
self.api.createDeterministicAddresses(self._seed, 1000),
|
|
|
|
r'API Error 0005:')
|
|
|
|
|
|
|
|
addresses = json.loads(
|
|
|
|
self.api.createDeterministicAddresses(self._seed, 2, 4)
|
|
|
|
)['addresses']
|
|
|
|
self.assertEqual(len(addresses), 2)
|
|
|
|
self.assertEqual(addresses[0], 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
|
|
|
|
for addr in addresses:
|
|
|
|
self.assertEqual(self.api.deleteAddress(addr), 'success')
|
2018-04-10 17:31:44 +02:00
|
|
|
|
2018-04-11 17:55:43 +02:00
|
|
|
def test_create_random_address(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""API command 'createRandomAddress': basic BM-address validation"""
|
2018-04-11 17:55:43 +02:00
|
|
|
addr = self._add_random_address('random_1')
|
|
|
|
self.assertRegexpMatches(addr, r'^BM-')
|
|
|
|
self.assertRegexpMatches(addr[3:], r'[a-zA-Z1-9]+$')
|
2018-08-02 16:15:05 +02:00
|
|
|
# Whitepaper says "around 36 character"
|
|
|
|
self.assertLessEqual(len(addr[3:]), 40)
|
2018-04-11 17:55:43 +02:00
|
|
|
self.assertEqual(self.api.deleteAddress(addr), 'success')
|
2018-04-10 17:31:44 +02:00
|
|
|
|
|
|
|
def test_addressbook(self):
|
2018-04-12 12:34:37 +02:00
|
|
|
"""Testing API commands for addressbook manipulations"""
|
2018-04-10 17:31:44 +02:00
|
|
|
# Initially it's empty
|
|
|
|
self.assertEqual(
|
|
|
|
json.loads(self.api.listAddressBookEntries()).get('addresses'),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
# Add known address
|
|
|
|
self.api.addAddressBookEntry(
|
|
|
|
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK',
|
|
|
|
base64.encodestring('tiger_4')
|
|
|
|
)
|
|
|
|
# Check addressbook entry
|
|
|
|
entries = json.loads(
|
|
|
|
self.api.listAddressBookEntries()).get('addresses')[0]
|
|
|
|
self.assertEqual(
|
|
|
|
entries['address'], 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
|
|
|
|
self.assertEqual(
|
|
|
|
base64.decodestring(entries['label']), 'tiger_4')
|
|
|
|
# Remove known address
|
|
|
|
self.api.deleteAddressBookEntry(
|
|
|
|
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
|
|
|
|
# Addressbook should be empty again
|
|
|
|
self.assertEqual(
|
|
|
|
json.loads(self.api.listAddressBookEntries()).get('addresses'),
|
|
|
|
[]
|
|
|
|
)
|
2018-04-11 17:55:43 +02:00
|
|
|
|
2019-11-11 16:31:16 +01:00
|
|
|
def test_subscriptions(self):
|
|
|
|
"""Testing the API commands related to subscriptions"""
|
|
|
|
for s in json.loads(self.api.listSubscriptions())['subscriptions']:
|
|
|
|
# special address, added when sqlThread starts
|
|
|
|
if s['address'] == 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw':
|
|
|
|
self.assertEqual(
|
|
|
|
base64.decodestring(s['label']),
|
|
|
|
'Bitmessage new releases/announcements')
|
|
|
|
self.assertTrue(s['enabled'])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail(
|
|
|
|
'Could not find Bitmessage new releases/announcements'
|
|
|
|
' in subscriptions')
|
|
|
|
self.assertEqual(
|
|
|
|
self.api.deleteSubscription('BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'),
|
|
|
|
'Deleted subscription if it existed.')
|
|
|
|
self.assertEqual(
|
|
|
|
json.loads(self.api.listSubscriptions())['subscriptions'], [])
|
|
|
|
|
|
|
|
def test_send(self):
|
|
|
|
"""Test message sending"""
|
|
|
|
# self.api.createDeterministicAddresses(self._seed, 1, 4)
|
2018-04-12 12:34:37 +02:00
|
|
|
addr = self._add_random_address('random_2')
|
2019-02-13 11:25:28 +01:00
|
|
|
msg = base64.encodestring('test message')
|
2019-11-11 16:31:16 +01:00
|
|
|
msg_subject = base64.encodestring('test_subject')
|
|
|
|
ackdata = self.api.sendMessage(
|
|
|
|
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK', addr, msg_subject, msg)
|
|
|
|
try:
|
|
|
|
# Check ackdata and message status
|
|
|
|
int(ackdata, 16)
|
|
|
|
status = self.api.getStatus(ackdata)
|
|
|
|
if status == 'notfound':
|
|
|
|
raise KeyError
|
|
|
|
self.assertIn(
|
|
|
|
status, (
|
|
|
|
'msgqueued', 'awaitingpubkey', 'msgsent', 'ackreceived',
|
|
|
|
'doingpubkeypow', 'doingmsgpow', 'msgsentnoackexpected'
|
|
|
|
))
|
|
|
|
# Find the message in sent
|
|
|
|
for m in json.loads(
|
|
|
|
self.api.getSentMessagesByAddress(addr))['sentMessages']:
|
|
|
|
if m['ackData'] == ackdata:
|
|
|
|
sent_msg = m['message']
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise KeyError
|
|
|
|
# Find the message in inbox
|
|
|
|
# for m in json.loads(
|
|
|
|
# self.api.getInboxMessagesByReceiver(
|
|
|
|
# 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'))['inboxMessages']:
|
|
|
|
# if m['subject'] == msg_subject:
|
|
|
|
# inbox_msg = m['message']
|
|
|
|
# break
|
|
|
|
except ValueError:
|
|
|
|
self.fail('sendMessage returned error or ackData is not hex')
|
|
|
|
except KeyError:
|
|
|
|
self.fail('Could not find sent message in sent messages')
|
|
|
|
else:
|
|
|
|
# Check found message
|
|
|
|
try:
|
|
|
|
self.assertEqual(sent_msg, msg.strip())
|
|
|
|
except UnboundLocalError:
|
|
|
|
self.fail('Could not find sent message in sent messages')
|
|
|
|
# self.assertEqual(inbox_msg, msg.strip())
|
|
|
|
self.assertEqual(json.loads(
|
|
|
|
self.api.getSentMessageByAckData(ackdata)
|
|
|
|
)['sentMessage'][0]['message'], sent_msg)
|
|
|
|
# Trash the message
|
|
|
|
self.assertEqual(
|
|
|
|
self.api.trashSentMessageByAckData(ackdata),
|
|
|
|
'Trashed sent message (assuming message existed).')
|
|
|
|
# Empty trash
|
|
|
|
self.assertEqual(self.api.deleteAndVacuum(), 'done')
|
|
|
|
# The message should disappear
|
|
|
|
self.assertIsNone(json.loads(
|
|
|
|
self.api.getSentMessageByAckData(ackdata)))
|
|
|
|
finally:
|
|
|
|
self.assertEqual(self.api.deleteAddress(addr), 'success')
|
|
|
|
|
|
|
|
def test_send_broadcast(self):
|
|
|
|
"""Test broadcast sending"""
|
|
|
|
addr = self._add_random_address('random_2')
|
|
|
|
msg = base64.encodestring('test broadcast')
|
2019-02-13 11:25:28 +01:00
|
|
|
ackdata = self.api.sendBroadcast(
|
|
|
|
addr, base64.encodestring('test_subject'), msg)
|
2018-04-12 12:34:37 +02:00
|
|
|
try:
|
2019-02-13 11:25:28 +01:00
|
|
|
int(ackdata, 16)
|
|
|
|
status = self.api.getStatus(ackdata)
|
|
|
|
if status == 'notfound':
|
|
|
|
raise KeyError
|
|
|
|
self.assertIn(
|
|
|
|
status, ('broadcastqueued', 'broadcastsent', 'doingmsgpow'))
|
2019-11-11 16:31:16 +01:00
|
|
|
# Find the message and its ID in sent
|
2019-02-13 11:25:28 +01:00
|
|
|
for m in json.loads(self.api.getAllSentMessages())['sentMessages']:
|
|
|
|
if m['ackData'] == ackdata:
|
|
|
|
sent_msg = m['message']
|
2019-11-11 16:31:16 +01:00
|
|
|
sent_msgid = m['msgid']
|
2019-02-13 11:25:28 +01:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise KeyError
|
2018-04-12 12:34:37 +02:00
|
|
|
except ValueError:
|
|
|
|
self.fail('sendBroadcast returned error or ackData is not hex')
|
2019-02-13 11:25:28 +01:00
|
|
|
except KeyError:
|
|
|
|
self.fail('Could not find sent broadcast in sent messages')
|
|
|
|
else:
|
2019-11-11 16:31:16 +01:00
|
|
|
# Check found message and its ID
|
|
|
|
try:
|
|
|
|
self.assertEqual(sent_msg, msg.strip())
|
|
|
|
except UnboundLocalError:
|
|
|
|
self.fail('Could not find sent message in sent messages')
|
|
|
|
self.assertEqual(json.loads(
|
|
|
|
self.api.getSentMessageById(sent_msgid)
|
|
|
|
)['sentMessage'][0]['message'], sent_msg)
|
|
|
|
self.assertIn(
|
|
|
|
{'msgid': sent_msgid}, json.loads(
|
|
|
|
self.api.getAllSentMessageIds())['sentMessageIds'])
|
|
|
|
# Trash the message by ID
|
|
|
|
self.assertEqual(
|
|
|
|
self.api.trashSentMessage(sent_msgid),
|
|
|
|
'Trashed sent message (assuming message existed).')
|
|
|
|
self.assertEqual(self.api.deleteAndVacuum(), 'done')
|
|
|
|
self.assertIsNone(json.loads(
|
|
|
|
self.api.getSentMessageById(sent_msgid)))
|
2018-04-12 12:34:37 +02:00
|
|
|
finally:
|
|
|
|
self.assertEqual(self.api.deleteAddress(addr), 'success')
|
2018-04-12 17:49:20 +02:00
|
|
|
|
|
|
|
def test_chan(self):
|
|
|
|
"""Testing chan creation/joining"""
|
2019-11-11 16:31:16 +01:00
|
|
|
# Create chan with known address
|
2018-04-12 17:49:20 +02:00
|
|
|
self.assertEqual(
|
|
|
|
self.api.createChan(self._seed),
|
|
|
|
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'
|
|
|
|
)
|
|
|
|
# cleanup
|
|
|
|
self.assertEqual(
|
|
|
|
self.api.leaveChan('BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'),
|
|
|
|
'success'
|
|
|
|
)
|
|
|
|
# Join chan with addresses of version 3 or 4
|
|
|
|
for addr in (
|
2019-11-11 16:31:16 +01:00
|
|
|
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK',
|
|
|
|
'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN'
|
2018-04-12 17:49:20 +02:00
|
|
|
):
|
|
|
|
self.assertEqual(self.api.joinChan(self._seed, addr), 'success')
|
|
|
|
self.assertEqual(self.api.leaveChan(addr), 'success')
|
|
|
|
# Joining with wrong address should fail
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
self.api.joinChan(self._seed, 'BM-2cWzSnwjJ7yRP3nLEW'),
|
|
|
|
r'^API Error 0008:'
|
|
|
|
)
|