You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2015/11/11 03:45:57 UTC

[1/3] incubator-mynewt-larva git commit: add circular buffer tests

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 8895970ad -> 313f65326


add circular buffer tests


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/313f6532
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/313f6532
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/313f6532

Branch: refs/heads/master
Commit: 313f65326abe15e9d3ea829afb969a7cbd003b19
Parents: 2957110
Author: Sterling Hughes <st...@apache.org>
Authored: Tue Nov 10 18:45:19 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Tue Nov 10 18:45:26 2015 -0800

----------------------------------------------------------------------
 libs/util/egg.yml                   |   1 +
 libs/util/include/util/cbmem.h      |   2 +
 libs/util/src/test/cbmem_test.c     | 173 +++++++++++++++++++++++++++++++
 libs/util/src/test/util_test.c      |  42 ++++++++
 libs/util/src/test/util_test_priv.h |  22 ++++
 5 files changed, 240 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/313f6532/libs/util/egg.yml
----------------------------------------------------------------------
diff --git a/libs/util/egg.yml b/libs/util/egg.yml
index 464da8b..2891aa3 100644
--- a/libs/util/egg.yml
+++ b/libs/util/egg.yml
@@ -3,6 +3,7 @@ egg.vers: 0.1
 egg.deps:
     - hw/hal
     - libs/os
+    - libs/testutil
 egg.deps.SHELL:
     - libs/console/full 
     - libs/shell 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/313f6532/libs/util/include/util/cbmem.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/cbmem.h b/libs/util/include/util/cbmem.h
index e3347a5..1a0fa34 100644
--- a/libs/util/include/util/cbmem.h
+++ b/libs/util/include/util/cbmem.h
@@ -56,6 +56,8 @@ struct cbmem_entry_hdr *cbmem_iter_next(struct cbmem *cbmem,
         struct cbmem_iter *iter);
 int cbmem_read(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, void *buf, 
         uint16_t off, uint16_t len);
+int cbmem_walk(struct cbmem *cbmem, cbmem_walk_func_t walk_func, void *arg);
+
 int cbmem_flush(struct cbmem *);
 
 #endif /* __UTIL_CBMEM_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/313f6532/libs/util/src/test/cbmem_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/cbmem_test.c b/libs/util/src/test/cbmem_test.c
new file mode 100644
index 0000000..af4b323
--- /dev/null
+++ b/libs/util/src/test/cbmem_test.c
@@ -0,0 +1,173 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 <stdio.h>
+#include <string.h>
+
+#include "testutil/testutil.h"
+#include "util/cbmem.h" 
+
+#define CBMEM1_BUF_SIZE (64 * 1024)
+
+struct cbmem cbmem1;
+uint8_t cbmem1_buf[CBMEM1_BUF_SIZE];
+uint8_t cbmem1_entry[1024];
+
+/*
+ * Things to test.
+ *
+ * - Wrap of the circular buffer.  
+ * - Reading through all entries.
+ */
+
+static void
+setup_cbmem1(void)
+{
+    int i;
+    int rc;
+
+    rc = cbmem_init(&cbmem1, cbmem1_buf, CBMEM1_BUF_SIZE);
+    TEST_ASSERT_FATAL(rc == 0, "cbmem_init() failed, non-zero RC = %d", rc);
+
+    memset(cbmem1_entry, 0xff, sizeof(cbmem1_entry));
+
+    /* Insert 65 1024 entries, and overflow buffer.  
+     * This should overflow two entries, because the buffer is sized for 64 
+     * entries, and then the headers themselves will eat into one of the entries, 
+     * so there should be a total of 63 entries.
+     * Ensure no data corruption.
+     */
+    for (i = 0; i < 65; i++) {
+        cbmem1_entry[0] = i;
+        rc = cbmem_append(&cbmem1, cbmem1_entry, sizeof(cbmem1_entry));
+        TEST_ASSERT_FATAL(rc == 0, "Could not append entry %d, rc = %d", i, rc);
+    }
+}
+
+static int 
+cbmem_test_case_1_walk(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, 
+        void *arg)
+{
+    uint8_t expected;
+    uint8_t actual;
+    int rc;
+
+    expected = *(uint8_t *) arg;
+
+    rc = cbmem_read(cbmem, hdr, &actual, 0, sizeof(actual));
+    TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
+    TEST_ASSERT_FATAL(actual == expected, 
+            "Actual doesn't equal expected (%d = %d)", actual, expected);
+
+    *(uint8_t *) arg = ++expected;
+
+    return (0);
+}
+
+TEST_CASE(cbmem_test_case_1) 
+{
+    int i;
+    int rc;
+
+    /* i starts at 2, for the 2 overwritten entries. */
+    i = 2;
+    rc = cbmem_walk(&cbmem1, cbmem_test_case_1_walk, &i);
+    TEST_ASSERT_FATAL(rc == 0, "Could not walk cbmem tree!  rc = %d", rc);
+    TEST_ASSERT_FATAL(i == 65, 
+            "Did not go through every element of walk, %d processed", i - 2);
+
+}
+
+TEST_CASE(cbmem_test_case_2)
+{
+    struct cbmem_entry_hdr *hdr;
+    struct cbmem_iter iter;
+    uint8_t i;
+    uint8_t val;
+    int rc;
+
+    i = 2;
+    cbmem_iter_start(&cbmem1, &iter);
+    while (1) {
+        hdr = cbmem_iter_next(&cbmem1, &iter);
+        if (hdr == NULL) {
+            break;
+        }
+
+        rc = cbmem_read(&cbmem1, hdr, &val, 0, sizeof(val));
+        TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
+        TEST_ASSERT_FATAL(val == i, "Entry index does not match %d vs %d", 
+                val, i);
+
+        i++;
+    }
+
+    /* i starts at 2, for the 2 overwritten entries */
+    TEST_ASSERT_FATAL(i == 65, 
+            "Did not iterate through all 63 elements of CBMEM1, processed %d", 
+            i - 2);
+}
+
+TEST_CASE(cbmem_test_case_3)
+{
+    struct cbmem_entry_hdr *hdr;
+    struct cbmem_iter iter;
+    uint16_t off;
+    uint16_t len;
+    uint8_t buf[128];
+    int i;
+    int rc;
+
+    i = 0;
+    cbmem_iter_start(&cbmem1, &iter);
+    while (1) {
+        hdr = cbmem_iter_next(&cbmem1, &iter);
+        if (hdr == NULL) {
+            break;
+        }
+        
+        /* first ensure we can read the entire entry */
+        off = 0;
+        len = 0;
+        while (1) {
+            rc = cbmem_read(&cbmem1, hdr, buf, off, sizeof(buf));
+            TEST_ASSERT_FATAL(rc >= 0,
+                    "Error reading from buffer rc=%d, off=%d,len=%d", rc, off, 
+                    sizeof(buf));
+            if (rc == 0) {
+                break;
+            }
+            off += rc;
+            len += rc;
+        }
+        TEST_ASSERT_FATAL(len == 1024, 
+                "Couldn't read full entry, expected %d got %d", 1024, len);
+        i++;
+
+        /* go apesh*t, and read data out of bounds, see what we get. */
+        rc = cbmem_read(&cbmem1, hdr, buf, 2048, sizeof(buf));
+        TEST_ASSERT_FATAL(rc < 0, 
+                "Reading invalid should return error, instead %d returned.",
+                rc);
+    }
+}
+
+TEST_SUITE(cbmem_test_suite)
+{
+    setup_cbmem1();
+    cbmem_test_case_1();
+    cbmem_test_case_2();
+    cbmem_test_case_3();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/313f6532/libs/util/src/test/util_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test.c b/libs/util/src/test/util_test.c
new file mode 100644
index 0000000..93d4125
--- /dev/null
+++ b/libs/util/src/test/util_test.c
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 <assert.h>
+#include <stddef.h>
+#include "testutil/testutil.h"
+#include "util_test_priv.h" 
+
+int
+util_test_all(void)
+{
+    cbmem_test_suite();
+    return tu_case_failed;
+}
+
+#ifdef PKG_TEST
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    util_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/313f6532/libs/util/src/test/util_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test_priv.h b/libs/util/src/test/util_test_priv.h
new file mode 100644
index 0000000..873dded
--- /dev/null
+++ b/libs/util/src/test/util_test_priv.h
@@ -0,0 +1,22 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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.
+ */
+
+#ifndef __UTIL_TEST_PRIV_
+#define __UTIL_TEST_PRIV_
+
+int cbmem_test_suite(void);
+
+#endif


[2/3] incubator-mynewt-larva git commit: check offset on cbmem_read() to make sure we're not reading beyond the bounds of the buffer. align cbmem_walk() on a single line.

Posted by st...@apache.org.
check offset on cbmem_read() to make sure we're not reading beyond the bounds of the buffer. align cbmem_walk() on a single line.


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

Branch: refs/heads/master
Commit: 29571104fbe433b1d1576995a5806805ca9ff030
Parents: 2a06140
Author: Sterling Hughes <st...@apache.org>
Authored: Tue Nov 10 18:44:52 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Tue Nov 10 18:45:26 2015 -0800

----------------------------------------------------------------------
 libs/util/src/cbmem.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/29571104/libs/util/src/cbmem.c
----------------------------------------------------------------------
diff --git a/libs/util/src/cbmem.c b/libs/util/src/cbmem.c
index 945270e..07968e1 100644
--- a/libs/util/src/cbmem.c
+++ b/libs/util/src/cbmem.c
@@ -215,6 +215,12 @@ cbmem_read(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, void *buf,
         len = hdr->ceh_len - off;
     }
 
+    if (off > hdr->ceh_len) {
+        rc = -1;
+        cbmem_lock_release(cbmem);
+        goto err;
+    }
+
     memcpy(buf, (uint8_t *) hdr + sizeof(*hdr) + off, len);
 
     rc = cbmem_lock_release(cbmem);
@@ -229,8 +235,7 @@ err:
 
 
 int 
-cbmem_walk(struct cbmem *cbmem, cbmem_walk_func_t walk_func, 
-        void *arg)
+cbmem_walk(struct cbmem *cbmem, cbmem_walk_func_t walk_func, void *arg)
 {
     struct cbmem_entry_hdr *hdr;
     struct cbmem_iter iter;


Re: [3/3] incubator-mynewt-larva git commit: ensure that tu_suite_name and tu_suite_faiiled don't get removed by linker.

Posted by Sterling Hughes <st...@apache.org>.
Wanted to bring this up for discussion on dev@ list.

This is a hack to have these symbols included.  We think we ran into
this error because of linker ordering issues: testutil was always
after the other packages, but when we added util, and tests on the
util package, we finally had a package that included testutil, that
was after it in the linker order.  This meant the linker discarded
these symbols, before seeing that they were being used.

We should pass the linker the resolve circular dependencies option on
sim in order to avoid this specific issues.

However, I think we also should look at having a special section in
our linker files, for symbols that we always want included that isn't
.data?  That way you're not required to assign these variables at
compile time in order to have them used.

Thoughts?


On Tue, Nov 10, 2015 at 6:45 PM,  <st...@apache.org> wrote:
> ensure that tu_suite_name and tu_suite_faiiled don't get removed by linker.
>
>
> Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
> Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/2a061408
> Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/2a061408
> Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/2a061408
>
> Branch: refs/heads/master
> Commit: 2a06140858a145fe1e1ea46e930990e8d288d8f7
> Parents: 8895970
> Author: Sterling Hughes <st...@apache.org>
> Authored: Tue Nov 10 18:43:52 2015 -0800
> Committer: Sterling Hughes <st...@apache.org>
> Committed: Tue Nov 10 18:45:26 2015 -0800
>
> ----------------------------------------------------------------------
>  libs/testutil/src/suite.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2a061408/libs/testutil/src/suite.c
> ----------------------------------------------------------------------
> diff --git a/libs/testutil/src/suite.c b/libs/testutil/src/suite.c
> index b6decb0..a6f2126 100644
> --- a/libs/testutil/src/suite.c
> +++ b/libs/testutil/src/suite.c
> @@ -18,8 +18,8 @@
>  #include "testutil/testutil.h"
>  #include "testutil_priv.h"
>
> -const char *tu_suite_name;
> -int tu_suite_failed;
> +const char *tu_suite_name = 0;
> +int tu_suite_failed = 0;
>
>  static void
>  tu_suite_set_name(const char *name)
>

[3/3] incubator-mynewt-larva git commit: ensure that tu_suite_name and tu_suite_faiiled don't get removed by linker.

Posted by st...@apache.org.
ensure that tu_suite_name and tu_suite_faiiled don't get removed by linker.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/2a061408
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/2a061408
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/2a061408

Branch: refs/heads/master
Commit: 2a06140858a145fe1e1ea46e930990e8d288d8f7
Parents: 8895970
Author: Sterling Hughes <st...@apache.org>
Authored: Tue Nov 10 18:43:52 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Tue Nov 10 18:45:26 2015 -0800

----------------------------------------------------------------------
 libs/testutil/src/suite.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2a061408/libs/testutil/src/suite.c
----------------------------------------------------------------------
diff --git a/libs/testutil/src/suite.c b/libs/testutil/src/suite.c
index b6decb0..a6f2126 100644
--- a/libs/testutil/src/suite.c
+++ b/libs/testutil/src/suite.c
@@ -18,8 +18,8 @@
 #include "testutil/testutil.h"
 #include "testutil_priv.h"
 
-const char *tu_suite_name;
-int tu_suite_failed;
+const char *tu_suite_name = 0;
+int tu_suite_failed = 0;
 
 static void
 tu_suite_set_name(const char *name)