2020-10-15 14:08:38 +00:00
|
|
|
import json
|
2020-10-15 17:48:48 +00:00
|
|
|
import os
|
2020-10-15 14:08:38 +00:00
|
|
|
import ssl
|
2020-10-15 17:07:17 +00:00
|
|
|
import threading
|
|
|
|
import time
|
2020-10-15 14:08:38 +00:00
|
|
|
import unittest
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
|
|
|
from torch_sub import torch_sub
|
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
broker_hostname = "mqtt.example.com"
|
|
|
|
broker_port = 8883
|
2020-10-15 21:35:49 +00:00
|
|
|
agent_config_path = "test/agent-config/"
|
2020-10-15 17:07:17 +00:00
|
|
|
mqtt_ca_file = agent_config_path + "ca.crt"
|
|
|
|
mqtt_cert_file = agent_config_path + "vagrant.crt"
|
|
|
|
mqtt_key_file = agent_config_path + "vagrant.key"
|
|
|
|
|
2020-10-15 21:35:49 +00:00
|
|
|
subscriber_config_path = "test/subscriber-config/"
|
2020-10-15 17:07:17 +00:00
|
|
|
subscriber_ca_file = subscriber_config_path + "ca.crt"
|
|
|
|
subscriber_cert_file = subscriber_config_path + "subscriber.crt"
|
|
|
|
subscriber_key_file = subscriber_config_path + "subscriber.key"
|
2020-10-15 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def agent_connect():
|
|
|
|
client = mqtt.Client()
|
|
|
|
client.tls_set(
|
2020-10-15 14:44:34 +00:00
|
|
|
ca_certs=mqtt_ca_file,
|
|
|
|
certfile=mqtt_cert_file,
|
|
|
|
keyfile=mqtt_key_file,
|
2020-10-15 14:08:38 +00:00
|
|
|
cert_reqs=ssl.CERT_REQUIRED)
|
2020-10-15 17:48:48 +00:00
|
|
|
client.connect(broker_hostname, broker_port, 60)
|
2020-10-15 14:08:38 +00:00
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
def agent_publish(client_id, onion_hostname):
|
2020-10-15 17:48:48 +00:00
|
|
|
|
2020-10-15 14:08:38 +00:00
|
|
|
payload = {
|
|
|
|
'clientId': client_id,
|
|
|
|
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
|
|
|
|
'onionAddress': onion_hostname,
|
|
|
|
'sshPort': 22
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
time.sleep(0.2)
|
2020-10-15 14:08:38 +00:00
|
|
|
client = agent_connect()
|
2020-10-15 17:48:48 +00:00
|
|
|
client.publish("torch/" + client_id + "/wake", json.dumps(payload))
|
2020-10-15 14:08:38 +00:00
|
|
|
client.disconnect()
|
2020-10-15 17:48:48 +00:00
|
|
|
time.sleep(0.2)
|
2020-10-15 14:08:38 +00:00
|
|
|
|
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
class GivenBrokerAndTorchAgent(unittest.TestCase):
|
2020-10-15 14:08:38 +00:00
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
def setUp(self) -> None:
|
|
|
|
if os.path.exists(torch_sub.database_file):
|
|
|
|
os.remove(torch_sub.database_file)
|
2020-10-15 17:07:17 +00:00
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
def tearDown(self) -> None:
|
|
|
|
if os.path.exists(torch_sub.database_file):
|
|
|
|
os.remove(torch_sub.database_file)
|
2020-10-15 17:07:17 +00:00
|
|
|
|
2020-10-15 14:44:34 +00:00
|
|
|
def test_when_agent_publishes_should_get_hostname_from_subscriber(self):
|
2020-10-15 17:48:48 +00:00
|
|
|
self.run_subscriber()
|
|
|
|
agent_publish("client1", "crazy_onion.onion")
|
|
|
|
database = self.loadDatabase()
|
|
|
|
self.assertEqual(database['client1']['onionAddress'], "crazy_onion.onion")
|
|
|
|
|
|
|
|
def test_when_agent_publishes_should_get_hostname_from_subscriber2(self):
|
|
|
|
self.run_subscriber()
|
|
|
|
agent_publish("client2", "crazy_onion2.onion")
|
|
|
|
database = self.loadDatabase()
|
|
|
|
self.assertEqual(database['client2']['onionAddress'], "crazy_onion2.onion")
|
|
|
|
|
2020-10-15 22:01:39 +00:00
|
|
|
def test_when_agent_publishes_multiple_hosts_should_provide_latest(self):
|
|
|
|
self.run_subscriber()
|
|
|
|
agent_publish("client2", "crazy_onion2-34.onion")
|
|
|
|
agent_publish("client3", "crazy_onion3.onion")
|
|
|
|
agent_publish("client1", "crazy_onion1.onion")
|
|
|
|
agent_publish("client2", "crazy_onion2-56.onion")
|
|
|
|
agent_publish("client3", "crazy_onion3.onion")
|
|
|
|
database = self.loadDatabase()
|
|
|
|
self.assertEqual(database['client1']['onionAddress'], "crazy_onion1.onion")
|
|
|
|
self.assertEqual(database['client2']['onionAddress'], "crazy_onion2-56.onion")
|
|
|
|
self.assertEqual(database['client3']['onionAddress'], "crazy_onion3.onion")
|
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
@staticmethod
|
|
|
|
def run_subscriber():
|
|
|
|
threading.Thread(target=torch_sub.subscribe,
|
|
|
|
args=(broker_hostname,
|
|
|
|
broker_port,
|
2020-10-15 17:07:17 +00:00
|
|
|
"torch/+/wake",
|
|
|
|
subscriber_ca_file,
|
|
|
|
subscriber_cert_file,
|
|
|
|
subscriber_key_file),
|
|
|
|
daemon=True).start()
|
2020-10-15 14:44:34 +00:00
|
|
|
|
2020-10-15 17:48:48 +00:00
|
|
|
@staticmethod
|
|
|
|
def loadDatabase():
|
|
|
|
with open(torch_sub.database_file, "r") as database_file:
|
|
|
|
return json.load(database_file)
|
2020-10-15 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|