Exemplo n.º 1
0
    def setUpClass(cls):
      setData()
      global callback, callback2, aclient, bclient
      cleanup()

      callback = Callbacks()
      callback2 = Callbacks()

      #aclient = mqtt_client.Client(b"\xEF\xBB\xBF" + "myclientid".encode("utf-8"))
      aclient = mqtt_client.Client("myclientid".encode("utf-8"))
      aclient.registerCallback(callback)

      bclient = mqtt_client.Client("myclientid2".encode("utf-8"))
      bclient.registerCallback(callback2)
Exemplo n.º 2
0
    def test_flow_control2(self):
      testcallback = Callbacks()
      # no callback means no background thread, to control receiving
      testclient = mqtt_client.Client("myclientid".encode("utf-8"))

      # get receive maximum - the number of concurrent QoS 1 and 2 messages
      connect_properties = MQTTV5.Properties(MQTTV5.PacketTypes.CONNECT)
      connect_properties.SessionExpiryInterval = 0
      connack = testclient.connect(host=host, port=port, cleanstart=True)

      serverReceiveMaximum = 2**16-1 # the default
      if hasattr(connack.properties, "ReceiveMaximum"):
        serverReceiveMaximum = connack.properties.ReceiveMaximum

      receiver = testclient.getReceiver()

      # send number of messages to exceed receive maximum
      qos = 2
      pubs = 0
      for i in range(1, serverReceiveMaximum + 2):
        testclient.publish(topics[0], "message %d" % i, qos)
        pubs += 1

      # should get disconnected...
      while (testcallback.disconnects == []):
        receiver.receive(testcallback)
      self.waitfor(testcallback.disconnects, 1, 1)
      self.assertEqual(len(testcallback.disconnects), 1, len(testcallback.disconnects))
      self.assertEqual(testcallback.disconnects[0]["reasonCode"].value, 147,
                       testcallback.disconnects[0]["reasonCode"].value)
Exemplo n.º 3
0
def cleanRetained():
  callback = Callbacks()
  curclient = mqtt_client.Client("clean retained".encode("utf-8"))
  curclient.registerCallback(callback)
  curclient.connect(host=host, port=port, cleanstart=True)
  curclient.subscribe(["#"], [MQTTV5.SubscribeOptions(0)])
  time.sleep(2) # wait for all retained messages to arrive
  for message in callback.messages:
    logging.info("deleting retained message for topic", message[0])
    curclient.publish(message[0], b"", 0, retained=True)
  curclient.disconnect()
  time.sleep(.1)
Exemplo n.º 4
0
    def setUpClass(cls):
      setData()
      global callback, callback2, aclient, bclient,cclient,dclient,callback3,callback4
      # cleanup()

      callback = Callbacks()
      callback2 = Callbacks()
      callback3 = Callbacks()
      # callback4 = broker3callback.Callbacks

      #aclient = mqtt_client.Client(b"\xEF\xBB\xBF" + "myclientid".encode("utf-8"))
      aclient = mqtt_client.Client(clientid1.encode("utf-8"))
      aclient.registerCallback(callback)
      aclient.setUserName(username1, password1)

      bclient = mqtt_client.Client(clientid2.encode("utf-8"))
      bclient.registerCallback(callback2)
      bclient.setUserName(username2, password2)

      cclient = mqtt_client.Client(clientid3.encode("utf-8"))
      cclient.registerCallback(callback3)
      cclient.setUserName(username3, password3)
Exemplo n.º 5
0
def cleanup():
  # clean all client state
  print("clean up starting")
  clientids = (clientid1, clientid2)

  # for clientid in clientids:
  curclient = mqtt_client.Client(clientid1.encode("utf-8"))
  curclient.setUserName(username1, password1)
  curclient.connect(host=host, port=port, cleanstart=True)
  time.sleep(.1)
  curclient.disconnect()
  time.sleep(.1)

  curclient = mqtt_client.Client(clientid2.encode("utf-8"))
  curclient.setUserName(username2, password2)
  curclient.connect(host=host, port=port, cleanstart=True)
  time.sleep(.1)
  curclient.disconnect()
  time.sleep(.1)

  # clean retained messages
  cleanRetained()
  print("clean up finished")
Exemplo n.º 6
0
 def test_session_less_than_connect(self):
     print("Basic test starting")
     succeeded = True
     try:
         aclient = mqtt_client.Client(clientid1.encode("utf-8"))
         aclient.registerCallback(callback)
         username1 = "errorusername"
         aclient.setUserName(username1, password1)
         aclient.connect(host=host, port=port,cleanstart=True)
         print("connect succeeded")
     except:
         # traceback.print_exc()
         succeeded = False
     time.sleep(10)
     self.assertEqual(succeeded, True)
Exemplo n.º 7
0
def cleanup():
  # clean all client state
  print("clean up starting")
  clientids = ("myclientid", "myclientid2")

  for clientid in clientids:
    curclient = mqtt_client.Client(clientid.encode("utf-8"))
    curclient.connect(host=host, port=port, cleanstart=True)
    time.sleep(.1)
    curclient.disconnect()
    time.sleep(.1)

  # clean retained messages
  cleanRetained()
  print("clean up finished")
Exemplo n.º 8
0
 def test_zero_length_clientid(self):
   logging.info("Zero length clientid test starting")
   succeeded = True
   try:
     client0 = mqtt_client.Client("")
     fails = False
     try:
       client0.connect(host=host, port=port, cleanstart=False) # should be rejected
     except:
       fails = True
     assert fails == True
     fails = False
     try:
       client0.connect(host=host, port=port, cleanstart=True) # should work
     except:
       fails = True
     assert fails == False
     client0.disconnect()
   except:
     traceback.print_exc()
     succeeded = False
   logging.info("Zero length clientid test %s", "succeeded" if succeeded else "failed")
   return succeeded
Exemplo n.º 9
0
    def test_flow_control1(self):
      testcallback = Callbacks()
      # no callback means no background thread, to control receiving
      testclient = mqtt_client.Client("myclientid".encode("utf-8"))

      # set receive maximum - the number of concurrent QoS 1 and 2 messages
      clientReceiveMaximum = 2 # set to low number so we can test
      connect_properties = MQTTV5.Properties(MQTTV5.PacketTypes.CONNECT)
      connect_properties.ReceiveMaximum = clientReceiveMaximum
      connect_properties.SessionExpiryInterval = 0
      connack = testclient.connect(host=host, port=port, cleanstart=True,
                   properties=connect_properties)

      serverReceiveMaximum = 2**16-1 # the default
      if hasattr(connack.properties, "ReceiveMaximum"):
        serverReceiveMaximum = connack.properties.ReceiveMaximum

      receiver = testclient.getReceiver()

      testclient.subscribe([topics[0]], [MQTTV5.SubscribeOptions(2)])
      receiver.receive(testcallback)
      self.waitfor(testcallback.subscribeds, 1, 3)

      pubs = 0
      for i in range(1, clientReceiveMaximum + 2):
        testclient.publish(topics[0], "message %d" % i, 1)
        pubs += 1

      # get two publishes
      acks = 0
      while True:
        response1 = MQTTV5.unpackPacket(MQTTV5.getPacket(testclient.sock))
        if response1.fh.PacketType == MQTTV5.PacketTypes.PUBLISH:
          break
        self.assertEqual(response1.fh.PacketType, MQTTV5.PacketTypes.PUBACK)
        acks += 1
        del receiver.outMsgs[response1.packetIdentifier]
      self.assertEqual(response1.fh.PacketType, MQTTV5.PacketTypes.PUBLISH)
      self.assertEqual(response1.fh.QoS, 1, response1.fh.QoS)

      while True:
        response2 = MQTTV5.unpackPacket(MQTTV5.getPacket(testclient.sock))
        if response2.fh.PacketType == MQTTV5.PacketTypes.PUBLISH:
          break
        self.assertEqual(response2.fh.PacketType, MQTTV5.PacketTypes.PUBACK)
        acks += 1
        del receiver.outMsgs[response2.packetIdentifier]
      self.assertEqual(response2.fh.PacketType, MQTTV5.PacketTypes.PUBLISH)
      self.assertEqual(response2.fh.QoS, 1, response1.fh.QoS)

      while acks < pubs:
        ack = MQTTV5.unpackPacket(MQTTV5.getPacket(testclient.sock))
        self.assertEqual(ack.fh.PacketType, MQTTV5.PacketTypes.PUBACK)
        acks += 1
        del receiver.outMsgs[ack.packetIdentifier]

      with self.assertRaises(socket.timeout):
        # this should time out because we haven't acknowledged the first one
        response3 = MQTTV5.unpackPacket(MQTTV5.getPacket(testclient.sock))

      # ack the first one
      puback = MQTTV5.Pubacks()
      puback.packetIdentifier = response1.packetIdentifier
      testclient.sock.send(puback.pack())

      # now get the next packet
      response3 = MQTTV5.unpackPacket(MQTTV5.getPacket(testclient.sock))
      self.assertEqual(response3.fh.PacketType, MQTTV5.PacketTypes.PUBLISH)
      self.assertEqual(response3.fh.QoS, 1, response1.fh.QoS)

      # ack the second one
      puback.packetIdentifier = response2.packetIdentifier
      testclient.sock.send(puback.pack())

      # ack the third one
      puback.packetIdentifier = response3.packetIdentifier
      testclient.sock.send(puback.pack())

      testclient.disconnect()
Exemplo n.º 10
0
 def test_assigned_clientid(self):
   noidclient = mqtt_client.Client("")
   connack = noidclient.connect(host=host, port=port, cleanstart=True)
   noidclient.disconnect()
   logging.info("Assigned client identifier %s" % connack.properties.AssignedClientIdentifier)
   self.assertTrue(connack.properties.AssignedClientIdentifier != "")