[Git][NTPsec/ntpsec][master] 11 commits: Added stdbool include.

Eric S. Raymond gitlab at mg.gitlab.com
Thu Feb 21 15:57:57 UTC 2019


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
3a9fefeb by Ian Bruene at 2019-02-21T15:49:48Z
Added stdbool include.

- - - - -
16b608a1 by Ian Bruene at 2019-02-21T15:49:48Z
Added nts_append_record_null test.

- - - - -
5bcf6bd8 by Ian Bruene at 2019-02-21T15:49:48Z
Added test for nts_append_record_uint16.

- - - - -
ff074558 by Ian Bruene at 2019-02-21T15:49:48Z
Added test for nts_append_record_bytes.

- - - - -
782b6227 by Ian Bruene at 2019-02-21T15:49:48Z
Added test for nts_append_header.

- - - - -
bad4b561 by Ian Bruene at 2019-02-21T15:49:48Z
Fix for old debians not understanding explicit types.

- - - - -
e17ae135 by Ian Bruene at 2019-02-21T15:49:48Z
Added test for nts_append_uint16.

- - - - -
099c3269 by Ian Bruene at 2019-02-21T15:49:48Z
Added test for nts_append_bytes.

- - - - -
d50f8588 by Ian Bruene at 2019-02-21T15:49:48Z
Added test and docstring for nts_next_record.

- - - - -
540f67f7 by Ian Bruene at 2019-02-21T15:49:48Z
Added test and docstring for nts_next_uint16.

- - - - -
8e2e70aa by Ian Bruene at 2019-02-21T15:49:48Z
Added test and docstring for nts_next_bytes.

- - - - -


4 changed files:

- include/nts.h
- ntpd/nts.c
- + tests/ntpd/nts.c
- tests/wscript


Changes:

=====================================
include/nts.h
=====================================
@@ -4,6 +4,7 @@
 #ifndef GUARD_NTS_H
 #define GUARD_NTS_H
 
+#include <stdbool.h>
 #include <openssl/ssl.h>
 
 #define NTS_MAX_KEYLEN		64	/* used in cookies */


=====================================
ntpd/nts.c
=====================================
@@ -11,6 +11,7 @@
  * The exception is client configuration, for which various bits have
  * to be set by the config parser.
  */
+
 #include "config.h"
 
 #include <arpa/inet.h>
@@ -192,7 +193,7 @@ void nts_append_bytes(BufCtl* buf, uint8_t *data, int length) {
   buf->left -= length;
 }
 
-
+/* Reads type and length of the next record, and moves cursor to the data */
 uint16_t nts_next_record(BufCtl* buf, int *length) {
   uint16_t *ptr = (uint16_t *)buf->next;
   uint16_t type = ntohs(*ptr++);
@@ -202,6 +203,7 @@ uint16_t nts_next_record(BufCtl* buf, int *length) {
   return type;
 }
 
+/* Reads a uint16 from the record and advances to the next data */
 uint16_t nts_next_uint16(BufCtl* buf) {
   uint16_t *ptr = (uint16_t *)buf->next;
   uint16_t data = ntohs(*ptr++);
@@ -210,6 +212,7 @@ uint16_t nts_next_uint16(BufCtl* buf) {
   return data;
 }
 
+/* Reads a string of bytes from the record and advances to the next data */
 uint16_t nts_next_bytes(BufCtl* buf, uint8_t *data, int length) {
   memcpy(data, buf->next, length);
   buf->next += length;


=====================================
tests/ntpd/nts.c
=====================================
@@ -0,0 +1,247 @@
+#include "nts_lib.h"
+#include "nts.h"
+#include "unity.h"
+#include "unity_fixture.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+TEST_GROUP(nts);
+
+TEST_SETUP(nts) {}
+
+TEST_TEAR_DOWN(nts) {}
+
+TEST(nts, nts_append_record_null) {
+  // Setup
+  uint8_t buf[128];
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 128;
+  // Run test
+  nts_append_record_null(&cursor, 0xFA7E);
+  // Check
+  TEST_ASSERT_EQUAL(buf[0], 0xFA);
+  TEST_ASSERT_EQUAL(buf[1], 0x7E);
+  TEST_ASSERT_EQUAL(buf[2], 0);
+  TEST_ASSERT_EQUAL(buf[3], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[4]);
+  TEST_ASSERT_EQUAL(cursor.left, 124);
+}
+
+TEST(nts, nts_append_record_uint16) {
+  // Test change
+  // Setup
+  uint8_t buf[16] = {0, 0, 0, 0, 0, 0, 0, 0,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  // Run test
+  nts_append_record_uint16(&cursor, 0xCAFE, 0x1234);
+  // Check
+  TEST_ASSERT_EQUAL(buf[0], 0xCA);
+  TEST_ASSERT_EQUAL(buf[1], 0xFE);
+  TEST_ASSERT_EQUAL(buf[2], 0x12);
+  TEST_ASSERT_EQUAL(buf[3], 0x34);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[4]);
+  TEST_ASSERT_EQUAL(cursor.left, 12);
+  // Test no change
+  // Setup
+  cursor.left = 0;
+  // Run test
+  nts_append_record_uint16(&cursor, 0xCAFE, 0x1234);
+  // Check
+  TEST_ASSERT_EQUAL(buf[4], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[4]);
+  TEST_ASSERT_EQUAL(cursor.left, 0);
+}
+
+TEST(nts, nts_append_record_bytes) {
+  // Test change
+  // Setup
+  uint8_t buf[16] = {0, 0, 0, 0, 0, 0, 0, 0,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  uint8_t data[6] = {0, 1, 2, 3, 4, 5};
+  // Run test
+  nts_append_record_bytes(&cursor, 0xCAFE, data, 6);
+  // Check
+  TEST_ASSERT_EQUAL(buf[0], 0xCA);
+  TEST_ASSERT_EQUAL(buf[1], 0xFE);
+  TEST_ASSERT_EQUAL(buf[2], 0);
+  TEST_ASSERT_EQUAL(buf[3], 1);
+  TEST_ASSERT_EQUAL(buf[4], 2);
+  TEST_ASSERT_EQUAL(buf[5], 3);
+  TEST_ASSERT_EQUAL(buf[6], 4);
+  TEST_ASSERT_EQUAL(buf[7], 5);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[8]);
+  TEST_ASSERT_EQUAL(cursor.left, 8);
+  // Test no change
+  // Setup
+  cursor.left = 0;
+  // Run test
+  nts_append_record_bytes(&cursor, 0xCAFE, data, 6);
+  // Check
+  TEST_ASSERT_EQUAL(buf[8], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[8]);
+  TEST_ASSERT_EQUAL(cursor.left, 0);
+}
+
+TEST(nts, nts_append_header) {
+  // Test change
+  // Setup
+  uint8_t buf[16];
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  // Run test
+  nts_append_header(&cursor, 0xFADE, 0x1234);
+  // Check
+  TEST_ASSERT_EQUAL(buf[0], 0xFA);
+  TEST_ASSERT_EQUAL(buf[1], 0xDE);
+  TEST_ASSERT_EQUAL(buf[2], 0x12);
+  TEST_ASSERT_EQUAL(buf[3], 0x34);
+  TEST_ASSERT_EQUAL(buf[4], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[4]);
+  TEST_ASSERT_EQUAL(cursor.left, 12);
+  // Test no change
+  // Setup
+  cursor.left = 0;
+  // Run test
+  nts_append_header(&cursor, 0xFEED, 0xABCD);
+  // Check
+  TEST_ASSERT_EQUAL(buf[4], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[4]);
+  TEST_ASSERT_EQUAL(cursor.left, 0);
+}
+
+TEST(nts, nts_append_uint16) {
+  // Test change
+  // Setup
+  uint8_t buf[16] = {0, 0, 0, 0, 0, 0, 0, 0,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  // Run test
+  nts_append_uint16(&cursor, 0x1234);
+  // Check
+  TEST_ASSERT_EQUAL(buf[0], 0x12);
+  TEST_ASSERT_EQUAL(buf[1], 0x34);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[2]);
+  TEST_ASSERT_EQUAL(cursor.left, 14);
+  // Test no change
+  // Setup
+  cursor.left = 0;
+  // Run test
+  nts_append_uint16(&cursor, 0x5678);
+  // Check
+  TEST_ASSERT_EQUAL(buf[2], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[2]);
+  TEST_ASSERT_EQUAL(cursor.left, 0);
+}
+
+TEST(nts, nts_append_bytes) {
+  // Test change
+  // Setup
+  uint8_t buf[16] = {0, 0, 0, 0, 0, 0, 0, 0,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  uint8_t data[6] = {0, 1, 2, 3, 4, 5};
+  // Run test
+  nts_append_bytes(&cursor, data, 6);
+  // Check
+  TEST_ASSERT_EQUAL(buf[0], 0);
+  TEST_ASSERT_EQUAL(buf[1], 1);
+  TEST_ASSERT_EQUAL(buf[2], 2);
+  TEST_ASSERT_EQUAL(buf[3], 3);
+  TEST_ASSERT_EQUAL(buf[4], 4);
+  TEST_ASSERT_EQUAL(buf[5], 5);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[6]);
+  TEST_ASSERT_EQUAL(cursor.left, 10);
+  // Test no change
+  // Setup
+  cursor.left = 0;
+  // Run test
+  nts_append_bytes(&cursor, data, 6);
+  // Check
+  TEST_ASSERT_EQUAL(buf[6], 0);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[6]);
+  TEST_ASSERT_EQUAL(cursor.left, 0);
+}
+
+TEST(nts, nts_next_record) {
+  // Setup
+  uint8_t buf[16] = {0xFA, 0xCE, 0, 4, 0xFF, 0xEE, 0xDD, 0xCC,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  int length;
+  uint16_t type;
+  // Run test
+  type = nts_next_record(&cursor, &length);
+  // Check
+  TEST_ASSERT_EQUAL(length, 4);
+  TEST_ASSERT_EQUAL(type, 0xFACE);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[4]);
+  TEST_ASSERT_EQUAL(cursor.left, 12);
+}
+
+TEST(nts, nts_next_uint16) {
+  // Setup
+  uint8_t buf[16] = {0xFA, 0xCE, 0, 4, 0xFF, 0xEE, 0xDD, 0xCC,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  uint16_t data;
+  // Run test
+  data = nts_next_uint16(&cursor);
+  // Check
+  TEST_ASSERT_EQUAL(data, 0xFACE);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[2]);
+  TEST_ASSERT_EQUAL(cursor.left, 14);
+}
+
+TEST(nts, nts_next_bytes) {
+  // Setup
+  uint8_t buf[16] = {0xFA, 0xCE, 0, 4, 0xFF, 0xEE, 0xDD, 0xCC,
+					 0, 0, 0, 0, 0, 0, 0, 0};
+  BufCtl cursor;
+  cursor.next = buf;
+  cursor.left = 16;
+  uint8_t data[8];
+  uint16_t length;
+  // Run test
+  length = nts_next_bytes(&cursor, data, 8);
+  // Check
+  TEST_ASSERT_EQUAL(length, 8);
+  TEST_ASSERT_EQUAL(data[0], 0xFA);
+  TEST_ASSERT_EQUAL(data[1], 0xCE);
+  TEST_ASSERT_EQUAL(data[2], 0);
+  TEST_ASSERT_EQUAL(data[3], 4);
+  TEST_ASSERT_EQUAL(data[4], 0xFF);
+  TEST_ASSERT_EQUAL(data[5], 0xEE);
+  TEST_ASSERT_EQUAL(data[6], 0xDD);
+  TEST_ASSERT_EQUAL(data[7], 0xCC);
+  TEST_ASSERT_EQUAL(cursor.next, &buf[8]);
+  TEST_ASSERT_EQUAL(cursor.left, 8);
+}
+
+TEST_GROUP_RUNNER(nts) {
+  RUN_TEST_CASE(nts, nts_append_record_null);
+  RUN_TEST_CASE(nts, nts_append_record_uint16);
+  RUN_TEST_CASE(nts, nts_append_record_bytes);
+  RUN_TEST_CASE(nts, nts_append_header);
+  RUN_TEST_CASE(nts, nts_append_uint16);
+  RUN_TEST_CASE(nts, nts_append_bytes);
+  RUN_TEST_CASE(nts, nts_next_record);
+  RUN_TEST_CASE(nts, nts_next_uint16);
+  RUN_TEST_CASE(nts, nts_next_bytes);
+}


=====================================
tests/wscript
=====================================
@@ -101,7 +101,8 @@ def build(ctx):
         "ntpd/leapsec.c",
         "ntpd/restrict.c",
         "ntpd/recvbuff.c",
-        "ntpd/nts_lib.c"
+        "ntpd/nts_lib.c",
+		"ntpd/nts.c"
     ] + common_source
 
     ctx.ntp_test(



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/8b4629daed98eed461a8bf9acaa3d0a90fbb7a74...8e2e70aa670aaaa1d1aabbfedb8513c4a3ed61f0

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/8b4629daed98eed461a8bf9acaa3d0a90fbb7a74...8e2e70aa670aaaa1d1aabbfedb8513c4a3ed61f0
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190221/f9770600/attachment-0001.html>


More information about the vc mailing list