summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/packetHandling.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/ntp/sntp/tests/packetHandling.c')
-rw-r--r--contrib/ntp/sntp/tests/packetHandling.c141
1 files changed, 76 insertions, 65 deletions
diff --git a/contrib/ntp/sntp/tests/packetHandling.c b/contrib/ntp/sntp/tests/packetHandling.c
index 1036fc3..595efa3 100644
--- a/contrib/ntp/sntp/tests/packetHandling.c
+++ b/contrib/ntp/sntp/tests/packetHandling.c
@@ -27,25 +27,29 @@ void test_HandleCorrectPacket(void);
void
-setUp(void) {
+setUp(void)
+{
init_lib();
}
int
-LfpEquality(const l_fp expected, const l_fp actual) {
- if (L_ISEQU(&expected, &actual))
- return TRUE;
- else
- return FALSE;
+LfpEquality(
+ const l_fp expected,
+ const l_fp actual
+ )
+{
+ return !!(L_ISEQU(&expected, &actual));
}
void
-test_GenerateUnauthenticatedPacket(void) {
- struct pkt testpkt;
+test_GenerateUnauthenticatedPacket(void)
+{
+ struct pkt testpkt;
+ struct timeval xmt;
+ l_fp expected_xmt, actual_xmt;
- struct timeval xmt;
GETTIMEOFDAY(&xmt, NULL);
xmt.tv_sec += JAN_1970;
@@ -59,7 +63,6 @@ test_GenerateUnauthenticatedPacket(void) {
TEST_ASSERT_EQUAL(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
TEST_ASSERT_EQUAL(8, testpkt.ppoll);
- l_fp expected_xmt, actual_xmt;
TVTOTS(&xmt, &expected_xmt);
NTOHL_FP(&testpkt.xmt, &actual_xmt);
TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt));
@@ -67,22 +70,25 @@ test_GenerateUnauthenticatedPacket(void) {
void
-test_GenerateAuthenticatedPacket(void) {
- struct key testkey;
+test_GenerateAuthenticatedPacket(void)
+{
+ static const int EXPECTED_PKTLEN = LEN_PKT_NOMAC + MAX_MD5_LEN;
+
+ struct key testkey;
+ struct pkt testpkt;
+ struct timeval xmt;
+ l_fp expected_xmt, actual_xmt;
+ char expected_mac[MAX_MD5_LEN];
+
testkey.next = NULL;
testkey.key_id = 30;
testkey.key_len = 9;
memcpy(testkey.key_seq, "123456789", testkey.key_len);
memcpy(testkey.type, "MD5", 3);
- struct pkt testpkt;
-
- struct timeval xmt;
GETTIMEOFDAY(&xmt, NULL);
xmt.tv_sec += JAN_1970;
- const int EXPECTED_PKTLEN = LEN_PKT_NOMAC + MAX_MD5_LEN;
-
TEST_ASSERT_EQUAL(EXPECTED_PKTLEN,
generate_pkt(&testpkt, &xmt, testkey.key_id, &testkey));
@@ -93,103 +99,102 @@ test_GenerateAuthenticatedPacket(void) {
TEST_ASSERT_EQUAL(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
TEST_ASSERT_EQUAL(8, testpkt.ppoll);
- l_fp expected_xmt, actual_xmt;
TVTOTS(&xmt, &expected_xmt);
NTOHL_FP(&testpkt.xmt, &actual_xmt);
TEST_ASSERT_TRUE(LfpEquality(expected_xmt, actual_xmt));
TEST_ASSERT_EQUAL(testkey.key_id, ntohl(testpkt.exten[0]));
- char expected_mac[MAX_MD5_LEN];
- TEST_ASSERT_EQUAL(MAX_MD5_LEN - 4, // Remove the key_id, only keep the mac.
- make_mac((char*)&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac));
+ TEST_ASSERT_EQUAL(MAX_MD5_LEN - 4, /* Remove the key_id, only keep the mac. */
+ make_mac(&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac));
TEST_ASSERT_EQUAL_MEMORY(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4);
}
void
-test_OffsetCalculationPositiveOffset(void) {
- struct pkt rpkt;
-
- rpkt.precision = -16; // 0,000015259
+test_OffsetCalculationPositiveOffset(void)
+{
+ struct pkt rpkt;
+ l_fp reftime, tmp;
+ struct timeval dst;
+ double offset, precision, synch_distance;
+
+ rpkt.precision = -16; /* 0,000015259 */
rpkt.rootdelay = HTONS_FP(DTOUFP(0.125));
rpkt.rootdisp = HTONS_FP(DTOUFP(0.25));
- // Synch Distance: (0.125+0.25)/2.0 == 0.1875
- l_fp reftime;
+
+ /* Synch Distance: (0.125+0.25)/2.0 == 0.1875 */
get_systime(&reftime);
HTONL_FP(&reftime, &rpkt.reftime);
- l_fp tmp;
-
- // T1 - Originate timestamp
+ /* T1 - Originate timestamp */
tmp.l_ui = 1000000000UL;
tmp.l_uf = 0UL;
HTONL_FP(&tmp, &rpkt.org);
- // T2 - Receive timestamp
+ /* T2 - Receive timestamp */
tmp.l_ui = 1000000001UL;
tmp.l_uf = 2147483648UL;
HTONL_FP(&tmp, &rpkt.rec);
- // T3 - Transmit timestamp
+ /* T3 - Transmit timestamp */
tmp.l_ui = 1000000002UL;
tmp.l_uf = 0UL;
HTONL_FP(&tmp, &rpkt.xmt);
- // T4 - Destination timestamp as standard timeval
+ /* T4 - Destination timestamp as standard timeval */
tmp.l_ui = 1000000001UL;
tmp.l_uf = 0UL;
- struct timeval dst;
TSTOTV(&tmp, &dst);
dst.tv_sec -= JAN_1970;
- double offset, precision, synch_distance;
offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance);
TEST_ASSERT_EQUAL_DOUBLE(1.25, offset);
TEST_ASSERT_EQUAL_DOUBLE(1. / ULOGTOD(16), precision);
- // 1.1250150000000001 ?
+ /* 1.1250150000000001 ? */
TEST_ASSERT_EQUAL_DOUBLE(1.125015, synch_distance);
}
void
-test_OffsetCalculationNegativeOffset(void) {
- struct pkt rpkt;
+test_OffsetCalculationNegativeOffset(void)
+{
+ struct pkt rpkt;
+ l_fp reftime, tmp;
+ struct timeval dst;
+ double offset, precision, synch_distance;
rpkt.precision = -1;
rpkt.rootdelay = HTONS_FP(DTOUFP(0.5));
rpkt.rootdisp = HTONS_FP(DTOUFP(0.5));
- // Synch Distance is (0.5+0.5)/2.0, or 0.5
- l_fp reftime;
+
+ /* Synch Distance is (0.5+0.5)/2.0, or 0.5 */
get_systime(&reftime);
HTONL_FP(&reftime, &rpkt.reftime);
- l_fp tmp;
-
- // T1 - Originate timestamp
+ /* T1 - Originate timestamp */
tmp.l_ui = 1000000001UL;
tmp.l_uf = 0UL;
HTONL_FP(&tmp, &rpkt.org);
- // T2 - Receive timestamp
+ /* T2 - Receive timestamp */
tmp.l_ui = 1000000000UL;
tmp.l_uf = 2147483648UL;
HTONL_FP(&tmp, &rpkt.rec);
- // T3 - Transmit timestamp
+ /*/ T3 - Transmit timestamp */
tmp.l_ui = 1000000001UL;
tmp.l_uf = 2147483648UL;
HTONL_FP(&tmp, &rpkt.xmt);
- // T4 - Destination timestamp as standard timeval
+ /* T4 - Destination timestamp as standard timeval */
tmp.l_ui = 1000000003UL;
tmp.l_uf = 0UL;
- struct timeval dst;
+
TSTOTV(&tmp, &dst);
dst.tv_sec -= JAN_1970;
- double offset, precision, synch_distance;
offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance);
TEST_ASSERT_EQUAL_DOUBLE(-1, offset);
@@ -199,8 +204,9 @@ test_OffsetCalculationNegativeOffset(void) {
void
-test_HandleUnusableServer(void) {
- struct pkt rpkt;
+test_HandleUnusableServer(void)
+{
+ struct pkt rpkt;
sockaddr_u host;
int rpktl;
@@ -212,8 +218,9 @@ test_HandleUnusableServer(void) {
void
-test_HandleUnusablePacket(void) {
- struct pkt rpkt;
+test_HandleUnusablePacket(void)
+{
+ struct pkt rpkt;
sockaddr_u host;
int rpktl;
@@ -225,8 +232,9 @@ test_HandleUnusablePacket(void) {
void
-test_HandleServerAuthenticationFailure(void) {
- struct pkt rpkt;
+test_HandleServerAuthenticationFailure(void)
+{
+ struct pkt rpkt;
sockaddr_u host;
int rpktl;
@@ -238,12 +246,13 @@ test_HandleServerAuthenticationFailure(void) {
void
-test_HandleKodDemobilize(void) {
- const char * HOSTNAME = "192.0.2.1";
- const char * REASON = "DENY";
+test_HandleKodDemobilize(void)
+{
+ static const char * HOSTNAME = "192.0.2.1";
+ static const char * REASON = "DENY";
struct pkt rpkt;
- sockaddr_u host;
- int rpktl;
+ sockaddr_u host;
+ int rpktl;
struct kod_entry * entry;
rpktl = KOD_DEMOBILIZE;
@@ -253,7 +262,7 @@ test_HandleKodDemobilize(void) {
host.sa4.sin_family = AF_INET;
host.sa4.sin_addr.s_addr = inet_addr(HOSTNAME);
- // Test that the KOD-entry is added to the database.
+ /* Test that the KOD-entry is added to the database. */
kod_init_kod_db("/dev/null", TRUE);
TEST_ASSERT_EQUAL(1, handle_pkt(rpktl, &rpkt, &host, HOSTNAME));
@@ -264,8 +273,9 @@ test_HandleKodDemobilize(void) {
void
-test_HandleKodRate(void) {
- struct pkt rpkt;
+test_HandleKodRate(void)
+{
+ struct pkt rpkt;
sockaddr_u host;
int rpktl;
@@ -277,13 +287,14 @@ test_HandleKodRate(void) {
void
-test_HandleCorrectPacket(void) {
- struct pkt rpkt;
+test_HandleCorrectPacket(void)
+{
+ struct pkt rpkt;
sockaddr_u host;
int rpktl;
l_fp now;
- // We don't want our testing code to actually change the system clock.
+ /* We don't want our testing code to actually change the system clock. */
TEST_ASSERT_FALSE(ENABLED_OPT(STEP));
TEST_ASSERT_FALSE(ENABLED_OPT(SLEW));
OpenPOWER on IntegriCloud