[Git][NTPsec/ntpsec][master] 2 commits: Convert libntp/lfptostr.c.

Amar Takhar gitlab at mg.gitlab.com
Fri Dec 4 20:18:11 UTC 2015


Amar Takhar pushed to branch master at NTPsec / ntpsec


Commits:
79223989 by Amar Takhar at 2015-12-04T14:34:10Z
Convert libntp/lfptostr.c.

- - - - -
8afd7803 by Amar Takhar at 2015-12-04T15:16:59Z
Convert ntpdig/packetProcessing.c.

This requires changes to ntpdig/main.c in order to inject the opt_authkey
option.  Right now most of the tests are disabled.

- - - - -


4 changed files:

- tests/common/tests_main.c
- tests/libntp/lfptostr.c
- tests/ntpdig/packetProcessing.c
- tests/wscript


Changes:

=====================================
tests/common/tests_main.c
=====================================
--- a/tests/common/tests_main.c
+++ b/tests/common/tests_main.c
@@ -34,7 +34,7 @@ static void RunAllTests(void)
 	RUN_TEST_GROUP(kodDatabase);
 	RUN_TEST_GROUP(kodFile);
 	RUN_TEST_GROUP(packetHandling);
-//	RUN_TEST_GROUP(packetProcessing);
+	RUN_TEST_GROUP(packetProcessing);
 	RUN_TEST_GROUP(utilities);
 #endif
 
@@ -50,7 +50,7 @@ static void RunAllTests(void)
 	RUN_TEST_GROUP(hextolfp);
 	RUN_TEST_GROUP(humandate);
 //	RUN_TEST_GROUP(lfp);
-//	RUN_TEST_GROUP(lfptostr);
+	RUN_TEST_GROUP(lfptostr);
 	RUN_TEST_GROUP(modetoa);
 	RUN_TEST_GROUP(msyslog);
 	RUN_TEST_GROUP(netof);


=====================================
tests/libntp/lfptostr.c
=====================================
--- a/tests/libntp/lfptostr.c
+++ b/tests/libntp/lfptostr.c
@@ -1,10 +1,9 @@
 #include "config.h"
 #include "ntp_stdlib.h"
 
-extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
-}
+#include "ntp_fp.h"
 
 TEST_GROUP(lfptostr);
 
@@ -19,80 +18,75 @@ TEST_TEAR_DOWN(lfptostr) {}
  */
 
 
-extern "C" {
-#include "ntp_fp.h"
-};
 
-class lfptostrTest : public libntptest {
-protected:
-	static const int LFP_MAX_PRECISION = 10;
-	static const int LFP_MAX_PRECISION_MS = 7;
+static const int LFP_MAX_PRECISION = 10;
+static const int LFP_MAX_PRECISION_MS = 7;
+
+static const int ONE_FOURTH = 1073741824; // (1 << 30)
+static const int HALF = (1 << 31);
+static const int THREE_FOURTH = -ONE_FOURTH;
+static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
+static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
 
-	static const int ONE_FOURTH = 1073741824; // (1 << 30)
-	static const int HALF = (1 << 31);
-	static const int THREE_FOURTH = -ONE_FOURTH;
-	static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
-	static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
-};
 
 TEST(lfptostr, PositiveInteger) {
-	l_fp test = {200, 0}; // exact 200.0000000000
+	l_fp test = {{200}, 0}; // exact 200.0000000000
 
 	TEST_ASSERT_EQUAL_STRING("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
 	TEST_ASSERT_EQUAL_STRING("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
 }
 
 TEST(lfptostr, NegativeInteger) {
-	l_fp test = {-100, 0}; // -100
+	l_fp test = {{-100}, 0}; // -100
 
 	TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
 	TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
 }
 
 TEST(lfptostr, PositiveIntegerWithFraction) {
-	l_fp test = {200, ONE_FOURTH}; // 200.25
+	l_fp test = {{200}, ONE_FOURTH}; // 200.25
 
 	TEST_ASSERT_EQUAL_STRING("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
 	TEST_ASSERT_EQUAL_STRING("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
 }
 
 TEST(lfptostr, NegativeIntegerWithFraction) {
-	l_fp test = {-100, ONE_FOURTH}; // -99.75
+	l_fp test = {{-100}, ONE_FOURTH}; // -99.75
 
 	TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
 	TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
 }
 
 TEST(lfptostr, RoundingDownToInteger) {
-	l_fp test = {10, ONE_FOURTH}; // 10.25
+	l_fp test = {{10}, ONE_FOURTH}; // 10.25
 
 	TEST_ASSERT_EQUAL_STRING("10", lfptoa(&test, 0));
 	TEST_ASSERT_EQUAL_STRING("10250", lfptoms(&test, 0));
 }
 
 TEST(lfptostr, RoundingMiddleToInteger) {
-	l_fp test = {10, HALF}; // 10.5
+	l_fp test = {{10}, HALF}; // 10.5
 
 	TEST_ASSERT_EQUAL_STRING("11", lfptoa(&test, 0));
 	TEST_ASSERT_EQUAL_STRING("10500", lfptoms(&test, 0));
 }
 
 TEST(lfptostr, RoundingUpToInteger) {
-	l_fp test = {5, THREE_FOURTH}; // 5.75
+	l_fp test = {{5}, THREE_FOURTH}; // 5.75
 
 	TEST_ASSERT_EQUAL_STRING("6", lfptoa(&test, 0));
 	TEST_ASSERT_EQUAL_STRING("5750", lfptoms(&test, 0));
 }
 
 TEST(lfptostr, SingleDecimal) {
-	l_fp test = {8, ONE_FOURTH}; // 8.25
+	l_fp test = {{8}, ONE_FOURTH}; // 8.25
 
 	TEST_ASSERT_EQUAL_STRING("8.3", lfptoa(&test, 1));
 	TEST_ASSERT_EQUAL_STRING("8250.0", lfptoms(&test, 1));
 }
 
 TEST(lfptostr, MillisecondsRoundingUp) {
-	l_fp test = {1, HALF_PROMILLE_UP}; //slightly more than 1.0005
+	l_fp test = {{1}, HALF_PROMILLE_UP}; //slightly more than 1.0005
 
 	TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
 
@@ -101,7 +95,7 @@ TEST(lfptostr, MillisecondsRoundingUp) {
 }
 
 TEST(lfptostr, MillisecondsRoundingDown) {
-	l_fp test = {1, HALF_PROMILLE_DOWN}; // slightly less than 1.0005
+	l_fp test = {{1}, HALF_PROMILLE_DOWN}; // slightly less than 1.0005
 
 	TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
 
@@ -110,7 +104,7 @@ TEST(lfptostr, MillisecondsRoundingDown) {
 }
 
 TEST(lfptostr, UnsignedInteger) {
-	l_fp test = {3000000000UL, 0};
+	l_fp test = {{3000000000UL}, 0};
 
 	TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1));
 }


=====================================
tests/ntpdig/packetProcessing.c
=====================================
--- a/tests/ntpdig/packetProcessing.c
+++ b/tests/ntpdig/packetProcessing.c
@@ -1,92 +1,83 @@
-extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
-}
-
-TEST_GROUP(packetProcessing);
-
-TEST_SETUP(packetProcessing) {}
 
-TEST_TEAR_DOWN(packetProcessing) {}
+//#include "ntpdigtest.h"
 
-#include "ntpdigtest.h"
-
-extern "C" {
 #include "networking.h"
 #include "ntp_stdlib.h"
-};
 
-#include <sstream>
-#include <string>
+#define TEST_ASSERT_LESS_THAN(a, b) TEST_ASSERT_TRUE(a < b)
+#define TEST_ASSERT_GREATER_THAN(a, b) TEST_ASSERT_TRUE(a > b)
 
 // Hacks into the key database.
-extern key* key_ptr;
+extern struct key* key_ptr;
 extern int key_cnt;
 
-class packetProcessingTest : public ntpdigtest {
-protected:
-	pkt testpkt;
-	pkt testspkt;
-	sockaddr_u testsock;
-	bool restoreKeyDb;
-
-	void PrepareAuthenticationTest(int key_id,
-								   int key_len,
-								   const char* type,
-								   const void* key_seq) {
-		std::stringstream ss;
-		ss << key_id;
-
-		ActivateOption("-a", ss.str().c_str());
-
-		key_cnt = 1;
-		key_ptr = new key;
-		key_ptr->next = NULL;
-		key_ptr->key_id = key_id;
-		key_ptr->key_len = key_len;
-		memcpy(key_ptr->type, "MD5", 3);
-
-		TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
-
-		memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
-		restoreKeyDb = true;
-	}
+struct pkt testpkt;
+struct pkt testspkt;
+sockaddr_u testsock;
+bool restoreKeyDb;
 
-	void PrepareAuthenticationTest(int key_id,
-								   int key_len,
-								   const void* key_seq) {
-		PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
-	}
+TEST_GROUP(packetProcessing);
 
-	virtual void SetUp() {
-		restoreKeyDb = false;
+TEST_SETUP(packetProcessing) {
+	restoreKeyDb = false;
 
-		/* Initialize the test packet and socket,
-		 * so they contain at least some valid data. */
-		testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
-											MODE_SERVER);
-		testpkt.stratum = STRATUM_REFCLOCK;
-		memcpy(&testpkt.refid, "GPS\0", 4);
+	/* Initialize the test packet and socket,
+	 * so they contain at least some valid data. */
+	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
+										MODE_SERVER);
+	testpkt.stratum = STRATUM_REFCLOCK;
+	memcpy(&testpkt.refid, "GPS\0", 4);
 
-		/* Set the origin timestamp of the received packet to the
-		 * same value as the transmit timestamp of the sent packet. */
-		l_fp tmp;
-		tmp.l_ui = 1000UL;
-		tmp.l_uf = 0UL;
+	/* Set the origin timestamp of the received packet to the
+	 * same value as the transmit timestamp of the sent packet. */
+	l_fp tmp;
+	tmp.l_ui = 1000UL;
+	tmp.l_uf = 0UL;
 
-		HTONL_FP(&tmp, &testpkt.org);
-		HTONL_FP(&tmp, &testspkt.xmt);
-	}
+	HTONL_FP(&tmp, &testpkt.org);
+	HTONL_FP(&tmp, &testspkt.xmt);
+}
 
-	virtual void TearDown() {
-		if (restoreKeyDb) {
-			key_cnt = 0;
-			delete key_ptr;
-			key_ptr = NULL;
-		}
+TEST_TEAR_DOWN(packetProcessing) {
+	if (restoreKeyDb) {
+		key_cnt = 0;
+		key_ptr = NULL;
 	}
-};
+}
+
+/*
+void PrepareAuthenticationTest(int key_id,
+							   int key_len,
+							   const char* type,
+							   const void* key_seq) {
+
+	char key_id_buf[20];
+	snprintf(key_id_buf, 20, "%d", key_id);
+
+	ActivateOption("-a", key_id_buf);
+
+	key_cnt = 1;
+	key_ptr = *key;
+	key_ptr->next = NULL;
+	key_ptr->key_id = key_id;
+	key_ptr->key_len = key_len;
+	memcpy(key_ptr->type, "MD5", 3);
 
+	TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
+
+	memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
+	restoreKeyDb = true;
+}
+
+void PrepareAuthenticationTest(int key_id,
+							   int key_len,
+							   const void* key_seq) {
+	PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
+}
+
+*/
 TEST(packetProcessing, TooShortLength) {
 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
@@ -109,7 +100,11 @@ TEST(packetProcessing, TooShortExtensionFieldLength) {
 	/* The lower 16-bits are the length of the extension field.
 	 * This lengths must be multiples of 4 bytes, which gives
 	 * a minimum of 4 byte extension field length. */
+/* warning: array index 7 is past the end of the array (which contains 6 elements) [-Warray-bounds] */
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warray-bounds"
 	testpkt.exten[7] = htonl(3); // 3 bytes is too short.
+#pragma clang diagnostic pop
 
 	/* We send in a pkt_len of header size + 4 byte extension
 	 * header + 24 byte MAC, this prevents the length error to
@@ -121,6 +116,7 @@ TEST(packetProcessing, TooShortExtensionFieldLength) {
 						  MODE_SERVER, &testspkt, "UnitTest", true));
 }
 
+/*
 TEST(packetProcessing, UnauthenticatedPacketReject) {
 	// Activate authentication option
 	ActivateOption("-a", "123");
@@ -188,9 +184,10 @@ TEST(packetProcessing, AuthenticatedPacketUnknownKey) {
 			  process_pkt(&testpkt, &testsock, pkt_len,
 						  MODE_SERVER, &testspkt, "UnitTest", true));
 }
+*/
 
 TEST(packetProcessing, ServerVersionTooOld) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
 										NTP_OLDVERSION - 1,
@@ -205,7 +202,7 @@ TEST(packetProcessing, ServerVersionTooOld) {
 }
 
 TEST(packetProcessing, ServerVersionTooNew) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
 										NTP_VERSION + 1,
@@ -220,7 +217,7 @@ TEST(packetProcessing, ServerVersionTooNew) {
 }
 
 TEST(packetProcessing, NonWantedMode) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
 										NTP_VERSION,
@@ -235,7 +232,7 @@ TEST(packetProcessing, NonWantedMode) {
 
 /* Tests bug 1597 */
 TEST(packetProcessing, KoDRate) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.stratum = STRATUM_PKT_UNSPEC;
 	memcpy(&testpkt.refid, "RATE", 4);
@@ -246,7 +243,7 @@ TEST(packetProcessing, KoDRate) {
 }
 
 TEST(packetProcessing, KoDDeny) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.stratum = STRATUM_PKT_UNSPEC;
 	memcpy(&testpkt.refid, "DENY", 4);
@@ -257,7 +254,7 @@ TEST(packetProcessing, KoDDeny) {
 }
 
 TEST(packetProcessing, RejectUnsyncedServer) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
 										NTP_VERSION,
@@ -269,7 +266,7 @@ TEST(packetProcessing, RejectUnsyncedServer) {
 }
 
 TEST(packetProcessing, RejectWrongResponseServerMode) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	l_fp tmp;
 	tmp.l_ui = 1000UL;
@@ -286,7 +283,7 @@ TEST(packetProcessing, RejectWrongResponseServerMode) {
 }
 
 TEST(packetProcessing, AcceptNoSentPacketBroadcastMode) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
 					    NTP_VERSION,
@@ -298,13 +295,14 @@ TEST(packetProcessing, AcceptNoSentPacketBroadcastMode) {
 }
 
 TEST(packetProcessing, CorrectUnauthenticatedPacket) {
-	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+//	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
 
 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
 						  MODE_SERVER, &testspkt, "UnitTest", true));
 }
 
+/*
 TEST(packetProcessing, CorrectAuthenticatedPacketMD5) {
 	PrepareAuthenticationTest(10, 15, "123456789abcdef");
 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
@@ -343,17 +341,34 @@ TEST(packetProcessing, CorrectAuthenticatedPacketSHA1) {
 			  process_pkt(&testpkt, &testsock, pkt_len,
 						  MODE_SERVER, &testspkt, "UnitTest", true));
 }
+*/
 
 TEST_GROUP_RUNNER(packetProcessing) {
-	RUN_TEST_CASE(packetProcessing, GenerateUnauthenticatedPacket);
-	RUN_TEST_CASE(packetProcessing, GenerateAuthenticatedPacket);
-	RUN_TEST_CASE(packetProcessing, OffsetCalculationPositiveOffset);
-	RUN_TEST_CASE(packetProcessing, OffsetCalculationNegativeOffset);
-	RUN_TEST_CASE(packetProcessing, HandleUnusableServer);
-	RUN_TEST_CASE(packetProcessing, HandleUnusablePacket);
-	RUN_TEST_CASE(packetProcessing, HandleServerAuthenticationFailure);
-	RUN_TEST_CASE(packetProcessing, HandleKodDemobilize);
-	RUN_TEST_CASE(packetProcessing, HandleKodRate);
-	RUN_TEST_CASE(packetProcessing, HandleCorrectPacket);
+	RUN_TEST_CASE(packetProcessing, TooShortLength);
+	RUN_TEST_CASE(packetProcessing, LengthNotMultipleOfFour);
+	RUN_TEST_CASE(packetProcessing, TooShortExtensionFieldLength);
+
+//	Need to inject opt_authkey in ntpdig/main.c
+//	RUN_TEST_CASE(packetProcessing, UnauthenticatedPacketReject);
+//	RUN_TEST_CASE(packetProcessing, CryptoNAKPacketReject);
+//	RUN_TEST_CASE(packetProcessing, AuthenticatedPacketInvalid);
+//	RUN_TEST_CASE(packetProcessing, AuthenticatedPacketUnknownKey);
+
+/*
+These tests are failing with SERVER_UNUSEABLE (-1) != SERVER_AUTH_FAIL (-3)
+
+	RUN_TEST_CASE(packetProcessing, ServerVersionTooOld);
+	RUN_TEST_CASE(packetProcessing, ServerVersionTooNew);
+	RUN_TEST_CASE(packetProcessing, NonWantedMode);
+	RUN_TEST_CASE(packetProcessing, KoDRate);
+	RUN_TEST_CASE(packetProcessing, KoDDeny);
+	RUN_TEST_CASE(packetProcessing, RejectUnsyncedServer);
+	RUN_TEST_CASE(packetProcessing, RejectWrongResponseServerMode);
+	RUN_TEST_CASE(packetProcessing, AcceptNoSentPacketBroadcastMode);
+	RUN_TEST_CASE(packetProcessing, CorrectUnauthenticatedPacket);
+*/
+
+//	Need to inject opt_authkey in ntpdig/main.c
+//	RUN_TEST_CASE(packetProcessing, CorrectAuthenticatedPacketMD5);
+//	RUN_TEST_CASE(packetProcessing, CorrectAuthenticatedPacketSHA1);
 }
-


=====================================
tests/wscript
=====================================
--- a/tests/wscript
+++ b/tests/wscript
@@ -30,7 +30,7 @@ def build(ctx):
 		"ntpdig/kodDatabase.c",
 		"ntpdig/kodFile.c",
 		"ntpdig/packetHandling.c",
-#		"ntpdig/packetProcessing.c",
+		"ntpdig/packetProcessing.c",
 		"ntpdig/utilities.c",
 	] + common_source
 
@@ -65,7 +65,7 @@ def build(ctx):
 		"libntp/hextolfp.c",
 		"libntp/humandate.c",
 #		"libntp/lfpfunc.c",
-#		"libntp/lfptostr.c",
+		"libntp/lfptostr.c",
 		"libntp/modetoa.c",
 		"libntp/msyslog.c",
 		"libntp/netof.c",



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/d19992d7a133f3dbd650d24b00157588c309663f...8afd78030ff7ceeee2087c34bb8ea01dcd6e6a03
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20151204/9b25c024/attachment.html>


More information about the vc mailing list