]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
test: extend ntp_core unit test
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 27 Aug 2018 15:18:20 +0000 (17:18 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 28 Aug 2018 16:27:03 +0000 (18:27 +0200)
test/unit/ntp_core.c
test/unit/ntp_core.keys

index 074c4d7bb68eeccf31274c9ad0db00d8ed1a1e75..5e519e5641c4e8273542e2e245a3832b8645f26d 100644 (file)
@@ -63,6 +63,18 @@ advance_time(double x)
   UTI_AddDoubleToTimespec(&current_time, x, &current_time);
 }
 
+static uint32_t
+get_random_key_id(void)
+{
+  uint32_t id;
+
+  do {
+    id = random() % 6 + 2;
+  } while (!KEY_KeyKnown(id));
+
+  return id;
+}
+
 static void
 send_request(NCR_Instance inst)
 {
@@ -122,6 +134,7 @@ static void
 send_response(int interleaved, int authenticated, int allow_update, int valid_ts, int valid_auth)
 {
   NTP_Packet *req, *res;
+  int auth_len = 0;
 
   req = &req_buffer.ntp_pkt;
   res = &res_buffer.ntp_pkt;
@@ -168,24 +181,41 @@ send_response(int interleaved, int authenticated, int allow_update, int valid_ts
   }
 
   if (authenticated) {
-    res->auth_keyid = req->auth_keyid;
-    KEY_GenerateAuth(ntohl(res->auth_keyid), (unsigned char *)res, NTP_NORMAL_PACKET_LENGTH,
-                     res->auth_data, 16);
-    res_length = NTP_NORMAL_PACKET_LENGTH + 4 + 16;
+    res->auth_keyid = req->auth_keyid ? req->auth_keyid : htonl(get_random_key_id());
+    auth_len = KEY_GetAuthLength(ntohl(res->auth_keyid));
+    assert(auth_len);
+    if (NTP_LVM_TO_VERSION(res->lvm) == 4 && random() % 2)
+      auth_len = MIN(auth_len, NTP_MAX_V4_MAC_LENGTH - 4);
+
+    if (KEY_GenerateAuth(ntohl(res->auth_keyid), (unsigned char *)res,
+                         NTP_NORMAL_PACKET_LENGTH, res->auth_data, auth_len) != auth_len)
+      assert(0);
+    res_length = NTP_NORMAL_PACKET_LENGTH + 4 + auth_len;
   } else {
     res_length = NTP_NORMAL_PACKET_LENGTH;
   }
 
-  if (!valid_auth) {
-    switch (random() % 3) {
+  if (!valid_auth && authenticated) {
+    assert(auth_len);
+
+    switch (random() % 4) {
       case 0:
-        res->auth_keyid++;
+        res->auth_keyid = htonl(ntohl(res->auth_keyid) + 1);
         break;
       case 1:
-        res->auth_data[random() % 16]++;
+        res->auth_keyid = htonl(ntohl(res->auth_keyid) ^ 1);
+        if (KEY_GenerateAuth(ntohl(res->auth_keyid), (unsigned char *)res,
+                             NTP_NORMAL_PACKET_LENGTH, res->auth_data, auth_len) != auth_len)
+          assert(0);
         break;
       case 2:
-        res_length = NTP_NORMAL_PACKET_LENGTH;
+        res->auth_data[random() % auth_len]++;
+        break;
+      case 3:
+        res_length = NTP_NORMAL_PACKET_LENGTH + 4 * (random() % ((4 + auth_len) / 4));
+        if (NTP_LVM_TO_VERSION(res->lvm) == 4 &&
+            res_length == NTP_NORMAL_PACKET_LENGTH + NTP_MAX_V4_MAC_LENGTH)
+          res_length -= 4;
         break;
       default:
         assert(0);
@@ -292,7 +322,10 @@ test_unit(void)
   NIO_Initialise(IPADDR_UNSPEC);
   NCR_Initialise();
   REF_Initialise();
+
+  TST_SuspendLogging();
   KEY_Initialise();
+  TST_ResumeLogging();
 
   CNF_SetupAccessRestrictions();
 
@@ -302,7 +335,7 @@ test_unit(void)
     if (random() % 2)
       source.params.interleaved = 1;
     if (random() % 2)
-      source.params.authkey = 1;
+      source.params.authkey = get_random_key_id();
     source.params.version = random() % 4 + 1;
 
     UTI_ZeroTimespec(&current_time);
index 7a70e52a1a4bd71b9021b3a2069f7488e9ddc459..f06237f4c3b5e7cb76657fc805eba292ac7cbf41 100644 (file)
@@ -1,2 +1,6 @@
-1 MD5 HEX:38979C567358C0896F4D9D459A3C8B8478654579
 2 MD5 HEX:38979C567358C0896F4D9D459A3C8B8478654579
+3 MD5 HEX:38979C567358C0896F4D9D459A3C8B8478654579
+4 SHA1 HEX:B71744EA01FBF01CA30D173ECDDF901952AE356A
+5 SHA1 HEX:B71744EA01FBF01CA30D173ECDDF901952AE356A
+6 SHA512 HEX:DE027482F22B201FC20863F58C74095E7906089F
+7 SHA512 HEX:DE027482F22B201FC20863F58C74095E7906089F