Пример #1
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))
Пример #2
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()
Пример #3
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))
Пример #4
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(timeout_s=1))

        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(timeout_s=1))

        # 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(timeout_s=1))

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

        # Bootstrap Delete shall succeed
        for obj in (OID.Security, OID.Server, OID.AccessControl):
            req = Lwm2mDelete('/%d' % obj)
            self.bootstrap_server.send(req)
            self.assertMsgEqual(
                Lwm2mDeleted.matching(req)(),
                self.bootstrap_server.recv(timeout_s=1))
        # now send Bootstrap Finish
        req = Lwm2mBootstrapFinish()
        self.bootstrap_server.send(req)
        self.assertMsgEqual(
            Lwm2mChanged.matching(req)(),
            self.bootstrap_server.recv(timeout_s=1))
        self.assertDemoDeregisters(server=regular_serv)

        self.request_demo_shutdown()

        regular_serv.close()