From: Damir Tomic Date: Sun, 7 Jun 2015 09:02:10 +0000 (+0200) Subject: adding new test files and their runners X-Git-Tag: NTP_4_3_40~6^2~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=602700b132c103851d6369efc656bfc5c97385a3;p=thirdparty%2Fntp.git adding new test files and their runners bk: 55740892BxHefdCcTPMvCokU7eaVUQ --- diff --git a/tests/libntp/a_md5encrypt.c b/tests/libntp/a_md5encrypt.c new file mode 100644 index 000000000..bd794f28c --- /dev/null +++ b/tests/libntp/a_md5encrypt.c @@ -0,0 +1,98 @@ + +#include "config.h" +#include "unity.h" + +#include "testcalshims.h" + + +#ifdef OPENSSL +# include "openssl/err.h" +# include "openssl/rand.h" +# include "openssl/evp.h" +#endif +#include "ntp.h" +#include "ntp_stdlib.h" + + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +/* + * 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"; +#define packetLength 16 //const int packetLength = 16; +#define keyIdLength 4 //const int keyIdLength = 4; +#define digestLength 16 //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"; + +void test_Encrypt() { + + char *packetPtr = malloc(totalLength*sizeof(*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); + + TEST_ASSERT_TRUE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)packetPtr, packetLength, length)); + + TEST_ASSERT_EQUAL(20, length); + TEST_ASSERT_TRUE(memcmp(expectedPacket, packetPtr, totalLength) == 0); + + free(packetPtr); //delete[] packetPtr; +} + +void test_DecryptValid() { + cache_secretsize = keyLength; + + TEST_ASSERT_TRUE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)expectedPacket, packetLength, 20)); +} + +void test_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"; + + TEST_ASSERT_FALSE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)invalidPacket, packetLength, 20)); +} + +void test_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; + + TEST_ASSERT_EQUAL(address, addr2refid(&addr)); +} + +void test_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; + + TEST_ASSERT_EQUAL(expected, addr2refid(&addr)); +} diff --git a/tests/libntp/atouint.c b/tests/libntp/atouint.c new file mode 100644 index 000000000..1006abdc2 --- /dev/null +++ b/tests/libntp/atouint.c @@ -0,0 +1,40 @@ +#include "config.h" +#include "ntp_fp.h" + +#include "unity.h" + +void test_RegularPositive() { + const char *str = "305"; + u_long actual; + + TEST_ASSERT_TRUE(atouint(str, &actual)); + TEST_ASSERT_EQUAL(305, actual); +} + +void test_PositiveOverflowBoundary() { + const char *str = "4294967296"; + u_long actual; + + TEST_ASSERT_FALSE(atouint(str, &actual)); +} + +void test_PositiveOverflowBig() { + const char *str = "8000000000"; + u_long actual; + + TEST_ASSERT_FALSE(atouint(str, &actual)); +} + +void test_Negative() { + const char *str = "-1"; + u_long actual; + + TEST_ASSERT_FALSE(atouint(str, &actual)); +} + +void test_IllegalChar() { + const char *str = "50c3"; + u_long actual; + + TEST_ASSERT_FALSE(atouint(str, &actual)); +} diff --git a/tests/libntp/authkeys.c b/tests/libntp/authkeys.c new file mode 100644 index 000000000..437c73700 --- /dev/null +++ b/tests/libntp/authkeys.c @@ -0,0 +1,107 @@ +/* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */ + +#include "config.h" +#include "testcalshims.h" +#include "unity.h" + +#ifdef OPENSSL +# include "openssl/err.h" +# include "openssl/rand.h" +# include "openssl/evp.h" +#endif +#include "ntp.h" +#include "ntp_stdlib.h" + +// old code from google test framework, moved to SetUp() for unity +void setUp(void) +{ +/* + * 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 tearDown(void) +{ +} + + +static const int KEYTYPE = KEY_TYPE_MD5; + + + + +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); +} + +void test_AddTrustedKeys() { + const keyid_t KEYNO1 = 5; + const keyid_t KEYNO2 = 8; + + AddTrustedKey(KEYNO1); + AddTrustedKey(KEYNO2); + + TEST_ASSERT_TRUE(authistrusted(KEYNO1)); + TEST_ASSERT_TRUE(authistrusted(KEYNO2)); +} + +void test_AddUntrustedKey() { + const keyid_t KEYNO = 3; + + AddUntrustedKey(KEYNO); + + TEST_ASSERT_FALSE(authistrusted(KEYNO)); +} + +void test_HaveKeyCorrect() { + const keyid_t KEYNO = 3; + + AddTrustedKey(KEYNO); + + TEST_ASSERT_TRUE(auth_havekey(KEYNO)); + TEST_ASSERT_TRUE(authhavekey(KEYNO)); +} + +void test_HaveKeyIncorrect() { + const keyid_t KEYNO = 2; + + TEST_ASSERT_FALSE(auth_havekey(KEYNO)); + TEST_ASSERT_FALSE(authhavekey(KEYNO)); +} + +void test_AddWithAuthUseKey() { + const keyid_t KEYNO = 5; + const char* KEY = "52a"; + + TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); +} + +void test_EmptyKey() { + const keyid_t KEYNO = 3; + const char* KEY = ""; + + + TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); +} diff --git a/tests/libntp/run-test-a_md5encrypt.c b/tests/libntp/run-test-a_md5encrypt.c new file mode 100644 index 000000000..81a7624b3 --- /dev/null +++ b/tests/libntp/run-test-a_md5encrypt.c @@ -0,0 +1,56 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ + +//=======Test Runner Used To Run Each Test Below===== +#define RUN_TEST(TestFunc, TestLineNum) \ +{ \ + Unity.CurrentTestName = #TestFunc; \ + Unity.CurrentTestLineNumber = TestLineNum; \ + Unity.NumberOfTests++; \ + if (TEST_PROTECT()) \ + { \ + setUp(); \ + TestFunc(); \ + } \ + if (TEST_PROTECT() && !TEST_IS_IGNORED) \ + { \ + tearDown(); \ + } \ + UnityConcludeTest(); \ +} + +//=======Automagically Detected Files To Include===== +#include "unity.h" +#include +#include + +//=======External Functions This Runner Calls===== +extern void setUp(void); +extern void tearDown(void); +extern void test_Encrypt(); +extern void test_DecryptValid(); +extern void test_DecryptInvalid(); +extern void test_IPv4AddressToRefId(); +extern void test_IPv6AddressToRefId(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + + +//=======MAIN===== +int main(void) +{ + Unity.TestFile = "a_md5encrypt.c"; + UnityBegin("a_md5encrypt.c"); + RUN_TEST(test_Encrypt, 38); + RUN_TEST(test_DecryptValid, 56); + RUN_TEST(test_DecryptInvalid, 62); + RUN_TEST(test_IPv4AddressToRefId, 70); + RUN_TEST(test_IPv6AddressToRefId, 81); + + return (UnityEnd()); +} diff --git a/tests/libntp/run-test-atouint.c b/tests/libntp/run-test-atouint.c new file mode 100644 index 000000000..3f1cbc5ed --- /dev/null +++ b/tests/libntp/run-test-atouint.c @@ -0,0 +1,56 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ + +//=======Test Runner Used To Run Each Test Below===== +#define RUN_TEST(TestFunc, TestLineNum) \ +{ \ + Unity.CurrentTestName = #TestFunc; \ + Unity.CurrentTestLineNumber = TestLineNum; \ + Unity.NumberOfTests++; \ + if (TEST_PROTECT()) \ + { \ + setUp(); \ + TestFunc(); \ + } \ + if (TEST_PROTECT() && !TEST_IS_IGNORED) \ + { \ + tearDown(); \ + } \ + UnityConcludeTest(); \ +} + +//=======Automagically Detected Files To Include===== +#include "unity.h" +#include +#include + +//=======External Functions This Runner Calls===== +extern void setUp(void); +extern void tearDown(void); +extern void test_RegularPositive(); +extern void test_PositiveOverflowBoundary(); +extern void test_PositiveOverflowBig(); +extern void test_Negative(); +extern void test_IllegalChar(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + + +//=======MAIN===== +int main(void) +{ + Unity.TestFile = "atouint.c"; + UnityBegin("atouint.c"); + RUN_TEST(test_RegularPositive, 6); + RUN_TEST(test_PositiveOverflowBoundary, 14); + RUN_TEST(test_PositiveOverflowBig, 21); + RUN_TEST(test_Negative, 28); + RUN_TEST(test_IllegalChar, 35); + + return (UnityEnd()); +} diff --git a/tests/libntp/run-test-authkeys.c b/tests/libntp/run-test-authkeys.c new file mode 100644 index 000000000..5ac6ef52f --- /dev/null +++ b/tests/libntp/run-test-authkeys.c @@ -0,0 +1,58 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ + +//=======Test Runner Used To Run Each Test Below===== +#define RUN_TEST(TestFunc, TestLineNum) \ +{ \ + Unity.CurrentTestName = #TestFunc; \ + Unity.CurrentTestLineNumber = TestLineNum; \ + Unity.NumberOfTests++; \ + if (TEST_PROTECT()) \ + { \ + setUp(); \ + TestFunc(); \ + } \ + if (TEST_PROTECT() && !TEST_IS_IGNORED) \ + { \ + tearDown(); \ + } \ + UnityConcludeTest(); \ +} + +//=======Automagically Detected Files To Include===== +#include "unity.h" +#include +#include + +//=======External Functions This Runner Calls===== +extern void setUp(void); +extern void tearDown(void); +extern void test_AddTrustedKeys(); +extern void test_AddUntrustedKey(); +extern void test_HaveKeyCorrect(); +extern void test_HaveKeyIncorrect(); +extern void test_AddWithAuthUseKey(); +extern void test_EmptyKey(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + + +//=======MAIN===== +int main(void) +{ + Unity.TestFile = "authkeys.c"; + UnityBegin("authkeys.c"); + RUN_TEST(test_AddTrustedKeys, 59); + RUN_TEST(test_AddUntrustedKey, 70); + RUN_TEST(test_HaveKeyCorrect, 78); + RUN_TEST(test_HaveKeyIncorrect, 87); + RUN_TEST(test_AddWithAuthUseKey, 94); + RUN_TEST(test_EmptyKey, 101); + + return (UnityEnd()); +}