Reduced the number of warnings by half. Damir Tomić.
* Updated generate_test_runner.rb and updated the sntp/unity/auto directory
with the latest Unity updates from Mark. Damir Tomić.
+* Retire google test - phase I. Harlan Stenn.
---
(4.2.8p3) 2015/06/29 Released by Harlan Stenn <stenn@ntp.org>
main.h \
networking.h \
sntp-opts.h \
- tests_main.h \
utilities.h \
$(NULL)
NULL =
BUILT_SOURCES =
-CLEANFILES = kod-output-blank kod-output-single kod-output-multiple debug-output-lfp-dec debug-output-lfp-bin debug-output-pkt
-DISTCLEANFILES = kod-output-blank kod-output-single kod-output-multiple
EXTRA_PROGRAMS =
+CLEANFILES = \
+ debug-output-lfp-bin \
+ debug-output-lfp-dec \
+ debug-output-pkt \
+ kod-output-blank \
+ kod-output-multiple \
+ kod-output-single \
+ $(NULL)
+
+DISTCLEANFILES = \
+ kod-output-blank \
+ kod-output-single \
+ kod-output-multiple \
+ $(NULL)
+
std_unity_list = \
$(srcdir)/../unity/auto/generate_test_runner.rb \
$(NULL)
test-utilities \
$(NULL)
-EXTRA_PROGRAMS += tests
-
-tests_SOURCES = \
- ../tests_main.cpp \
- g_crypto.cpp \
- g_fileHandlingTest.h \
- g_networking.cpp \
- g_packetHandling.cpp \
- g_packetProcessing.cpp \
- g_sntptest.h \
- $(NULL)
-
-tests_LDADD = \
- $(base_LDADD) \
- $(GTEST_LDFLAGS) \
- $(GTEST_LIBS) \
- $(NULL)
-
-tests_CPPFLAGS = \
- $(AM_CPPFLAGS) \
- $(CPPFLAGS) \
- $(GTEST_CPPFLAGS) \
- $(NULL)
-
-AM_CXXFLAGS = $(GTEST_CXXFLAGS)
-
noinst_HEADERS = \
sntptest.h \
$(NULL)
+++ /dev/null
-#include "g_sntptest.h"
-
-extern "C" {
-#include "crypto.h"
-};
-
-class cryptoTest : public sntptest {
-};
-
-#define MD5_LENGTH 16
-#define SHA1_LENGTH 20
-
-TEST_F(cryptoTest, MakeMd5Mac) {
-
- const char* PKT_DATA = "abcdefgh0123";
- const int PKT_LEN = strlen(PKT_DATA);
- const char* EXPECTED_DIGEST =
- "\x52\x6c\xb8\x38\xaf\x06\x5a\xfb\x6c\x98\xbb\xc0\x9b\x0a\x7a\x1b";
- char actual[MD5_LENGTH];
-
- key md5;
- md5.next = NULL;
- md5.key_id = 10;
- md5.key_len = 6;
- memcpy(&md5.key_seq, "md5seq", md5.key_len);
- memcpy(&md5.type, "MD5", 4);
-
- EXPECT_EQ(MD5_LENGTH,
- make_mac((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5, actual));
-
- EXPECT_TRUE(memcmp(EXPECTED_DIGEST, actual, MD5_LENGTH) == 0);
-}
-
-#ifdef OPENSSL
-TEST_F(cryptoTest, MakeSHA1Mac) {
- const char* PKT_DATA = "abcdefgh0123";
- const int PKT_LEN = strlen(PKT_DATA);
- const char* EXPECTED_DIGEST =
- "\x17\xaa\x82\x97\xc7\x17\x13\x6a\x9b\xa9"
- "\x63\x85\xb4\xce\xbe\x94\xa0\x97\x16\x1d";
- char actual[SHA1_LENGTH];
-
- key sha1;
- sha1.next = NULL;
- sha1.key_id = 20;
- sha1.key_len = 7;
- memcpy(&sha1.key_seq, "sha1seq", sha1.key_len);
- memcpy(&sha1.type, "SHA1", 5);
-
- EXPECT_EQ(SHA1_LENGTH,
- make_mac((char*)PKT_DATA, PKT_LEN, SHA1_LENGTH, &sha1, actual));
-
- EXPECT_TRUE(memcmp(EXPECTED_DIGEST, actual, SHA1_LENGTH) == 0);
-}
-#endif /* OPENSSL */
-
-TEST_F(cryptoTest, VerifyCorrectMD5) {
- const char* PKT_DATA =
- "sometestdata" // Data
- "\0\0\0\0" // Key-ID (unused)
- "\xc7\x58\x99\xdd\x99\x32\x0f\x71" // MAC
- "\x2b\x7b\xfe\x4f\xa2\x32\xcf\xac";
- const int PKT_LEN = 12;
-
- key md5;
- md5.next = NULL;
- md5.key_id = 0;
- md5.key_len = 6;
- memcpy(&md5.key_seq, "md5key", md5.key_len);
- memcpy(&md5.type, "MD5", 4);
-
- EXPECT_TRUE(auth_md5((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5));
-}
-
-#ifdef OPENSSL
-TEST_F(cryptoTest, VerifySHA1) {
- const char* PKT_DATA =
- "sometestdata" // Data
- "\0\0\0\0" // Key-ID (unused)
- "\xad\x07\xde\x36\x39\xa6\x77\xfa\x5b\xce" // MAC
- "\x2d\x8a\x7d\x06\x96\xe6\x0c\xbc\xed\xe1";
- const int PKT_LEN = 12;
-
- key sha1;
- sha1.next = NULL;
- sha1.key_id = 0;
- sha1.key_len = 7;
- memcpy(&sha1.key_seq, "sha1key", sha1.key_len);
- memcpy(&sha1.type, "SHA1", 5);
-
- EXPECT_TRUE(auth_md5((char*)PKT_DATA, PKT_LEN, SHA1_LENGTH, &sha1));
-}
-#endif /* OPENSSL */
-
-TEST_F(cryptoTest, VerifyFailure) {
- /* We use a copy of the MD5 verification code, but modify
- * the last bit to make sure verification fails. */
- const char* PKT_DATA =
- "sometestdata" // Data
- "\0\0\0\0" // Key-ID (unused)
- "\xc7\x58\x99\xdd\x99\x32\x0f\x71" // MAC
- "\x2b\x7b\xfe\x4f\xa2\x32\xcf\x00"; // Last byte is wrong!
- const int PKT_LEN = 12;
-
- key md5;
- md5.next = NULL;
- md5.key_id = 0;
- md5.key_len = 6;
- memcpy(&md5.key_seq, "md5key", md5.key_len);
- memcpy(&md5.type, "MD5", 4);
-
- EXPECT_FALSE(auth_md5((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5));
-}
-
-TEST_F(cryptoTest, PacketSizeNotMultipleOfFourBytes) {
- const char* PKT_DATA = "123456";
- const int PKT_LEN = 6;
- char actual[MD5_LENGTH];
-
- key md5;
- md5.next = NULL;
- md5.key_id = 10;
- md5.key_len = 6;
- memcpy(&md5.key_seq, "md5seq", md5.key_len);
- memcpy(&md5.type, "MD5", 4);
-
- EXPECT_EQ(0, make_mac((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5, actual));
-}
-
+++ /dev/null
-#ifndef FILE_HANDLING_TEST_H
-#define FILE_HANDLING_TEST_H
-
-#include "g_sntptest.h"
-
-#include <fstream>
-#include <string>
-
-using std::ifstream;
-using std::string;
-using std::ios;
-
-class fileHandlingTest : public sntptest {
-protected:
- enum DirectoryType {
- INPUT_DIR = 0,
- OUTPUT_DIR = 1
- };
-
- std::string CreatePath(const char* filename, DirectoryType argument) {
- std::string path;
-
- if (m_params.size() >= argument + 1) {
- path = m_params[argument];
- }
-
- if (path[path.size()-1] != DIR_SEP && !path.empty()) {
- path.append(1, DIR_SEP);
- }
- path.append(filename);
-
- return path;
- }
-
- int GetFileSize(ifstream& file) {
- int initial = file.tellg();
-
- file.seekg(0, ios::end);
- int length = file.tellg();
- file.seekg(initial);
-
- return length;
- }
-
- void CompareFileContent(ifstream& expected, ifstream& actual) {
- int currentLine = 1;
- while (actual.good() && expected.good()) {
- string actualLine, expectedLine;
- getline(actual, actualLine);
- getline(expected, expectedLine);
-
- EXPECT_EQ(expectedLine, actualLine) << "Comparision failed on line " << currentLine;
- currentLine++;
- }
- }
-
- void ClearFile(const std::string& filename) {
- std::ofstream clear(filename.c_str(), ios::trunc);
- ASSERT_TRUE(clear.good());
- clear.close();
- }
-};
-
-#endif // FILE_HANDLING_TEST_H
+++ /dev/null
-#include "g_fileHandlingTest.h"
-
-extern "C" {
-#include "crypto.h"
-};
-
-class keyFileTest : public fileHandlingTest {
-protected:
- ::testing::AssertionResult CompareKeys(key& expected, key& actual) {
- if (expected.key_id != actual.key_id)
- return ::testing::AssertionFailure()
- << "Expected key_id: " << expected.key_id
- << " but was: " << actual.key_id;
- if (expected.key_len != actual.key_len)
- return ::testing::AssertionFailure()
- << "Expected key_len: " << expected.key_len
- << " but was: " << actual.key_len;
- if (strcmp(expected.type, actual.type) != 0)
- return ::testing::AssertionFailure()
- << "Expected key_type: " << expected.type
- << " but was: " << actual.type;
- if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0)
- return ::testing::AssertionFailure()
- << "Key mismatch!";
- return ::testing::AssertionSuccess();
- }
-
- ::testing::AssertionResult CompareKeys(int key_id,
- int key_len,
- const char* type,
- const char* key_seq,
- key& actual) {
- key temp;
-
- temp.key_id = key_id;
- temp.key_len = key_len;
- strlcpy(temp.type, type, sizeof(temp.type));
- memcpy(temp.key_seq, key_seq, key_len);
-
- return CompareKeys(temp, actual);
- }
-};
-
-TEST_F(keyFileTest, ReadEmptyKeyFile) {
- key* keys = NULL;
-
- ASSERT_EQ(0, auth_init(CreatePath("key-test-empty", INPUT_DIR).c_str(), &keys));
-
- EXPECT_TRUE(keys == NULL);
-}
-
-TEST_F(keyFileTest, ReadASCIIKeys) {
- key* keys = NULL;
-
- ASSERT_EQ(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR).c_str(), &keys));
-
- ASSERT_TRUE(keys != NULL);
-
- key* result = NULL;
- get_key(40, &result);
- ASSERT_TRUE(result != NULL);
- EXPECT_TRUE(CompareKeys(40, 11, "MD5", "asciikeyTwo", *result));
-
- result = NULL;
- get_key(50, &result);
- ASSERT_TRUE(result != NULL);
- EXPECT_TRUE(CompareKeys(50, 11, "MD5", "asciikeyOne", *result));
-}
-
-TEST_F(keyFileTest, ReadHexKeys) {
- key* keys = NULL;
-
- ASSERT_EQ(3, auth_init(CreatePath("key-test-hex", INPUT_DIR).c_str(), &keys));
-
- ASSERT_TRUE(keys != NULL);
-
- key* result = NULL;
- get_key(10, &result);
- ASSERT_TRUE(result != NULL);
- EXPECT_TRUE(CompareKeys(10, 13, "MD5",
- "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
-
- result = NULL;
- get_key(20, &result);
- ASSERT_TRUE(result != NULL);
- char data1[15]; memset(data1, 0x11, 15);
- EXPECT_TRUE(CompareKeys(20, 15, "MD5", data1, *result));
-
- result = NULL;
- get_key(30, &result);
- ASSERT_TRUE(result != NULL);
- char data2[13]; memset(data2, 0x01, 13);
- EXPECT_TRUE(CompareKeys(30, 13, "MD5", data2, *result));
-}
-
-TEST_F(keyFileTest, ReadKeyFileWithComments) {
- key* keys = NULL;
-
- ASSERT_EQ(2, auth_init(CreatePath("key-test-comments", INPUT_DIR).c_str(), &keys));
-
- ASSERT_TRUE(keys != NULL);
-
- key* result = NULL;
- get_key(10, &result);
- ASSERT_TRUE(result != NULL);
- char data[15]; memset(data, 0x01, 15);
- EXPECT_TRUE(CompareKeys(10, 15, "MD5", data, *result));
-
- result = NULL;
- get_key(34, &result);
- ASSERT_TRUE(result != NULL);
- EXPECT_TRUE(CompareKeys(34, 3, "MD5", "xyz", *result));
-}
-
-TEST_F(keyFileTest, ReadKeyFileWithInvalidHex) {
- key* keys = NULL;
-
- ASSERT_EQ(1, auth_init(CreatePath("key-test-invalid-hex", INPUT_DIR).c_str(), &keys));
-
- ASSERT_TRUE(keys != NULL);
-
- key* result = NULL;
- get_key(10, &result);
- ASSERT_TRUE(result != NULL);
- char data[15]; memset(data, 0x01, 15);
- EXPECT_TRUE(CompareKeys(10, 15, "MD5", data, *result));
-
- result = NULL;
- get_key(30, &result); // Should not exist, and result should remain NULL.
- ASSERT_TRUE(result == NULL);
-}
+++ /dev/null
-#include "g_sntptest.h"
-
-extern "C" {
-#include "kod_management.h"
-};
-
-class kodDatabaseTest : public sntptest {
-protected:
- virtual void SetUp() {
- kod_init_kod_db("/dev/null", TRUE);
- }
-};
-
-TEST_F(kodDatabaseTest, SingleEntryHandling) {
- char HOST[] = "192.0.2.5";
- char REASON[] = "DENY";
-
- add_entry(HOST, REASON);
-
- kod_entry* result;
-
- EXPECT_EQ(1, search_entry(HOST, &result));
- EXPECT_STREQ(HOST, result->hostname);
- EXPECT_STREQ(REASON, result->type);
-}
-
-TEST_F(kodDatabaseTest, MultipleEntryHandling) {
- char HOST1[] = "192.0.2.3";
- char REASON1[] = "DENY";
-
- char HOST2[] = "192.0.5.5";
- char REASON2[] = "RATE";
-
- char HOST3[] = "192.0.10.1";
- char REASON3[] = "DENY";
-
- add_entry(HOST1, REASON1);
- add_entry(HOST2, REASON2);
- add_entry(HOST3, REASON3);
-
- kod_entry* result;
-
- EXPECT_EQ(1, search_entry(HOST1, &result));
- EXPECT_STREQ(HOST1, result->hostname);
- EXPECT_STREQ(REASON1, result->type);
-
- EXPECT_EQ(1, search_entry(HOST2, &result));
- EXPECT_STREQ(HOST2, result->hostname);
- EXPECT_STREQ(REASON2, result->type);
-
- EXPECT_EQ(1, search_entry(HOST3, &result));
- EXPECT_STREQ(HOST3, result->hostname);
- EXPECT_STREQ(REASON3, result->type);
-
- free(result);
-}
-
-TEST_F(kodDatabaseTest, NoMatchInSearch) {
- char HOST_ADD[] = "192.0.2.6";
- char HOST_NOTADD[] = "192.0.6.1";
- char REASON[] = "DENY";
-
- add_entry(HOST_ADD, REASON);
-
- kod_entry* result;
-
- EXPECT_EQ(0, search_entry(HOST_NOTADD, &result));
- EXPECT_TRUE(result == NULL);
-}
-
-TEST_F(kodDatabaseTest, AddDuplicate) {
- char HOST[] = "192.0.2.3";
- char REASON1[] = "RATE";
- char REASON2[] = "DENY";
-
- add_entry(HOST, REASON1);
- kod_entry* result1;
- ASSERT_EQ(1, search_entry(HOST, &result1));
-
- /*
- * Sleeps for two seconds since we want to ensure that
- * the timestamp is updated to a new value.
- */
- sleep(2);
-
- add_entry(HOST, REASON2);
- kod_entry* result2;
- ASSERT_EQ(1, search_entry(HOST, &result2));
-
- EXPECT_NE(result1->timestamp, result2->timestamp);
-
- free(result1);
- free(result2);
-}
-
-TEST_F(kodDatabaseTest, DeleteEntry) {
- char HOST1[] = "192.0.2.1";
- char HOST2[] = "192.0.2.2";
- char HOST3[] = "192.0.2.3";
- char REASON[] = "DENY";
-
- add_entry(HOST1, REASON);
- add_entry(HOST2, REASON);
- add_entry(HOST3, REASON);
-
- kod_entry* result;
-
- ASSERT_EQ(1, search_entry(HOST2, &result));
- free(result);
-
- delete_entry(HOST2, REASON);
-
- EXPECT_EQ(0, search_entry(HOST2, &result));
-
- // Ensure that the other entry is still there.
- EXPECT_EQ(1, search_entry(HOST1, &result));
- free(result);
-}
+++ /dev/null
-#include "g_fileHandlingTest.h"
-
-extern "C" {
-#include "kod_management.h"
-
-#include "ntp_stdlib.h" // For estrdup()
-};
-
-/*
- * We access some parts of the kod database directly, without
- * going through the public interface
- */
-extern int kod_db_cnt;
-extern kod_entry** kod_db;
-extern char* kod_db_file;
-
-class kodFileTest : public fileHandlingTest {
-protected:
- virtual void SetUp() {
- kod_db_cnt = 0;
- kod_db = NULL;
- }
-
- virtual void TearDown() {
- }
-};
-
-TEST_F(kodFileTest, ReadEmptyFile) {
- kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR).c_str(), TRUE);
-
- EXPECT_EQ(0, kod_db_cnt);
-}
-
-TEST_F(kodFileTest, ReadCorrectFile) {
- kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR).c_str(), TRUE);
-
- EXPECT_EQ(2, kod_db_cnt);
-
- kod_entry* res;
-
- ASSERT_EQ(1, search_entry("192.0.2.5", &res));
- EXPECT_STREQ("DENY", res->type);
- EXPECT_STREQ("192.0.2.5", res->hostname);
- EXPECT_EQ(0x12345678, res->timestamp);
-
- ASSERT_EQ(1, search_entry("192.0.2.100", &res));
- EXPECT_STREQ("RSTR", res->type);
- EXPECT_STREQ("192.0.2.100", res->hostname);
- EXPECT_EQ(0xfff, res->timestamp);
-}
-
-TEST_F(kodFileTest, ReadFileWithBlankLines) {
- kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR).c_str(), TRUE);
-
- EXPECT_EQ(3, kod_db_cnt);
-
- kod_entry* res;
-
- ASSERT_EQ(1, search_entry("192.0.2.5", &res));
- EXPECT_STREQ("DENY", res->type);
- EXPECT_STREQ("192.0.2.5", res->hostname);
- EXPECT_EQ(0x12345678, res->timestamp);
-
- ASSERT_EQ(1, search_entry("192.0.2.100", &res));
- EXPECT_STREQ("RSTR", res->type);
- EXPECT_STREQ("192.0.2.100", res->hostname);
- EXPECT_EQ(0xfff, res->timestamp);
-
- ASSERT_EQ(1, search_entry("example.com", &res));
- EXPECT_STREQ("DENY", res->type);
- EXPECT_STREQ("example.com", res->hostname);
- EXPECT_EQ(0xabcd, res->timestamp);
-}
-
-TEST_F(kodFileTest, WriteEmptyFile) {
- kod_db_file = estrdup(CreatePath("kod-output-blank", OUTPUT_DIR).c_str());
-
- write_kod_db();
-
- // Open file and ensure that the filesize is 0 bytes.
- std::ifstream is(kod_db_file, std::ios::binary);
- ASSERT_FALSE(is.fail());
-
- EXPECT_EQ(0, GetFileSize(is));
-
- is.close();
-}
-
-TEST_F(kodFileTest, WriteFileWithSingleEntry) {
- kod_db_file = estrdup(CreatePath("kod-output-single", OUTPUT_DIR).c_str());
-
- add_entry("host1", "DENY");
-
- /* Here we must manipulate the timestamps, so they match the one in
- * the expected file.
- */
- kod_db[0]->timestamp = 1;
-
- write_kod_db();
-
- // Open file and compare sizes.
- ifstream actual(kod_db_file, ios::binary);
- ifstream expected(CreatePath("kod-expected-single", INPUT_DIR).c_str());
- ASSERT_TRUE(actual.good());
- ASSERT_TRUE(expected.good());
-
- ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));
-
- CompareFileContent(expected, actual);
-}
-
-TEST_F(kodFileTest, WriteFileWithMultipleEntries) {
- kod_db_file = estrdup(CreatePath("kod-output-multiple", OUTPUT_DIR).c_str());
-
- add_entry("example.com", "RATE");
- add_entry("192.0.2.1", "DENY");
- add_entry("192.0.2.5", "RSTR");
-
- /*
- * Manipulate timestamps. This is a bit of a hack, ideally these
- * tests should not care about the internal representation.
- */
- kod_db[0]->timestamp = 0xabcd;
- kod_db[1]->timestamp = 0xabcd;
- kod_db[2]->timestamp = 0xabcd;
-
- write_kod_db();
-
- // Open file and compare sizes and content.
- ifstream actual(kod_db_file, ios::binary);
- ifstream expected(CreatePath("kod-expected-multiple", INPUT_DIR).c_str());
- ASSERT_TRUE(actual.good());
- ASSERT_TRUE(expected.good());
-
- ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));
-
- CompareFileContent(expected, actual);
-}
+++ /dev/null
-#include "g_sntptest.h"
-
-extern "C" {
-#include "networking.h"
-};
-
-class networkingTest : public sntptest {
-protected:
- ::testing::AssertionResult CompareAddrinfo(const char* host,
- int family, int flags,
- const addrinfo& actual) {
- if (family != actual.ai_family)
- return ::testing::AssertionFailure()
- << "Family mismatch, expected: " << family
- << " but was: " << actual.ai_family;
- sockaddr_u* sock = new sockaddr_u;
- void* expectedaddr = NULL, *actualaddr = NULL;
- int size = 0, addrsize = 0;
- if (family == AF_INET) {
- expectedaddr = &sock->sa4.sin_addr;
- actualaddr = &((sockaddr_u*)actual.ai_addr)->sa4.sin_addr;
- size = sizeof(sock->sa4);
- addrsize = sizeof(sock->sa4.sin_addr);
- } else {
- expectedaddr = &sock->sa6.sin6_addr;
- actualaddr = &((sockaddr_u*)actual.ai_addr)->sa6.sin6_addr;
- size = sizeof(sock->sa6);
- addrsize = sizeof(sock->sa6.sin6_addr);
- }
- sock->sa.sa_family = family;
-
- if (inet_pton(family, host, expectedaddr) != 1)
- return ::testing::AssertionFailure()
- << "inet_pton failed!";
-
- if (flags != actual.ai_flags)
- return ::testing::AssertionFailure()
- << "Flags mismatch, expected: " << flags
- << " but was: " << actual.ai_flags;
-
- if (size != actual.ai_addrlen)
- return ::testing::AssertionFailure()
- << "Address length mismatch, expected: " << size
- << " but was: " << actual.ai_addrlen;
-
- if (memcmp(expectedaddr, actualaddr, addrsize) != 0)
- return ::testing::AssertionFailure()
- << "Address mismatch";
- return ::testing::AssertionSuccess();
- }
-};
-
-TEST_F(networkingTest, ResolveSingleAddress) {
- const char* HOSTS[1] = {"192.0.2.1"};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
-
- ASSERT_TRUE(actual != NULL);
- EXPECT_TRUE(CompareAddrinfo(HOSTS[0], AF_INET, 0, **actual));
-}
-
-TEST_F(networkingTest, ResolveMultipleAddresses) {
- const char* HOSTS[3] = {"192.0.2.1", "192.0.2.5", "192.0.2.10"};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(3, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
-
- ASSERT_TRUE(actual != NULL);
- for (int i=0; i<HOSTCOUNT; i++) {
- EXPECT_TRUE(CompareAddrinfo(HOSTS[i], AF_INET, 0, *actual[i]))
- << "Failed for host number " << i;
- }
-}
-
-TEST_F(networkingTest, ResolveIPv6Address) {
- const char* HOSTS[1] = {"2001:0DB8:AC10:FE01:0:0:0:0"};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
- ASSERT_TRUE(actual != NULL);
-
- EXPECT_TRUE(CompareAddrinfo(HOSTS[0], AF_INET6, 0, **actual));
-}
-
-TEST_F(networkingTest, ResolveIPv6InvalidComparision) {
- const char* HOSTS[1] = {"2001:0DB8:AC10:FE01::"};
- const char* INVALID = "2001:0db8:ac10:fe01:0:1:0:0";
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
- ASSERT_TRUE(actual != NULL);
-
- EXPECT_FALSE(CompareAddrinfo(INVALID, AF_INET6, 0, **actual));
-}
-
-TEST_F(networkingTest, ResolveMixedAddressTypes) {
- const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
- "192.0.2.30", "2001:ab0:1000::"};
- const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(4, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
- ASSERT_TRUE(actual != NULL);
-
- for (int i=0; i<HOSTCOUNT; i++) {
- EXPECT_TRUE(CompareAddrinfo(HOSTS[i], FAMILIES[i], 0, *actual[i]))
- << "Failed for host number " << i;
- }
-}
-
-TEST_F(networkingTest, ResolveInvalidAddress) {
- const char* HOSTS[1] = {"192.258.2.1"};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(0, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
-}
-
-TEST_F(networkingTest, ResolveMixedAddressValidity) {
- const char* HOSTS[3] = {"2001:52ij:130:1::", "192.0.2.13", "192.0.257.1"};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
- ASSERT_TRUE(actual != NULL);
-
- EXPECT_TRUE(CompareAddrinfo(HOSTS[1], AF_INET, 0, **actual));
-}
-
-TEST_F(networkingTest, ResolveIgnoringIPv6) {
- const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
- "192.0.2.30", "2001:ab0:1000::"};
- const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(2, resolve_hosts(HOSTS, HOSTCOUNT, &actual, AF_INET));
- ASSERT_TRUE(actual != NULL);
-
- EXPECT_TRUE(CompareAddrinfo(HOSTS[1], FAMILIES[1], 0, *actual[0]));
- EXPECT_TRUE(CompareAddrinfo(HOSTS[2], FAMILIES[2], 0, *actual[1]));
-}
-
-TEST_F(networkingTest, ResolveIgnoringIPv4) {
- const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
- "192.0.2.30", "2001:ab0:1000::"};
- const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
- const int HOSTCOUNT = COUNTOF(HOSTS);
-
- addrinfo** actual = NULL;
-
- ASSERT_EQ(2, resolve_hosts(HOSTS, HOSTCOUNT, &actual, AF_INET6));
- ASSERT_TRUE(actual != NULL);
-
- EXPECT_TRUE(CompareAddrinfo(HOSTS[0], FAMILIES[0], 0, *actual[0]));
- EXPECT_TRUE(CompareAddrinfo(HOSTS[3], FAMILIES[3], 0, *actual[1]));
-}
+++ /dev/null
-#include "g_sntptest.h"
-
-extern "C" {
-#include "networking.h"
-};
+++ /dev/null
-#include "g_sntptest.h"
-
-extern "C" {
-#include "kod_management.h"
-#include "main.h"
-#include "networking.h"
-#include "ntp.h"
-};
-
-class mainTest : public sntptest {
-protected:
- ::testing::AssertionResult LfpEquality(const l_fp &expected, const l_fp &actual) {
- if (L_ISEQU(&expected, &actual)) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << " expected: " << lfptoa(&expected, FRACTION_PREC)
- << " (" << expected.l_ui << "." << expected.l_uf << ")"
- << " but was: " << lfptoa(&actual, FRACTION_PREC)
- << " (" << actual.l_ui << "." << actual.l_uf << ")";
- }
- }
-};
-
-TEST_F(mainTest, GenerateUnauthenticatedPacket) {
- pkt testpkt;
-
- timeval xmt;
- GETTIMEOFDAY(&xmt, NULL);
- xmt.tv_sec += JAN_1970;
-
- EXPECT_EQ(LEN_PKT_NOMAC,
- generate_pkt(&testpkt, &xmt, 0, NULL));
-
- EXPECT_EQ(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
- EXPECT_EQ(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
- EXPECT_EQ(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
-
- EXPECT_EQ(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
- EXPECT_EQ(8, testpkt.ppoll);
-
- l_fp expected_xmt, actual_xmt;
- TVTOTS(&xmt, &expected_xmt);
- NTOHL_FP(&testpkt.xmt, &actual_xmt);
- EXPECT_TRUE(LfpEquality(expected_xmt, actual_xmt));
-}
-
-TEST_F(mainTest, GenerateAuthenticatedPacket) {
- key testkey;
- testkey.next = NULL;
- testkey.key_id = 30;
- testkey.key_len = 9;
- memcpy(testkey.key_seq, "123456789", testkey.key_len);
- memcpy(testkey.type, "MD5", 3);
-
- pkt testpkt;
-
- timeval xmt;
- GETTIMEOFDAY(&xmt, NULL);
- xmt.tv_sec += JAN_1970;
-
- const int EXPECTED_PKTLEN = LEN_PKT_NOMAC + MAX_MD5_LEN;
-
- EXPECT_EQ(EXPECTED_PKTLEN,
- generate_pkt(&testpkt, &xmt, testkey.key_id, &testkey));
-
- EXPECT_EQ(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
- EXPECT_EQ(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
- EXPECT_EQ(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
-
- EXPECT_EQ(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
- EXPECT_EQ(8, testpkt.ppoll);
-
- l_fp expected_xmt, actual_xmt;
- TVTOTS(&xmt, &expected_xmt);
- NTOHL_FP(&testpkt.xmt, &actual_xmt);
- EXPECT_TRUE(LfpEquality(expected_xmt, actual_xmt));
-
- EXPECT_EQ(testkey.key_id, ntohl(testpkt.exten[0]));
-
- char expected_mac[MAX_MD5_LEN];
- ASSERT_EQ(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));
- EXPECT_TRUE(memcmp(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4) == 0);
-}
-
-TEST_F(mainTest, OffsetCalculationPositiveOffset) {
- pkt rpkt;
-
- 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;
- get_systime(&reftime);
- HTONL_FP(&reftime, &rpkt.reftime);
-
- l_fp tmp;
-
- // T1 - Originate timestamp
- tmp.l_ui = 1000000000UL;
- tmp.l_uf = 0UL;
- HTONL_FP(&tmp, &rpkt.org);
-
- // T2 - Receive timestamp
- tmp.l_ui = 1000000001UL;
- tmp.l_uf = 2147483648UL;
- HTONL_FP(&tmp, &rpkt.rec);
-
- // T3 - Transmit timestamp
- tmp.l_ui = 1000000002UL;
- tmp.l_uf = 0UL;
- HTONL_FP(&tmp, &rpkt.xmt);
-
- // T4 - Destination timestamp as standard timeval
- tmp.l_ui = 1000000001UL;
- tmp.l_uf = 0UL;
- 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);
-
- EXPECT_DOUBLE_EQ(1.25, offset);
- EXPECT_DOUBLE_EQ(1. / ULOGTOD(16), precision);
- // 1.1250150000000001 ?
- EXPECT_DOUBLE_EQ(1.125015, synch_distance);
-}
-
-TEST_F(mainTest, OffsetCalculationNegativeOffset) {
- pkt rpkt;
-
- 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;
- get_systime(&reftime);
- HTONL_FP(&reftime, &rpkt.reftime);
-
- l_fp tmp;
-
- // T1 - Originate timestamp
- tmp.l_ui = 1000000001UL;
- tmp.l_uf = 0UL;
- HTONL_FP(&tmp, &rpkt.org);
-
- // T2 - Receive timestamp
- tmp.l_ui = 1000000000UL;
- tmp.l_uf = 2147483648UL;
- HTONL_FP(&tmp, &rpkt.rec);
-
- // T3 - Transmit timestamp
- tmp.l_ui = 1000000001UL;
- tmp.l_uf = 2147483648UL;
- HTONL_FP(&tmp, &rpkt.xmt);
-
- // T4 - Destination timestamp as standard timeval
- tmp.l_ui = 1000000003UL;
- tmp.l_uf = 0UL;
- 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);
-
- EXPECT_DOUBLE_EQ(-1, offset);
- EXPECT_DOUBLE_EQ(1. / ULOGTOD(1), precision);
- EXPECT_DOUBLE_EQ(1.3333483333333334, synch_distance);
-}
-
-TEST_F(mainTest, HandleUnusableServer) {
- pkt rpkt;
- sockaddr_u host;
- int rpktl;
-
- ZERO(rpkt);
- ZERO(host);
- rpktl = SERVER_UNUSEABLE;
- EXPECT_EQ(-1, handle_pkt(rpktl, &rpkt, &host, ""));
-}
-
-TEST_F(mainTest, HandleUnusablePacket) {
- pkt rpkt;
- sockaddr_u host;
- int rpktl;
-
- ZERO(rpkt);
- ZERO(host);
- rpktl = PACKET_UNUSEABLE;
- EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, ""));
-}
-
-TEST_F(mainTest, HandleServerAuthenticationFailure) {
- pkt rpkt;
- sockaddr_u host;
- int rpktl;
-
- ZERO(rpkt);
- ZERO(host);
- rpktl = SERVER_AUTH_FAIL;
- EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, ""));
-}
-
-TEST_F(mainTest, HandleKodDemobilize) {
- const char * HOSTNAME = "192.0.2.1";
- const char * REASON = "DENY";
- pkt rpkt;
- sockaddr_u host;
- int rpktl;
- kod_entry * entry;
-
- rpktl = KOD_DEMOBILIZE;
- ZERO(rpkt);
- memcpy(&rpkt.refid, REASON, 4);
- ZERO(host);
- 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.
- kod_init_kod_db("/dev/null", TRUE);
-
- EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, HOSTNAME));
-
- ASSERT_EQ(1, search_entry(HOSTNAME, &entry));
- EXPECT_TRUE(memcmp(REASON, entry->type, 4) == 0);
-}
-
-TEST_F(mainTest, HandleKodRate) {
- pkt rpkt;
- sockaddr_u host;
- int rpktl;
-
- ZERO(rpkt);
- ZERO(host);
- rpktl = KOD_RATE;
- EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, ""));
-}
-
-TEST_F(mainTest, HandleCorrectPacket) {
- pkt rpkt;
- sockaddr_u host;
- int rpktl;
- l_fp now;
-
- // We don't want our testing code to actually change the system clock.
- ASSERT_FALSE(ENABLED_OPT(STEP));
- ASSERT_FALSE(ENABLED_OPT(SLEW));
-
- get_systime(&now);
- HTONL_FP(&now, &rpkt.reftime);
- HTONL_FP(&now, &rpkt.org);
- HTONL_FP(&now, &rpkt.rec);
- HTONL_FP(&now, &rpkt.xmt);
- rpktl = LEN_PKT_NOMAC;
- ZERO(host);
- AF(&host) = AF_INET;
-
- EXPECT_EQ(0, handle_pkt(rpktl, &rpkt, &host, ""));
-}
-
-/* packetHandling.cpp */
+++ /dev/null
-#include "g_sntptest.h"
-
-extern "C" {
-#include "networking.h"
-#include "ntp_stdlib.h"
-};
-
-#include <sstream>
-#include <string>
-
-// Hacks into the key database.
-extern key* key_ptr;
-extern int key_cnt;
-
-class packetProcessingTest : public sntptest {
-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);
-
- 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);
- }
-
- virtual void SetUp() {
- 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);
-
- /* 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);
- }
-
- virtual void TearDown() {
- if (restoreKeyDb) {
- key_cnt = 0;
- delete key_ptr;
- key_ptr = NULL;
- }
- }
-};
-
-TEST_F(packetProcessingTest, TooShortLength) {
- EXPECT_EQ(PACKET_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
- MODE_SERVER, &testspkt, "UnitTest"));
- EXPECT_EQ(PACKET_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
- MODE_BROADCAST, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, LengthNotMultipleOfFour) {
- EXPECT_EQ(PACKET_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 6,
- MODE_SERVER, &testspkt, "UnitTest"));
- EXPECT_EQ(PACKET_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 3,
- MODE_BROADCAST, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, 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. */
- testpkt.exten[7] = htonl(3); // 3 bytes is too short.
-
- /* We send in a pkt_len of header size + 4 byte extension
- * header + 24 byte MAC, this prevents the length error to
- * be caught at an earlier stage */
- int pkt_len = LEN_PKT_NOMAC + 4 + 24;
-
- EXPECT_EQ(PACKET_UNUSEABLE,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, UnauthenticatedPacketReject) {
- // Activate authentication option
- ActivateOption("-a", "123");
- ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
-
- int pkt_len = LEN_PKT_NOMAC;
-
- // We demand authentication, but no MAC header is present.
- EXPECT_EQ(SERVER_AUTH_FAIL,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, CryptoNAKPacketReject) {
- // Activate authentication option
- ActivateOption("-a", "123");
- ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
-
- int pkt_len = LEN_PKT_NOMAC + 4; // + 4 byte MAC = Crypto-NAK
-
- EXPECT_EQ(SERVER_AUTH_FAIL,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, AuthenticatedPacketInvalid) {
- // Activate authentication option
- PrepareAuthenticationTest(50, 9, "123456789");
- ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
-
- // Prepare the packet.
- int pkt_len = LEN_PKT_NOMAC;
-
- testpkt.exten[0] = htonl(50);
- int mac_len = make_mac((char*)&testpkt, pkt_len,
- MAX_MD5_LEN, key_ptr,
- (char*)&testpkt.exten[1]);
-
- pkt_len += 4 + mac_len;
-
- // Now, alter the MAC so it becomes invalid.
- testpkt.exten[1] += 1;
-
- EXPECT_EQ(SERVER_AUTH_FAIL,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, AuthenticatedPacketUnknownKey) {
- // Activate authentication option
- PrepareAuthenticationTest(30, 9, "123456789");
- ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
-
- // Prepare the packet. Observe that the Key-ID expected is 30,
- // but the packet has a key id of 50.
- int pkt_len = LEN_PKT_NOMAC;
-
- testpkt.exten[0] = htonl(50);
- int mac_len = make_mac((char*)&testpkt, pkt_len,
- MAX_MD5_LEN, key_ptr,
- (char*)&testpkt.exten[1]);
- pkt_len += 4 + mac_len;
-
- EXPECT_EQ(SERVER_AUTH_FAIL,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, ServerVersionTooOld) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
- NTP_OLDVERSION - 1,
- MODE_CLIENT);
- ASSERT_LT(PKT_VERSION(testpkt.li_vn_mode), NTP_OLDVERSION);
-
- int pkt_len = LEN_PKT_NOMAC;
-
- EXPECT_EQ(SERVER_UNUSEABLE,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, ServerVersionTooNew) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
- NTP_VERSION + 1,
- MODE_CLIENT);
- ASSERT_GT(PKT_VERSION(testpkt.li_vn_mode), NTP_VERSION);
-
- int pkt_len = LEN_PKT_NOMAC;
-
- EXPECT_EQ(SERVER_UNUSEABLE,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, NonWantedMode) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
- NTP_VERSION,
- MODE_CLIENT);
-
- // The packet has a mode of MODE_CLIENT, but process_pkt expects MODE_SERVER
-
- EXPECT_EQ(SERVER_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-/* Tests bug 1597 */
-TEST_F(packetProcessingTest, KoDRate) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.stratum = STRATUM_PKT_UNSPEC;
- memcpy(&testpkt.refid, "RATE", 4);
-
- EXPECT_EQ(KOD_RATE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, KoDDeny) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.stratum = STRATUM_PKT_UNSPEC;
- memcpy(&testpkt.refid, "DENY", 4);
-
- EXPECT_EQ(KOD_DEMOBILIZE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, RejectUnsyncedServer) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
- NTP_VERSION,
- MODE_SERVER);
-
- EXPECT_EQ(SERVER_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, RejectWrongResponseServerMode) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- l_fp tmp;
- tmp.l_ui = 1000UL;
- tmp.l_uf = 0UL;
- HTONL_FP(&tmp, &testpkt.org);
-
- tmp.l_ui = 2000UL;
- tmp.l_uf = 0UL;
- HTONL_FP(&tmp, &testspkt.xmt);
-
- EXPECT_EQ(PACKET_UNUSEABLE,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, AcceptNoSentPacketBroadcastMode) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
- NTP_VERSION,
- MODE_BROADCAST);
-
- EXPECT_EQ(LEN_PKT_NOMAC,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_BROADCAST, NULL, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, CorrectUnauthenticatedPacket) {
- ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
-
- EXPECT_EQ(LEN_PKT_NOMAC,
- process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
-
-TEST_F(packetProcessingTest, CorrectAuthenticatedPacketMD5) {
- PrepareAuthenticationTest(10, 15, "123456789abcdef");
- ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
-
- int pkt_len = LEN_PKT_NOMAC;
-
- // Prepare the packet.
- testpkt.exten[0] = htonl(10);
- int mac_len = make_mac((char*)&testpkt, pkt_len,
- MAX_MD5_LEN, key_ptr,
- (char*)&testpkt.exten[1]);
-
- pkt_len += 4 + mac_len;
-
- EXPECT_EQ(pkt_len,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-
-}
-
-TEST_F(packetProcessingTest, CorrectAuthenticatedPacketSHA1) {
- PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno");
- ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
-
- int pkt_len = LEN_PKT_NOMAC;
-
- // Prepare the packet.
- testpkt.exten[0] = htonl(20);
- int mac_len = make_mac((char*)&testpkt, pkt_len,
- MAX_MAC_LEN, key_ptr,
- (char*)&testpkt.exten[1]);
-
- pkt_len += 4 + mac_len;
-
- EXPECT_EQ(pkt_len,
- process_pkt(&testpkt, &testsock, pkt_len,
- MODE_SERVER, &testspkt, "UnitTest"));
-}
+++ /dev/null
-#ifndef SNTPTEST_H
-#define SNTPTEST_H
-
-#include "tests_main.h"
-
-extern "C" {
-#include "ntp_stdlib.h"
-#include "sntp-opts.h"
-};
-
-class sntptest : public ntptest {
-protected:
- sntptest() {
- optionSaveState(&sntpOptions);
- }
-
- ~sntptest() {
- optionRestore(&sntpOptions);
- }
-
- void ActivateOption(const char* option, const char* argument) {
- const int ARGV_SIZE = 4;
-
- char* opts[ARGV_SIZE];
-
- opts[0] = estrdup("sntpopts");
- opts[1] = estrdup(option);
- opts[2] = estrdup(argument);
- opts[3] = estrdup("127.0.0.1");
-
- optionProcess(&sntpOptions, ARGV_SIZE, opts);
- }
-};
-
-#endif // SNTPTEST_H
+++ /dev/null
-#include "g_sntptest.h"
-#include "g_fileHandlingTest.h"
-
-extern "C" {
-#include "main.h"
-#include "utilities.h"
-const char * Version = "stub unit test Version string";
-};
-
-using std::string;
-
-class utilitiesTest : public sntptest {
-protected:
- sockaddr_u CreateSockaddr4(const char* address) {
- sockaddr_u s;
- s.sa4.sin_family = AF_INET;
- s.sa4.sin_addr.s_addr = inet_addr(address);
- SET_PORT(&s, 123);
-
- return s;
- }
-
- addrinfo CreateAddrinfo(sockaddr_u* sock) {
- addrinfo a;
- a.ai_family = sock->sa.sa_family;
- a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family);
- a.ai_addr = &sock->sa;
- return a;
- }
-};
-
-class debugUtilitiesTest : public fileHandlingTest {
-protected:
- bool outputFileOpened;
- FILE* outputFile;
-
- debugUtilitiesTest() : outputFileOpened(false) {}
-
- void InitDebugTest(const string& filename) {
- // Clear the contents of the current file.
- // Open the output file
- outputFile = fopen(filename.c_str(), "w+");
- ASSERT_TRUE(outputFile != NULL);
- outputFileOpened = true;
- }
-
- // Closes outputFile, and compare contents.
- void FinishDebugTest(const string& expected,
- const string& actual) {
- if (outputFileOpened)
- fclose(outputFile);
-
- ifstream e(expected.c_str());
- ifstream a(actual.c_str());
- ASSERT_TRUE(e.good());
- ASSERT_TRUE(a.good());
-
- CompareFileContent(e, a);
- }
-};
-
-/*
- * These tests are essentially a copy of the tests for socktoa()
- * in libntp. If sntp switches to using that functions, these
- * tests can be removed.
- */
-
-TEST_F(utilitiesTest, IPv4Address) {
- const char* ADDR = "192.0.2.10";
-
- sockaddr_u input = CreateSockaddr4(ADDR);
- addrinfo inputA = CreateAddrinfo(&input);
-
- EXPECT_STREQ(ADDR, ss_to_str(&input));
- EXPECT_STREQ(ADDR, addrinfo_to_str(&inputA));
-}
-
-TEST_F(utilitiesTest, IPv6Address) {
- const struct in6_addr address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- };
- const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
- sockaddr_u input;
- addrinfo inputA;
-
- memset(&input, 0, sizeof(input));
- input.sa6.sin6_family = AF_INET6;
- input.sa6.sin6_addr = address;
- EXPECT_STREQ(expected, ss_to_str(&input));
-
- inputA = CreateAddrinfo(&input);
- EXPECT_STREQ(expected, addrinfo_to_str(&inputA));
-}
-
-TEST_F(utilitiesTest, SetLiVnMode1) {
- pkt expected;
- expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
- NTP_VERSION,
- MODE_SERVER);
-
- pkt actual;
- set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION,
- MODE_SERVER);
-
- EXPECT_EQ(expected.li_vn_mode, actual.li_vn_mode);
-}
-
-TEST_F(utilitiesTest, SetLiVnMode2) {
- pkt expected;
- expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
- NTP_OLDVERSION,
- MODE_BROADCAST);
-
- pkt actual;
- set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION,
- MODE_BROADCAST);
-
- EXPECT_EQ(expected.li_vn_mode, actual.li_vn_mode);
-}
-
-/* Debug utilities tests */
-
-TEST_F(debugUtilitiesTest, PktOutput) {
- string filename = CreatePath("debug-output-pkt", OUTPUT_DIR);
- InitDebugTest(filename);
-
- pkt testpkt;
- memset(&testpkt, 0, sizeof(pkt));
- testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
- NTP_VERSION,
- MODE_SERVER);
-
- l_fp test;
- test.l_ui = 8;
- test.l_uf = 2147483647; // Lots of ones.
- HTONL_FP(&test, &testpkt.xmt);
-
- pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile);
-
- FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename);
-}
-
-TEST_F(debugUtilitiesTest, LfpOutputBinaryFormat) {
- string filename = CreatePath("debug-output-lfp-bin", OUTPUT_DIR);
- InitDebugTest(filename);
-
- l_fp test;
- test.l_ui = 63; // 00000000 00000000 00000000 00111111
- test.l_uf = 127; // 00000000 00000000 00000000 01111111
-
- l_fp network;
- HTONL_FP(&test, &network);
-
- l_fp_output_bin(&network, outputFile);
-
- FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename);
-}
-
-TEST_F(debugUtilitiesTest, LfpOutputDecimalFormat) {
- string filename = CreatePath("debug-output-lfp-dec", OUTPUT_DIR);
- InitDebugTest(filename);
-
- l_fp test;
- test.l_ui = 6310; // 0x000018A6
- test.l_uf = 308502; // 0x00004B516
-
- l_fp network;
- HTONL_FP(&test, &network);
-
- l_fp_output_dec(&network, outputFile);
-
- FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename);
-}
+++ /dev/null
-#include "tests_main.h"
-
-extern "C" {
-const char * Version = "stub unit test Version string";
-};
-
-int main(int argc, char **argv) {
- ::testing::InitGoogleTest(&argc, argv);
-
- init_lib();
- init_auth();
-
- // Some tests makes use of extra parameters passed to the tests
- // executable. Save these params as static members of the base class.
- if (argc > 1) {
- ntptest::SetExtraParams(1, argc-1, argv);
- }
-
- return RUN_ALL_TESTS();
-}
-
-std::vector<std::string> ntptest::m_params;
-
-void ntptest::SetExtraParams(int start, int count, char** argv)
-{
- for (int i=0; i<count; i++) {
- m_params.push_back(argv[i+start]);
- }
-}
+++ /dev/null
-#ifndef TESTS_MAIN_H
-#define TESTS_MAIN_H
-
-#include "config.h"
-
-#include <string>
-#include <vector>
-
-#include <gtest/gtest.h>
-
-extern "C" {
-#include "ntp_stdlib.h"
-}
-
-class ntptest : public ::testing::Test {
-public:
- static void SetExtraParams(int start, int count, char** argv);
-protected:
- static std::vector<std::string> m_params;
-};
-
-#endif // TESTS_MAIN_H
NULL =
SUBDIRS =
-DIST_SUBDIRS =
-
-if GTEST_AVAILABLE
-SUBDIRS += \
- ntpd \
- $(NULL)
-endif
SUBDIRS += \
- bug-2803 \
- libntp \
- sec-2853 \
- $(NULL)
-
-DIST_SUBDIRS += \
bug-2803 \
libntp \
ntpd \
test-ymd2yd \
$(NULL)
-if GTEST_AVAILABLE
-check_PROGRAMS += tests
-else
-EXTRA_PROGRAMS = tests
-endif
-
LDADD = \
$(top_builddir)/libntp/libntp.a \
$(LDADD_LIBNTP) \
$(top_builddir)/sntp/unity/libunity.a \
$(NULL)
-tests_LDADD = \
- $(LDADD) \
- $(GTEST_LDFLAGS) \
- $(GTEST_LIBS) \
- $(NULL)
-
AM_CFLAGS = $(CFLAGS_NTP)
-AM_CXXFLAGS = $(GTEST_CXXFLAGS)
AM_CPPFLAGS = $(NTP_INCS)
AM_CPPFLAGS += -I$(top_srcdir)/sntp
AM_CPPFLAGS += -DUNITY_INCLUDE_CONFIG_H
AM_CPPFLAGS += -I$(top_srcdir)/sntp/unity
-tests_CPPFLAGS = $(AM_CPPFLAGS)
-tests_CPPFLAGS += $(GTEST_CPPFLAGS)
-
AM_LDFLAGS = $(LDFLAGS_NTP)
-tests_SOURCES = \
- $(top_srcdir)/sntp/tests_main.cpp \
- g_libntptest.cpp \
- g_a_md5encrypt.cpp \
- g_atoint.cpp \
- g_atouint.cpp \
- g_authkeys.cpp \
- g_buftvtots.cpp \
- g_calendar.cpp \
- g_caljulian.cpp \
- g_caltontp.cpp \
- g_calyearstart.cpp \
- g_clocktime.cpp \
- g_decodenetnum.cpp \
- g_hextoint.cpp \
- g_hextolfp.cpp \
- g_humandate.cpp \
- g_lfpfunc.cpp \
- g_lfptest.h \
- g_lfptostr.cpp \
- g_libntptest.h \
- g_modetoa.cpp \
- g_msyslog.cpp \
- g_netof.cpp \
- g_numtoa.cpp \
- g_numtohost.cpp \
- g_octtoint.cpp \
- g_prettydate.cpp \
- g_recvbuff.cpp \
- g_refnumtoa.cpp \
- g_sfptostr.cpp \
- g_sockaddrtest.h \
- g_socktoa.cpp \
- g_ssl_init.cpp \
- g_statestr.cpp \
- g_strtolfp.cpp \
- g_timespecops.cpp \
- g_timestructs.cpp \
- g_timestructs.h \
- g_timevalops.cpp \
- g_tstotv.cpp \
- g_tvtots.cpp \
- g_uglydate.cpp \
- g_vi64ops.cpp \
- g_ymd2yd.cpp \
- $(NULL)
-
BUILT_SOURCES += \
$(srcdir)/run-a_md5encrypt.c \
$(srcdir)/run-atoint.c \
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#ifdef OPENSSL
-# include "openssl/err.h"
-# include "openssl/rand.h"
-# include "openssl/evp.h"
-#endif
-#include "ntp.h"
-#include "ntp_stdlib.h"
-};
-
-class a_md5encryptTest : public libntptest {
-protected:
-};
-
-/*
- * Example packet with MD5 hash calculated manually.
- */
-const int keytype = KEY_TYPE_MD5;
-const char *key = "abcdefgh";
-const u_short keyLength = 8;
-const char *packet = "ijklmnopqrstuvwx";
-const int packetLength = 16;
-const int keyIdLength = 4;
-const int digestLength = 16;
-const int totalLength = packetLength + keyIdLength + digestLength;
-const char *expectedPacket = "ijklmnopqrstuvwx\0\0\0\0\x0c\x0e\x84\xcf\x0b\xb7\xa8\x68\x8e\x52\x38\xdb\xbc\x1c\x39\x53";
-
-TEST_F(a_md5encryptTest, Encrypt) {
- char *packetPtr = new char[totalLength];
- memset(packetPtr+packetLength, 0, keyIdLength);
- memcpy(packetPtr, packet, packetLength);
-
- cache_secretsize = keyLength;
-
- int length = MD5authencrypt(keytype, (u_char*)key, (u_int32*)packetPtr, packetLength);
-
- EXPECT_TRUE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)packetPtr, packetLength, length));
-
- EXPECT_EQ(20, length);
- EXPECT_TRUE(memcmp(expectedPacket, packetPtr, totalLength) == 0);
-
- delete[] packetPtr;
-}
-
-TEST_F(a_md5encryptTest, DecryptValid) {
- cache_secretsize = keyLength;
-
- EXPECT_TRUE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)expectedPacket, packetLength, 20));
-}
-
-TEST_F(a_md5encryptTest, DecryptInvalid) {
- cache_secretsize = keyLength;
-
- const char *invalidPacket = "ijklmnopqrstuvwx\0\0\0\0\x0c\x0e\x84\xcf\x0b\xb7\xa8\x68\x8e\x52\x38\xdb\xbc\x1c\x39\x54";
-
- EXPECT_FALSE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)invalidPacket, packetLength, 20));
-}
-
-TEST_F(a_md5encryptTest, IPv4AddressToRefId) {
- sockaddr_u addr;
- addr.sa4.sin_family = AF_INET;
- addr.sa4.sin_port = htons(80);
-
- u_int32 address = inet_addr("192.0.2.1");
- addr.sa4.sin_addr.s_addr = address;
-
- EXPECT_EQ(address, addr2refid(&addr));
-}
-
-TEST_F(a_md5encryptTest, IPv6AddressToRefId) {
- const struct in6_addr address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- };
-
-
- sockaddr_u addr;
- addr.sa6.sin6_family = AF_INET6;
-
- addr.sa6.sin6_addr = address;
-
- const int expected = 0x75cffd52;
-
- EXPECT_EQ(expected, addr2refid(&addr));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class atointTest : public libntptest {
-};
-
-TEST_F(atointTest, RegularPositive) {
- const char *str = "17";
- long val;
-
- ASSERT_TRUE(atoint(str, &val));
- EXPECT_EQ(17, val);
-}
-
-TEST_F(atointTest, RegularNegative) {
- const char *str = "-20";
- long val;
-
- ASSERT_TRUE(atoint(str, &val));
- EXPECT_EQ(-20, val);
-}
-
-TEST_F(atointTest, PositiveOverflowBoundary) {
- const char *str = "2147483648";
- long val;
-
- EXPECT_FALSE(atoint(str, &val));
-}
-
-TEST_F(atointTest, NegativeOverflowBoundary) {
- const char *str = "-2147483649";
- long val;
-
- EXPECT_FALSE(atoint(str, &val));
-}
-
-TEST_F(atointTest, PositiveOverflowBig) {
- const char *str = "2300000000";
- long val;
-
- EXPECT_FALSE(atoint(str, &val));
-}
-
-TEST_F(atointTest, IllegalCharacter) {
- const char *str = "4500l";
- long val;
-
- EXPECT_FALSE(atoint(str, &val));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class atouintTest : public libntptest {
-};
-
-TEST_F(atouintTest, RegularPositive) {
- const char *str = "305";
- u_long actual;
-
- ASSERT_TRUE(atouint(str, &actual));
- EXPECT_EQ(305, actual);
-}
-
-TEST_F(atouintTest, PositiveOverflowBoundary) {
- const char *str = "4294967296";
- u_long actual;
-
- ASSERT_FALSE(atouint(str, &actual));
-}
-
-TEST_F(atouintTest, PositiveOverflowBig) {
- const char *str = "8000000000";
- u_long actual;
-
- ASSERT_FALSE(atouint(str, &actual));
-}
-
-TEST_F(atouintTest, Negative) {
- const char *str = "-1";
- u_long actual;
-
- ASSERT_FALSE(atouint(str, &actual));
-}
-
-TEST_F(atouintTest, IllegalChar) {
- const char *str = "50c3";
- u_long actual;
-
- ASSERT_FALSE(atouint(str, &actual));
-}
+++ /dev/null
-/* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
-
-#include "g_libntptest.h"
-
-extern "C" {
-#ifdef OPENSSL
-# include "openssl/err.h"
-# include "openssl/rand.h"
-# include "openssl/evp.h"
-#endif
-#include "ntp.h"
-#include "ntp_stdlib.h"
-};
-
-class authkeysTest : public libntptest {
-protected:
- static const int KEYTYPE = KEY_TYPE_MD5;
-
- virtual void SetUp() {
- /*
- * init_auth() is called by tests_main.cpp earlier. It
- * does not initialize global variables like
- * authnumkeys, so let's reset them to zero here.
- */
- authnumkeys = 0;
-
- /*
- * Especially, empty the key cache!
- */
- cache_keyid = 0;
- cache_type = 0;
- cache_flags = 0;
- cache_secret = NULL;
- cache_secretsize = 0;
- }
-
- void AddTrustedKey(keyid_t keyno) {
- /*
- * We need to add a MD5-key in addition to setting the
- * trust, because authhavekey() requires type != 0.
- */
- MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
-
- authtrust(keyno, TRUE);
- }
-
- void AddUntrustedKey(keyid_t keyno) {
- authtrust(keyno, FALSE);
- }
-};
-
-TEST_F(authkeysTest, AddTrustedKeys) {
- const keyid_t KEYNO1 = 5;
- const keyid_t KEYNO2 = 8;
-
- AddTrustedKey(KEYNO1);
- AddTrustedKey(KEYNO2);
-
- EXPECT_TRUE(authistrusted(KEYNO1));
- EXPECT_TRUE(authistrusted(KEYNO2));
-}
-
-TEST_F(authkeysTest, AddUntrustedKey) {
- const keyid_t KEYNO = 3;
-
- AddUntrustedKey(KEYNO);
-
- EXPECT_FALSE(authistrusted(KEYNO));
-}
-
-TEST_F(authkeysTest, HaveKeyCorrect) {
- const keyid_t KEYNO = 3;
-
- AddTrustedKey(KEYNO);
-
- EXPECT_TRUE(auth_havekey(KEYNO));
- EXPECT_TRUE(authhavekey(KEYNO));
-}
-
-TEST_F(authkeysTest, HaveKeyIncorrect) {
- const keyid_t KEYNO = 2;
-
- EXPECT_FALSE(auth_havekey(KEYNO));
- EXPECT_FALSE(authhavekey(KEYNO));
-}
-
-TEST_F(authkeysTest, AddWithAuthUseKey) {
- const keyid_t KEYNO = 5;
- const char* KEY = "52a";
-
- EXPECT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
-}
-
-TEST_F(authkeysTest, EmptyKey) {
- const keyid_t KEYNO = 3;
- const char* KEY = "";
-
-
- EXPECT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
-}
+++ /dev/null
-#include "g_lfptest.h"
-
-extern "C" {
-#include "ntp_unixtime.h"
-};
-
-// Required for Solaris.
-#include <math.h>
-
-class buftvtotsTest : public lfptest {
-};
-
-#ifndef SYS_WINNT
-TEST_F(buftvtotsTest, ZeroBuffer) {
- const timeval input = {0, 0};
- const l_fp expected = {0 + JAN_1970, 0};
-
- l_fp actual;
-
- ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(buftvtotsTest, IntegerAndFractionalBuffer) {
- const timeval input = {5, 500000}; // 5.5
- const l_fp expected = {5 + JAN_1970, HALF};
-
- l_fp actual;
-
- ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
-
- // Compare the fractional part with an absolute error given.
- EXPECT_EQ(expected.l_ui, actual.l_ui);
-
- double expectedDouble, actualDouble;
- M_LFPTOD(0, expected.l_uf, expectedDouble);
- M_LFPTOD(0, actual.l_uf, actualDouble);
-
- // The error should be less than 0.5 us
- EXPECT_NEAR(expectedDouble, actualDouble, 0.0000005);
-}
-
-TEST_F(buftvtotsTest, IllegalMicroseconds) {
- const timeval input = {0, 1100000}; // > 999 999 microseconds.
-
- l_fp actual;
-
- ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
-}
-
-#else
-TEST_F(buftvtotsTest, AlwaysFalseOnWindows) {
- /*
- * Under Windows, buftvtots will just return
- * 0 (false).
- */
- l_fp actual;
- ASSERT_FALSE(buftvtots("", &actual));
-}
-
-#endif
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_calendar.h"
-}
-
-#include <string>
-#include <sstream>
-
-class calendarTest : public libntptest {
-protected:
- static int leapdays(int year);
-
- std::string CalendarToString(const calendar &cal);
- std::string CalendarToString(const isodate &iso);
- ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual);
- ::testing::AssertionResult IsEqual(const isodate &expected, const isodate &actual);
-
- std::string DateToString(const calendar &cal);
- std::string DateToString(const isodate &iso);
- ::testing::AssertionResult IsEqualDate(const calendar &expected, const calendar &actual);
- ::testing::AssertionResult IsEqualDate(const isodate &expected, const isodate &actual);
-};
-
-
-// ---------------------------------------------------------------------
-// test support stuff
-// ---------------------------------------------------------------------
-int
-calendarTest::leapdays(int year)
-{
- if (year % 400 == 0)
- return 1;
- if (year % 100 == 0)
- return 0;
- if (year % 4 == 0)
- return 1;
- return 0;
-}
-
-std::string
-calendarTest::CalendarToString(const calendar &cal) {
- std::ostringstream ss;
- ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
- << " (" << cal.yearday << ") " << (u_int)cal.hour << ":"
- << (u_int)cal.minute << ":" << (u_int)cal.second;
- return ss.str();
-}
-
-std::string
-calendarTest:: CalendarToString(const isodate &iso) {
- std::ostringstream ss;
- ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday
- << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second;
- return ss.str();
-}
-
-::testing::AssertionResult
-calendarTest:: IsEqual(const calendar &expected, const calendar &actual) {
- if (expected.year == actual.year &&
- (!expected.yearday || expected.yearday == actual.yearday) &&
- expected.month == actual.month &&
- expected.monthday == actual.monthday &&
- expected.hour == actual.hour &&
- expected.minute == actual.minute &&
- expected.second == actual.second) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << CalendarToString(expected) << " but was "
- << CalendarToString(actual);
- }
-}
-
-::testing::AssertionResult
-calendarTest:: IsEqual(const isodate &expected, const isodate &actual) {
- if (expected.year == actual.year &&
- expected.week == actual.week &&
- expected.weekday == actual.weekday &&
- expected.hour == actual.hour &&
- expected.minute == actual.minute &&
- expected.second == actual.second) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << CalendarToString(expected) << " but was "
- << CalendarToString(actual);
- }
-}
-
-std::string
-calendarTest:: DateToString(const calendar &cal) {
- std::ostringstream ss;
- ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
- << " (" << cal.yearday << ")";
- return ss.str();
-}
-
-std::string
-calendarTest:: DateToString(const isodate &iso) {
- std::ostringstream ss;
- ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday;
- return ss.str();
-}
-
-::testing::AssertionResult
-calendarTest:: IsEqualDate(const calendar &expected, const calendar &actual) {
- if (expected.year == actual.year &&
- (!expected.yearday || expected.yearday == actual.yearday) &&
- expected.month == actual.month &&
- expected.monthday == actual.monthday) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << DateToString(expected) << " but was "
- << DateToString(actual);
- }
-}
-
-::testing::AssertionResult
-calendarTest:: IsEqualDate(const isodate &expected, const isodate &actual) {
- if (expected.year == actual.year &&
- expected.week == actual.week &&
- expected.weekday == actual.weekday) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << DateToString(expected) << " but was "
- << DateToString(actual);
- }
-}
-
-
-// ---------------------------------------------------------------------
-// test cases
-// ---------------------------------------------------------------------
-static const u_short real_month_table[2][13] = {
- /* -*- table for regular years -*- */
- { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
- /* -*- table for leap years -*- */
- { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
-};
-
-// days in month, with one month wrap-around at both ends
-static const u_short real_month_days[2][14] = {
- /* -*- table for regular years -*- */
- { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 },
- /* -*- table for leap years -*- */
- { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }
-};
-
-// test the day/sec join & split ops, making sure that 32bit
-// intermediate results would definitely overflow and the hi DWORD of
-// the 'vint64' is definitely needed.
-TEST_F(calendarTest, DaySplitMerge) {
- for (int32 day = -1000000; day <= 1000000; day += 100) {
- for (int32 sec = -100000; sec <= 186400; sec += 10000) {
- vint64 merge = ntpcal_dayjoin(day, sec);
- ntpcal_split split = ntpcal_daysplit(&merge);
- int32 eday = day;
- int32 esec = sec;
-
- while (esec >= 86400) {
- eday += 1;
- esec -= 86400;
- }
- while (esec < 0) {
- eday -= 1;
- esec += 86400;
- }
-
- EXPECT_EQ(eday, split.hi);
- EXPECT_EQ(esec, split.lo);
- }
- }
-}
-
-TEST_F(calendarTest, SplitYearDays1) {
- for (int32 eyd = -1; eyd <= 365; eyd++) {
- ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
- if (split.lo >= 0 && split.hi >= 0) {
- EXPECT_GT(12, split.hi);
- EXPECT_GT(real_month_days[0][split.hi+1], split.lo);
- int32 tyd = real_month_table[0][split.hi] + split.lo;
- EXPECT_EQ(eyd, tyd);
- } else
- EXPECT_TRUE(eyd < 0 || eyd > 364);
- }
-}
-
-TEST_F(calendarTest, SplitYearDays2) {
- for (int32 eyd = -1; eyd <= 366; eyd++) {
- ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
- if (split.lo >= 0 && split.hi >= 0) {
- EXPECT_GT(12, split.hi);
- EXPECT_GT(real_month_days[1][split.hi+1], split.lo);
- int32 tyd = real_month_table[1][split.hi] + split.lo;
- EXPECT_EQ(eyd, tyd);
- } else
- EXPECT_TRUE(eyd < 0 || eyd > 365);
- }
-}
-
-TEST_F(calendarTest, RataDie1) {
- int32 testDate = 1; // 0001-01-01 (proleptic date)
- calendar expected = { 1, 1, 1, 1 };
- calendar actual;
-
- ntpcal_rd_to_date(&actual, testDate);
- EXPECT_TRUE(IsEqualDate(expected, actual));
-}
-
-// check last day of february for first 10000 years
-TEST_F(calendarTest, LeapYears1) {
- calendar dateIn, dateOut;
-
- for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
- dateIn.month = 2;
- dateIn.monthday = 28 + leapdays(dateIn.year);
- dateIn.yearday = 31 + dateIn.monthday;
-
- ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
-
- EXPECT_TRUE(IsEqualDate(dateIn, dateOut));
- }
-}
-
-// check first day of march for first 10000 years
-TEST_F(calendarTest, LeapYears2) {
- calendar dateIn, dateOut;
-
- for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
- dateIn.month = 3;
- dateIn.monthday = 1;
- dateIn.yearday = 60 + leapdays(dateIn.year);
-
- ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
- EXPECT_TRUE(IsEqualDate(dateIn, dateOut));
- }
-}
-
-// Full roundtrip for 1601-01-01 to 2400-12-31
-// checks sequence of rata die numbers and validates date output
-// (since the input is all nominal days of the calendar in that range
-// and the result of the inverse calculation must match the input no
-// invalid output can occur.)
-TEST_F(calendarTest, RoundTripDate) {
- calendar truDate, expDate = { 1600, 0, 12, 31 };;
- int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
- int leaps;
-
- while (expDate.year < 2400) {
- expDate.year++;
- expDate.month = 0;
- expDate.yearday = 0;
- leaps = leapdays(expDate.year);
- while (expDate.month < 12) {
- expDate.month++;
- expDate.monthday = 0;
- while (expDate.monthday < real_month_days[leaps][expDate.month]) {
- expDate.monthday++;
- expDate.yearday++;
- expRdn++;
-
- truRdn = ntpcal_date_to_rd(&expDate);
- EXPECT_EQ(expRdn, truRdn);
-
- ntpcal_rd_to_date(&truDate, truRdn);
- EXPECT_TRUE(IsEqualDate(expDate, truDate));
- }
- }
- }
-}
-
-// Roundtrip testing on calyearstart
-TEST_F(calendarTest, RoundTripYearStart) {
- static const time_t pivot = 0;
- u_int32 ntp, expys, truys;
- calendar date;
-
- for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
- truys = calyearstart(ntp, &pivot);
- ntpcal_ntp_to_date(&date, ntp, &pivot);
- date.month = date.monthday = 1;
- date.hour = date.minute = date.second = 0;
- expys = ntpcal_date_to_ntp(&date);
- EXPECT_EQ(expys, truys);
- }
-}
-
-// Roundtrip testing on calymonthstart
-TEST_F(calendarTest, RoundTripMonthStart) {
- static const time_t pivot = 0;
- u_int32 ntp, expms, trums;
- calendar date;
-
- for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
- trums = calmonthstart(ntp, &pivot);
- ntpcal_ntp_to_date(&date, ntp, &pivot);
- date.monthday = 1;
- date.hour = date.minute = date.second = 0;
- expms = ntpcal_date_to_ntp(&date);
- EXPECT_EQ(expms, trums);
- }
-}
-
-// Roundtrip testing on calweekstart
-TEST_F(calendarTest, RoundTripWeekStart) {
- static const time_t pivot = 0;
- u_int32 ntp, expws, truws;
- isodate date;
-
- for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
- truws = calweekstart(ntp, &pivot);
- isocal_ntp_to_date(&date, ntp, &pivot);
- date.hour = date.minute = date.second = 0;
- date.weekday = 1;
- expws = isocal_date_to_ntp(&date);
- EXPECT_EQ(expws, truws);
- }
-}
-
-// Roundtrip testing on caldaystart
-TEST_F(calendarTest, RoundTripDayStart) {
- static const time_t pivot = 0;
- u_int32 ntp, expds, truds;
- calendar date;
-
- for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
- truds = caldaystart(ntp, &pivot);
- ntpcal_ntp_to_date(&date, ntp, &pivot);
- date.hour = date.minute = date.second = 0;
- expds = ntpcal_date_to_ntp(&date);
- EXPECT_EQ(expds, truds);
- }
-}
-
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_calendar.h"
-}
-
-#include <string>
-#include <sstream>
-
-class caljulianTest : public libntptest {
-protected:
- virtual void SetUp();
- virtual void TearDown();
-
- std::string CalendarToString(const calendar &cal) {
- std::ostringstream ss;
- ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
- << " (" << cal.yearday << ") " << (u_int)cal.hour << ":"
- << (u_int)cal.minute << ":" << (u_int)cal.second;
- return ss.str();
- }
-
- ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual) {
- if (expected.year == actual.year &&
- (expected.yearday == actual.yearday ||
- (expected.month == actual.month &&
- expected.monthday == actual.monthday)) &&
- expected.hour == actual.hour &&
- expected.minute == actual.minute &&
- expected.second == actual.second) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << CalendarToString(expected) << " but was "
- << CalendarToString(actual);
- }
- }
-};
-
-void caljulianTest::SetUp()
-{
- ntpcal_set_timefunc(timefunc);
- settime(1970, 1, 1, 0, 0, 0);
-}
-
-void caljulianTest::TearDown()
-{
- ntpcal_set_timefunc(NULL);
-}
-
-
-TEST_F(caljulianTest, RegularTime) {
- u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
- calendar expected = {2010,160,6,9,14,0,0};
-
- calendar actual;
-
- caljulian(testDate, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(caljulianTest, LeapYear) {
- u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
- calendar expected = {2012, 179, 6, 28, 20, 0, 0};
-
- calendar actual;
-
- caljulian(input, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(caljulianTest, uLongBoundary) {
- u_long time = 4294967295UL; // 2036-02-07 6:28:15
- calendar expected = {2036,0,2,7,6,28,15};
-
- calendar actual;
-
- caljulian(time, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(caljulianTest, uLongWrapped) {
- u_long time = 0;
- calendar expected = {2036,0,2,7,6,28,16};
-
- calendar actual;
-
- caljulian(time, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_calendar.h"
-}
-
-class caltontpTest : public libntptest {
-};
-
-TEST_F(caltontpTest, DateGivenMonthDay) {
- // 2010-06-24 12:50:00
- calendar input = {2010, 0, 6, 24, 12, 50, 0};
-
- u_long expected = 3486372600UL; // This is the timestamp above.
-
- EXPECT_EQ(expected, caltontp(&input));
-}
-
-TEST_F(caltontpTest, DateGivenYearDay) {
- // 2010-06-24 12:50:00
- // This is the 175th day of 2010.
- calendar input = {2010, 175, 0, 0, 12, 50, 0};
-
- u_long expected = 3486372600UL; // This is the timestamp above.
-
- EXPECT_EQ(expected, caltontp(&input));
-}
-
-TEST_F(caltontpTest, DateLeapYear) {
- // 2012-06-24 12:00:00
- // This is the 176th day of 2012 (since 2012 is a leap year).
- calendar inputYd = {2012, 176, 0, 0, 12, 00, 00};
- calendar inputMd = {2012, 0, 6, 24, 12, 00, 00};
-
- u_long expected = 3549528000UL;
-
- EXPECT_EQ(expected, caltontp(&inputYd));
- EXPECT_EQ(expected, caltontp(&inputMd));
-}
-
-TEST_F(caltontpTest, WraparoundDateIn2036) {
- // 2036-02-07 06:28:16
- // This is (one) wrapping boundary where we go from ULONG_MAX to 0.
- calendar input = {2036, 0, 2, 7, 6, 28, 16};
-
- u_long expected = 0UL;
-
- EXPECT_EQ(expected, caltontp(&input));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class calyearstartTest : public libntptest {
-protected:
- virtual void SetUp();
- virtual void TearDown();
-};
-
-void calyearstartTest::SetUp()
-{
- ntpcal_set_timefunc(timefunc);
- settime(1970, 1, 1, 0, 0, 0);
-}
-
-void calyearstartTest::TearDown()
-{
- ntpcal_set_timefunc(NULL);
-}
-
-
-TEST_F(calyearstartTest, NoWrapInDateRange) {
- const u_int32 input = 3486372600UL; // 2010-06-24 12:50:00.
- const u_int32 expected = 3471292800UL; // 2010-01-01 00:00:00
-
- EXPECT_EQ(expected, calyearstart(input, &nowtime));
- EXPECT_EQ(expected, calyearstart(input, NULL));
-}
-
-TEST_F(calyearstartTest, NoWrapInDateRangeLeapYear) {
- const u_int32 input = 3549528000UL; // 2012-06-24 12:00:00
- const u_int32 expected = 3534364800UL; // 2012-01-01 00:00:00
-
- EXPECT_EQ(expected, calyearstart(input, &nowtime));
- EXPECT_EQ(expected, calyearstart(input, NULL));
-}
-
-TEST_F(calyearstartTest, WrapInDateRange) {
- const u_int32 input = 19904UL; // 2036-02-07 12:00:00
- const u_int32 expected = 4291747200UL; // 2036-01-01 00:00:00
-
- EXPECT_EQ(expected, calyearstart(input, &nowtime));
- EXPECT_EQ(expected, calyearstart(input, NULL));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-// ---------------------------------------------------------------------
-// test fixture
-//
-// The clocktimeTest uses the NTP calendar feature to use a mockup
-// function for getting the current system time, so the tests are not
-// dependent on the actual system time.
-
-class clocktimeTest : public libntptest {
- virtual void SetUp();
- virtual void TearDown();
-};
-
-void clocktimeTest::SetUp()
-{
- ntpcal_set_timefunc(timefunc);
- settime(2000, 1, 1, 0, 0, 0);
-}
-
-void clocktimeTest::TearDown()
-{
- ntpcal_set_timefunc(NULL);
-}
-
-// ---------------------------------------------------------------------
-// test cases
-
-TEST_F(clocktimeTest, CurrentYear) {
- // Timestamp: 2010-06-24 12:50:00Z
- const u_int32 timestamp = 3486372600UL;
- const u_int32 expected = timestamp; // exactly the same.
-
- const int yday=175, hour=12, minute=50, second=0, tzoff=0;
-
- u_long yearstart=0;
- u_int32 actual;
-
- ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
- EXPECT_EQ(expected, actual);
-}
-
-TEST_F(clocktimeTest, CurrentYearFuzz) {
- /*
- * Timestamp (rec_ui) is: 2010-06-24 12:50:00
- * Time sent into function is 12:00:00.
- *
- * Since the fuzz is rather small, we should get a NTP
- * timestamp for the 12:00:00 time.
- */
-
- const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z
- const u_int32 expected = 3486369600UL; // 2010-06-24 12:00:00Z
-
- const int yday=175, hour=12, minute=0, second=0, tzoff=0;
-
- u_long yearstart=0;
- u_int32 actual;
-
- ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
- EXPECT_EQ(expected, actual);
-}
-
-TEST_F(clocktimeTest, TimeZoneOffset) {
- /*
- * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
- * (which is 2010-06-24 04:00:00Z)
- *
- * Time sent into function is 04:00:00 +0800
- */
- const u_int32 timestamp = 3486369600UL;
- const u_int32 expected = timestamp;
-
- const int yday=175, hour=4, minute=0, second=0, tzoff=8;
-
- u_long yearstart=0;
- u_int32 actual;
-
- ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
- EXPECT_EQ(expected, actual);
-}
-
-TEST_F(clocktimeTest, WrongYearStart) {
- /*
- * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z
- * Time sent into function is 11:00:00.
- * Yearstart sent into function is the yearstart of 2009!
- */
- const u_int32 timestamp = 3471418800UL;
- const u_int32 expected = timestamp;
-
- const int yday=2, hour=11, minute=0, second=0, tzoff=0;
-
- u_long yearstart = 302024100UL; // Yearstart of 2009.
- u_int32 actual;
-
- ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
- EXPECT_EQ(expected, actual);
-}
-
-TEST_F(clocktimeTest, PreviousYear) {
- /*
- * Timestamp is: 2010-01-01 01:00:00Z
- * Time sent into function is 23:00:00
- * (which is meant to be 2009-12-31 23:00:00Z)
- */
- const u_int32 timestamp = 3471296400UL;
- const u_int32 expected = 3471289200UL;
-
- const int yday=365, hour=23, minute=0, second=0, tzoff=0;
-
- u_long yearstart = 0;
- u_int32 actual;
-
- ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
- EXPECT_EQ(expected, actual);
-}
-
-TEST_F(clocktimeTest, NextYear) {
- /*
- * Timestamp is: 2009-12-31 23:00:00Z
- * Time sent into function is 01:00:00
- * (which is meant to be 2010-01-01 01:00:00Z)
- */
- const u_int32 timestamp = 3471289200UL;
- const u_int32 expected = 3471296400UL;
-
- const int yday=1, hour=1, minute=0, second=0, tzoff=0;
- u_long yearstart = 0;
- u_int32 actual;
-
- ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
- EXPECT_EQ(expected, actual);
-}
-
-TEST_F(clocktimeTest, NoReasonableConversion) {
- /* Timestamp is: 2010-01-02 11:00:00Z */
- const u_int32 timestamp = 3471418800UL;
-
- const int yday=100, hour=12, minute=0, second=0, tzoff=0;
- u_long yearstart = 0;
- u_int32 actual;
-
- ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
- &yearstart, &actual));
-}
-
-TEST_F(clocktimeTest, AlwaysInLimit) {
- /* Timestamp is: 2010-01-02 11:00:00Z */
- const u_int32 timestamp = 3471418800UL;
- const u_short prime_incs[] = { 127, 151, 163, 179 };
- int cyc;
- int yday;
- u_char whichprime;
- u_short ydayinc;
- int hour;
- int minute;
- int second;
- u_long yearstart;
- u_int32 actual;
- u_int32 diff;
-
- yearstart = 0;
- for (cyc = 0; cyc < 5; cyc++) {
- settime(1900 + cyc * 65, 1, 1, 0, 0, 0);
- for (yday = -26000; yday < 26000; yday += ydayinc) {
- whichprime = abs(yday) % COUNTOF(prime_incs);
- ydayinc = prime_incs[whichprime];
- for (hour = -204; hour < 204; hour += 2) {
- for (minute = -60; minute < 60; minute++) {
- clocktime(yday, hour, minute, 30, 0,
- timestamp, &yearstart, &actual);
- diff = actual - timestamp;
- if (diff >= 0x80000000UL)
- diff = ~diff + 1;
- ASSERT_LE(diff, (183u * SECSPERDAY));
- }
- }
- }
- }
-}
+++ /dev/null
-#include "g_sockaddrtest.h"
-
-class decodenetnumTest : public sockaddrtest {
-};
-
-TEST_F(decodenetnumTest, IPv4AddressOnly) {
- const char *str = "192.0.2.1";
- sockaddr_u actual;
-
- sockaddr_u expected;
- expected.sa4.sin_family = AF_INET;
- expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
- SET_PORT(&expected, NTP_PORT);
-
- ASSERT_TRUE(decodenetnum(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(decodenetnumTest, IPv4AddressWithPort) {
- const char *str = "192.0.2.2:2000";
- sockaddr_u actual;
-
- sockaddr_u expected;
- expected.sa4.sin_family = AF_INET;
- expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
- SET_PORT(&expected, 2000);
-
- ASSERT_TRUE(decodenetnum(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(decodenetnumTest, IPv6AddressOnly) {
- const struct in6_addr address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- };
-
- const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
- sockaddr_u actual;
-
- sockaddr_u expected;
- expected.sa6.sin6_family = AF_INET6;
- expected.sa6.sin6_addr = address;
- SET_PORT(&expected, NTP_PORT);
-
- ASSERT_TRUE(decodenetnum(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(decodenetnumTest, IPv6AddressWithPort) {
- const struct in6_addr address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- };
-
- const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
- sockaddr_u actual;
-
- sockaddr_u expected;
- expected.sa6.sin6_family = AF_INET6;
- expected.sa6.sin6_addr = address;
- SET_PORT(&expected, 3000);
-
- ASSERT_TRUE(decodenetnum(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(decodenetnumTest, IllegalAddress) {
- const char *str = "192.0.2.270:2000";
- sockaddr_u actual;
-
- ASSERT_FALSE(decodenetnum(str, &actual));
-}
-
-TEST_F(decodenetnumTest, IllegalCharInPort) {
- /* An illegal port does not make the decodenetnum fail, but instead
- * makes it use the standard port.
- */
- const char *str = "192.0.2.1:a700";
- sockaddr_u actual;
-
- sockaddr_u expected;
- expected.sa4.sin_family = AF_INET;
- expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
- SET_PORT(&expected, NTP_PORT);
-
- ASSERT_TRUE(decodenetnum(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class hextointTest : public libntptest {
-};
-
-TEST_F(hextointTest, SingleDigit) {
- const char *str = "a"; // 10 decimal
- u_long actual;
-
- ASSERT_TRUE(hextoint(str, &actual));
- EXPECT_EQ(10, actual);
-}
-
-TEST_F(hextointTest, MultipleDigits) {
- const char *str = "8F3"; // 2291 decimal
- u_long actual;
-
- ASSERT_TRUE(hextoint(str, &actual));
- EXPECT_EQ(2291, actual);
-}
-
-TEST_F(hextointTest, MaxUnsigned) {
- const char *str = "ffffffff"; // 4294967295 decimal
- u_long actual;
-
- ASSERT_TRUE(hextoint(str, &actual));
- EXPECT_EQ(4294967295UL, actual);
-}
-
-TEST_F(hextointTest, Overflow) {
- const char *str = "100000000"; // Overflow by 1
- u_long actual;
-
- ASSERT_FALSE(hextoint(str, &actual));
-}
-
-TEST_F(hextointTest, IllegalChar) {
- const char *str = "5gb"; // Illegal character g
- u_long actual;
-
- ASSERT_FALSE(hextoint(str, &actual));
-}
+++ /dev/null
-#include "g_lfptest.h"
-
-class hextolfpTest : public lfptest {
-};
-
-TEST_F(hextolfpTest, PositiveInteger) {
- const char *str = "00001000.00000000";
- l_fp actual;
-
- l_fp expected = {4096, 0}; // 16^3, no fraction part.
-
- ASSERT_TRUE(hextolfp(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(hextolfpTest, NegativeInteger) {
- const char *str = "ffffffff.00000000"; // -1 decimal
- l_fp actual;
-
- l_fp expected = {-1, 0};
-
- ASSERT_TRUE(hextolfp(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(hextolfpTest, PositiveFraction) {
- const char *str = "00002000.80000000"; // 8196.5 decimal
- l_fp actual;
-
- l_fp expected = {8192, HALF};
-
- ASSERT_TRUE(hextolfp(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(hextolfpTest, NegativeFraction) {
- const char *str = "ffffffff.40000000"; // -1 + 0.25 decimal
- l_fp actual;
-
- l_fp expected = {-1, QUARTER}; //-1 + 0.25
-
- ASSERT_TRUE(hextolfp(str, &actual));
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(hextolfpTest, IllegalNumberOfInteger) {
- const char *str = "1000000.00000000"; // Missing one digit in integral part.
- l_fp actual;
-
- ASSERT_FALSE(hextolfp(str, &actual));
-}
-
-TEST_F(hextolfpTest, IllegalChar) {
- const char *str = "10000000.0000h000"; // Illegal character h.
- l_fp actual;
-
- ASSERT_FALSE(hextolfp(str, &actual));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-#include <sstream>
-#include <string>
-
-class humandateTest : public libntptest {
-};
-
-TEST_F(humandateTest, RegularTime) {
- time_t sample = 1276601278;
- std::ostringstream expected;
-
- tm* time;
- time = localtime(&sample);
- ASSERT_TRUE(time != NULL);
-
- expected << std::setfill('0')
- << std::setw(2) << time->tm_hour << ":"
- << std::setw(2) << time->tm_min << ":"
- << std::setw(2) << time->tm_sec;
-
- EXPECT_STREQ(expected.str().c_str(), humantime(sample));
-}
-
-TEST_F(humandateTest, CurrentTime) {
- time_t sample;
- std::ostringstream expected;
-
- time(&sample);
-
- tm* time;
- time = localtime(&sample);
- ASSERT_TRUE(time != NULL);
-
- expected << std::setfill('0')
- << std::setw(2) << time->tm_hour << ":"
- << std::setw(2) << time->tm_min << ":"
- << std::setw(2) << time->tm_sec;
-
- EXPECT_STREQ(expected.str().c_str(), humantime(sample));
-}
+++ /dev/null
-#include "g_libntptest.h"
-#include "g_timestructs.h"
-
-extern "C" {
-#include "ntp_fp.h"
-}
-
-#include <float.h>
-#include <math.h>
-
-#include <string>
-#include <sstream>
-
-class lfpTest : public libntptest
-{
- // nothing new right now
-};
-
-struct lfp_hl {
- uint32_t h, l;
-};
-
-//----------------------------------------------------------------------
-// OO-wrapper for 'l_fp'
-//----------------------------------------------------------------------
-
-class LFP
-{
-public:
- ~LFP();
- LFP();
- LFP(const LFP& rhs);
- LFP(int32 i, u_int32 f);
-
- LFP operator+ (const LFP &rhs) const;
- LFP& operator+=(const LFP &rhs);
-
- LFP operator- (const LFP &rhs) const;
- LFP& operator-=(const LFP &rhs);
-
- LFP& operator=(const LFP &rhs);
- LFP operator-() const;
-
- bool operator==(const LFP &rhs) const;
-
- LFP neg() const;
- LFP abs() const;
- int signum() const;
-
- bool l_isgt (const LFP &rhs) const
- { return L_ISGT(&_v, &rhs._v); }
- bool l_isgtu(const LFP &rhs) const
- { return L_ISGTU(&_v, &rhs._v); }
- bool l_ishis(const LFP &rhs) const
- { return L_ISHIS(&_v, &rhs._v); }
- bool l_isgeq(const LFP &rhs) const
- { return L_ISGEQ(&_v, &rhs._v); }
- bool l_isequ(const LFP &rhs) const
- { return L_ISEQU(&_v, &rhs._v); }
-
- int ucmp(const LFP & rhs) const;
- int scmp(const LFP & rhs) const;
-
- std::string toString() const;
- std::ostream& toStream(std::ostream &oo) const;
-
- operator double() const;
- explicit LFP(double);
-
-protected:
- LFP(const l_fp &rhs);
-
- static int cmp_work(u_int32 a[3], u_int32 b[3]);
-
- l_fp _v;
-};
-
-static std::ostream& operator<<(std::ostream &oo, const LFP& rhs)
-{
- return rhs.toStream(oo);
-}
-
-//----------------------------------------------------------------------
-// reference comparision
-// This is implementad as a full signed MP-subtract in 3 limbs, where
-// the operands are zero or sign extended before the subtraction is
-// executed.
-//----------------------------------------------------------------------
-int LFP::scmp(const LFP & rhs) const
-{
- u_int32 a[3], b[3];
- const l_fp &op1(_v), &op2(rhs._v);
-
- a[0] = op1.l_uf; a[1] = op1.l_ui; a[2] = 0;
- b[0] = op2.l_uf; b[1] = op2.l_ui; b[2] = 0;
-
- a[2] -= (op1.l_i < 0);
- b[2] -= (op2.l_i < 0);
-
- return cmp_work(a,b);
-}
-
-int LFP::ucmp(const LFP & rhs) const
-{
- u_int32 a[3], b[3];
- const l_fp &op1(_v), &op2(rhs._v);
-
- a[0] = op1.l_uf; a[1] = op1.l_ui; a[2] = 0;
- b[0] = op2.l_uf; b[1] = op2.l_ui; b[2] = 0;
-
- return cmp_work(a,b);
-}
-
-int LFP::cmp_work(u_int32 a[3], u_int32 b[3])
-{
- u_int32 cy, idx, tmp;
- for (cy = idx = 0; idx < 3; ++idx) {
- tmp = a[idx]; cy = (a[idx] -= cy ) > tmp;
- tmp = a[idx]; cy |= (a[idx] -= b[idx]) > tmp;
- }
- if (a[2])
- return -1;
- return a[0] || a[1];
-}
-
-//----------------------------------------------------------------------
-// imlementation of the LFP stuff
-// This should be easy enough...
-//----------------------------------------------------------------------
-
-LFP::~LFP()
-{
- // NOP
-}
-
-LFP::LFP()
-{
- _v.l_ui = 0;
- _v.l_uf = 0;
-}
-
-LFP::LFP(int32 i, u_int32 f)
-{
- _v.l_i = i;
- _v.l_uf = f;
-}
-
-LFP::LFP(const LFP &rhs)
-{
- _v = rhs._v;
-}
-
-LFP::LFP(const l_fp & rhs)
-{
- _v = rhs;
-}
-
-LFP& LFP::operator=(const LFP & rhs)
-{
- _v = rhs._v;
- return *this;
-}
-
-LFP& LFP::operator+=(const LFP & rhs)
-{
- L_ADD(&_v, &rhs._v);
- return *this;
-}
-
-LFP& LFP::operator-=(const LFP & rhs)
-{
- L_SUB(&_v, &rhs._v);
- return *this;
-}
-
-LFP LFP::operator+(const LFP &rhs) const
-{
- LFP tmp(*this);
- return tmp += rhs;
-}
-
-LFP LFP::operator-(const LFP &rhs) const
-{
- LFP tmp(*this);
- return tmp -= rhs;
-}
-
-LFP LFP::operator-() const
-{
- LFP tmp(*this);
- L_NEG(&tmp._v);
- return tmp;
-}
-
-LFP
-LFP::neg() const
-{
- LFP tmp(*this);
- L_NEG(&tmp._v);
- return tmp;
-}
-
-LFP
-LFP::abs() const
-{
- LFP tmp(*this);
- if (L_ISNEG(&tmp._v))
- L_NEG(&tmp._v);
- return tmp;
-}
-
-int
-LFP::signum() const
-{
- if (_v.l_ui & 0x80000000u)
- return -1;
- return (_v.l_ui || _v.l_uf);
-}
-
-std::string
-LFP::toString() const
-{
- std::ostringstream oss;
- toStream(oss);
- return oss.str();
-}
-
-std::ostream&
-LFP::toStream(std::ostream &os) const
-{
- return os
- << mfptoa(_v.l_ui, _v.l_uf, 9)
- << " [$" << std::setw(8) << std::setfill('0') << std::hex << _v.l_ui
- << ':' << std::setw(8) << std::setfill('0') << std::hex << _v.l_uf
- << ']';
-}
-
-bool LFP::operator==(const LFP &rhs) const
-{
- return L_ISEQU(&_v, &rhs._v);
-}
-
-
-LFP::operator double() const
-{
- double res;
- LFPTOD(&_v, res);
- return res;
-}
-
-LFP::LFP(double rhs)
-{
- DTOLFP(rhs, &_v);
-}
-
-
-//----------------------------------------------------------------------
-// testing the relational macros works better with proper predicate
-// formatting functions; it slows down the tests a bit, but makes for
-// readable failure messages.
-//----------------------------------------------------------------------
-
-testing::AssertionResult isgt_p(
- const LFP &op1, const LFP &op2)
-{
- if (op1.l_isgt(op2))
- return testing::AssertionSuccess()
- << "L_ISGT(" << op1 << "," << op2 << ") is true";
- else
- return testing::AssertionFailure()
- << "L_ISGT(" << op1 << "," << op2 << ") is false";
-}
-
-testing::AssertionResult isgeq_p(
- const LFP &op1, const LFP &op2)
-{
- if (op1.l_isgeq(op2))
- return testing::AssertionSuccess()
- << "L_ISGEQ(" << op1 << "," << op2 << ") is true";
- else
- return testing::AssertionFailure()
- << "L_ISGEQ(" << op1 << "," << op2 << ") is false";
-}
-
-testing::AssertionResult isgtu_p(
- const LFP &op1, const LFP &op2)
-{
- if (op1.l_isgtu(op2))
- return testing::AssertionSuccess()
- << "L_ISGTU(" << op1 << "," << op2 << ") is true";
- else
- return testing::AssertionFailure()
- << "L_ISGTU(" << op1 << "," << op2 << ") is false";
-}
-
-testing::AssertionResult ishis_p(
- const LFP &op1, const LFP &op2)
-{
- if (op1.l_ishis(op2))
- return testing::AssertionSuccess()
- << "L_ISHIS(" << op1 << "," << op2 << ") is true";
- else
- return testing::AssertionFailure()
- << "L_ISHIS(" << op1 << "," << op2 << ") is false";
-}
-
-testing::AssertionResult isequ_p(
- const LFP &op1, const LFP &op2)
-{
- if (op1.l_isequ(op2))
- return testing::AssertionSuccess()
- << "L_ISEQU(" << op1 << "," << op2 << ") is true";
- else
- return testing::AssertionFailure()
- << "L_ISEQU(" << op1 << "," << op2 << ") is false";
-}
-
-//----------------------------------------------------------------------
-// test data table for add/sub and compare
-//----------------------------------------------------------------------
-
-static const lfp_hl addsub_tab[][3] = {
- // trivial idendity:
- {{0 ,0 }, { 0,0 }, { 0,0}},
- // with carry from fraction and sign change:
- {{-1,0x80000000}, { 0,0x80000000}, { 0,0}},
- // without carry from fraction
- {{ 1,0x40000000}, { 1,0x40000000}, { 2,0x80000000}},
- // with carry from fraction:
- {{ 1,0xC0000000}, { 1,0xC0000000}, { 3,0x80000000}},
- // with carry from fraction and sign change:
- {{0x7FFFFFFF, 0x7FFFFFFF}, {0x7FFFFFFF,0x7FFFFFFF}, {0xFFFFFFFE,0xFFFFFFFE}},
- // two tests w/o carry (used for l_fp<-->double):
- {{0x55555555,0xAAAAAAAA}, {0x11111111,0x11111111}, {0x66666666,0xBBBBBBBB}},
- {{0x55555555,0x55555555}, {0x11111111,0x11111111}, {0x66666666,0x66666666}},
- // wide-range test, triggers compare trouble
- {{0x80000000,0x00000001}, {0xFFFFFFFF,0xFFFFFFFE}, {0x7FFFFFFF,0xFFFFFFFF}}
-};
-static const size_t addsub_cnt(sizeof(addsub_tab)/sizeof(addsub_tab[0]));
-static const size_t addsub_tot(sizeof(addsub_tab)/sizeof(addsub_tab[0][0]));
-
-
-//----------------------------------------------------------------------
-// epsilon estimation for the precision of a conversion double --> l_fp
-//
-// The error estimation limit is as follows:
-// * The 'l_fp' fixed point fraction has 32 bits precision, so we allow
-// for the LSB to toggle by clamping the epsilon to be at least 2^(-31)
-//
-// * The double mantissa has a precsion 54 bits, so the other minimum is
-// dval * (2^(-53))
-//
-// The maximum of those two boundaries is used for the check.
-//
-// Note: once there are more than 54 bits between the highest and lowest
-// '1'-bit of the l_fp value, the roundtrip *will* create truncation
-// errors. This is an inherent property caused by the 54-bit mantissa of
-// the 'double' type.
-double eps(double d)
-{
- return std::max<double>(ldexp(1.0, -31), ldexp(fabs(d), -53));
-}
-
-//----------------------------------------------------------------------
-// test addition
-//----------------------------------------------------------------------
-TEST_F(lfpTest, AdditionLR) {
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- LFP op2(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- LFP exp(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
- LFP res(op1 + op2);
-
- ASSERT_EQ(exp, res);
- }
-}
-
-TEST_F(lfpTest, AdditionRL) {
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP op2(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- LFP op1(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- LFP exp(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
- LFP res(op1 + op2);
-
- ASSERT_EQ(exp, res);
- }
-}
-
-//----------------------------------------------------------------------
-// test subtraction
-//----------------------------------------------------------------------
-TEST_F(lfpTest, SubtractionLR) {
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP op2(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- LFP exp(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- LFP op1(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
- LFP res(op1 - op2);
-
- ASSERT_EQ(exp, res);
- }
-}
-
-TEST_F(lfpTest, SubtractionRL) {
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP exp(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- LFP op2(addsub_tab[idx][1].h, addsub_tab[idx][1].l);
- LFP op1(addsub_tab[idx][2].h, addsub_tab[idx][2].l);
- LFP res(op1 - op2);
-
- ASSERT_EQ(exp, res);
- }
-}
-
-//----------------------------------------------------------------------
-// test negation
-//----------------------------------------------------------------------
-TEST_F(lfpTest, Negation) {
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- LFP op2(-op1);
- LFP sum(op1 + op2);
-
- ASSERT_EQ(LFP(0,0), sum);
- }
-}
-
-//----------------------------------------------------------------------
-// test absolute value
-//----------------------------------------------------------------------
-TEST_F(lfpTest, Absolute) {
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- LFP op2(op1.abs());
-
- ASSERT_TRUE(op2.signum() >= 0);
-
- if (op1.signum() >= 0)
- op1 -= op2;
- else
- op1 += op2;
- ASSERT_EQ(LFP(0,0), op1);
- }
-
- // There is one special case we have to check: the minimum
- // value cannot be negated, or, to be more precise, the
- // negation reproduces the original pattern.
- LFP minVal(0x80000000, 0x00000000);
- LFP minAbs(minVal.abs());
- ASSERT_EQ(-1, minVal.signum());
- ASSERT_EQ(minVal, minAbs);
-}
-
-//----------------------------------------------------------------------
-// fp -> double -> fp rountrip test
-//----------------------------------------------------------------------
-TEST_F(lfpTest, FDF_RoundTrip) {
- // since a l_fp has 64 bits in it's mantissa and a double has
- // only 54 bits available (including the hidden '1') we have to
- // make a few concessions on the roundtrip precision. The 'eps()'
- // function makes an educated guess about the avilable precision
- // and checks the difference in the two 'l_fp' values against
- // that limit.
- for (size_t idx=0; idx < addsub_cnt; ++idx) {
- LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l);
- double op2(op1);
- LFP op3(op2);
- // for manual checks only:
- // std::cout << std::setprecision(16) << op2 << std::endl;
- ASSERT_LE(fabs(op1-op3), eps(op2));
- }
-}
-
-//----------------------------------------------------------------------
-// test the compare stuff
-//
-// This uses the local compare and checks if the operations using the
-// macros in 'ntp_fp.h' produce mathing results.
-// ----------------------------------------------------------------------
-TEST_F(lfpTest, SignedRelOps) {
- const lfp_hl * tv(&addsub_tab[0][0]);
- for (size_t lc=addsub_tot-1; lc; --lc,++tv) {
- LFP op1(tv[0].h,tv[0].l);
- LFP op2(tv[1].h,tv[1].l);
- int cmp(op1.scmp(op2));
-
- switch (cmp) {
- case -1:
- std::swap(op1, op2);
- case 1:
- EXPECT_TRUE (isgt_p(op1,op2));
- EXPECT_FALSE(isgt_p(op2,op1));
-
- EXPECT_TRUE (isgeq_p(op1,op2));
- EXPECT_FALSE(isgeq_p(op2,op1));
-
- EXPECT_FALSE(isequ_p(op1,op2));
- EXPECT_FALSE(isequ_p(op2,op1));
- break;
- case 0:
- EXPECT_FALSE(isgt_p(op1,op2));
- EXPECT_FALSE(isgt_p(op2,op1));
-
- EXPECT_TRUE (isgeq_p(op1,op2));
- EXPECT_TRUE (isgeq_p(op2,op1));
-
- EXPECT_TRUE (isequ_p(op1,op2));
- EXPECT_TRUE (isequ_p(op2,op1));
- break;
- default:
- FAIL() << "unexpected SCMP result: " << cmp;
- }
- }
-}
-
-TEST_F(lfpTest, UnsignedRelOps) {
- const lfp_hl * tv(&addsub_tab[0][0]);
- for (size_t lc=addsub_tot-1; lc; --lc,++tv) {
- LFP op1(tv[0].h,tv[0].l);
- LFP op2(tv[1].h,tv[1].l);
- int cmp(op1.ucmp(op2));
-
- switch (cmp) {
- case -1:
- std::swap(op1, op2);
- case 1:
- EXPECT_TRUE (isgtu_p(op1,op2));
- EXPECT_FALSE(isgtu_p(op2,op1));
-
- EXPECT_TRUE (ishis_p(op1,op2));
- EXPECT_FALSE(ishis_p(op2,op1));
- break;
- case 0:
- EXPECT_FALSE(isgtu_p(op1,op2));
- EXPECT_FALSE(isgtu_p(op2,op1));
-
- EXPECT_TRUE (ishis_p(op1,op2));
- EXPECT_TRUE (ishis_p(op2,op1));
- break;
- default:
- FAIL() << "unexpected UCMP result: " << cmp;
- }
- }
-}
-
-//----------------------------------------------------------------------
-// that's all folks... but feel free to add things!
-//----------------------------------------------------------------------
+++ /dev/null
-#ifndef NTP_TESTS_LFPTEST_H
-#define NTP_TESTS_LFPTEST_H
-
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_fp.h"
-};
-
-class lfptest : public libntptest {
-protected:
- ::testing::AssertionResult IsEqual(const l_fp &expected, const l_fp &actual) {
- if (L_ISEQU(&expected, &actual)) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << " expected: " << lfptoa(&expected, FRACTION_PREC)
- << " (" << expected.l_ui << "." << expected.l_uf << ")"
- << " but was: " << lfptoa(&actual, FRACTION_PREC)
- << " (" << actual.l_ui << "." << actual.l_uf << ")";
- }
- }
-
- static const int32 HALF = -2147483647L - 1L;
- static const int32 HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
- static const int32 HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
- static const int32 QUARTER = 1073741824L;
- static const int32 QUARTER_PROMILLE_APPRX = 1073742L;
-};
-
-#endif
-/* NTP_TESTS_LFPTEST_H */
+++ /dev/null
-/*
- * This file contains test for both mfptoa and mfptoms (which uses dolfptoa),
- * since all these functions are very similar. It also tests ulfptoa, which is
- * a macro.
- */
-
-#include "g_libntptest.h"
-
-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 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_F(lfptostrTest, PositiveInteger) {
- l_fp test = {200, 0}; // exact 200.0000000000
-
- EXPECT_STREQ("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
- EXPECT_STREQ("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
-}
-
-TEST_F(lfptostrTest, NegativeInteger) {
- l_fp test = {-100, 0}; // -100
-
- EXPECT_STREQ("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
- EXPECT_STREQ("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
-}
-
-TEST_F(lfptostrTest, PositiveIntegerWithFraction) {
- l_fp test = {200, ONE_FOURTH}; // 200.25
-
- EXPECT_STREQ("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
- EXPECT_STREQ("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
-}
-
-TEST_F(lfptostrTest, NegativeIntegerWithFraction) {
- l_fp test = {-100, ONE_FOURTH}; // -99.75
-
- EXPECT_STREQ("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
- EXPECT_STREQ("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
-}
-
-TEST_F(lfptostrTest, RoundingDownToInteger) {
- l_fp test = {10, ONE_FOURTH}; // 10.25
-
- EXPECT_STREQ("10", lfptoa(&test, 0));
- EXPECT_STREQ("10250", lfptoms(&test, 0));
-}
-
-TEST_F(lfptostrTest, RoundingMiddleToInteger) {
- l_fp test = {10, HALF}; // 10.5
-
- EXPECT_STREQ("11", lfptoa(&test, 0));
- EXPECT_STREQ("10500", lfptoms(&test, 0));
-}
-
-TEST_F(lfptostrTest, RoundingUpToInteger) {
- l_fp test = {5, THREE_FOURTH}; // 5.75
-
- EXPECT_STREQ("6", lfptoa(&test, 0));
- EXPECT_STREQ("5750", lfptoms(&test, 0));
-}
-
-TEST_F(lfptostrTest, SingleDecimal) {
- l_fp test = {8, ONE_FOURTH}; // 8.25
-
- EXPECT_STREQ("8.3", lfptoa(&test, 1));
- EXPECT_STREQ("8250.0", lfptoms(&test, 1));
-}
-
-TEST_F(lfptostrTest, MillisecondsRoundingUp) {
- l_fp test = {1, HALF_PROMILLE_UP}; //slightly more than 1.0005
-
- EXPECT_STREQ("1.0", lfptoa(&test, 1));
-
- EXPECT_STREQ("1000.5", lfptoms(&test, 1));
- EXPECT_STREQ("1001", lfptoms(&test, 0));
-}
-
-TEST_F(lfptostrTest, MillisecondsRoundingDown) {
- l_fp test = {1, HALF_PROMILLE_DOWN}; // slightly less than 1.0005
-
- EXPECT_STREQ("1.0", lfptoa(&test, 1));
-
- EXPECT_STREQ("1000.5", lfptoms(&test, 1));
- EXPECT_STREQ("1000", lfptoms(&test, 0));
-}
-
-TEST_F(lfptostrTest, UnsignedInteger) {
- l_fp test = {3000000000UL, 0};
-
- EXPECT_STREQ("3000000000.0", ulfptoa(&test, 1));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-/* This file contains various constants that libntp needs to be set
- * and that is normally defined in ntpd/ntpq/...
- */
-
-u_long current_time = 4; // needed by authkeys. Used only in to calculate lifetime.
-const char *progname = "libntptest";
-
-time_t libntptest::nowtime = 0;
-
-time_t libntptest::timefunc(time_t *ptr)
-{
- if (ptr)
- *ptr = nowtime;
- return nowtime;
-}
-
-void libntptest::settime(int y, int m, int d, int H, int M, int S)
-{
-
- time_t days(ntpcal_edate_to_eradays(y-1, m-1, d-1) + 1 - DAY_UNIX_STARTS);
- time_t secs(ntpcal_etime_to_seconds(H, M, S));
-
- nowtime = days * SECSPERDAY + secs;
-}
-
+++ /dev/null
-#include "tests_main.h"
-
-extern "C" {
-#include "ntp_stdlib.h"
-#include "ntp_calendar.h"
-};
-
-class libntptest : public ntptest {
-
-protected:
- static time_t timefunc(time_t*);
- static time_t nowtime;
- static void settime(int y, int m, int d, int H, int M, int S);
-
-};
+++ /dev/null
-#include "g_libntptest.h"
-
-class modetoaTest : public libntptest {
-};
-
-TEST_F(modetoaTest, KnownMode) {
- const int MODE = 3; // Should be "client"
-
- EXPECT_STREQ("client", modetoa(MODE));
-}
-
-TEST_F(modetoaTest, UnknownMode) {
- const int MODE = 100;
-
- EXPECT_STREQ("mode#100", modetoa(MODE));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#ifndef VSNPRINTF_PERCENT_M
-// format_errmsg() is normally private to msyslog.c
-void format_errmsg (char *, size_t, const char *, int);
-#endif
-};
-
-class msyslogTest : public libntptest {
-};
-
-// msnprintf()
-TEST_F(msyslogTest, msnprintf)
-{
-#define FMT_PREFIX "msyslog.cpp ENOENT: "
- char exp_buf[512];
- char act_buf[512];
- int exp_cnt;
- int act_cnt;
-
- exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
- strerror(ENOENT));
- errno = ENOENT;
- act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
- EXPECT_EQ(exp_cnt, act_cnt);
- EXPECT_STREQ(exp_buf, act_buf);
-}
-
-TEST_F(msyslogTest, msnprintfLiteralPercentm)
-{
- char exp_buf[32];
- char act_buf[32];
- int exp_cnt;
- int act_cnt;
-
- exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
- errno = ENOENT;
- act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
- EXPECT_EQ(exp_cnt, act_cnt);
- EXPECT_STREQ(exp_buf, act_buf);
-}
-
-TEST_F(msyslogTest, msnprintfBackslashLiteralPercentm)
-{
- char exp_buf[32];
- char act_buf[32];
- int exp_cnt;
- int act_cnt;
-
- exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
- errno = ENOENT;
- act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
- EXPECT_EQ(exp_cnt, act_cnt);
- EXPECT_STREQ(exp_buf, act_buf);
-}
-
-TEST_F(msyslogTest, msnprintfBackslashPercent)
-{
- char exp_buf[32];
- char act_buf[32];
- int exp_cnt;
- int act_cnt;
-
- exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
- strerror(ENOENT));
- errno = ENOENT;
- act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
- EXPECT_EQ(exp_cnt, act_cnt);
- EXPECT_STREQ(exp_buf, act_buf);
-}
-
-TEST_F(msyslogTest, msnprintfHangingPercent)
-{
- static char fmt[] = "percent then nul term then non-nul %\0oops!";
- char exp_buf[64];
- char act_buf[64];
- int exp_cnt;
- int act_cnt;
-
- ZERO(exp_buf);
- ZERO(act_buf);
- exp_cnt = snprintf(exp_buf, sizeof(exp_buf), fmt);
- act_cnt = msnprintf(act_buf, sizeof(act_buf), fmt);
- EXPECT_EQ(exp_cnt, act_cnt);
- EXPECT_STREQ(exp_buf, act_buf);
- EXPECT_STREQ("", act_buf + 1 + strlen(act_buf));
-}
-
-#ifndef VSNPRINTF_PERCENT_M
-TEST_F(msyslogTest, format_errmsgHangingPercent)
-{
- static char fmt[] = "percent then nul term then non-nul %\0oops!";
- char act_buf[64];
-
- ZERO(act_buf);
- format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
- EXPECT_STREQ(fmt, act_buf);
- EXPECT_STREQ("", act_buf + 1 + strlen(act_buf));
-}
-#endif
-
-TEST_F(msyslogTest, msnprintfNullTarget)
-{
- int exp_cnt;
- int act_cnt;
-
- exp_cnt = snprintf(NULL, 0, "%d", 123);
- errno = ENOENT;
- act_cnt = msnprintf(NULL, 0, "%d", 123);
- EXPECT_EQ(exp_cnt, act_cnt);
-}
-
-TEST_F(msyslogTest, msnprintfTruncate)
-{
- char undist[] = "undisturbed";
- char exp_buf[512];
- char act_buf[512];
- int exp_cnt;
- int act_cnt;
-
- memcpy(exp_buf + 3, undist, sizeof(undist));
- memcpy(act_buf + 3, undist, sizeof(undist));
- exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
- errno = ENOENT;
- act_cnt = msnprintf(act_buf, 3, "%m");
- EXPECT_EQ('\0', exp_buf[2]);
- EXPECT_EQ('\0', act_buf[2]);
- EXPECT_TRUE(act_cnt > 0);
- EXPECT_EQ(exp_cnt, act_cnt);
- EXPECT_STREQ(exp_buf, act_buf);
- EXPECT_STREQ(exp_buf + 3, undist);
- EXPECT_STREQ(act_buf + 3, undist);
-}
+++ /dev/null
-#include "g_sockaddrtest.h"
-
-class netofTest : public sockaddrtest {
-};
-
-TEST_F(netofTest, ClassBAddress) {
- sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT);
- sockaddr_u expected = CreateSockaddr4("172.16.0.0", NTP_PORT);
-
- sockaddr_u* actual = netof(&input);
-
- ASSERT_TRUE(actual != NULL);
- EXPECT_TRUE(IsEqual(expected, *actual));
-}
-
-TEST_F(netofTest, ClassCAddress) {
- sockaddr_u input = CreateSockaddr4("192.0.2.255", NTP_PORT);
- sockaddr_u expected = CreateSockaddr4("192.0.2.0", NTP_PORT);
-
- sockaddr_u* actual = netof(&input);
-
- ASSERT_TRUE(actual != NULL);
- EXPECT_TRUE(IsEqual(expected, *actual));
-}
-
-TEST_F(netofTest, ClassAAddress) {
- /* Class A addresses are assumed to be classless,
- * thus the same address should be returned.
- */
- sockaddr_u input = CreateSockaddr4("10.20.30.40", NTP_PORT);
- sockaddr_u expected = CreateSockaddr4("10.20.30.40", NTP_PORT);
-
- sockaddr_u* actual = netof(&input);
-
- ASSERT_TRUE(actual != NULL);
- EXPECT_TRUE(IsEqual(expected, *actual));
-}
-
-TEST_F(netofTest, IPv6Address) {
- /* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */
- const struct in6_addr input_address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- }; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
-
- const struct in6_addr expected_address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00
- }; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
-
- sockaddr_u input;
- input.sa6.sin6_family = AF_INET6;
- input.sa6.sin6_addr = input_address;
- SET_PORT(&input, 3000);
-
- sockaddr_u expected;
- expected.sa6.sin6_family = AF_INET6;
- expected.sa6.sin6_addr = expected_address;
- SET_PORT(&expected, 3000);
-
- sockaddr_u* actual = netof(&input);
-
- ASSERT_TRUE(actual != NULL);
- EXPECT_TRUE(IsEqual(expected, *actual));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class numtoaTest : public libntptest {
-};
-
-TEST_F(numtoaTest, Address) {
- u_int32 input = htonl(3221225472UL+512UL+1UL); // 192.0.2.1
-
- EXPECT_STREQ("192.0.2.1", numtoa(input));
-}
-
-TEST_F(numtoaTest, Netmask) {
- // 255.255.255.0
- u_int32 hostOrder = 255UL*256UL*256UL*256UL + 255UL*256UL*256UL + 255UL*256UL;
- u_int32 input = htonl(hostOrder);
-
- EXPECT_STREQ("255.255.255.0", numtoa(input));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class numtohostTest : public libntptest {
-};
-
-TEST_F(numtohostTest, LoopbackNetNonResolve) {
- /* A loopback address in 127.0.0.0/8 is chosen, and
- * numtohost() should not try to resolve it unless
- * it is 127.0.0.1
- */
-
- u_int32 input = 127*256*256*256 + 1*256 + 1; // 127.0.1.1
-
- EXPECT_STREQ("127.0.1.1", numtohost(htonl(input)));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-class octtointTest : public libntptest {
-};
-
-TEST_F(octtointTest, SingleDigit) {
- const char* str = "5";
- u_long actual;
-
- ASSERT_TRUE(octtoint(str, &actual));
- EXPECT_EQ(5, actual);
-}
-
-TEST_F(octtointTest, MultipleDigits) {
- const char* str = "271";
- u_long actual;
-
- ASSERT_TRUE(octtoint(str, &actual));
- EXPECT_EQ(185, actual);
-}
-
-TEST_F(octtointTest, Zero) {
- const char* str = "0";
- u_long actual;
-
- ASSERT_TRUE(octtoint(str, &actual));
- EXPECT_EQ(0, actual);
-}
-
-TEST_F(octtointTest, MaximumUnsigned32bit) {
- const char* str = "37777777777";
- u_long actual;
-
- ASSERT_TRUE(octtoint(str, &actual));
- EXPECT_EQ(4294967295UL, actual);
-}
-
-TEST_F(octtointTest, Overflow) {
- const char* str = "40000000000";
- u_long actual;
-
- ASSERT_FALSE(octtoint(str, &actual));
-}
-
-TEST_F(octtointTest, IllegalCharacter) {
- const char* str = "5ac2";
- u_long actual;
-
- ASSERT_FALSE(octtoint(str, &actual));
-}
-
-TEST_F(octtointTest, IllegalDigit) {
- const char* str = "5283";
- u_long actual;
-
- ASSERT_FALSE(octtoint(str, &actual));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_fp.h"
-};
-
-class prettydateTest : public libntptest {
-protected:
- static const u_int32 HALF = 2147483648UL;
-};
-
-TEST_F(prettydateTest, ConstantDate) {
- l_fp time = {3485080800UL, HALF}; // 2010-06-09 14:00:00.5
-
- ASSERT_STREQ("cfba1ce0.80000000 Wed, Jun 9 2010 14:00:00.500", gmprettydate(&time));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "recvbuff.h"
-};
-
-class recvbuffTest : public libntptest {
-protected:
- virtual void SetUp() {
- init_recvbuff(RECV_INIT);
- }
-};
-
-TEST_F(recvbuffTest, Initialization) {
- EXPECT_EQ(RECV_INIT, free_recvbuffs());
- EXPECT_EQ(0, full_recvbuffs());
- EXPECT_FALSE(has_full_recv_buffer());
- EXPECT_TRUE(get_full_recv_buffer() == NULL);
-}
-
-TEST_F(recvbuffTest, GetAndFree) {
- int initial = free_recvbuffs();
- recvbuf_t* buf = get_free_recv_buffer();
-
- EXPECT_EQ(initial-1, free_recvbuffs());
- freerecvbuf(buf);
- EXPECT_EQ(initial, free_recvbuffs());
-}
-
-TEST_F(recvbuffTest, GetAndFill) {
- int initial = free_recvbuffs();
- recvbuf_t* buf = get_free_recv_buffer();
-
- add_full_recv_buffer(buf);
- EXPECT_EQ(1, full_recvbuffs());
- EXPECT_TRUE(has_full_recv_buffer());
- EXPECT_EQ(buf, get_full_recv_buffer());
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-#include "ntp_net.h"
-#include "ntp_refclock.h"
-
-#include <sstream>
-
-class refnumtoaTest : public libntptest {
-protected:
- /* Might need to be updated if a new refclock gets this id. */
- static const int UNUSED_REFCLOCK_ID = 250;
-};
-
-#ifdef REFCLOCK /* clockname() is useless otherwise */
-TEST_F(refnumtoaTest, LocalClock) {
- /* We test with a refclock address of type LOCALCLOCK.
- * with id 8
- */
- u_int32 addr = REFCLOCK_ADDR;
- addr |= REFCLK_LOCALCLOCK << 8;
- addr |= 0x8;
-
- sockaddr_u address;
- address.sa4.sin_family = AF_INET;
- address.sa4.sin_addr.s_addr = htonl(addr);
-
- std::ostringstream expected;
- expected << clockname(REFCLK_LOCALCLOCK)
- << "(8)";
-
- EXPECT_STREQ(expected.str().c_str(), refnumtoa(&address));
-}
-#endif /* REFCLOCK */
-
-#ifdef REFCLOCK /* refnumtoa() is useless otherwise */
-TEST_F(refnumtoaTest, UnknownId) {
- /* We test with a currently unused refclock ID */
- u_int32 addr = REFCLOCK_ADDR;
- addr |= UNUSED_REFCLOCK_ID << 8;
- addr |= 0x4;
-
- sockaddr_u address;
- address.sa4.sin_family = AF_INET;
- address.sa4.sin_addr.s_addr = htonl(addr);
-
- std::ostringstream expected;
- expected << "REFCLK(" << UNUSED_REFCLOCK_ID
- << ",4)";
-
- EXPECT_STREQ(expected.str().c_str(), refnumtoa(&address));
-}
-#endif /* REFCLOCK */
+++ /dev/null
-/*
- * This file contains test for both fptoa and fptoms (which uses dofptoa),
- * since all these functions are very similar.
- */
-
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_fp.h"
-};
-
-class sfptostr : public libntptest {
-protected:
- static const int SFP_MAX_PRECISION = 6;
-};
-
-TEST_F(sfptostr, PositiveInteger) {
- s_fp test = 300 << 16; // exact 300.000000
-
- EXPECT_STREQ("300.000000", fptoa(test, SFP_MAX_PRECISION));
- EXPECT_STREQ("300000.000", fptoms(test, SFP_MAX_PRECISION));
-}
-
-TEST_F(sfptostr, NegativeInteger) {
- s_fp test = -200 << 16; // exact -200.000000
-
- EXPECT_STREQ("-200.000000", fptoa(test, SFP_MAX_PRECISION));
- EXPECT_STREQ("-200000.000", fptoms(test, SFP_MAX_PRECISION));
-}
-
-TEST_F(sfptostr, PositiveIntegerPositiveFraction) {
- s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
-
- EXPECT_STREQ("300.500000", fptoa(test, SFP_MAX_PRECISION));
- EXPECT_STREQ("300500.000", fptoms(test, SFP_MAX_PRECISION));
-}
-
-TEST_F(sfptostr, NegativeIntegerNegativeFraction) {
- s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
-
- EXPECT_STREQ("-200.500000", fptoa(test, SFP_MAX_PRECISION));
- EXPECT_STREQ("-200500.000", fptoms(test, SFP_MAX_PRECISION));
-}
-
-TEST_F(sfptostr, PositiveIntegerNegativeFraction) {
- s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
-
- EXPECT_STREQ("299.750000", fptoa(test, SFP_MAX_PRECISION));
- EXPECT_STREQ("299750.000", fptoms(test, SFP_MAX_PRECISION));
-}
-
-TEST_F(sfptostr, NegativeIntegerPositiveFraction) {
- s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
-
- EXPECT_STREQ("-199.250000", fptoa(test, SFP_MAX_PRECISION));
- EXPECT_STREQ("-199250.000", fptoms(test, SFP_MAX_PRECISION));
-}
-
-TEST_F(sfptostr, SingleDecimalInteger) {
- s_fp test = 300 << 16; // 300
-
- EXPECT_STREQ("300.0", fptoa(test, 1));
- EXPECT_STREQ("300000.0", fptoms(test, 1));
-}
-
-TEST_F(sfptostr, SingleDecimalRounding) {
- s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
-
- EXPECT_STREQ("2.8", fptoa(test, 1));
- EXPECT_STREQ("2750.0", fptoms(test, 1));
-}
+++ /dev/null
-#ifndef TESTS_SOCKADDRTEST_H
-#define TESTS_SOCKADDRTEST_H
-
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp.h"
-};
-
-class sockaddrtest : public libntptest {
-protected:
- ::testing::AssertionResult IsEqual(const sockaddr_u &expected, const sockaddr_u &actual) {
- if (expected.sa.sa_family != actual.sa.sa_family) {
- return ::testing::AssertionFailure()
- << "Expected sa_family: " << expected.sa.sa_family
- << " but got: " << actual.sa.sa_family;
- }
-
- if (actual.sa.sa_family == AF_INET) { // IPv4
- if (expected.sa4.sin_port == actual.sa4.sin_port &&
- memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr,
- sizeof(in_addr)) == 0) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "IPv4 comparision failed, expected: "
- << expected.sa4.sin_addr.s_addr
- << "(" << socktoa(&expected) << ")"
- << " but was: "
- << actual.sa4.sin_addr.s_addr
- << "(" << socktoa(&actual) << ")";
- }
- } else if (actual.sa.sa_family == AF_INET6) { //IPv6
- if (expected.sa6.sin6_port == actual.sa6.sin6_port &&
- memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr,
- sizeof(in6_addr)) == 0) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "IPv6 comparision failed";
- }
- } else { // Unknown family
- return ::testing::AssertionFailure()
- << "Unknown sa_family: " << actual.sa.sa_family;
- }
- }
-
- sockaddr_u CreateSockaddr4(const char* address, unsigned int port) {
- sockaddr_u s;
- s.sa4.sin_family = AF_INET;
- s.sa4.sin_addr.s_addr = inet_addr(address);
- SET_PORT(&s, port);
-
- return s;
- }
-};
-
-#endif // TESTS_SOCKADDRTEST_H
-
+++ /dev/null
-#include "g_sockaddrtest.h"
-
-class socktoaTest : public sockaddrtest {
-};
-
-TEST_F(socktoaTest, IPv4AddressWithPort) {
- sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
-
- EXPECT_STREQ("192.0.2.10", socktoa(&input));
- EXPECT_STREQ("192.0.2.10:123", sockporttoa(&input));
-}
-
-TEST_F(socktoaTest, IPv6AddressWithPort) {
- const struct in6_addr address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- };
-
- const char* expected =
- "2001:db8:85a3:8d3:1319:8a2e:370:7334";
- const char* expected_port =
- "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
-
- sockaddr_u input;
- memset(&input, 0, sizeof(input));
- AF(&input) = AF_INET6;
- SET_ADDR6N(&input, address);
- SET_PORT(&input, 123);
-
- EXPECT_STREQ(expected, socktoa(&input));
- EXPECT_STREQ(expected_port, sockporttoa(&input));
-}
-
-#ifdef ISC_PLATFORM_HAVESCOPEID
-TEST_F(socktoaTest, ScopedIPv6AddressWithPort) {
- const struct in6_addr address = {
- 0xfe, 0x80, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x12, 0x3f, 0xff,
- 0xfe, 0x29, 0xff, 0xfa
- };
-
- const char* expected =
- "fe80::212:3fff:fe29:fffa%5";
- const char* expected_port =
- "[fe80::212:3fff:fe29:fffa%5]:123";
-
- sockaddr_u input;
- memset(&input, 0, sizeof(input));
- AF(&input) = AF_INET6;
- SET_ADDR6N(&input, address);
- SET_PORT(&input, 123);
- SCOPE_VAR(&input) = 5;
-
- EXPECT_STREQ(expected, socktoa(&input));
- EXPECT_STREQ(expected_port, sockporttoa(&input));
-}
-#endif /* ISC_PLATFORM_HAVESCOPEID */
-
-TEST_F(socktoaTest, HashEqual) {
- sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
- sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
-
- ASSERT_TRUE(IsEqual(input1, input2));
- EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
-}
-
-TEST_F(socktoaTest, HashNotEqual) {
- /* These two addresses should not generate the same hash. */
- sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
- sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
-
- ASSERT_FALSE(IsEqual(input1, input2));
- EXPECT_NE(sock_hash(&input1), sock_hash(&input2));
-}
-
-TEST_F(socktoaTest, IgnoreIPv6Fields) {
- const struct in6_addr address = {
- 0x20, 0x01, 0x0d, 0xb8,
- 0x85, 0xa3, 0x08, 0xd3,
- 0x13, 0x19, 0x8a, 0x2e,
- 0x03, 0x70, 0x73, 0x34
- };
-
- sockaddr_u input1, input2;
-
- input1.sa6.sin6_family = AF_INET6;
- input1.sa6.sin6_addr = address;
- input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
- SET_PORT(&input1, NTP_PORT);
-
- input2.sa6.sin6_family = AF_INET6;
- input2.sa6.sin6_addr = address;
- input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
- SET_PORT(&input2, NTP_PORT);
-
- EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#ifdef OPENSSL
-# include "openssl/err.h"
-# include "openssl/rand.h"
-# include "openssl/evp.h"
-#endif
-#include "ntp.h"
-};
-
-class ssl_initTest : public libntptest {
-protected:
- static const size_t TEST_MD5_DIGEST_LENGTH = 16;
- static const size_t TEST_SHA1_DIGEST_LENGTH = 20;
-};
-
-// keytype_from_text()
-TEST_F(ssl_initTest, MD5KeyTypeWithoutDigestLength) {
- ASSERT_EQ(KEY_TYPE_MD5, keytype_from_text("MD5", NULL));
-}
-
-TEST_F(ssl_initTest, MD5KeyTypeWithDigestLength) {
- size_t digestLength;
- size_t expected = TEST_MD5_DIGEST_LENGTH;
-
- EXPECT_EQ(KEY_TYPE_MD5, keytype_from_text("MD5", &digestLength));
- EXPECT_EQ(expected, digestLength);
-}
-
-#ifdef OPENSSL
-TEST_F(ssl_initTest, SHA1KeyTypeWithDigestLength) {
- size_t digestLength;
- size_t expected = TEST_SHA1_DIGEST_LENGTH;
-
- EXPECT_EQ(NID_sha, keytype_from_text("SHA", &digestLength));
- EXPECT_EQ(expected, digestLength);
-}
-#endif /* OPENSSL */
-
-// keytype_name()
-TEST_F(ssl_initTest, MD5KeyName) {
- EXPECT_STREQ("MD5", keytype_name(KEY_TYPE_MD5));
-}
-
-#ifdef OPENSSL
-TEST_F(ssl_initTest, SHA1KeyName) {
- EXPECT_STREQ("SHA", keytype_name(NID_sha));
-}
-#endif /* OPENSSL */
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp.h" // Needed for MAX_MAC_LEN used in ntp_control.h
-#include "ntp_control.h"
-};
-
-class statestrTest : public libntptest {
-};
-
-// eventstr()
-TEST_F(statestrTest, PeerRestart) {
- EXPECT_STREQ("restart", eventstr(PEVNT_RESTART));
-}
-
-TEST_F(statestrTest, SysUnspecified) {
- EXPECT_STREQ("unspecified", eventstr(EVNT_UNSPEC));
-}
-
-// ceventstr()
-TEST_F(statestrTest, ClockCodeExists) {
- EXPECT_STREQ("clk_unspec", ceventstr(CTL_CLK_OKAY));
-}
-
-TEST_F(statestrTest, ClockCodeUnknown) {
- EXPECT_STREQ("clk_-1", ceventstr(-1));
-}
-
+++ /dev/null
-#include "g_lfptest.h"
-
-/* This class tests both atolfp and mstolfp */
-
-class strtolfpTest : public lfptest {
-};
-
-TEST_F(strtolfpTest, PositiveInteger) {
- const char *str = "500";
- const char *str_ms = "500000";
-
- l_fp expected = {500,0};
- l_fp actual, actual_ms;
-
- ASSERT_TRUE(atolfp(str, &actual));
- ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
-
- EXPECT_TRUE(IsEqual(expected, actual));
- EXPECT_TRUE(IsEqual(expected, actual_ms));
-}
-
-TEST_F(strtolfpTest, NegativeInteger) {
- const char *str = "-300";
- const char *str_ms = "-300000";
-
- l_fp expected;
- expected.l_i = -300;
- expected.l_uf = 0;
-
- l_fp actual, actual_ms;
-
- ASSERT_TRUE(atolfp(str, &actual));
- ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
-
- EXPECT_TRUE(IsEqual(expected, actual));
- EXPECT_TRUE(IsEqual(expected, actual_ms));
-}
-
-TEST_F(strtolfpTest, PositiveFraction) {
- const char *str = "+500.5";
- const char *str_ms = "500500.0";
-
- l_fp expected = {500, HALF};
- l_fp actual, actual_ms;
-
- ASSERT_TRUE(atolfp(str, &actual));
- ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
-
- EXPECT_TRUE(IsEqual(expected, actual));
- EXPECT_TRUE(IsEqual(expected, actual_ms));
-}
-
-TEST_F(strtolfpTest, NegativeFraction) {
- const char *str = "-300.75";
- const char *str_ms = "-300750";
-
- l_fp expected;
- expected.l_i = -301;
- expected.l_uf = QUARTER;
-
- l_fp actual, actual_ms;
-
- ASSERT_TRUE(atolfp(str, &actual));
- ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
-
- EXPECT_TRUE(IsEqual(expected, actual));
- EXPECT_TRUE(IsEqual(expected, actual_ms));
-}
-
-TEST_F(strtolfpTest, PositiveMsFraction) {
- const char *str = "300.00025";
- const char *str_ms = "300000.25";
-
- l_fp expected = {300, QUARTER_PROMILLE_APPRX};
- l_fp actual, actual_ms;
-
- ASSERT_TRUE(atolfp(str, &actual));
- ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
-
- EXPECT_TRUE(IsEqual(expected, actual));
- EXPECT_TRUE(IsEqual(expected, actual_ms));
-}
-
-TEST_F(strtolfpTest, NegativeMsFraction) {
- const char *str = "-199.99975";
- const char *str_ms = "-199999.75";
-
- l_fp expected;
- expected.l_i = -200;
- expected.l_uf = QUARTER_PROMILLE_APPRX;
-
- l_fp actual, actual_ms;
-
- ASSERT_TRUE(atolfp(str, &actual));
- ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
-
- EXPECT_TRUE(IsEqual(expected, actual));
- EXPECT_TRUE(IsEqual(expected, actual_ms));
-}
-
-TEST_F(strtolfpTest, InvalidChars) {
- const char *str = "500.4a2";
- l_fp actual, actual_ms;
-
- ASSERT_FALSE(atolfp(str, &actual));
- ASSERT_FALSE(mstolfp(str, &actual_ms));
-}
+++ /dev/null
-#include "g_libntptest.h"
-#include "g_timestructs.h"
-
-extern "C" {
-#include <math.h>
-#include "timespecops.h"
-}
-
-#include <string>
-#include <sstream>
-
-using namespace timeStruct;
-
-class timespecTest : public libntptest {
-protected:
- static u_int32 my_tick_to_tsf(u_int32 ticks);
- static u_int32 my_tsf_to_tick(u_int32 tsf);
-
- // that's it...
- struct lfpfracdata {
- long nsec;
- u_int32 frac;
- };
- static const lfpfracdata fdata[];
-};
-
-u_int32
-timespecTest::my_tick_to_tsf(
- u_int32 ticks
- )
-{
- // convert nanoseconds to l_fp fractional units, using double
- // precision float calculations or, if available, 64bit integer
- // arithmetic. This should give the precise fraction, rounded to
- // the nearest representation.
-#ifdef HAVE_U_INT64
- return u_int32(((u_int64(ticks) << 32) + 500000000) / 1000000000);
-#else
- return u_int32(double(ticks) * 4.294967296 + 0.5);
-#endif
- // And before you ask: if ticks >= 1000000000, the result is
- // truncated nonsense, so don't use it out-of-bounds.
-}
-
-u_int32
-timespecTest::my_tsf_to_tick(
- u_int32 tsf
- )
-{
- // Inverse operation: converts fraction to microseconds.
-#ifdef HAVE_U_INT64
- return u_int32((u_int64(tsf) * 1000000000 + 0x80000000) >> 32);
-#else
- return u_int32(double(tsf) / 4.294967296 + 0.5);
-#endif
- // Beware: The result might be 10^9 due to rounding!
-}
-
-const timespecTest::lfpfracdata timespecTest::fdata [] = {
- { 0, 0x00000000 }, { 2218896, 0x00916ae6 },
- { 16408100, 0x0433523d }, { 125000000, 0x20000000 },
- { 250000000, 0x40000000 }, { 287455871, 0x4996b53d },
- { 375000000, 0x60000000 }, { 500000000, 0x80000000 },
- { 518978897, 0x84dbcd0e }, { 563730222, 0x90509fb3 },
- { 563788007, 0x9054692c }, { 583289882, 0x95527c57 },
- { 607074509, 0x9b693c2a }, { 625000000, 0xa0000000 },
- { 645184059, 0xa52ac851 }, { 676497788, 0xad2ef583 },
- { 678910895, 0xadcd1abb }, { 679569625, 0xadf84663 },
- { 690926741, 0xb0e0932d }, { 705656483, 0xb4a5e73d },
- { 723553854, 0xb93ad34c }, { 750000000, 0xc0000000 },
- { 763550253, 0xc3780785 }, { 775284917, 0xc6791284 },
- { 826190764, 0xd3813ce8 }, { 875000000, 0xe0000000 },
- { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c }
-};
-
-
-// ---------------------------------------------------------------------
-// test support stuff -- part 1
-// ---------------------------------------------------------------------
-
-TEST_F(timespecTest, Helpers1) {
- timespec_wrap x;
-
- for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) {
- x.V.tv_nsec = -1;
- ASSERT_FALSE(x.valid());
- x.V.tv_nsec = 0;
- ASSERT_TRUE(x.valid());
- x.V.tv_nsec = 999999999;
- ASSERT_TRUE(x.valid());
- x.V.tv_nsec = 1000000000;
- ASSERT_FALSE(x.valid());
- }
-}
-
-//----------------------------------------------------------------------
-// test normalisation
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, Normalise) {
- for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) {
- timespec_wrap x(0, ns);
-
- x = normalize_tspec(x);
- ASSERT_TRUE(x.valid());
- }
-}
-
-//----------------------------------------------------------------------
-// test classification
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, SignNoFrac) {
- // sign test, no fraction
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 0);
- int E = (i > 0) - (i < 0);
- int r = test_tspec(a);
-
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timespecTest, SignWithFrac) {
- // sign test, with fraction
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 10);
- int E = (i >= 0) - (i < 0);
- int r = test_tspec(a);
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// test compare
-//----------------------------------------------------------------------
-TEST_F(timespecTest, CmpFracEQ) {
- // fractions are equal
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a( i , 200);
- timespec_wrap b( j , 200);
- int E = (i > j) - (i < j);
- int r = cmp_tspec_denorm(a, b);
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timespecTest, CmpFracGT) {
- // fraction a bigger fraction b
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a(i, 999999800);
- timespec_wrap b(j, 200);
- int E = (i >= j) - (i < j);
- int r = cmp_tspec_denorm(a, b);
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timespecTest, CmpFracLT) {
- // fraction a less fraction b
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a(i, 200);
- timespec_wrap b(j, 999999800);
- int E = (i > j) - (i <= j);
- int r = cmp_tspec_denorm(a, b);
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// Test addition (sum)
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, AddFullNorm) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a(i, 200);
- timespec_wrap b(j, 400);
- timespec_wrap E(i + j, 200 + 400);
- timespec_wrap c;
-
- c = add_tspec(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timespecTest, AddFullOflow1) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a(i, 200);
- timespec_wrap b(j, 999999900);
- timespec_wrap E(i + j + 1, 100);
- timespec_wrap c;
-
- c = add_tspec(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timespecTest, AddNsecNorm) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 200);
- timespec_wrap E(i, 600);
- timespec_wrap c;
-
- c = add_tspec_ns(a, 600 - 200);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timespecTest, AddNsecOflow1) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 200);
- timespec_wrap E(i + 1, 100);
- timespec_wrap c;
-
- c = add_tspec_ns(a, NANOSECONDS - 100);
- ASSERT_EQ(E, c);
- }
-}
-
-//----------------------------------------------------------------------
-// test subtraction (difference)
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, SubFullNorm) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a( i , 600);
- timespec_wrap b( j , 400);
- timespec_wrap E(i-j, 200);
- timespec_wrap c;
-
- c = sub_tspec(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timespecTest, SubFullOflow) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timespec_wrap a( i , 100);
- timespec_wrap b( j , 999999900);
- timespec_wrap E(i-j-1, 200);
- timespec_wrap c;
-
- c = sub_tspec(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timespecTest, SubNsecNorm) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 600);
- timespec_wrap E(i, 200);
- timespec_wrap c;
-
- c = sub_tspec_ns(a, 600 - 200);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timespecTest, SubNsecOflow) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a( i , 100);
- timespec_wrap E(i-1, 200);
- timespec_wrap c;
-
- c = sub_tspec_ns(a, NANOSECONDS - 100);
- ASSERT_EQ(E, c);
- }
-}
-
-//----------------------------------------------------------------------
-// test negation
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, Neg) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 100);
- timespec_wrap b;
- timespec_wrap c;
-
- b = neg_tspec(a);
- c = add_tspec(a, b);
- ASSERT_EQ(0, test_tspec(c));
- }
-}
-
-//----------------------------------------------------------------------
-// test abs value
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, AbsNoFrac) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i , 0);
- timespec_wrap b;
-
- b = abs_tspec(a);
- ASSERT_EQ((i != 0), test_tspec(b));
- }
-}
-
-TEST_F(timespecTest, AbsWithFrac) {
- for (int i = -4; i <= 4; ++i) {
- timespec_wrap a(i, 100);
- timespec_wrap b;
-
- b = abs_tspec(a);
- ASSERT_EQ(1, test_tspec(b));
- }
-}
-
-// ---------------------------------------------------------------------
-// test support stuff -- part 2
-// ---------------------------------------------------------------------
-
-TEST_F(timespecTest, Helpers2) {
- AssertTimespecClose isClose(0, 2);
- timespec_wrap x, y;
-
- for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++)
- for (x.V.tv_nsec = 1;
- x.V.tv_nsec < 1000000000;
- x.V.tv_nsec += 499999999) {
- for (long i = -4; i < 5; i++) {
- y = x;
- y.V.tv_nsec += i;
- if (i >= -2 && i <= 2)
- ASSERT_PRED_FORMAT2(isClose, x, y);
- else
- ASSERT_PRED_FORMAT2(!isClose, x, y);
- }
- }
-}
-
-// global predicate instances we're using here
-static AssertFpClose FpClose(0, 1);
-static AssertTimespecClose TimespecClose(0, 2);
-
-//----------------------------------------------------------------------
-// conversion to l_fp
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, ToLFPbittest) {
- for (u_int32 i = 0; i < 1000000000; i+=1000) {
- timespec_wrap a(1, i);
- l_fp_wrap E(1, my_tick_to_tsf(i));
- l_fp_wrap r;
-
- r = tspec_intv_to_lfp(a);
- ASSERT_PRED_FORMAT2(FpClose, E, r);
- }
-}
-
-TEST_F(timespecTest, ToLFPrelPos) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- timespec_wrap a(1, fdata[i].nsec);
- l_fp_wrap E(1, fdata[i].frac);
- l_fp_wrap r;
-
- r = tspec_intv_to_lfp(a);
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timespecTest, ToLFPrelNeg) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- timespec_wrap a(-1, fdata[i].nsec);
- l_fp_wrap E(~0, fdata[i].frac);
- l_fp_wrap r;
-
- r = tspec_intv_to_lfp(a);
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timespecTest, ToLFPabs) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- timespec_wrap a(1, fdata[i].nsec);
- l_fp_wrap E(1 + JAN_1970, fdata[i].frac);
- l_fp_wrap r;
-
- r = tspec_stamp_to_lfp(a);
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// conversion from l_fp
-//----------------------------------------------------------------------
-TEST_F(timespecTest, FromLFPbittest) {
- // 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.
- for (u_int32 tsf = 0; tsf < ~u_int32(1000); tsf += 1000) {
- timespec_wrap E(1, my_tsf_to_tick(tsf));
- l_fp_wrap a(1, tsf);
- timespec_wrap r;
-
- r = lfp_intv_to_tspec(a);
- // The conversion might be off by one nanosecond when
- // comparing to calculated value.
- ASSERT_PRED_FORMAT2(TimespecClose, E, r);
- }
-}
-
-TEST_F(timespecTest, FromLFPrelPos) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- l_fp_wrap a(1, fdata[i].frac);
- timespec_wrap E(1, fdata[i].nsec);
- timespec_wrap r;
-
- r = lfp_intv_to_tspec(a);
- ASSERT_PRED_FORMAT2(TimespecClose, E, r);
- }
-}
-
-TEST_F(timespecTest, FromLFPrelNeg) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- l_fp_wrap a(~0, fdata[i].frac);
- timespec_wrap E(-1, fdata[i].nsec);
- timespec_wrap r;
-
- r = lfp_intv_to_tspec(a);
- ASSERT_PRED_FORMAT2(TimespecClose, E, r);
- }
-}
-
-
-// nsec -> frac -> nsec roundtrip, using a prime start and increment
-TEST_F(timespecTest, LFProundtrip) {
- for (int32_t t = -1; t < 2; ++t)
- for (u_int32 i = 4999; i < 1000000000; i+=10007) {
- timespec_wrap E(t, i);
- l_fp_wrap a;
- timespec_wrap r;
-
- a = tspec_intv_to_lfp(E);
- r = lfp_intv_to_tspec(a);
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// string formatting
-//----------------------------------------------------------------------
-
-TEST_F(timespecTest, ToString) {
- static const struct {
- time_t sec;
- long nsec;
- const char * repr;
- } data [] = {
- { 0, 0, "0.000000000" },
- { 2, 0, "2.000000000" },
- {-2, 0, "-2.000000000" },
- { 0, 1, "0.000000001" },
- { 0,-1, "-0.000000001" },
- { 1,-1, "0.999999999" },
- {-1, 1, "-0.999999999" },
- {-1,-1, "-1.000000001" },
- };
- for (int i = 0; i < COUNTOF(data); i++) {
- timespec_wrap a(data[i].sec, data[i].nsec);
- std::string E(data[i].repr);
- std::string r(tspectoa(a));
- ASSERT_EQ(E, r);
- }
-}
-
-// -*- EOF -*-
+++ /dev/null
-/*
- * timestructs.cpp -- test bed adaptors for time structs.
- *
- * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
- * The contents of 'html/copyright.html' apply.
- */
-#include "g_libntptest.h"
-#include "g_timestructs.h"
-
-extern "C" {
-#include "timetoa.h"
-#include "timevalops.h"
-#include "timespecops.h"
-}
-
-namespace timeStruct {
-
-std::ostream&
-operator << (std::ostream& os, const timeStruct::l_fp_wrap& val)
-{
- // raw data formatting
- os << "0x" << std::hex << val.V.l_ui << ':'
- << std::setfill('0') << std::setw(8) << val.V.l_uf
- << std::dec;
- // human-readable format
- os << '[' << lfptoa(&val.V, 10) << ']';
- return os;
-}
-
-std::ostream&
-operator << (std::ostream& os, const timeStruct::timeval_wrap& val)
-{
- // raw data formatting
- os << val.V.tv_sec << ':' << val.V.tv_usec;
- // human-readable format
- os << '['
- << format_time_fraction(val.V.tv_sec, val.V.tv_usec, 6)
- << ']';
- return os;
-}
-
-std::ostream&
-operator << (std::ostream& os, const timeStruct::timespec_wrap& val)
-{
- // raw data formatting
- os << val.V.tv_sec << ':' << val.V.tv_nsec;
- // human-readable format
- os << '['
- << format_time_fraction(val.V.tv_sec, val.V.tv_nsec, 9)
- << ']';
- return os;
-}
-
-// Implementation of the l_fp closeness predicate
-
-AssertFpClose::AssertFpClose(
- u_int32 hi,
- u_int32 lo
- )
-{
- limit.l_ui = hi;
- limit.l_uf = lo;
-}
-
-::testing::AssertionResult
-AssertFpClose::operator()(
- const char* m_expr,
- const char* n_expr,
- const l_fp & m,
- const l_fp & n
- )
-{
- l_fp diff;
-
- if (L_ISGEQ(&m, &n)) {
- diff = m;
- L_SUB(&diff, &n);
- } else {
- diff = n;
- L_SUB(&diff, &m);
- }
- if (L_ISGEQ(&limit, &diff))
- return ::testing::AssertionSuccess();
-
- return ::testing::AssertionFailure()
- << m_expr << " which is " << l_fp_wrap(m)
- << "\nand\n"
- << n_expr << " which is " << l_fp_wrap(n)
- << "\nare not close; diff=" << l_fp_wrap(diff);
-}
-
-// Implementation of the timeval closeness predicate
-
-AssertTimevalClose::AssertTimevalClose(
- time_t hi,
- int32 lo
- )
-{
- limit.tv_sec = hi;
- limit.tv_usec = lo;
-}
-
-::testing::AssertionResult
-AssertTimevalClose::operator()(
- const char* m_expr,
- const char* n_expr,
- const struct timeval & m,
- const struct timeval & n
- )
-{
- struct timeval diff;
-
- diff = abs_tval(sub_tval(m, n));
- if (cmp_tval(limit, diff) >= 0)
- return ::testing::AssertionSuccess();
-
- return ::testing::AssertionFailure()
- << m_expr << " which is " << timeval_wrap(m)
- << "\nand\n"
- << n_expr << " which is " << timeval_wrap(n)
- << "\nare not close; diff=" << timeval_wrap(diff);
-}
-
-// Implementation of the timespec closeness predicate
-
-AssertTimespecClose::AssertTimespecClose(
- time_t hi,
- int32 lo
- )
-{
- limit.tv_sec = hi;
- limit.tv_nsec = lo;
-}
-
-::testing::AssertionResult
-AssertTimespecClose::operator()(
- const char* m_expr,
- const char* n_expr,
- const struct timespec & m,
- const struct timespec & n
- )
-{
- struct timespec diff;
-
- diff = abs_tspec(sub_tspec(m, n));
- if (cmp_tspec(limit, diff) >= 0)
- return ::testing::AssertionSuccess();
-
- return ::testing::AssertionFailure()
- << m_expr << " which is " << timespec_wrap(m)
- << "\nand\n"
- << n_expr << " which is " << timespec_wrap(n)
- << "\nare not close; diff=" << timespec_wrap(diff);
-}
-
-} // namespace timeStruct
+++ /dev/null
-/*
- * timestructs.h -- test bed adaptors for time structs.
- *
- * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
- * The contents of 'html/copyright.html' apply.
- *
- * Some wrapper classes and a closeness predicate that are used to
- * bridge the gap between the goggletest framework and the structs used
- * for representing time stamps (l_fp, struct timeval, struct timespec).
- *
- * Some ostream conversion operators are provided to give diagnostic
- * output on errors. The normal string conversion functions will give
- * HRVs (human readable values) but we might also be interested in the
- * machine representation for diagnostic purposes.
- */
-#ifndef TIMESTRUCTS_H
-#define TIMESTRUCTS_H
-
-extern "C" {
-#include "ntp_fp.h"
-}
-
-namespace timeStruct {
-
-// wrap a l_fp struct with common operations
-class l_fp_wrap {
- public:
- l_fp V;
-
- l_fp_wrap()
- { ZERO(V); }
- l_fp_wrap(u_int32 hi, u_int32 lo)
- { V.l_ui = hi; V.l_uf = lo; }
- l_fp_wrap(const l_fp &rhs)
- { V = rhs; }
- bool operator == (const l_fp_wrap& rhs) const
- { return L_ISEQU(&V, &rhs.V); }
- operator l_fp* ()
- { return &V; }
- operator l_fp& ()
- { return V; }
- l_fp_wrap & operator = (const l_fp_wrap& rhs)
- { V = rhs.V; return *this; }
- l_fp_wrap& operator = (const l_fp& rhs)
- { V = rhs; return *this; }
- };
-
-// wrap a 'struct timeval' with common operations
-class timeval_wrap {
-public:
- struct timeval V;
-
- timeval_wrap()
- { ZERO(V); }
- timeval_wrap(time_t hi, long lo)
- { V.tv_sec = hi; V.tv_usec = lo; }
- timeval_wrap(const struct timeval & rhs)
- { V = rhs; }
- timeval_wrap(const timeval_wrap & rhs)
- { V = rhs.V; }
- bool operator == (const timeval_wrap& rhs) const
- { return V.tv_sec == rhs.V.tv_sec &&
- V.tv_usec == rhs.V.tv_usec ; }
- bool valid() const
- { return V.tv_usec >= 0 && V.tv_usec < 1000000; }
- operator struct timeval* ()
- { return &V; }
- operator struct timeval& ()
- { return V; }
- timeval_wrap& operator = (const timeval_wrap& rhs)
- { V = rhs.V; return *this; }
- timeval_wrap& operator = (const struct timeval& rhs)
- { V = rhs; return *this; }
-};
-
-// wrap a 'struct timespec' with common operations
-class timespec_wrap {
-public:
- struct timespec V;
-
- timespec_wrap()
- { ZERO(V); }
- timespec_wrap(time_t hi, long lo)
- { V.tv_sec = hi; V.tv_nsec = lo; }
- timespec_wrap(const struct timespec & rhs)
- { V = rhs; }
- timespec_wrap(const timespec_wrap & rhs)
- { V = rhs.V; }
- bool operator == (const timespec_wrap& rhs) const
- { return V.tv_sec == rhs.V.tv_sec &&
- V.tv_nsec == rhs.V.tv_nsec ; }
- bool valid() const
- { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; }
- operator struct timespec* ()
- { return &V; }
- operator struct timespec& ()
- { return V; }
- timespec_wrap& operator = (const timespec_wrap& rhs)
- { V = rhs.V; return *this; }
- timespec_wrap& operator = (const struct timespec& rhs)
- { V = rhs; return *this; }
-};
-
-// l_fp closeness testing predicate
-//
-// This predicate is used for the closeness ('near') testing of l_fp
-// values. Once constructed with a limit, it can be used to check the
-// absolute difference of two l_fp structs against that limit; if the
-// difference is less or equal to this limit, the test passes.
-class AssertFpClose {
-private:
- l_fp limit;
-
-public:
- AssertFpClose(u_int32 hi, u_int32 lo);
-
- ::testing::AssertionResult
- operator()(const char* m_expr, const char* n_expr,
- const l_fp & m, const l_fp & n);
-};
-
-
-// timeval closeness testing predicate
-//
-// CAVEAT: This class uses the timevalops functions
-// - sub_tval
-// - abs_tval
-// - cmp_tval
-//
-// This creates a dependency loop of sorts. The loop is defused by the
-// fact that these basic operations can be tested by exact value tests,
-// so once the basic timeval operations passed it's safe to use this
-// predicate.
-class AssertTimevalClose {
-private:
- struct timeval limit;
-
-public:
- // note: (hi,lo) should be a positive normalised timeval;
- // the constructor does not normalise the values!
- AssertTimevalClose(time_t hi, int32 lo);
-
- ::testing::AssertionResult
- operator()(const char* m_expr, const char* n_expr,
- const struct timeval & m, const struct timeval & n);
-};
-
-
-// timespec closeness testing predicate
-//
-// CAVEAT: This class uses the timespecops functions
-// - sub_tspec
-// - abs_tspec
-// - cmp_tspec
-//
-// See the equivalent timeval helper.
-class AssertTimespecClose {
-private:
- struct timespec limit;
-
-public:
- // note: (hi,lo) should be a positive normalised timespec;
- // the constructor does not normalise the values!
- AssertTimespecClose(time_t hi, int32 lo);
-
- ::testing::AssertionResult
- operator()(const char* m_expr, const char* n_expr,
- const struct timespec & m, const struct timespec & n);
-};
-
-
-// since googletest wants to string format items, we declare the
-// necessary operators. Since all adaptors have only public members
-// there is need for friend declarations anywhere.
-
-extern std::ostream& operator << (std::ostream& os,
- const timeStruct::l_fp_wrap& val);
-extern std::ostream& operator << (std::ostream& os,
- const timeStruct::timeval_wrap& val);
-extern std::ostream& operator << (std::ostream& os,
- const timeStruct::timespec_wrap& val);
-
-} // namespace timeStruct
-
-#endif // TIMESTRUCTS_H
+++ /dev/null
-#include "g_libntptest.h"
-#include "g_timestructs.h"
-
-extern "C" {
-#include <math.h>
-#include "timevalops.h"
-}
-
-#include <string>
-#include <sstream>
-
-using namespace timeStruct;
-
-class timevalTest : public libntptest {
-protected:
- static u_int32 my_tick_to_tsf(u_int32 ticks);
- static u_int32 my_tsf_to_tick(u_int32 tsf);
-
- // that's it...
- struct lfpfracdata {
- long usec;
- u_int32 frac;
- };
- static const lfpfracdata fdata[];
-};
-
-u_int32
-timevalTest::my_tick_to_tsf(
- u_int32 ticks
- )
-{
- // convert microseconds to l_fp fractional units, using double
- // precision float calculations or, if available, 64bit integer
- // arithmetic. This should give the precise fraction, rounded to
- // the nearest representation.
-#ifdef HAVE_U_INT64
- return u_int32(((u_int64(ticks) << 32) + 500000) / 1000000);
-#else
- return u_int32(double(ticks) * 4294.967296 + 0.5);
-#endif
- // And before you ask: if ticks >= 1000000, the result is
- // truncated nonsense, so don't use it out-of-bounds.
-}
-
-u_int32
-timevalTest::my_tsf_to_tick(
- u_int32 tsf
- )
-{
- // Inverse operation: converts fraction to microseconds.
-#ifdef HAVE_U_INT64
- return u_int32((u_int64(tsf) * 1000000 + 0x80000000) >> 32);
-#else
- return u_int32(double(tsf) / 4294.967296 + 0.5);
-#endif
- // Beware: The result might be 10^6 due to rounding!
-}
-
-const timevalTest::lfpfracdata timevalTest::fdata [] = {
- { 0, 0x00000000 }, { 7478, 0x01ea1405 },
- { 22077, 0x05a6d699 }, { 125000, 0x20000000 },
- { 180326, 0x2e29d841 }, { 207979, 0x353e1c9b },
- { 250000, 0x40000000 }, { 269509, 0x44fe8ab5 },
- { 330441, 0x5497c808 }, { 333038, 0x5541fa76 },
- { 375000, 0x60000000 }, { 394734, 0x650d4995 },
- { 446327, 0x72427c7c }, { 500000, 0x80000000 },
- { 517139, 0x846338b4 }, { 571953, 0x926b8306 },
- { 587353, 0x965cc426 }, { 625000, 0xa0000000 },
- { 692136, 0xb12fd32c }, { 750000, 0xc0000000 },
- { 834068, 0xd5857aff }, { 848454, 0xd9344806 },
- { 854222, 0xdaae4b02 }, { 861465, 0xdc88f862 },
- { 875000, 0xe0000000 }, { 910661, 0xe921144d },
- { 922162, 0xec12cf10 }, { 942190, 0xf1335d25 }
-};
-
-
-// ---------------------------------------------------------------------
-// test support stuff - part1
-// ---------------------------------------------------------------------
-
-TEST_F(timevalTest, Helpers1) {
- timeval_wrap x;
-
- for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) {
- x.V.tv_usec = -1;
- ASSERT_FALSE(x.valid());
- x.V.tv_usec = 0;
- ASSERT_TRUE(x.valid());
- x.V.tv_usec = 999999;
- ASSERT_TRUE(x.valid());
- x.V.tv_usec = 1000000;
- ASSERT_FALSE(x.valid());
- }
-}
-
-//----------------------------------------------------------------------
-// test normalisation
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, Normalise) {
- for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) {
- timeval_wrap x(0, ns);
-
- x = normalize_tval(x);
- ASSERT_TRUE(x.valid());
- }
-}
-
-//----------------------------------------------------------------------
-// test classification
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, SignNoFrac) {
- // sign test, no fraction
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 0);
- int E = (i > 0) - (i < 0);
- int r = test_tval(a);
-
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timevalTest, SignWithFrac) {
- // sign test, with fraction
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 10);
- int E = (i >= 0) - (i < 0);
- int r = test_tval(a);
-
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// test compare
-//----------------------------------------------------------------------
-TEST_F(timevalTest, CmpFracEQ) {
- // fractions are equal
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a(i, 200);
- timeval_wrap b(j, 200);
- int E = (i > j) - (i < j);
- int r = cmp_tval_denorm(a, b);
-
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timevalTest, CmpFracGT) {
- // fraction a bigger fraction b
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a( i , 999800);
- timeval_wrap b( j , 200);
- int E = (i >= j) - (i < j);
- int r = cmp_tval_denorm(a, b);
-
- ASSERT_EQ(E, r);
- }
-}
-
-TEST_F(timevalTest, CmpFracLT) {
- // fraction a less fraction b
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a(i, 200);
- timeval_wrap b(j, 999800);
- int E = (i > j) - (i <= j);
- int r = cmp_tval_denorm(a, b);
-
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// Test addition (sum)
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, AddFullNorm) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a(i, 200);
- timeval_wrap b(j, 400);
- timeval_wrap E(i + j, 200 + 400);
- timeval_wrap c;
-
- c = add_tval(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timevalTest, AddFullOflow1) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a(i, 200);
- timeval_wrap b(j, 999900);
- timeval_wrap E(i + j + 1, 100);
- timeval_wrap c;
-
- c = add_tval(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timevalTest, AddUsecNorm) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 200);
- timeval_wrap E(i, 600);
- timeval_wrap c;
-
- c = add_tval_us(a, 600 - 200);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timevalTest, AddUsecOflow1) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 200);
- timeval_wrap E(i + 1, 100);
- timeval_wrap c;
-
- c = add_tval_us(a, MICROSECONDS - 100);
- ASSERT_EQ(E, c);
- }
-}
-
-//----------------------------------------------------------------------
-// test subtraction (difference)
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, SubFullNorm) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a(i, 600);
- timeval_wrap b(j, 400);
- timeval_wrap E(i - j, 600 - 400);
- timeval_wrap c;
-
- c = sub_tval(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timevalTest, SubFullOflow) {
- for (int i = -4; i <= 4; ++i)
- for (int j = -4; j <= 4; ++j) {
- timeval_wrap a(i, 100);
- timeval_wrap b(j, 999900);
- timeval_wrap E(i - j - 1, 200);
- timeval_wrap c;
-
- c = sub_tval(a, b);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timevalTest, SubUsecNorm) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 600);
- timeval_wrap E(i, 200);
- timeval_wrap c;
-
- c = sub_tval_us(a, 600 - 200);
- ASSERT_EQ(E, c);
- }
-}
-
-TEST_F(timevalTest, SubUsecOflow) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 100);
- timeval_wrap E(i - 1, 200);
- timeval_wrap c;
-
- c = sub_tval_us(a, MICROSECONDS - 100);
- ASSERT_EQ(E, c);
- }
-}
-
-//----------------------------------------------------------------------
-// test negation
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, Neg) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 100);
- timeval_wrap b;
- timeval_wrap c;
-
- b = neg_tval(a);
- c = add_tval(a, b);
- ASSERT_EQ(0, test_tval(c));
- }
-}
-
-//----------------------------------------------------------------------
-// test abs value
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, AbsNoFrac) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 0);
- timeval_wrap b;
-
- b = abs_tval(a);
- ASSERT_EQ((i != 0), test_tval(b));
- }
-}
-
-TEST_F(timevalTest, AbsWithFrac) {
- for (int i = -4; i <= 4; ++i) {
- timeval_wrap a(i, 100);
- timeval_wrap b;
-
- b = abs_tval(a);
- ASSERT_EQ(1, test_tval(b));
- }
-}
-
-// ---------------------------------------------------------------------
-// test support stuff -- part 2
-// ---------------------------------------------------------------------
-
-TEST_F(timevalTest, Helpers2) {
- AssertTimevalClose isClose(0, 2);
- timeval_wrap x, y;
-
- for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++)
- for (x.V.tv_usec = 1;
- x.V.tv_usec < 1000000;
- x.V.tv_usec += 499999) {
- for (long i = -4; i < 5; i++) {
- y = x;
- y.V.tv_usec += i;
- if (i >= -2 && i <= 2)
- ASSERT_PRED_FORMAT2(isClose, x, y);
- else
- ASSERT_PRED_FORMAT2(!isClose, x, y);
- }
- }
-}
-
-// and the global predicate instances we're using here
-static AssertFpClose FpClose(0, 1);
-static AssertTimevalClose TimevalClose(0, 1);
-
-//----------------------------------------------------------------------
-// conversion to l_fp
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, ToLFPbittest) {
- for (u_int32 i = 0; i < 1000000; i++) {
- timeval_wrap a(1, i);
- l_fp_wrap E(1, my_tick_to_tsf(i));
- l_fp_wrap r;
-
- r = tval_intv_to_lfp(a);
- ASSERT_PRED_FORMAT2(FpClose, E, r);
- }
-}
-
-TEST_F(timevalTest, ToLFPrelPos) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- timeval_wrap a(1, fdata[i].usec);
- l_fp_wrap E(1, fdata[i].frac);
- l_fp_wrap r;
-
- r = tval_intv_to_lfp(a);
- ASSERT_PRED_FORMAT2(FpClose, E, r);
- }
-}
-
-TEST_F(timevalTest, ToLFPrelNeg) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- timeval_wrap a(-1, fdata[i].usec);
- l_fp_wrap E(~0, fdata[i].frac);
- l_fp_wrap r;
-
- r = tval_intv_to_lfp(a);
- ASSERT_PRED_FORMAT2(FpClose, E, r);
- }
-}
-
-TEST_F(timevalTest, ToLFPabs) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- timeval_wrap a(1, fdata[i].usec);
- l_fp_wrap E(1 + JAN_1970, fdata[i].frac);
- l_fp_wrap r;
-
- r = tval_stamp_to_lfp(a);
- ASSERT_PRED_FORMAT2(FpClose, E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// conversion from l_fp
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, FromLFPbittest) {
- // 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.
- for (u_int32 tsf = 0; tsf < ~u_int32(1000); tsf += 1000) {
- timeval_wrap E(1, my_tsf_to_tick(tsf));
- l_fp_wrap a(1, tsf);
- timeval_wrap r;
-
- r = lfp_intv_to_tval(a);
- // The conversion might be off by one microsecond when
- // comparing to calculated value.
- ASSERT_PRED_FORMAT2(TimevalClose, E, r);
- }
-}
-
-TEST_F(timevalTest, FromLFPrelPos) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- l_fp_wrap a(1, fdata[i].frac);
- timeval_wrap E(1, fdata[i].usec);
- timeval_wrap r;
-
- r = lfp_intv_to_tval(a);
- ASSERT_PRED_FORMAT2(TimevalClose, E, r);
- }
-}
-
-TEST_F(timevalTest, FromLFPrelNeg) {
- for (int i = 0; i < COUNTOF(fdata); i++) {
- l_fp_wrap a(~0, fdata[i].frac);
- timeval_wrap E(-1, fdata[i].usec);
- timeval_wrap r;
-
- r = lfp_intv_to_tval(a);
- ASSERT_PRED_FORMAT2(TimevalClose, E, r);
- }
-}
-
-// usec -> frac -> usec roundtrip, using a prime start and increment
-TEST_F(timevalTest, LFProundtrip) {
- for (int32_t t = -1; t < 2; ++t)
- for (u_int32 i = 5; i < 1000000; i+=11) {
- timeval_wrap E(t, i);
- l_fp_wrap a;
- timeval_wrap r;
-
- a = tval_intv_to_lfp(E);
- r = lfp_intv_to_tval(a);
- ASSERT_EQ(E, r);
- }
-}
-
-//----------------------------------------------------------------------
-// string formatting
-//----------------------------------------------------------------------
-
-TEST_F(timevalTest, ToString) {
- static const struct {
- time_t sec;
- long usec;
- const char * repr;
- } data [] = {
- { 0, 0, "0.000000" },
- { 2, 0, "2.000000" },
- {-2, 0, "-2.000000" },
- { 0, 1, "0.000001" },
- { 0,-1, "-0.000001" },
- { 1,-1, "0.999999" },
- {-1, 1, "-0.999999" },
- {-1,-1, "-1.000001" },
- };
- for (int i = 0; i < COUNTOF(data); ++i) {
- timeval_wrap a(data[i].sec, data[i].usec);
- std::string E(data[i].repr);
- std::string r(tvaltoa(a));
-
- ASSERT_EQ(E, r);
- }
-}
-
-// -*- EOF -*-
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_fp.h"
-#include "timevalops.h"
-};
-
-class tstotvTest : public libntptest {
-protected:
- ::testing::AssertionResult IsEqual(const timeval& expected,
- const timeval& actual) {
- if (expected.tv_sec == actual.tv_sec &&
- expected.tv_usec == actual.tv_usec) {
- // Success
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << expected.tv_sec << "."
- << expected.tv_usec
- << " but was: " << actual.tv_sec << "."
- << actual.tv_usec;
- }
- }
-
- static const u_long HALF = 2147483648UL;
-};
-
-TEST_F(tstotvTest, Seconds) {
- const l_fp input = {50, 0}; // 50.0 s
- const timeval expected = {50, 0};
- timeval actual;
-
- TSTOTV(&input, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(tstotvTest, MicrosecondsExact) {
- const l_fp input = {50, HALF}; // 10.5 s
- const timeval expected = {50, 500000};
- timeval actual;
-
- TSTOTV(&input, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-
-}
-
-TEST_F(tstotvTest, MicrosecondsRounding) {
- const l_fp input = {50, 3865471UL}; // Should round to 50.0009
- const timeval expected = {50, 900};
- timeval actual;
-
- TSTOTV(&input, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
+++ /dev/null
-#include "g_lfptest.h"
-
-extern "C" {
-#include "timevalops.h"
-};
-
-// Required on Solaris for ldexp.
-#include <math.h>
-
-class tvtotsTest : public lfptest {
-};
-
-TEST_F(tvtotsTest, Seconds) {
- timeval input = {500, 0}; // 500.0 s
- l_fp expected = {500, 0};
- l_fp actual;
-
- TVTOTS(&input, &actual);
-
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(tvtotsTest, MicrosecondsRounded) {
- /* 0.0005 can not be represented exact in a l_fp structure.
- * It would equal to 2147483,648. This means that
- * HALF_PROMILLE_UP (which is 2147484) should be
- * the correct rounding. */
-
- timeval input = {0, 500}; // 0.0005 exact
- l_fp expected = {0, HALF_PROMILLE_UP};
- l_fp actual;
-
- TVTOTS(&input, &actual);
- EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(tvtotsTest, MicrosecondsExact) {
- // 0.5 can be represented exact in both l_fp and timeval.
- const timeval input = {10, 500000}; // 0.5 exact
- const l_fp expected = {10, HALF}; // 0.5 exact
- l_fp actual;
-
- TVTOTS(&input, &actual);
-
- // Compare the fractional part with an absolute error given.
- EXPECT_EQ(expected.l_ui, actual.l_ui);
-
- double expectedDouble, actualDouble;
- M_LFPTOD(0, expected.l_uf, expectedDouble);
- M_LFPTOD(0, actual.l_uf, actualDouble);
-
- // The error should be less than 0.5 us
- EXPECT_NEAR(expectedDouble, actualDouble, 0.0000005);
-}
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "ntp_fp.h"
-};
-
-class uglydateTest : public libntptest {
-protected:
- static const u_int32 HALF = 2147483648UL;
-};
-
-TEST_F(uglydateTest, ConstantDateTime) {
- l_fp time = {3485080800UL, HALF}; // 2010-06-09 14:00:00.5
-
- EXPECT_STREQ("3485080800.500000 10:159:14:00:00.500",
- uglydate(&time));
-}
-
+++ /dev/null
-#include "g_libntptest.h"
-
-extern "C" {
-#include "vint64ops.h"
-}
-
-class vi64Test : public libntptest {
-public:
- ::testing::AssertionResult IsEqual(const vint64 &expected, const vint64 &actual) {
- if (0 == memcmp(&expected, &actual, sizeof(vint64))) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: "
- << std::hex << expected.D_s.hi << '.'
- << std::hex << expected.D_s.lo
- << " but was "
- << std::hex << actual.D_s.hi << '.'
- << std::hex << actual.D_s.lo;
- }
- }
-};
-
-// ----------------------------------------------------------------------
-// test number parser
-TEST_F(vi64Test, ParseVUI64_pos) {
- vint64 act, exp;
- const char *sp;
- char *ep;
-
- sp = "1234x";
- exp.D_s.hi = 0;
- exp.D_s.lo = 1234;
- act = strtouv64(sp, &ep, 0);
- EXPECT_TRUE(IsEqual(exp, act));
- EXPECT_EQ(*ep, 'x');
-}
-
-TEST_F(vi64Test, ParseVUI64_neg) {
- vint64 act, exp;
- const char *sp;
- char *ep;
-
- sp = "-1234x";
- exp.D_s.hi = ~0;
- exp.D_s.lo = -1234;
- act = strtouv64(sp, &ep, 0);
- EXPECT_TRUE(IsEqual(exp, act));
- EXPECT_EQ(*ep, 'x');
-}
-
-TEST_F(vi64Test, ParseVUI64_case) {
- vint64 act, exp;
- const char *sp;
- char *ep;
-
- sp = "0123456789AbCdEf";
- exp.D_s.hi = 0x01234567;
- exp.D_s.lo = 0x89ABCDEF;
- act = strtouv64(sp, &ep, 16);
- EXPECT_TRUE(IsEqual(exp, act));
- EXPECT_EQ(*ep, '\0');
-}
-
+++ /dev/null
-#include "g_libntptest.h"
-
-class ymd2ydTest : public libntptest {
-};
-
-TEST_F(ymd2ydTest, NonLeapYearFebruary) {
- EXPECT_EQ(31+20, ymd2yd(2010,2,20)); //2010-02-20
-}
-
-TEST_F(ymd2ydTest, NonLeapYearJune) {
- int expected = 31+28+31+30+31+18; // 18 June non-leap year
- EXPECT_EQ(expected, ymd2yd(2011,6,18));
-}
-
-TEST_F(ymd2ydTest, LeapYearFebruary) {
- EXPECT_EQ(31+20, ymd2yd(2012,2,20)); //2012-02-20 (leap year)
-}
-
-TEST_F(ymd2ydTest, LeapYearDecember) {
- // 2012-12-31
- int expected = 31+29+31+30+31+30+31+31+30+31+30+31;
- EXPECT_EQ(expected, ymd2yd(2012,12,31));
-}
check_PROGRAMS = \
test-leapsec \
- test-ntp_prio_q \
- test-ntp_restrict \
test-rc_cmdlength \
- test-ntp_scanner \
$(NULL)
-# test-ntp_signd \ #doesn't work, can't link it because you can't link static function
-
-if GTEST_AVAILABLE
-check_PROGRAMS += tests
-else
-EXTRA_PROGRAMS = tests
-endif
+EXTRA_PROGRAMS = \
+ test-ntp_restrict \
+ test-ntp_scanner \
+ test-ntp_signd \
+ test-ntp_prio_q \
+ $(NULL)
LDADD = \
$(top_builddir)/ntpd/libntpd.a \
$(LDADD_LIBNTP) \
$(PTHREAD_LIBS) \
$(LDADD_NTP) \
- $(GTEST_LDFLAGS) \
- $(GTEST_LIBS) \
$(NULL)
unity_tests_LDADD = \
$(NULL)
AM_CFLAGS = $(CFLAGS_NTP)
-AM_CXXFLAGS = $(GTEST_CXXFLAGS)
AM_CPPFLAGS = $(NTP_INCS)
AM_CPPFLAGS += -I$(top_srcdir)/sntp
AM_CPPFLAGS += -I$(top_srcdir)/ntpd
AM_CPPFLAGS += -I$(top_srcdir)/tests/libntp
-AM_CPPFLAGS += $(GTEST_CPPFLAGS)
AM_CPPFLAGS += $(CPPFLAGS_NTP)
AM_CPPFLAGS += -DUNITY_INCLUDE_CONFIG_H
AM_CPPFLAGS += -I$(top_srcdir)/sntp/unity
AM_LDFLAGS = $(LDFLAGS_NTP)
-tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \
- g_ntpdtest.cpp \
- $(top_srcdir)/ntpd/ntp_leapsec.c \
- $(top_srcdir)/ntpd/ntp_prio_q.c \
- $(top_srcdir)/ntpd/ntp_restrict.c \
- $(top_srcdir)/ntpd/rc_cmdlength.c \
- $(top_srcdir)/ntpd/ntp_signd.c \
- g_leapsec.cpp \
- $(NULL)
-
BUILT_SOURCES += \
$(srcdir)/run-leapsec.c \
$(srcdir)/run-ntp_prio_q.c \
$(srcdir)/run-ntp_restrict.c \
- $(srcdir)/run-rc_cmdlength.c \
- $(srcdir)/run-ntp_signd.c \
+ $(srcdir)/run-rc_cmdlength.c \
+ $(srcdir)/run-ntp_signd.c \
$(NULL)
-noinst_HEADERS = g_ntpdtest.h \
- $(srcdir)/../libntp/test-libntp.h \
- $(NULL)
+#noinst_HEADERS = \
+# g_ntpdtest.h \
+# $(srcdir)/../libntp/test-libntp.h \
+# $(NULL)
###
###
-test_ntp_prio_q_CFLAGS = \
+test_ntp_prio_q_CFLAGS = \
-I$(top_srcdir)/sntp/unity \
$(NULL)
test_ntp_prio_q_LDADD = \
$(unity_tests_LDADD) \
- ../../ntpd/ntpdsim-ntp_prio_q.o \
+ ../../ntpd/ntpdsim-ntp_prio_q.o \
$(NULL)
test_ntp_prio_q_SOURCES = \
- ntp_prio_q.c \
+ ntp_prio_q.c \
run-ntp_prio_q.c \
- $(srcdir)/../libntp/test-libntp.c \
+ $(srcdir)/../libntp/test-libntp.c \
$(NULL)
$(srcdir)/run-ntp_prio_q.c: $(srcdir)/ntp_prio_q.c $(std_unity_list)
###
-test_ntp_restrict_CFLAGS = \
+test_ntp_restrict_CFLAGS = \
-I$(top_srcdir)/sntp/unity \
$(NULL)
-test_ntp_restrict_LDADD = \
+test_ntp_restrict_LDADD = \
$(unity_tests_LDADD) \
$(NULL)
-test_ntp_restrict_SOURCES = \
+test_ntp_restrict_SOURCES = \
ntp_restrict.c \
- run-ntp_restrict.c \
- $(srcdir)/../libntp/test-libntp.c \
+ run-ntp_restrict.c \
+ $(srcdir)/../libntp/test-libntp.c \
$(NULL)
$(srcdir)/run-ntp_restrict.c: $(srcdir)/ntp_restrict.c $(std_unity_list)
###
-test_rc_cmdlength_CFLAGS = \
+test_rc_cmdlength_CFLAGS = \
-I$(top_srcdir)/sntp/unity \
$(NULL)
-test_rc_cmdlength_LDADD = \
+test_rc_cmdlength_LDADD = \
$(unity_tests_LDADD) \
$(NULL)
-test_rc_cmdlength_SOURCES = \
+test_rc_cmdlength_SOURCES = \
rc_cmdlength.c \
- run-rc_cmdlength.c \
- $(srcdir)/../libntp/test-libntp.c \
+ run-rc_cmdlength.c \
+ $(srcdir)/../libntp/test-libntp.c \
$(NULL)
$(srcdir)/run-rc_cmdlength.c: $(srcdir)/rc_cmdlength.c $(std_unity_list)
$(unity_tests_LDADD) \
$(NULL)
-test_ntp_signd_SOURCES = \
+test_ntp_signd_SOURCES = \
ntp_signd.c \
run-ntp_signd.c \
- $(srcdir)/../libntp/test-libntp.c \
+ $(srcdir)/../libntp/test-libntp.c \
$(NULL)
$(srcdir)/run-ntp_signd.c: $(srcdir)/ntp_signd.c $(std_unity_list)
$(run_unity) ntp_signd.c run-ntp_signd.c
###
-test_ntp_scanner_CFLAGS = \
+test_ntp_scanner_CFLAGS = \
-I$(top_srcdir)/sntp/unity \
$(NULL)
-test_ntp_scanner_LDADD = \
+test_ntp_scanner_LDADD = \
$(unity_tests_LDADD) \
- $(top_builddir)/ntpd/ntp_scanner.o \
+ $(top_builddir)/ntpd/ntp_scanner.o \
$(NULL)
test_ntp_scanner_SOURCES = \
$(run_unity) ntp_scanner.c run-ntp_scanner.c
-
TESTS =
if !NTP_CROSSCOMPILE
+++ /dev/null
-#include "g_ntpdtest.h"
-
-extern "C" {
-#include "ntp.h"
-#include "ntp_calendar.h"
-#include "ntp_leapsec.h"
-}
-
-#include <string>
-#include <sstream>
-
-static const char leap1 [] =
- "#\n"
- "#@ 3610569600\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- " \t \n"
- "2571782400 20 # 1 Jul 1981\n"
- "2603318400 21 # 1 Jul 1982\n"
- "2634854400 22 # 1 Jul 1983\n"
- "2698012800 23 # 1 Jul 1985\n"
- "2776982400 24 # 1 Jan 1988\n"
- "2840140800 25 # 1 Jan 1990\n"
- "2871676800 26 # 1 Jan 1991\n"
- "2918937600 27 # 1 Jul 1992\n"
- "2950473600 28 # 1 Jul 1993\n"
- "2982009600 29 # 1 Jul 1994\n"
- "3029443200 30 # 1 Jan 1996\n"
- "3076704000 31 # 1 Jul 1997\n"
- "3124137600 32 # 1 Jan 1999\n"
- "3345062400 33 # 1 Jan 2006\n"
- "3439756800 34 # 1 Jan 2009\n"
- "3550089600 35 # 1 Jul 2012\n"
- "#\n"
- "#h dc2e6b0b 5aade95d a0587abd 4e0dacb4 e4d5049e\n"
- "#\n";
-
-static const char leap2 [] =
- "#\n"
- "#@ 2950473700\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "2571782400 20 # 1 Jul 1981\n"
- "2603318400 21 # 1 Jul 1982\n"
- "2634854400 22 # 1 Jul 1983\n"
- "2698012800 23 # 1 Jul 1985\n"
- "2776982400 24 # 1 Jan 1988\n"
- "2840140800 25 # 1 Jan 1990\n"
- "2871676800 26 # 1 Jan 1991\n"
- "2918937600 27 # 1 Jul 1992\n"
- "2950473600 28 # 1 Jul 1993\n"
- "#\n";
-
-// Faked table with a leap second removal at 2009
-static const char leap3 [] =
- "#\n"
- "#@ 3610569600\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "2571782400 20 # 1 Jul 1981\n"
- "2603318400 21 # 1 Jul 1982\n"
- "2634854400 22 # 1 Jul 1983\n"
- "2698012800 23 # 1 Jul 1985\n"
- "2776982400 24 # 1 Jan 1988\n"
- "2840140800 25 # 1 Jan 1990\n"
- "2871676800 26 # 1 Jan 1991\n"
- "2918937600 27 # 1 Jul 1992\n"
- "2950473600 28 # 1 Jul 1993\n"
- "2982009600 29 # 1 Jul 1994\n"
- "3029443200 30 # 1 Jan 1996\n"
- "3076704000 31 # 1 Jul 1997\n"
- "3124137600 32 # 1 Jan 1999\n"
- "3345062400 33 # 1 Jan 2006\n"
- "3439756800 32 # 1 Jan 2009\n"
- "3550089600 33 # 1 Jul 2012\n"
- "#\n";
-
-// short table with good hash
-static const char leap_ghash [] =
- "#\n"
- "#@ 3610569600\n"
- "#$ 3610566000\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "#\n"
- "#h 4b304e10 95642b3f c10b91f9 90791725 25f280d0\n"
- "#\n";
-
-// short table with bad hash
-static const char leap_bhash [] =
- "#\n"
- "#@ 3610569600\n"
- "#$ 3610566000\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "#\n"
- "#h dc2e6b0b 5aade95d a0587abd 4e0dacb4 e4d5049e\n"
- "#\n";
-
-// short table with malformed hash
-static const char leap_mhash [] =
- "#\n"
- "#@ 3610569600\n"
- "#$ 3610566000\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "#\n"
- "#h f2349a02 788b9534 a8f2e141 f2029Q6d 4064a7ee\n"
- "#\n";
-
-// short table with only 4 hash groups
-static const char leap_shash [] =
- "#\n"
- "#@ 3610569600\n"
- "#$ 3610566000\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "#\n"
- "#h f2349a02 788b9534 a8f2e141 f2029Q6d\n"
- "#\n";
-
-// table with good hash and truncated/missing leading zeros
-static const char leap_gthash [] = {
- "#\n"
- "#$ 3535228800\n"
- "#\n"
- "# Updated through IERS Bulletin C46\n"
- "# File expires on: 28 June 2014\n"
- "#\n"
- "#@ 3612902400\n"
- "#\n"
- "2272060800 10 # 1 Jan 1972\n"
- "2287785600 11 # 1 Jul 1972\n"
- "2303683200 12 # 1 Jan 1973\n"
- "2335219200 13 # 1 Jan 1974\n"
- "2366755200 14 # 1 Jan 1975\n"
- "2398291200 15 # 1 Jan 1976\n"
- "2429913600 16 # 1 Jan 1977\n"
- "2461449600 17 # 1 Jan 1978\n"
- "2492985600 18 # 1 Jan 1979\n"
- "2524521600 19 # 1 Jan 1980\n"
- "2571782400 20 # 1 Jul 1981\n"
- "2603318400 21 # 1 Jul 1982\n"
- "2634854400 22 # 1 Jul 1983\n"
- "2698012800 23 # 1 Jul 1985\n"
- "2776982400 24 # 1 Jan 1988\n"
- "2840140800 25 # 1 Jan 1990\n"
- "2871676800 26 # 1 Jan 1991\n"
- "2918937600 27 # 1 Jul 1992\n"
- "2950473600 28 # 1 Jul 1993\n"
- "2982009600 29 # 1 Jul 1994\n"
- "3029443200 30 # 1 Jan 1996\n"
- "3076704000 31 # 1 Jul 1997\n"
- "3124137600 32 # 1 Jan 1999\n"
- "3345062400 33 # 1 Jan 2006\n"
- "3439756800 34 # 1 Jan 2009\n"
- "3550089600 35 # 1 Jul 2012\n"
- "#\n"
- "#h 1151a8f e85a5069 9000fcdb 3d5e5365 1d505b37"
-};
-
-static const uint32_t lsec2006 = 3345062400u; // +33, 1 Jan 2006, 00:00:00 utc
-static const uint32_t lsec2009 = 3439756800u; // +34, 1 Jan 2009, 00:00:00 utc
-static const uint32_t lsec2012 = 3550089600u; // +35, 1 Jul 2012, 00:00:00 utc
-static const uint32_t lsec2015 = 3644697600u; // +36, 1 Jul 2015, 00:00:00 utc
-
-int stringreader(void* farg)
-{
- const char ** cpp = (const char**)farg;
- if (**cpp)
- return *(*cpp)++;
- else
- return EOF;
-}
-
-static int/*BOOL*/
-setup_load_table(
- const char * cp,
- int blim=FALSE)
-{
- int rc;
- leap_table_t * pt = leapsec_get_table(0);
- rc = (pt != NULL) && leapsec_load(pt, stringreader, &cp, blim);
- rc = rc && leapsec_set_table(pt);
- return rc;
-}
-
-static int/*BOOL*/
-setup_clear_table()
-{
- int rc;
- leap_table_t * pt = leapsec_get_table(0);
- if (pt)
- leapsec_clear(pt);
- rc = leapsec_set_table(pt);
- return rc;
-}
-
-
-class leapsecTest : public ntpdtest {
-protected:
- virtual void SetUp();
- virtual void TearDown();
-
- std::string CalendarToString(const calendar &cal) {
- std::ostringstream ss;
- ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
- << " (" << cal.yearday << ") " << (u_int)cal.hour << ":"
- << (u_int)cal.minute << ":" << (u_int)cal.second;
- return ss.str();
- }
-
- ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual) {
- if (expected.year == actual.year &&
- (expected.yearday == actual.yearday ||
- (expected.month == actual.month &&
- expected.monthday == actual.monthday)) &&
- expected.hour == actual.hour &&
- expected.minute == actual.minute &&
- expected.second == actual.second) {
- return ::testing::AssertionSuccess();
- } else {
- return ::testing::AssertionFailure()
- << "expected: " << CalendarToString(expected) << " but was "
- << CalendarToString(actual);
- }
- }
-};
-
-void leapsecTest::SetUp()
-{
- ntpcal_set_timefunc(timefunc);
- settime(1970, 1, 1, 0, 0, 0);
- leapsec_ut_pristine();
-}
-
-void leapsecTest::TearDown()
-{
- ntpcal_set_timefunc(NULL);
-}
-
-// =====================================================================
-// VALIDATION TESTS
-// =====================================================================
-
-// ----------------------------------------------------------------------
-TEST_F(leapsecTest, ValidateGood) {
- const char *cp = leap_ghash;
- int rc = leapsec_validate(stringreader, &cp);
- EXPECT_EQ(LSVALID_GOODHASH, rc);
-}
-
-// ----------------------------------------------------------------------
-TEST_F(leapsecTest, ValidateNoHash) {
- const char *cp = leap2;
- int rc = leapsec_validate(stringreader, &cp);
- EXPECT_EQ(LSVALID_NOHASH, rc);
-}
-
-// ----------------------------------------------------------------------
-TEST_F(leapsecTest, ValidateBad) {
- const char *cp = leap_bhash;
- int rc = leapsec_validate(stringreader, &cp);
- EXPECT_EQ(LSVALID_BADHASH, rc);
-}
-
-// ----------------------------------------------------------------------
-TEST_F(leapsecTest, ValidateMalformed) {
- const char *cp = leap_mhash;
- int rc = leapsec_validate(stringreader, &cp);
- EXPECT_EQ(LSVALID_BADFORMAT, rc);
-}
-
-// ----------------------------------------------------------------------
-TEST_F(leapsecTest, ValidateMalformedShort) {
- const char *cp = leap_shash;
- int rc = leapsec_validate(stringreader, &cp);
- EXPECT_EQ(LSVALID_BADFORMAT, rc);
-}
-
-// ----------------------------------------------------------------------
-TEST_F(leapsecTest, ValidateNoLeadZero) {
- const char *cp = leap_gthash;
- int rc = leapsec_validate(stringreader, &cp);
- EXPECT_EQ(LSVALID_GOODHASH, rc);
-}
-
-// =====================================================================
-// BASIC FUNCTIONS
-// =====================================================================
-
-// ----------------------------------------------------------------------
-// test table selection
-TEST_F(leapsecTest, tableSelect) {
- leap_table_t *pt1, *pt2, *pt3, *pt4;
-
- pt1 = leapsec_get_table(0);
- pt2 = leapsec_get_table(0);
- EXPECT_EQ(pt1, pt2);
-
- pt1 = leapsec_get_table(1);
- pt2 = leapsec_get_table(1);
- EXPECT_EQ(pt1, pt2);
-
- pt1 = leapsec_get_table(1);
- pt2 = leapsec_get_table(0);
- EXPECT_NE(pt1, pt2);
-
- pt1 = leapsec_get_table(0);
- pt2 = leapsec_get_table(1);
- EXPECT_NE(pt1, pt2);
-
- leapsec_set_table(pt1);
- pt2 = leapsec_get_table(0);
- pt3 = leapsec_get_table(1);
- EXPECT_EQ(pt1, pt2);
- EXPECT_NE(pt2, pt3);
-
- pt1 = pt3;
- leapsec_set_table(pt1);
- pt2 = leapsec_get_table(0);
- pt3 = leapsec_get_table(1);
- EXPECT_EQ(pt1, pt2);
- EXPECT_NE(pt2, pt3);
-}
-
-// ----------------------------------------------------------------------
-// load file & check expiration
-TEST_F(leapsecTest, loadFileExpire) {
- const char *cp = leap1;
- int rc;
- leap_table_t * pt = leapsec_get_table(0);
-
- rc = leapsec_load(pt, stringreader, &cp, FALSE)
- && leapsec_set_table(pt);
- EXPECT_EQ(1, rc);
- rc = leapsec_expired(3439756800u, NULL);
- EXPECT_EQ(0, rc);
- rc = leapsec_expired(3610569601u, NULL);
- EXPECT_EQ(1, rc);
-}
-
-// ----------------------------------------------------------------------
-// load file & check time-to-live
-TEST_F(leapsecTest, loadFileTTL) {
- const char *cp = leap1;
- int rc;
- leap_table_t * pt = leapsec_get_table(0);
- time_t pivot = 0x70000000u;
-
- const uint32_t limit = 3610569600u;
-
- rc = leapsec_load(pt, stringreader, &cp, FALSE)
- && leapsec_set_table(pt);
- ASSERT_EQ(1, rc);
-
- // exactly 1 day to live
- rc = leapsec_daystolive(limit - 86400, &pivot);
- EXPECT_EQ( 1, rc);
- // less than 1 day to live
- rc = leapsec_daystolive(limit - 86399, &pivot);
- EXPECT_EQ( 0, rc);
- // hit expiration exactly
- rc = leapsec_daystolive(limit, &pivot);
- EXPECT_EQ( 0, rc);
- // expired since 1 sec
- rc = leapsec_daystolive(limit + 1, &pivot);
- EXPECT_EQ(-1, rc);
-}
-
-// =====================================================================
-// RANDOM QUERY TESTS
-// =====================================================================
-
-// ----------------------------------------------------------------------
-// test query in pristine state (bug#2745 misbehaviour)
-TEST_F(leapsecTest, lsQueryPristineState) {
- int rc;
- leap_result_t qr;
-
- rc = leapsec_query(&qr, lsec2012, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// ad-hoc jump: leap second at 2009.01.01 -60days
-TEST_F(leapsecTest, ls2009faraway) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
-
- // test 60 days before leap. Nothing scheduled or indicated.
- rc = leapsec_query(&qr, lsec2009 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(33, qr.tai_offs);
- EXPECT_EQ(0, qr.tai_diff);
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// ad-hoc jump: leap second at 2009.01.01 -1week
-TEST_F(leapsecTest, ls2009weekaway) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
-
- // test 7 days before leap. Leap scheduled, but not yet indicated.
- rc = leapsec_query(&qr, lsec2009 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(33, qr.tai_offs);
- EXPECT_EQ(1, qr.tai_diff);
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// ad-hoc jump: leap second at 2009.01.01 -1hr
-TEST_F(leapsecTest, ls2009houraway) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
-
- // test 1 hour before leap. 61 true seconds to go.
- rc = leapsec_query(&qr, lsec2009 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(33, qr.tai_offs);
- EXPECT_EQ(1, qr.tai_diff);
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// ad-hoc jump: leap second at 2009.01.01 -1sec
-TEST_F(leapsecTest, ls2009secaway) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
-
- // test 1 second before leap (last boundary...) 2 true seconds to go.
- rc = leapsec_query(&qr, lsec2009 - 1, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(33, qr.tai_offs);
- EXPECT_EQ(1, qr.tai_diff);
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// ad-hoc jump to leap second at 2009.01.01
-TEST_F(leapsecTest, ls2009onspot) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
-
- // test on-spot: treat leap second as already gone.
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(34, qr.tai_offs);
- EXPECT_EQ(0, qr.tai_diff);
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// test handling of the leap second at 2009.01.01 without table
-TEST_F(leapsecTest, ls2009nodata) {
- int rc;
- leap_result_t qr;
-
- rc = setup_clear_table();
- EXPECT_EQ(1, rc);
-
- // test on-spot with empty table
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.tai_offs);
- EXPECT_EQ(0, qr.tai_diff);
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// test handling of the leap second at 2009.01.01 with culled data
-TEST_F(leapsecTest, ls2009limdata) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1, TRUE);
- EXPECT_EQ(1, rc);
-
- // test on-spot with limited table - this is tricky.
- // The table used ends 2012; depending on the build date, the 2009 entry
- // might be included or culled. The resulting TAI offset must be either
- // 34 or 35 seconds, depending on the build date of the test.
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_LE(34, qr.tai_offs);
- EXPECT_GE(35, qr.tai_offs);
- EXPECT_EQ(0, qr.tai_diff);
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// Far-distance forward jump into a transiton window.
-TEST_F(leapsecTest, qryJumpFarAhead) {
- int rc;
- leap_result_t qr;
- int last, idx;
-
- for (int mode=0; mode < 2; ++mode) {
- leapsec_ut_pristine();
- rc = setup_load_table(leap1, FALSE);
- EXPECT_EQ(1, rc);
- leapsec_electric(mode);
-
- rc = leapsec_query(&qr, lsec2006, NULL);
- EXPECT_EQ(FALSE, rc);
-
- rc = leapsec_query(&qr, lsec2012, NULL);
- EXPECT_EQ(FALSE, rc);
- }
-}
-
-// ----------------------------------------------------------------------
-// Forward jump into the next transition window
-TEST_F(leapsecTest, qryJumpAheadToTransition) {
- int rc;
- leap_result_t qr;
- int last, idx;
-
- for (int mode=0; mode < 2; ++mode) {
- leapsec_ut_pristine();
- rc = setup_load_table(leap1, FALSE);
- EXPECT_EQ(1, rc);
- leapsec_electric(mode);
-
- rc = leapsec_query(&qr, lsec2009-SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
-
- rc = leapsec_query(&qr, lsec2009+1, NULL);
- EXPECT_EQ(TRUE, rc);
- }
-}
-
-// ----------------------------------------------------------------------
-// Forward jump over the next transition window
-TEST_F(leapsecTest, qryJumpAheadOverTransition) {
- int rc;
- leap_result_t qr;
- int last, idx;
-
- for (int mode=0; mode < 2; ++mode) {
- leapsec_ut_pristine();
- rc = setup_load_table(leap1, FALSE);
- EXPECT_EQ(1, rc);
- leapsec_electric(mode);
-
- rc = leapsec_query(&qr, lsec2009-SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
-
- rc = leapsec_query(&qr, lsec2009+5, NULL);
- EXPECT_EQ(FALSE, rc);
- }
-}
-
-// =====================================================================
-// TABLE MODIFICATION AT RUNTIME
-// =====================================================================
-
-// ----------------------------------------------------------------------
-// add dynamic leap second (like from peer/clock)
-TEST_F(leapsecTest, addDynamic) {
- int rc;
- leap_result_t qr;
-
- static const uint32_t insns[] = {
- 2982009600u, // 29 # 1 Jul 1994
- 3029443200u, // 30 # 1 Jan 1996
- 3076704000u, // 31 # 1 Jul 1997
- 3124137600u, // 32 # 1 Jan 1999
- 3345062400u, // 33 # 1 Jan 2006
- 3439756800u, // 34 # 1 Jan 2009
- 3550089600u, // 35 # 1 Jul 2012
- 0 // sentinel
- };
-
- rc = setup_load_table(leap2, FALSE);
- EXPECT_EQ(1, rc);
-
- leap_table_t * pt = leapsec_get_table(0);
- for (int idx=1; insns[idx]; ++idx) {
- rc = leapsec_add_dyn(TRUE, insns[idx] - 20*SECSPERDAY - 100, NULL);
- EXPECT_EQ(TRUE, rc);
- }
- // try to slip in a previous entry
- rc = leapsec_add_dyn(TRUE, insns[0] - 20*SECSPERDAY - 100, NULL);
- EXPECT_EQ(FALSE, rc);
- //leapsec_dump(pt, (leapsec_dumper)fprintf, stdout);
-}
-
-// ----------------------------------------------------------------------
-// add fixed leap seconds (like from network packet)
-#if 0 /* currently unused -- possibly revived later */
-TEST_F(leapsecTest, addFixed) {
- int rc;
- leap_result_t qr;
-
- static const struct { uint32_t tt; int of; } insns[] = {
- {2982009600u, 29},// # 1 Jul 1994
- {3029443200u, 30},// # 1 Jan 1996
- {3076704000u, 31},// # 1 Jul 1997
- {3124137600u, 32},// # 1 Jan 1999
- {3345062400u, 33},// # 1 Jan 2006
- {3439756800u, 34},// # 1 Jan 2009
- {3550089600u, 35},// # 1 Jul 2012
- {0,0} // sentinel
- };
-
- rc = setup_load_table(leap2, FALSE);
- EXPECT_EQ(1, rc);
-
- leap_table_t * pt = leapsec_get_table(0);
- // try to get in BAD time stamps...
- for (int idx=0; insns[idx].tt; ++idx) {
- rc = leapsec_add_fix(
- insns[idx].of,
- insns[idx].tt - 20*SECSPERDAY - 100,
- insns[idx].tt + SECSPERDAY,
- NULL);
- EXPECT_EQ(FALSE, rc);
- }
- // now do it right
- for (int idx=0; insns[idx].tt; ++idx) {
- rc = leapsec_add_fix(
- insns[idx].of,
- insns[idx].tt,
- insns[idx].tt + SECSPERDAY,
- NULL);
- EXPECT_EQ(TRUE, rc);
- }
- // try to slip in a previous entry
- rc = leapsec_add_fix(
- insns[0].of,
- insns[0].tt,
- insns[0].tt + SECSPERDAY,
- NULL);
- EXPECT_EQ(FALSE, rc);
- //leapsec_dump(pt, (leapsec_dumper)fprintf, stdout);
-}
-#endif
-
-// ----------------------------------------------------------------------
-// add fixed leap seconds (like from network packet)
-#if 0 /* currently unused -- possibly revived later */
-TEST_F(leapsecTest, addFixedExtend) {
- int rc;
- leap_result_t qr;
- int last, idx;
-
- static const struct { uint32_t tt; int of; } insns[] = {
- {2982009600u, 29},// # 1 Jul 1994
- {3029443200u, 30},// # 1 Jan 1996
- {0,0} // sentinel
- };
-
- rc = setup_load_table(leap2, FALSE);
- EXPECT_EQ(1, rc);
-
- leap_table_t * pt = leapsec_get_table(FALSE);
- for (last=idx=0; insns[idx].tt; ++idx) {
- last = idx;
- rc = leapsec_add_fix(
- insns[idx].of,
- insns[idx].tt,
- insns[idx].tt + SECSPERDAY,
- NULL);
- EXPECT_EQ(TRUE, rc);
- }
-
- // try to extend the expiration of the last entry
- rc = leapsec_add_fix(
- insns[last].of,
- insns[last].tt,
- insns[last].tt + 128*SECSPERDAY,
- NULL);
- EXPECT_EQ(TRUE, rc);
-
- // try to extend the expiration of the last entry with wrong offset
- rc = leapsec_add_fix(
- insns[last].of+1,
- insns[last].tt,
- insns[last].tt + 129*SECSPERDAY,
- NULL);
- EXPECT_EQ(FALSE, rc);
- //leapsec_dump(pt, (leapsec_dumper)fprintf, stdout);
-}
-#endif
-
-// ----------------------------------------------------------------------
-// add fixed leap seconds (like from network packet) in an otherwise
-// empty table and test queries before / between /after the tabulated
-// values.
-#if 0 /* currently unused -- possibly revived later */
-TEST_F(leapsecTest, setFixedExtend) {
- int rc;
- leap_result_t qr;
- int last, idx;
-
- static const struct { uint32_t tt; int of; } insns[] = {
- {2982009600u, 29},// # 1 Jul 1994
- {3029443200u, 30},// # 1 Jan 1996
- {0,0} // sentinel
- };
-
- leap_table_t * pt = leapsec_get_table(0);
- for (last=idx=0; insns[idx].tt; ++idx) {
- last = idx;
- rc = leapsec_add_fix(
- insns[idx].of,
- insns[idx].tt,
- insns[idx].tt + 128*SECSPERDAY,
- NULL);
- EXPECT_EQ(TRUE, rc);
- }
-
- rc = leapsec_query(&qr, insns[0].tt - 86400, NULL);
- EXPECT_EQ(28, qr.tai_offs);
-
- rc = leapsec_query(&qr, insns[0].tt + 86400, NULL);
- EXPECT_EQ(29, qr.tai_offs);
-
- rc = leapsec_query(&qr, insns[1].tt - 86400, NULL);
- EXPECT_EQ(29, qr.tai_offs);
-
- rc = leapsec_query(&qr, insns[1].tt + 86400, NULL);
- EXPECT_EQ(30, qr.tai_offs);
-
- //leapsec_dump(pt, (leapsec_dumper)fprintf, stdout);
-}
-#endif
-
-// =====================================================================
-// AUTOKEY LEAP TRANSFER TESTS
-// =====================================================================
-
-// ----------------------------------------------------------------------
-// Check if the offset can be applied to an empty table ONCE
-TEST_F(leapsecTest, taiEmptyTable) {
- int rc;
-
- rc = leapsec_autokey_tai(35, lsec2015-30*86400, NULL);
- EXPECT_EQ(TRUE, rc);
-
- rc = leapsec_autokey_tai(35, lsec2015-29*86400, NULL);
- EXPECT_EQ(FALSE, rc);
-}
-
-// ----------------------------------------------------------------------
-// Check that with fixed entries the operation fails
-TEST_F(leapsecTest, taiTableFixed) {
- int rc;
-
- rc = setup_load_table(leap1, FALSE);
- EXPECT_EQ(1, rc);
-
- rc = leapsec_autokey_tai(35, lsec2015-30*86400, NULL);
- EXPECT_EQ(FALSE, rc);
-}
-
-// ----------------------------------------------------------------------
-// test adjustment with a dynamic entry already there
-TEST_F(leapsecTest, taiTableDynamic) {
- int rc;
- leap_era_t era;
-
- rc = leapsec_add_dyn(TRUE, lsec2015-20*SECSPERDAY, NULL);
- EXPECT_EQ(TRUE, rc);
-
- leapsec_query_era(&era, lsec2015-10, NULL);
- EXPECT_EQ(0, era.taiof);
- leapsec_query_era(&era, lsec2015+10, NULL);
- EXPECT_EQ(1, era.taiof);
-
- rc = leapsec_autokey_tai(35, lsec2015-19*86400, NULL);
- EXPECT_EQ(TRUE, rc);
-
- rc = leapsec_autokey_tai(35, lsec2015-19*86400, NULL);
- EXPECT_EQ(FALSE, rc);
-
- leapsec_query_era(&era, lsec2015-10, NULL);
- EXPECT_EQ(35, era.taiof);
- leapsec_query_era(&era, lsec2015+10, NULL);
- EXPECT_EQ(36, era.taiof);
-}
-
-// ----------------------------------------------------------------------
-// test adjustment with a dynamic entry already there in dead zone
-TEST_F(leapsecTest, taiTableDynamicDeadZone) {
- int rc;
-
- rc = leapsec_add_dyn(TRUE, lsec2015-20*SECSPERDAY, NULL);
- EXPECT_EQ(TRUE, rc);
-
- rc = leapsec_autokey_tai(35, lsec2015-5, NULL);
- EXPECT_EQ(FALSE, rc);
-
- rc = leapsec_autokey_tai(35, lsec2015+5, NULL);
- EXPECT_EQ(FALSE, rc);
-}
-
-
-// =====================================================================
-// SEQUENCE TESTS
-// =====================================================================
-
-// ----------------------------------------------------------------------
-// leap second insert at 2009.01.01, electric mode
-TEST_F(leapsecTest, ls2009seqInsElectric) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
- leapsec_electric(1);
- EXPECT_EQ(1, leapsec_electric(-1));
-
- rc = leapsec_query(&qr, lsec2009 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 1, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(TRUE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- // second call, same time frame: no trigger!
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// leap second insert at 2009.01.01, dumb mode
-TEST_F(leapsecTest, ls2009seqInsDumb) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
- EXPECT_EQ(0, leapsec_electric(-1));
-
- rc = leapsec_query(&qr, lsec2009 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 1, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009+1, NULL);
- EXPECT_EQ(TRUE, rc);
- EXPECT_EQ(-1, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- // second call, same time frame: no trigger!
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-
-// ----------------------------------------------------------------------
-// fake leap second remove at 2009.01.01, electric mode
-TEST_F(leapsecTest, ls2009seqDelElectric) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap3);
- EXPECT_EQ(1, rc);
- leapsec_electric(1);
- EXPECT_EQ(1, leapsec_electric(-1));
-
- rc = leapsec_query(&qr, lsec2009 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 1, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(TRUE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- // second call, same time frame: no trigger!
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// fake leap second remove at 2009.01.01. dumb mode
-TEST_F(leapsecTest, ls2009seqDelDumb) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap3);
- EXPECT_EQ(1, rc);
- EXPECT_EQ(0, leapsec_electric(-1));
-
- rc = leapsec_query(&qr, lsec2009 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 2, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2009 - 1, NULL);
- EXPECT_EQ(TRUE, rc);
- EXPECT_EQ(1, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- // second call, same time frame: no trigger!
- rc = leapsec_query(&qr, lsec2009, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// leap second insert at 2012.07.01, electric mode
-TEST_F(leapsecTest, ls2012seqInsElectric) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
- leapsec_electric(1);
- EXPECT_EQ(1, leapsec_electric(-1));
-
- rc = leapsec_query(&qr, lsec2012 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012 - 1, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012, NULL);
- EXPECT_EQ(TRUE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- // second call, same time frame: no trigger!
- rc = leapsec_query(&qr, lsec2012, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// leap second insert at 2012.07.01, dumb mode
-TEST_F(leapsecTest, ls2012seqInsDumb) {
- int rc;
- leap_result_t qr;
-
- rc = setup_load_table(leap1);
- EXPECT_EQ(1, rc);
- EXPECT_EQ(0, leapsec_electric(-1));
-
- rc = leapsec_query(&qr, lsec2012 - 60*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012 - 7*SECSPERDAY, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_SCHEDULE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012 - SECSPERHR, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ANNOUNCE, qr.proximity);
-
- rc = leapsec_query(&qr, lsec2012 - 1, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- // This is just 1 sec before transition!
- rc = leapsec_query(&qr, lsec2012, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_ALERT, qr.proximity);
-
- // NOW the insert/backwarp must happen
- rc = leapsec_query(&qr, lsec2012+1, NULL);
- EXPECT_EQ(TRUE, rc);
- EXPECT_EQ(-1, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-
- // second call with transition time: no trigger!
- rc = leapsec_query(&qr, lsec2012, NULL);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
-}
-
-// ----------------------------------------------------------------------
-// test repeated query on empty table in dumb mode
-TEST_F(leapsecTest, lsEmptyTableDumb) {
- int rc;
- leap_result_t qr;
-
- const time_t pivot(lsec2012);
- const uint32_t t0 (lsec2012 - 10);
- const uint32_t tE (lsec2012 + 10);
-
- EXPECT_EQ(0, leapsec_electric(-1));
-
- for (uint32_t t = t0; t != tE; ++t) {
- rc = leapsec_query(&qr, t, &pivot);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
- }
-}
-
-// ----------------------------------------------------------------------
-// test repeated query on empty table in electric mode
-TEST_F(leapsecTest, lsEmptyTableElectric) {
- int rc;
- leap_result_t qr;
-
- leapsec_electric(1);
- EXPECT_EQ(1, leapsec_electric(-1));
-
- const time_t pivot(lsec2012);
- const uint32_t t0 (lsec2012 - 10);
- const uint32_t tE (lsec2012 + 10);
-
- for (time_t t = t0; t != tE; ++t) {
- rc = leapsec_query(&qr, t, &pivot);
- EXPECT_EQ(FALSE, rc);
- EXPECT_EQ(0, qr.warped );
- EXPECT_EQ(LSPROX_NOWARN, qr.proximity);
- }
-}
+++ /dev/null
-#include "g_ntpdtest.h"
-
-/* This file contains various constants that libntp needs to be set
- * and that is normally defined in ntpd/ntpq/...
- */
-
-u_long current_time = 4; // needed by authkeys. Used only in to calculate lifetime.
-const char *progname = "ntpdtest";
-
-time_t ntpdtest::nowtime = 0;
-
-time_t ntpdtest::timefunc(time_t *ptr)
-{
- if (ptr)
- *ptr = nowtime;
- return nowtime;
-}
-
-void ntpdtest::settime(int y, int m, int d, int H, int M, int S)
-{
-
- time_t days(ntpcal_edate_to_eradays(y-1, m-1, d-1) + 1 - DAY_UNIX_STARTS);
- time_t secs(ntpcal_etime_to_seconds(H, M, S));
-
- nowtime = days * SECSPERDAY + secs;
-}
-
+++ /dev/null
-#include "tests_main.h"
-
-extern "C" {
-#include "ntp_stdlib.h"
-#include "ntp_calendar.h"
-};
-
-class ntpdtest : public ntptest {
-
-protected:
- static time_t timefunc(time_t*);
- static time_t nowtime;
- static void settime(int y, int m, int d, int H, int M, int S);
-
-};
#AUTOMAKE_OPTIONS = foreign 2.9 subdir-objects
+
NULL =
BUILT_SOURCES =
CLEANFILES =
run_unity = cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb
-check_PROGRAMS = bug-2803 first-test second-test smeartest
+check_PROGRAMS = \
+ first-test \
+ smeartest \
+ $(NULL)
+
+EXTRA_PROGRAMS = \
+ bug-2803 \
+ second-test \
+ $(NULL)
# HMS: we may not need some of these:
LDADD = \
AM_LDFLAGS = $(LDFLAGS_NTP)
-bug_2803_SOURCES = \
- bug-2803.c \
- run-ut-2803.c \
- ut-2803.c \
+bug_2803_SOURCES = \
+ bug-2803.c \
+ run-ut-2803.c \
+ ut-2803.c \
$(NULL)
$(srcdir)/run-ut-2803.c: $(srcdir)/ut-2803.c $(std_unity_list)