You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by we...@apache.org on 2017/03/30 21:12:21 UTC

[04/37] incubator-mynewt-core git commit: net/oic; add a testcase for basic observe.

net/oic; add a testcase for basic observe.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/133a628f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/133a628f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/133a628f

Branch: refs/heads/nrf_cputime
Commit: 133a628fd0ebff170beb53703af388476dcf1d29
Parents: 4393af4
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Sun Mar 26 16:43:03 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Sun Mar 26 16:43:03 2017 -0700

----------------------------------------------------------------------
 net/oic/test/src/test_discovery.c      |  17 +--
 net/oic/test/src/test_getset.c         |  24 ++--
 net/oic/test/src/test_observe.c        | 171 ++++++++++++++++++++++++++++
 net/oic/test/src/test_oic.h            |   1 +
 net/oic/test/src/testcases/oic_tests.c |  11 +-
 5 files changed, 201 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_discovery.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_discovery.c b/net/oic/test/src/test_discovery.c
index 4722a1e..c9e1cfd 100644
--- a/net/oic/test/src/test_discovery.c
+++ b/net/oic/test/src/test_discovery.c
@@ -23,6 +23,7 @@
 
 static int test_discovery_state;
 static volatile int test_discovery_done;
+static struct oc_resource *test_res_light;
 
 static void test_discovery_next_step(struct os_event *);
 static struct os_event test_discovery_next_ev = {
@@ -121,15 +122,16 @@ test_discovery_next_step(struct os_event *ev)
         oic_test_reset_tmo("2nd discovery");
         break;
     case 3: {
-        oc_resource_t *res = oc_new_resource("/light/test", 1, 0);
+        test_res_light = oc_new_resource("/light/test", 1, 0);
 
-        oc_resource_bind_resource_type(res, "oic.r.light");
-        oc_resource_bind_resource_interface(res, OC_IF_RW);
-        oc_resource_set_default_interface(res, OC_IF_RW);
+        oc_resource_bind_resource_type(test_res_light, "oic.r.light");
+        oc_resource_bind_resource_interface(test_res_light, OC_IF_RW);
+        oc_resource_set_default_interface(test_res_light, OC_IF_RW);
 
-        oc_resource_set_discoverable(res);
-        oc_resource_set_request_handler(res, OC_GET, test_discovery_get);
-        oc_add_resource(res);
+        oc_resource_set_discoverable(test_res_light);
+        oc_resource_set_request_handler(test_res_light, OC_GET,
+                                        test_discovery_get);
+        oc_add_resource(test_res_light);
         oc_do_ip_discovery(NULL, test_discovery_cb);
         oic_test_reset_tmo("3rd discovery");
         break;
@@ -148,6 +150,7 @@ test_discovery(void)
     while (!test_discovery_done) {
         os_eventq_run(&oic_tapp_evq);
     }
+    oc_delete_resource(test_res_light);
 }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_getset.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_getset.c b/net/oic/test/src/test_getset.c
index 73e6c60..ff59657 100644
--- a/net/oic/test/src/test_getset.c
+++ b/net/oic/test/src/test_getset.c
@@ -23,6 +23,7 @@
 
 static int test_getset_state;
 static volatile int test_getset_done;
+static struct oc_resource *test_res_getset;
 
 static void test_getset_next_step(struct os_event *);
 static struct os_event test_getset_next_ev = {
@@ -80,20 +81,19 @@ test_getset_next_step(struct os_event *ev)
 
     test_getset_state++;
     switch (test_getset_state) {
-    case 1: {
-        oc_resource_t *res;
-
-        res = oc_new_resource("/getset", 1, 0);
-        TEST_ASSERT_FATAL(res);
+    case 1:
+        test_res_getset = oc_new_resource("/getset", 1, 0);
+        TEST_ASSERT_FATAL(test_res_getset);
 
-        oc_resource_bind_resource_interface(res, OC_IF_RW);
-        oc_resource_set_default_interface(res, OC_IF_RW);
+        oc_resource_bind_resource_interface(test_res_getset, OC_IF_RW);
+        oc_resource_set_default_interface(test_res_getset, OC_IF_RW);
 
-        oc_resource_set_request_handler(res, OC_GET, test_getset_get);
-        oc_resource_set_request_handler(res, OC_PUT, test_getset_put);
-        b_rc = oc_add_resource(res);
+        oc_resource_set_request_handler(test_res_getset, OC_GET,
+                                        test_getset_get);
+        oc_resource_set_request_handler(test_res_getset, OC_PUT,
+                                        test_getset_put);
+        b_rc = oc_add_resource(test_res_getset);
         TEST_ASSERT(b_rc == true);
-    }
         /* fall-through */
     case 2:
     case 3:
@@ -121,7 +121,7 @@ test_getset(void)
     while (!test_getset_done) {
         os_eventq_run(&oic_tapp_evq);
     }
-    oc_main_shutdown();
+    oc_delete_resource(test_res_getset);
 }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_observe.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_observe.c b/net/oic/test/src/test_observe.c
new file mode 100644
index 0000000..155e84a
--- /dev/null
+++ b/net/oic/test/src/test_observe.c
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "test_oic.h"
+
+#include <os/os.h>
+#include <oic/oc_api.h>
+
+#include <cborattr/cborattr.h>
+
+#include <oic/../../src/messaging/coap/observe.h>
+
+static int test_observe_state;
+static volatile int test_observe_done;
+static struct oc_resource *test_res_observe;
+
+static void test_observe_next_step(struct os_event *);
+static struct os_event test_observe_next_ev = {
+    .ev_cb = test_observe_next_step
+};
+
+static void
+test_observe_get(struct oc_request *request, oc_interface_mask_t interface)
+{
+    oc_rep_start_root_object();
+    switch (interface) {
+    case OC_IF_BASELINE:
+        oc_process_baseline_interface(request->resource);
+    case OC_IF_R:
+        switch (test_observe_state) {
+        case 1:
+        case 2:
+            /* initial get request */
+        case 3:
+        case 4:
+        case 5:
+        case 6:
+            oc_rep_set_int(root, value, test_observe_state);
+            break;
+        default:
+            break;
+        }
+    default:
+        break;
+    }
+    oc_rep_end_root_object();
+    oc_send_response(request, OC_STATUS_OK);
+}
+
+static void
+test_observe_rsp(struct oc_client_response *rsp)
+{
+    long long rsp_value;
+    struct cbor_attr_t attrs[] = {
+        [0] = {
+            .attribute = "state",
+            .type = CborAttrIntegerType,
+            .addr.integer = &rsp_value,
+            .dflt.integer = 0
+        },
+        [1] = {
+        }
+    };
+    struct os_mbuf *m;
+    uint16_t data_off;
+    int len;
+
+    switch (test_observe_state) {
+    case 1:
+        TEST_ASSERT(rsp->code == OC_STATUS_NOT_FOUND);
+        break;
+    case 2:
+    case 3:
+    case 4:
+        TEST_ASSERT(rsp->code == OC_STATUS_OK);
+        len = coap_get_payload(rsp->packet, &m, &data_off);
+        if (cbor_read_mbuf_attrs(m, data_off, len, attrs)) {
+            TEST_ASSERT(rsp_value == test_observe_state);
+        }
+        break;
+    default:
+        break;
+    }
+    os_eventq_put(&oic_tapp_evq, &test_observe_next_ev);
+}
+
+static void
+test_observe_next_step(struct os_event *ev)
+{
+    bool b_rc;
+    int rc;
+    struct oc_server_handle server;
+
+    test_observe_state++;
+    switch (test_observe_state) {
+    case 1:
+        test_res_observe = oc_new_resource("/observe", 1, 0);
+        TEST_ASSERT_FATAL(test_res_observe);
+
+        oc_resource_bind_resource_interface(test_res_observe, OC_IF_R);
+        oc_resource_set_default_interface(test_res_observe, OC_IF_R);
+        oc_resource_set_observable(test_res_observe);
+
+        oc_resource_set_request_handler(test_res_observe, OC_GET,
+                                        test_observe_get);
+        b_rc = oc_add_resource(test_res_observe);
+        TEST_ASSERT(b_rc == true);
+
+        /*
+         * Observe nonexistent URI
+         */
+        oic_test_get_endpoint(&server);
+        b_rc = oc_do_observe("/observe_wrong", &server, NULL, test_observe_rsp,
+          LOW_QOS);
+        TEST_ASSERT_FATAL(b_rc == true);
+
+        oic_test_reset_tmo("observe1");
+        break;
+    case 2:
+        oic_test_get_endpoint(&server);
+        b_rc = oc_do_observe("/observe", &server, NULL, test_observe_rsp,
+                             LOW_QOS);
+        TEST_ASSERT_FATAL(b_rc == true);
+
+        oic_test_reset_tmo("observe2");
+        break;
+    case 3:
+    case 4:
+        /*
+         * Valid notifications
+         */
+        rc = oc_notify_observers(test_res_observe);
+        TEST_ASSERT(rc == 1); /* one observer */
+        oic_test_reset_tmo("observe3-4");
+        break;
+    case 5:
+        test_observe_done = 1;
+        break;
+    default:
+        TEST_ASSERT_FATAL(0);
+        break;
+    }
+}
+
+void
+test_observe(void)
+{
+    os_eventq_put(&oic_tapp_evq, &test_observe_next_ev);
+
+    while (!test_observe_done) {
+        os_eventq_run(&oic_tapp_evq);
+    }
+    oc_delete_resource(test_res_observe);
+}
+
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/test_oic.h
----------------------------------------------------------------------
diff --git a/net/oic/test/src/test_oic.h b/net/oic/test/src/test_oic.h
index 5aef7b7..d247226 100644
--- a/net/oic/test/src/test_oic.h
+++ b/net/oic/test/src/test_oic.h
@@ -42,6 +42,7 @@ void oic_test_get_endpoint(struct oc_server_handle *);
 
 void test_discovery(void);
 void test_getset(void);
+void test_observe(void);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/133a628f/net/oic/test/src/testcases/oic_tests.c
----------------------------------------------------------------------
diff --git a/net/oic/test/src/testcases/oic_tests.c b/net/oic/test/src/testcases/oic_tests.c
index cacd731..aefdb3e 100644
--- a/net/oic/test/src/testcases/oic_tests.c
+++ b/net/oic/test/src/testcases/oic_tests.c
@@ -24,7 +24,7 @@
 #include <mn_socket/mn_socket.h>
 
 #define OIC_TAPP_PRIO       9
-#define OIC_TAPP_STACK_SIZE 1024
+#define OIC_TAPP_STACK_SIZE OS_STACK_ALIGN(1024)
 
 /*
  * How long to wait before declaring discovery process failure.
@@ -33,7 +33,7 @@
 
 static struct os_task oic_tapp;
 static const char *oic_test_phase;
-static os_stack_t oic_tapp_stack[OS_STACK_ALIGN(OIC_TAPP_STACK_SIZE)];
+static os_stack_t oic_tapp_stack[OIC_TAPP_STACK_SIZE];
 struct os_eventq oic_tapp_evq;
 static struct os_callout oic_test_timer;
 static struct oc_server_handle oic_tgt;
@@ -41,7 +41,8 @@ static struct oc_server_handle oic_tgt;
 static void
 oic_test_timer_cb(struct os_event *ev)
 {
-    TEST_ASSERT_FATAL(0, "test_phase: %s\n", oic_test_phase ? oic_test_phase : "unknwn");
+    TEST_ASSERT_FATAL(0, "test_phase: %s\n",
+                      oic_test_phase ? oic_test_phase : "unknwn");
 }
 
 void
@@ -85,6 +86,8 @@ oic_test_handler(void *arg)
     oc_main_init(&test_handler);
     test_discovery();
     test_getset();
+    test_observe();
+    oc_main_shutdown();
     tu_restart();
 }
 
@@ -102,7 +105,7 @@ oic_test_init(void)
 
     rc = os_task_init(&oic_tapp, "oic_test", oic_test_handler, NULL,
                       OIC_TAPP_PRIO, OS_WAIT_FOREVER,
-                      oic_tapp_stack, OS_STACK_ALIGN(OIC_TAPP_STACK_SIZE));
+                      oic_tapp_stack, OIC_TAPP_STACK_SIZE);
     TEST_ASSERT_FATAL(rc == 0);
     oc_evq_set(&oic_tapp_evq);