[Git][NTPsec/ntpsec][test_megapack] 13 commits: Add Gitlab CI job for coverity scans

Ian Bruene gitlab at mg.gitlab.com
Tue Jul 31 15:34:34 UTC 2018


Ian Bruene pushed to branch test_megapack at NTPsec / ntpsec


Commits:
aa4ac90a by Matt Selsky at 2018-07-31T02:20:07Z
Add Gitlab CI job for coverity scans

This job will only run via the scheduler; all other jobs will not run via the
scheduler.

- - - - -
e96f9441 by Matt Selsky at 2018-07-31T02:54:03Z
Combine curl arguments onto a single line

- - - - -
5b6bf7a2 by Matt Selsky at 2018-07-31T03:50:50Z
Replace local numtoa() with POSIX inet_ntoa()

- - - - -
29ef0204 by Ian Bruene at 2018-07-31T15:33:43Z
Added tests for statestr.c.

- - - - -
58b3d167 by Ian Bruene at 2018-07-31T15:33:43Z
Added tests for timespecops.c.

- - - - -
cb9aa81a by Ian Bruene at 2018-07-31T15:33:43Z
Added tests for ntp_endian.c.

- - - - -
1f08a41e by Ian Bruene at 2018-07-31T15:33:43Z
Fixed incorrect array size and missing #include.

- - - - -
4625a4a5 by Ian Bruene at 2018-07-31T15:33:43Z
Revert "Fixed incorrect array size and missing #include."

This reverts commit 50314ff2b12dca3ba5a47e64103ec6030169adab.

- - - - -
4eb1fdc0 by Ian Bruene at 2018-07-31T15:33:43Z
Fixed incorrect array size and missing #include.

- - - - -
b561c852 by Ian Bruene at 2018-07-31T15:33:43Z
Added tests for ntp_random.c.

- - - - -
4ed4c377 by Ian Bruene at 2018-07-31T15:33:43Z
Changed test value to something that should be universally available.

- - - - -
880d16ff by Ian Bruene at 2018-07-31T15:33:43Z
Added missing sys/time.h include.

- - - - -
c12a7338 by Ian Bruene at 2018-07-31T15:33:43Z
Changed sys/time.h imclude to sys/timex.h.

- - - - -


11 changed files:

- .gitlab-ci.yml
- include/ntp_stdlib.h
- libntp/numtoa.c
- ntpd/ntp_control.c
- ntpd/ntp_util.c
- + tests/libntp/ntp_endian.c
- tests/libntp/ntp_random.c
- tests/libntp/numtoa.c
- tests/libntp/statestr.c
- tests/libntp/timespecops.c
- tests/wscript


Changes:

=====================================
.gitlab-ci.yml
=====================================
@@ -1,6 +1,8 @@
 pages:
   stage: build
   image: alpine
+  except:
+    - schedules
   script:
     - apk update && apk add gcc bison musl-dev openssl-dev libcap-dev python2-dev asciidoc findutils gzip
     - python ./waf configure --enable-doc --prefix=/tmp/docbot-local --htmldir=`pwd`/public/latest/ build install
@@ -35,6 +37,8 @@ pages:
 
 .job_template: &job_definition
   stage: build
+  except:
+    - schedules
   artifacts:
     expire_in: 7d
     paths:
@@ -536,3 +540,14 @@ gentoo-hardened-refclocks:
     - python ./waf configure --refclock=all build
   tags:
     - gitlab-org
+
+coverity-scan:
+  script:
+    - ./waf configure
+    - /opt/cov-analysis/bin/cov-build --dir cov-int ./waf build
+    - tar czf ntpsec_coverity.tgz cov-int
+    - curl --form token=$COVERITY_TOKEN --form email=security at ntpsec.org --form file=@ntpsec_coverity.tgz --form version="$(git rev-parse --short HEAD)" --form description="Automatic submission by gitlab-ci" https://scan.coverity.com/builds?project=ntpsec
+  tags:
+    - ubuntu-1604-lts
+  only:
+    - schedules


=====================================
include/ntp_stdlib.h
=====================================
@@ -101,7 +101,6 @@ extern	const char * res_access_flags(unsigned short);
 extern	const char * k_st_flags	(uint32_t);
 extern	char *	statustoa	(int, int);
 extern	sockaddr_u * netof6	(sockaddr_u *);
-extern	char *	numtoa		(uint32_t);
 extern	const char * socktoa	(const sockaddr_u *);
 extern	const char * sockporttoa(const sockaddr_u *);
 extern	unsigned short	sock_hash(const sockaddr_u *) __attribute__((pure));


=====================================
libntp/numtoa.c
=====================================
@@ -12,25 +12,6 @@
 #include "lib_strbuf.h"
 #include "ntp_stdlib.h"
 
-char *
-numtoa(
-	uint32_t num
-	)
-{
-	uint32_t netnum;
-	char *buf;
-
-	netnum = ntohl(num);
-	buf = lib_getbuf();
-	snprintf(buf, LIB_BUFLENGTH, "%lu.%lu.%lu.%lu",
-		 ((unsigned long)netnum >> 24) & 0xff,
-		 ((unsigned long)netnum >> 16) & 0xff,
-		 ((unsigned long)netnum >> 8) & 0xff,
-		 (unsigned long)netnum & 0xff);
-	return buf;
-}
-
-
 /* Convert a refid & stratum to a string */
 const char *
 refid_str(
@@ -41,8 +22,11 @@ refid_str(
 	char *	text;
 	size_t	tlen;
 
-	if (stratum > 1)
-		return numtoa(refid);
+	if (stratum > 1) {
+		struct in_addr in4;
+		in4.s_addr = refid;
+		return inet_ntoa(in4);
+	}
 
 	text = lib_getbuf();
 	text[0] = '.';


=====================================
ntpd/ntp_control.c
=====================================
@@ -1365,8 +1365,11 @@ ctl_putadr(
 		*cp++ = *cq++;
 
 	*cp++ = '=';
-	if (NULL == addr)
-		cq = numtoa(addr32);
+	if (NULL == addr) {
+		struct in_addr in4;
+		in4.s_addr = addr32;
+		cq = inet_ntoa(in4);
+	}
 	else
 		cq = socktoa(addr);
 	INSIST((cp - buffer) < (int)sizeof(buffer));


=====================================
ntpd/ntp_util.c
=====================================
@@ -518,7 +518,7 @@ mprintf_clock_stats(
  * file format
  * day (MJD)
  * time (s past midnight)
- * source IP address old format) or drivername(unit) (new format)
+ * source IP address (old format) or drivername(unit) (new format)
  * destination peer address 
  * t1 t2 t3 t4 timestamps
  * various other local statistics


=====================================
tests/libntp/ntp_endian.c
=====================================
@@ -0,0 +1,36 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "ntp_endian.h"
+
+#include "unity.h"
+#include "unity_fixture.h"
+
+TEST_GROUP(endian);
+
+TEST_SETUP(endian) {}
+
+TEST_TEAR_DOWN(endian) {}
+
+TEST(endian, Bit16) {
+	uint8_t buffer[2] = {0x22, 0x11};
+
+	TEST_ASSERT_EQUAL(0x1122, ntp_be16dec((void *)buffer));
+}
+
+TEST(endian, Bit32) {
+	uint8_t buffer[4] = {0x44, 0x33, 0x22, 0x11};
+
+	TEST_ASSERT_EQUAL(0x11223344, ntp_be32dec((void *)buffer));
+}
+
+TEST(endian, Bit64) {
+	uint8_t buffer[8] = {0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
+
+	TEST_ASSERT_EQUAL(0x1122334455667788, ntp_be64dec((void *)buffer));
+}
+
+TEST_GROUP_RUNNER(endian) {
+	RUN_TEST_CASE(endian, Bit16);
+	RUN_TEST_CASE(endian, Bit32);
+	RUN_TEST_CASE(endian, Bit64);
+}


=====================================
tests/libntp/ntp_random.c
=====================================
@@ -32,6 +32,28 @@ TEST(random, random32) {
 	TEST_ASSERT_EQUAL_INT32(0, zeros);
 }
 
+TEST(random, random64) {
+	int i;
+	uint64_t ones = 0;
+	uint64_t zeros = ~0;
+
+	/* This is just a crude sanity check.
+	 * It could fail when working correctly,
+	 * but the chances are pretty small.
+	 * It won't be reproducable.  ;)
+	 * You can test this code by making the loop count smaller.
+	 */
+	for (i=0; i<99; i++) {
+		uint64_t sample = ntp_random64();
+		ones |= sample;
+		zeros &= sample; 
+	}
+
+	TEST_ASSERT_EQUAL_INT64(~0, ones);
+	TEST_ASSERT_EQUAL_INT64(0, zeros);
+}
+
 TEST_GROUP_RUNNER(random) {
 	RUN_TEST_CASE(random, random32);
+	RUN_TEST_CASE(random, random64);
 }


=====================================
tests/libntp/numtoa.c
=====================================
@@ -11,20 +11,6 @@ TEST_SETUP(numtoa) {}
 TEST_TEAR_DOWN(numtoa) {}
 
 
-TEST(numtoa, Address) {
-	uint32_t input = htonl(3221225472UL+512UL+1UL); // 192.0.2.1
-
-	TEST_ASSERT_EQUAL_STRING("192.0.2.1", numtoa(input));
-}
-
-TEST(numtoa, Netmask) {
-	// 255.255.255.0
-	uint32_t hostOrder = 255UL*256UL*256UL*256UL + 255UL*256UL*256UL + 255UL*256UL;
-	uint32_t input = htonl(hostOrder);
-
-	TEST_ASSERT_EQUAL_STRING("255.255.255.0", numtoa(input));
-}
-
 TEST(numtoa, RefidStr) {
 	const char *res;
 
@@ -37,7 +23,5 @@ TEST(numtoa, RefidStr) {
 }
 
 TEST_GROUP_RUNNER(numtoa) {
-	RUN_TEST_CASE(numtoa, Address);
-	RUN_TEST_CASE(numtoa, Netmask);
 	RUN_TEST_CASE(numtoa, RefidStr);
 }


=====================================
tests/libntp/statestr.c
=====================================
@@ -4,6 +4,8 @@
 #include "unity.h"
 #include "unity_fixture.h"
 
+#include <sys/timex.h>
+
 TEST_GROUP(statestr);
 
 TEST_SETUP(statestr) {}
@@ -15,6 +17,31 @@ TEST_TEAR_DOWN(statestr) {}
 #include "ntp_control.h"
 
 
+// res_match_flags()
+TEST(statestr, ResMatchFlags) {
+	TEST_ASSERT_EQUAL_STRING("ntpport", res_match_flags(RESM_NTPONLY));
+}
+
+// res_access_flags()
+TEST(statestr, ResAccessFlags) {
+	TEST_ASSERT_EQUAL_STRING("notrust", res_access_flags(RES_DONTTRUST));
+}
+
+// k_st_flags()
+TEST(statestr, KSTFlags) {
+	TEST_ASSERT_EQUAL_STRING("ppsfreq", k_st_flags(STA_PPSFREQ));
+}
+
+// statustoa
+TEST(statestr, StatusToA) {
+	TEST_ASSERT_EQUAL_STRING("leap_add_sec, sync_22, 7 events, no_sys_peer",
+							 statustoa(TYPE_SYS, 0x12345678));
+	TEST_ASSERT_EQUAL_STRING("authenb, reach, sel_sys.peer, 7 events, access_denied",
+							 statustoa(TYPE_PEER, 0x12345678));
+	TEST_ASSERT_EQUAL_STRING("7 events, clk_8",
+							 statustoa(TYPE_CLOCK, 0x12345678));
+}
+
 // eventstr()
 TEST(statestr, PeerRestart) {
 	TEST_ASSERT_EQUAL_STRING("restart", eventstr(PEVNT_RESTART));
@@ -34,6 +61,10 @@ TEST(statestr, ClockCodeUnknown) {
 }
 
 TEST_GROUP_RUNNER(statestr) {
+	RUN_TEST_CASE(statestr, ResMatchFlags);
+	RUN_TEST_CASE(statestr, ResAccessFlags);
+	RUN_TEST_CASE(statestr, KSTFlags);
+	RUN_TEST_CASE(statestr, StatusToA);
 	RUN_TEST_CASE(statestr, PeerRestart);
 	RUN_TEST_CASE(statestr, SysUnspecified);
 	RUN_TEST_CASE(statestr, ClockCodeExists);


=====================================
tests/libntp/timespecops.c
=====================================
@@ -180,7 +180,7 @@ TEST(timespecops, SignNoFrac) {
 	for (i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 0);
 		int E = (i > 0) - (i < 0);
-		int r = test_tspec(a);
+		int r = test_tspec_denorm(a);
 
 		TEST_ASSERT_EQUAL(E, r);
 	}
@@ -196,7 +196,7 @@ TEST(timespecops, SignWithFrac) {
 	for (i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 10);
 		int E = (i >= 0) - (i < 0);
-		int r = test_tspec(a);
+		int r = test_tspec_denorm(a);
 
 		TEST_ASSERT_EQUAL(E, r);
 	}
@@ -630,6 +630,124 @@ TEST(timespecops, test_LFProundtrip) {
 	return;
 }
 
+//----------------------------------------------------------------------
+// conversion from l_fp, unsigned
+//----------------------------------------------------------------------
+
+TEST(timespecops, test_FromLFPuBittest) {
+	struct timespec limit = timespec_init(0, 2);
+
+	// Not *exactly* a bittest, because 2**32 tests would take a
+	// really long time even on very fast machines! So we do test
+	// every 1000 fractional units.
+	uint32_t tsf;
+	for (tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
+		struct timespec E = timespec_init(1, (long)my_tsf_to_tick(tsf));
+		l_fp a = lfpinit(1, tsf);
+		struct timespec r;
+
+		r = lfp_uintv_to_tspec(a);
+		// The conversion might be off by one nanosecond when
+		// comparing to calculated value.
+		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
+	}
+
+	return;
+}
+
+
+TEST(timespecops, test_FromLFPuRelPos) {
+	struct timespec limit = timespec_init(0, 2);
+	int i;
+
+	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+		l_fp a = lfpinit(1, fdata[i].frac);
+		struct timespec E = timespec_init(1, fdata[i].nsec);
+		struct timespec r;
+
+		r = lfp_uintv_to_tspec(a);
+		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
+	}
+
+	return;
+}
+
+
+TEST(timespecops, test_FromLFPuRelNeg) {
+	struct timespec limit = timespec_init(0, 2);
+	int i;
+
+	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+		l_fp a = lfpinit(~0, fdata[i].frac);
+		struct timespec E = timespec_init(-1, fdata[i].nsec);
+		struct timespec r;
+
+		r = lfp_uintv_to_tspec(a);
+		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
+	}
+
+	return;
+}
+
+
+// nsec -> frac -> nsec roundtrip, using a prime start and increment
+TEST(timespecops, test_LFPuRoundtrip) {
+	int32_t t;
+	uint32_t i;
+
+	for (t = -1; t < 2; ++t)
+		for (i = 4999; i < 1000000000; i += 10007) {
+			struct timespec E = timespec_init(t, (long)i);
+			l_fp a;
+			struct timespec r;
+
+			a = tspec_intv_to_lfp(E);
+			r = lfp_uintv_to_tspec(a);
+			TEST_ASSERT_EQUAL_timespec(E, r);
+		}
+
+	return;
+}
+
+//----------------------------------------------------------------------
+// conversion from double
+//----------------------------------------------------------------------
+
+TEST(timespecops, DToTspec) {
+	struct timespec res;
+
+	res = d_to_tspec(42.25);
+	TEST_ASSERT_EQUAL(42, res.tv_sec);
+	TEST_ASSERT_EQUAL(250000000, res.tv_nsec);
+}
+
+//----------------------------------------------------------------------
+// conversion from lfp stamp
+//----------------------------------------------------------------------
+
+TEST(timespecops, LfpStampToTspec) {
+	struct timespec res;
+
+	res = lfp_stamp_to_tspec(86400, 100000);
+	TEST_ASSERT_EQUAL(2085978496, res.tv_sec);
+	TEST_ASSERT_EQUAL(20117, res.tv_nsec);
+}
+
+//----------------------------------------------------------------------
+// conversion from tval
+//----------------------------------------------------------------------
+
+TEST(timespecops, TvalToTspec) {
+	struct timespec res;
+	struct timeval in;
+
+	in.tv_sec = 42;
+	in.tv_usec = 23;
+	res = tval_to_tspec(in);
+	TEST_ASSERT_EQUAL(42, res.tv_sec);
+	TEST_ASSERT_EQUAL(23000, res.tv_nsec);
+}
+
 //----------------------------------------------------------------------
 // string formatting
 //----------------------------------------------------------------------
@@ -662,6 +780,13 @@ TEST_GROUP_RUNNER(timespecops) {
 	RUN_TEST_CASE(timespecops, test_FromLFPrelPos);
 	RUN_TEST_CASE(timespecops, test_FromLFPrelNeg);
 	RUN_TEST_CASE(timespecops, test_LFProundtrip);
+	RUN_TEST_CASE(timespecops, test_FromLFPuBittest);
+	RUN_TEST_CASE(timespecops, test_FromLFPuRelPos);
+	RUN_TEST_CASE(timespecops, test_FromLFPuRelNeg);
+	RUN_TEST_CASE(timespecops, test_LFPuRoundtrip);
+	RUN_TEST_CASE(timespecops, DToTspec);
+	RUN_TEST_CASE(timespecops, LfpStampToTspec);
+	RUN_TEST_CASE(timespecops, TvalToTspec);
 }
 
 


=====================================
tests/wscript
=====================================
@@ -35,6 +35,7 @@ def build(ctx):
     libntp_source = [
         "libntp/authkeys.c",
         "libntp/ntp_calendar.c",
+		"libntp/ntp_endian.c",
         "libntp/ntp_random.c",
         "libntp/clocktime.c",
         "libntp/decodenetnum.c",



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/d4bc94ec2b973f9a6fc76d8b91909614abbe1d1e...c12a73382f9891dbadf0f7fdcc58625115cc7253

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/d4bc94ec2b973f9a6fc76d8b91909614abbe1d1e...c12a73382f9891dbadf0f7fdcc58625115cc7253
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/20180731/10fb9bb3/attachment-0001.html>


More information about the vc mailing list