示例#1
0
    def runTest(self):
        req = self.bootstrap_server.recv(timeout_s=2)
        self.assertMsgEqual(
            Lwm2mRequestBootstrap(endpoint_name=DEMO_ENDPOINT_NAME), req)
        self.bootstrap_server.send(Lwm2mChanged.matching(req)())

        # Create Server object
        req = Lwm2mWrite(
            '/1/1',
            TLV.make_resource(RID.Server.Lifetime, 86400).serialize() +
            TLV.make_resource(RID.Server.ShortServerID, 1).serialize() +
            TLV.make_resource(RID.Server.NotificationStoring,
                              True).serialize() +
            TLV.make_resource(RID.Server.Binding, "U").serialize(),
            format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(),
            self.bootstrap_server.recv(timeout_s=1))

        # Create Security object
        regular_serv_uri = 'coap://127.0.0.1:%d' % self.serv.get_listen_port()

        req = Lwm2mWrite(
            '/0/2',
            TLV.make_resource(RID.Security.ServerURI,
                              regular_serv_uri).serialize() +
            TLV.make_resource(RID.Security.Bootstrap, 0).serialize() +
            TLV.make_resource(RID.Security.Mode, 3).serialize() +
            TLV.make_resource(RID.Security.ShortServerID, 1).serialize() +
            TLV.make_resource(RID.Security.PKOrIdentity, "").serialize() +
            TLV.make_resource(RID.Security.SecretKey, "").serialize(),
            format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(),
            self.bootstrap_server.recv(timeout_s=1))

        # send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(),
            self.bootstrap_server.recv(timeout_s=1))

        # demo should now try to register
        req = self.serv.recv()
        self.assertMsgEqual(
            Lwm2mRegister('/rd?lwm2m=%s&ep=%s&lt=86400' %
                          (DEMO_LWM2M_VERSION, DEMO_ENDPOINT_NAME)), req)

        # respond with 4.03
        self.serv.send(
            Lwm2mErrorResponse.matching(req)(code=coap.Code.RES_FORBIDDEN))

        # demo should retry Bootstrap
        req = self.bootstrap_server.recv(timeout_s=2)
        self.assertMsgEqual(
            Lwm2mRequestBootstrap(endpoint_name=DEMO_ENDPOINT_NAME), req)
        self.bootstrap_server.send(Lwm2mChanged.matching(req)())
示例#2
0
    def runTest(self):
        self.bootstrap_server.connect(('127.0.0.1', self.get_demo_port()))
        req = Lwm2mWrite('/1/42',
                         TLV.make_resource(RID.Server.Lifetime, 60).serialize()
                         + TLV.make_resource(RID.Server.Binding, "U").serialize()
                         + TLV.make_resource(RID.Server.ShortServerID, 42).serialize()
                         + TLV.make_resource(RID.Server.NotificationStoring, True).serialize(),
                         format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        regular_serv = Lwm2mServer()
        regular_serv_uri = 'coap://127.0.0.1:%d' % regular_serv.get_listen_port()

        # Create Security object
        req = Lwm2mWrite('/0/42',
                         TLV.make_resource(RID.Security.ServerURI, regular_serv_uri).serialize()
                         + TLV.make_resource(RID.Security.Bootstrap, 0).serialize()
                         + TLV.make_resource(RID.Security.Mode, 3).serialize()
                         + TLV.make_resource(RID.Security.ShortServerID, 42).serialize()
                         + TLV.make_resource(RID.Security.PKOrIdentity, "").serialize()
                         + TLV.make_resource(RID.Security.SecretKey, "").serialize(),
                         format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)

        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        # no Client Initiated bootstrap
        with self.assertRaises(socket.timeout):
            print(self.bootstrap_server.recv(timeout_s=4))

        # send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        self.assertDemoRegisters(server=regular_serv, lifetime=60)

        # Bootstrap Delete / shall succeed
        req = Lwm2mDelete('/')
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mDeleted.matching(req)(),
                            self.bootstrap_server.recv())
        # ...even twice
        req = Lwm2mDelete('/')
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mDeleted.matching(req)(),
                            self.bootstrap_server.recv())
        # now send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        self.request_demo_shutdown()

        regular_serv.close()
示例#3
0
 def set_bytes_burst(self, iid, size):
     req = Lwm2mWrite("/%d/%d" % (OID.Test, iid),
                      TLV.make_resource(RID.Test.ResBytesBurst, int(size)).serialize(),
                      format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
     self.serv.send(req)
     response = self.serv.recv()
     self.assertMsgEqual(Lwm2mChanged.matching(req)(), response);
示例#4
0
        def perform_typical_bootstrap(self,
                                      server_iid,
                                      security_iid,
                                      server_uri,
                                      lifetime=86400,
                                      finish=True):
            if self.holdoff_s is not None:
                # For the first holdoff_s seconds, the client should wait for
                # Server Initiated Bootstrap. Note that we subtract 1 second to
                # take into account code execution delays.
                with self.assertRaises(socket.timeout):
                    print(
                        self.bootstrap_server.recv(
                            timeout_s=max(0, self.holdoff_s - 1)))

            # We should get Bootstrap Request now
            self.assertDemoRequestsBootstrap()

            # Create typical Server Object instance
            self.write_instance(
                self.bootstrap_server,
                oid=OID.Server,
                iid=server_iid,
                content=TLV.make_resource(RID.Server.Lifetime,
                                          lifetime).serialize() +
                TLV.make_resource(RID.Server.ShortServerID,
                                  server_iid).serialize() +
                TLV.make_resource(RID.Server.NotificationStoring,
                                  True).serialize() +
                TLV.make_resource(RID.Server.Binding, "U").serialize())

            # Create typical (corresponding) Security Object instance
            self.write_instance(
                self.bootstrap_server,
                oid=OID.Security,
                iid=security_iid,
                content=TLV.make_resource(RID.Security.ServerURI,
                                          server_uri).serialize() +
                TLV.make_resource(RID.Security.Bootstrap, 0).serialize() +
                TLV.make_resource(RID.Security.Mode, 3).serialize() +
                TLV.make_resource(RID.Security.ShortServerID,
                                  server_iid).serialize() +
                TLV.make_resource(RID.Security.PKOrIdentity, "").serialize() +
                TLV.make_resource(RID.Security.SecretKey, "").serialize())

            if finish:
                self.perform_bootstrap_finish()
示例#5
0
    def runTest(self):
        pkt = self.bootstrap_server.recv()
        self.assertMsgEqual(Lwm2mRequestBootstrap(endpoint_name=DEMO_ENDPOINT_NAME),
                            pkt)
        self.bootstrap_server.send(Lwm2mChanged.matching(pkt)())

        # SSID is normally not writable, yet the Bootstrap Server can write to it
        req = Lwm2mWrite('/1/0',
                         TLV.make_resource(RID.Server.ShortServerID, 42).serialize()
                         + TLV.make_resource(RID.Server.Lifetime, 86400).serialize()
                         + TLV.make_resource(RID.Server.NotificationStoring, 0).serialize()
                         + TLV.make_resource(RID.Server.Binding, b'U').serialize(),
                         format=coap.ContentFormat.APPLICATION_LWM2M_TLV)

        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())
示例#6
0
 def set_bytes_burst(self, iid, size):
     req = Lwm2mWrite("/%d/%d" % (TEST_OBJECT_OID, iid),
                      TLV.make_resource(TEST_OBJECT_RES_BYTES_BURST,
                                        int(size)).serialize(),
                      format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
     self.serv.send(req)
     response = self.serv.recv()
     self.assertMsgEqual(Lwm2mChanged.matching(req)(), response)
示例#7
0
    def runTest(self):
        self.bootstrap_server.connect_to_client(
            ('127.0.0.1', self.get_demo_port()))

        req = Lwm2mWrite(
            '/0/42',
            TLV.make_resource(RID.Security.ServerURI,
                              'coap://1.2.3.4:5678').serialize() +
            TLV.make_resource(RID.Security.Bootstrap, 0).serialize() +
            TLV.make_resource(RID.Security.Mode, 3).serialize() +
            TLV.make_resource(RID.Security.ShortServerID, 42).serialize() +
            TLV.make_resource(RID.Security.PKOrIdentity, b'').serialize() +
            TLV.make_resource(RID.Security.SecretKey, b'').serialize(),
            format=coap.ContentFormat.APPLICATION_LWM2M_TLV)

        for _ in range(64):
            self.bootstrap_server.send(req)
            self.assertMsgEqual(
                Lwm2mChanged.matching(req)(), self.bootstrap_server.recv())

        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(), self.bootstrap_server.recv())
        self.request_demo_shutdown()
示例#8
0
    def runTest(self):
        pkt = self.bootstrap_server.recv()
        self.assertMsgEqual(Lwm2mRequestBootstrap(endpoint_name=DEMO_ENDPOINT_NAME),
                            pkt)
        self.bootstrap_server.send(Lwm2mChanged.matching(pkt)())

        # Bootstrap Server MUST NOT be allowed to create second Bootstrap Security Instance
        req = Lwm2mWrite('/0/42',
                         TLV.make_resource(RID.Security.ServerURI, 'coap://127.0.0.1:5683').serialize()
                         + TLV.make_resource(RID.Security.Bootstrap, 1).serialize()
                         + TLV.make_resource(RID.Security.Mode, 3).serialize()
                         + TLV.make_resource(RID.Security.ShortServerID, 42).serialize()
                         + TLV.make_resource(RID.Security.PKOrIdentity, "").serialize()
                         + TLV.make_resource(RID.Security.SecretKey, "").serialize(),
                         format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mErrorResponse.matching(req)(code=coap.Code.RES_BAD_REQUEST),
                            self.bootstrap_server.recv())
示例#9
0
    def runTest(self):
        self.perform_typical_bootstrap(server_iid=1,
                                       security_iid=2,
                                       server_uri='coap://unused-in-this-test',
                                       lifetime=86400,
                                       finish=False)

        # Bootstrap Server MUST NOT be allowed to create second Bootstrap Security Instance
        self.write_instance(
            self.bootstrap_server,
            oid=OID.Security,
            iid=42,
            content=TLV.make_resource(RID.Security.ServerURI,
                                      'coap://127.0.0.1:5683').serialize() +
            TLV.make_resource(RID.Security.Bootstrap, 1).serialize() +
            TLV.make_resource(RID.Security.Mode, 3).serialize() +
            TLV.make_resource(RID.Security.ShortServerID, 42).serialize() +
            TLV.make_resource(RID.Security.PKOrIdentity, "").serialize() +
            TLV.make_resource(RID.Security.SecretKey, "").serialize(),
            expect_error_code=coap.Code.RES_BAD_REQUEST)
示例#10
0
    def runTest(self):
        # for the first 3 seconds, the client should wait for Server Initiated Bootstrap
        with self.assertRaises(socket.timeout):
            print(self.bootstrap_server.recv(timeout_s=2))

        # we should get Bootstrap Request now
        pkt = self.bootstrap_server.recv()
        self.assertMsgEqual(Lwm2mRequestBootstrap(endpoint_name=DEMO_ENDPOINT_NAME),
                            pkt)
        self.bootstrap_server.send(Lwm2mChanged.matching(pkt)())

        # Create Server object
        req = Lwm2mWrite('/1/1',
                         TLV.make_resource(RID.Server.Lifetime, 60).serialize()
                         + TLV.make_resource(RID.Server.ShortServerID, 1).serialize()
                         + TLV.make_resource(RID.Server.NotificationStoring, True).serialize()
                         + TLV.make_resource(RID.Server.Binding, "U").serialize(),
                         format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        # Create Security object
        regular_serv_uri = 'coap://127.0.0.1:%d' % self.serv.get_listen_port()

        req = Lwm2mWrite('/0/2',
                         TLV.make_resource(RID.Security.ServerURI, regular_serv_uri).serialize()
                         + TLV.make_resource(RID.Security.Bootstrap, 0).serialize()
                         + TLV.make_resource(RID.Security.Mode, 3).serialize()
                         + TLV.make_resource(RID.Security.ShortServerID, 1).serialize()
                         + TLV.make_resource(RID.Security.PKOrIdentity, "").serialize()
                         + TLV.make_resource(RID.Security.SecretKey, "").serialize(),
                         format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        # send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(Lwm2mChanged.matching(req)(),
                            self.bootstrap_server.recv())

        # should now register with the non-bootstrap server
        self.assertDemoRegisters(self.serv, lifetime=60)

        # no message for now
        with self.assertRaises(socket.timeout):
            print(self.serv.recv(timeout_s=1))

        # no changes
        self.communicate('send-update')
        self.assertDemoUpdatesRegistration()

        # still no message
        with self.assertRaises(socket.timeout):
            print(self.serv.recv(timeout_s=3))

        # now the Bootstrap Server Account should be gone
        req = Lwm2mDiscover('/0')
        self.serv.send(req)
        res = self.serv.recv()
        self.assertMsgEqual(Lwm2mContent.matching(req)(), res)

        self.assertIn(b'</0/2/', res.content)
        self.assertNotIn(b'</0/1/', res.content)

        self.serv.send(Lwm2mChanged.matching(pkt)())

        # client did not try to register to a Bootstrap server (as in T847)
        with self.assertRaises(socket.timeout):
            print(self.bootstrap_server.recv(timeout_s=1))
示例#11
0
    def runTest(self):
        self.bootstrap_server.connect(('127.0.0.1', self.get_demo_port()))
        self.write_instance(
            server=self.bootstrap_server,
            oid=OID.Server,
            iid=42,
            content=TLV.make_resource(RID.Server.Lifetime, 60).serialize() +
            TLV.make_resource(RID.Server.Binding, "U").serialize() +
            TLV.make_resource(RID.Server.ShortServerID, 11).serialize() +
            TLV.make_resource(RID.Server.NotificationStoring,
                              True).serialize())

        self.write_instance(
            server=self.bootstrap_server,
            oid=OID.Server,
            iid=24,
            content=TLV.make_resource(RID.Server.Lifetime, 60).serialize() +
            TLV.make_resource(RID.Server.Binding, "U").serialize() +
            TLV.make_resource(RID.Server.ShortServerID, 12).serialize() +
            TLV.make_resource(RID.Server.NotificationStoring,
                              True).serialize())

        uri = 'coap://127.0.0.1:9999'
        self.write_instance(
            self.bootstrap_server,
            oid=OID.Security,
            iid=2,
            content=TLV.make_resource(RID.Security.ServerURI, uri).serialize()
            + TLV.make_resource(RID.Security.Bootstrap, 0).serialize() +
            TLV.make_resource(RID.Security.Mode, 3).serialize() +
            TLV.make_resource(RID.Security.ShortServerID, 11).serialize() +
            TLV.make_resource(RID.Security.PKOrIdentity, "").serialize() +
            TLV.make_resource(RID.Security.SecretKey, "").serialize())

        uri = 'coap://127.0.0.1:11111'
        self.write_instance(
            self.bootstrap_server,
            oid=OID.Security,
            iid=10,
            content=TLV.make_resource(RID.Security.ServerURI, uri).serialize()
            + TLV.make_resource(RID.Security.Bootstrap, 0).serialize() +
            TLV.make_resource(RID.Security.Mode, 3).serialize() +
            TLV.make_resource(RID.Security.ShortServerID, 12).serialize() +
            TLV.make_resource(RID.Security.PKOrIdentity, "").serialize() +
            TLV.make_resource(RID.Security.SecretKey, "").serialize())

        EXPECTED_PREFIX = b'lwm2m="1.0",</0>,</0/1>,</0/2>;ssid=11,</0/10>;ssid=12,' \
                          b'</1>,</1/24>;ssid=12,</1/42>;ssid=11,</2>,'
        discover_result = self.discover(self.bootstrap_server)
        self.assertEqual([coap.Option.CONTENT_FORMAT.APPLICATION_LINK],
                         discover_result.get_options(
                             coap.Option.CONTENT_FORMAT))
        self.assertLinkListValid(
            discover_result.content[len(EXPECTED_ENABLER_VERSION_STRING) + 1:])
        self.assertIn(
            b'</10>;ver="1.1"',
            discover_result.content[len(EXPECTED_ENABLER_VERSION_STRING) + 1:])
        # No more parameters
        self.assertEqual(
            2, len(discover_result.content[len(EXPECTED_PREFIX):].split(b';')))
        self.assertTrue(discover_result.content.startswith(EXPECTED_PREFIX))
示例#12
0
    def runTest(self):
        self.bootstrap_server.connect_to_client(
            ('127.0.0.1', self.get_demo_port()))
        req = Lwm2mWrite(
            '/%d/42' % (OID.Server, ),
            TLV.make_resource(RID.Server.Lifetime, 60).serialize() +
            TLV.make_resource(RID.Server.Binding, "U").serialize() +
            TLV.make_resource(RID.Server.ShortServerID, 42).serialize() +
            TLV.make_resource(RID.Server.NotificationStoring,
                              True).serialize(),
            format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(), self.bootstrap_server.recv())

        regular_serv = Lwm2mServer()
        regular_serv_uri = 'coap://127.0.0.1:%d' % regular_serv.get_listen_port(
        )

        # Create Security object
        req = Lwm2mWrite(
            '/%d/42' % (OID.Security, ),
            TLV.make_resource(RID.Security.ServerURI,
                              regular_serv_uri).serialize() +
            TLV.make_resource(RID.Security.Bootstrap, 0).serialize() +
            TLV.make_resource(RID.Security.Mode, 3).serialize() +
            TLV.make_resource(RID.Security.ShortServerID, 42).serialize() +
            TLV.make_resource(RID.Security.PKOrIdentity, "").serialize() +
            TLV.make_resource(RID.Security.SecretKey, "").serialize(),
            format=coap.ContentFormat.APPLICATION_LWM2M_TLV)
        self.bootstrap_server.send(req)

        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(), self.bootstrap_server.recv())

        # no Client Initiated bootstrap
        with self.assertRaises(socket.timeout):
            print(self.bootstrap_server.recv(timeout_s=4))

        # send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(), self.bootstrap_server.recv())

        self.assertDemoRegisters(server=regular_serv, lifetime=60)

        # Bootstrap Delete / shall succeed
        req = Lwm2mDelete('/')
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mDeleted.matching(req)(), self.bootstrap_server.recv())
        # ...even twice
        req = Lwm2mDelete('/')
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mDeleted.matching(req)(), self.bootstrap_server.recv())
        # now send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(), self.bootstrap_server.recv())

        # the client will now start Client Initiated bootstrap, because it has no regular server connection
        # this might happen after a backoff, if the Bootstrap Delete was handled before the response to Register
        pkt = self.bootstrap_server.recv(timeout_s=20)
        self.assertIsInstance(pkt, Lwm2mRequestBootstrap)
        self.bootstrap_server.send(Lwm2mChanged.matching(pkt)())

        self.request_demo_shutdown()

        regular_serv.close()