02-subscribe-qos0.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # Test whether a client sends a correct SUBSCRIBE to a topic with QoS 0.
  3. # The client should connect to port 1888 with keepalive=60, clean session set,
  4. # and client id subscribe-qos0-test
  5. # The test will send a CONNACK message to the client with rc=0. Upon receiving
  6. # the CONNACK and verifying that rc=0, the client should send a SUBSCRIBE
  7. # message to subscribe to topic "qos0/test" with QoS=0. If rc!=0, the client
  8. # should exit with an error.
  9. # Upon receiving the correct SUBSCRIBE message, the test will reply with a
  10. # SUBACK message with the accepted QoS set to 0. On receiving the SUBACK
  11. # message, the client should send a DISCONNECT message.
  12. from mosq_test_helper import *
  13. port = mosq_test.get_lib_port()
  14. rc = 1
  15. keepalive = 60
  16. connect_packet = mosq_test.gen_connect("subscribe-qos0-test", keepalive=keepalive)
  17. connack_packet = mosq_test.gen_connack(rc=0)
  18. disconnect_packet = mosq_test.gen_disconnect()
  19. mid = 1
  20. subscribe_packet = mosq_test.gen_subscribe(mid, "qos0/test", 0)
  21. suback_packet = mosq_test.gen_suback(mid, 0)
  22. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  24. sock.settimeout(10)
  25. sock.bind(('', port))
  26. sock.listen(5)
  27. client_args = sys.argv[1:]
  28. env = dict(os.environ)
  29. env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
  30. try:
  31. pp = env['PYTHONPATH']
  32. except KeyError:
  33. pp = ''
  34. env['PYTHONPATH'] = '../../lib/python:'+pp
  35. client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
  36. try:
  37. (conn, address) = sock.accept()
  38. conn.settimeout(10)
  39. mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
  40. mosq_test.do_receive_send(conn, subscribe_packet, suback_packet, "subscribe")
  41. mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
  42. rc = 0
  43. conn.close()
  44. except mosq_test.TestError:
  45. pass
  46. finally:
  47. client.terminate()
  48. client.wait()
  49. sock.close()
  50. exit(rc)