Imported Upstream version 878.70.2
[platform/upstream/mdnsresponder.git] / unittests / unittest.h
1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2015 Apple Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <assert.h>
22 #include <signal.h>
23 #include "unittest_common.h"
24
25 #include <MacTypes.h>
26 #ifndef _UNITTEST_H_
27 #define _UNITTEST_H_
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 typedef struct __test_item_
34 {
35     struct     __test_item_*   next;
36     const      char*           file;
37     unsigned   int             line;
38     const      char*           func;
39     const      char*           s;
40     int        iter_count;
41 }   __test_item;
42
43 int run_tests(void);
44
45 #define UNITTEST_HEADER(X) int X() { int __success = 1; __test_item* __i = NULL;
46
47 #define UNITTEST_GROUP(X) { printf("== %s ==\n", #X); __success = X() && __success; }
48 #define UNITTEST_TEST(X)  { printf("%s: ", #X); fflush(NULL); __success = X() && __success; }
49
50 int _unittest_assert_i(const int condition, const int i, const char * const conditionStr,
51                        const char * const filename, const unsigned int linenum,
52                        const char * const functionname, __test_item ** __i, int * const __success);
53 #define UNITTEST_ASSERTI(X,I) (_unittest_assert_i((X)!=0, (I), #X, __FILE__, __LINE__, __func__, &__i, &__success))
54 #define UNITTEST_ASSERT(X)    UNITTEST_ASSERTI(X, -1)
55 #define UNITTEST_ASSERTI_RETURN(X,I) { if (!UNITTEST_ASSERTI(X,I)) goto __unittest_footer__; }
56 #define UNITTEST_ASSERT_RETURN(X) UNITTEST_ASSERTI_RETURN(X, -1)
57
58 void _unittest_print_list(__test_item* __i);
59 #define UNITTEST_FOOTER       goto __unittest_footer__; __unittest_footer__: printf("\n"); fflush(NULL); _unittest_print_list(__i); return __success; }
60
61 #define UNITTEST_MAIN     int main (int argc, char** argv) \
62                           { \
63                               (void)(argv); \
64                               signal(SIGPIPE, SIG_IGN); \
65                               FILE* fp; \
66                               unlink("unittest_success"); \
67                               if (!run_tests()) \
68                               { \
69                                   printf("unit test FAILED\n"); \
70                                   return -1; \
71                               } \
72                               fp = fopen("unittest_success", "w"); \
73                               if (!fp) return -2; \
74                               fprintf(fp, "unit test %s\n", "SUCCEEDED"); \
75                               fclose(fp); \
76                               printf("unit test SUCCESS\n"); \
77                               if (argc != 1) \
78                               { \
79                                   char c; \
80                                   printf("run leaks now\n"); \
81                                   read(STDIN_FILENO, &c, 1); \
82                               } \
83                               return 0; \
84                           }
85 #define UNITTEST_SENDDNSMESSAGE mStatus mDNSSendDNSMessage(mDNS *const m, DNSMessage *const msg, mDNSu8 *end,  \
86         mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst,  \
87         mDNSIPPort dstport, TCPSocket *sock, DomainAuthInfo *authInfo,  \
88         mDNSBool useBackgroundTrafficClass) \
89         { \
90                 (void)(m); \
91                 (void)(msg); \
92                 (void)(end); \
93                 (void)(InterfaceID); \
94                 (void)(src); \
95                 (void)(dst); \
96                 (void)(dstport); \
97                 (void)(sock); \
98                 (void)(authInfo); \
99                 (void)(useBackgroundTrafficClass); \
100                 return 0; \
101         }
102
103 #define UNITTEST_SETSOCKOPT void mDNSPlatformSetSocktOpt(void *sockCxt, mDNSTransport_Type transType, \
104     mDNSAddr_Type addrType, const DNSQuestion *q) \
105     { \
106         (void)(sockCxt); \
107         (void)(transType); \
108         (void)(addrType); \
109         (void)(q); \
110         return; \
111     }
112
113 #define UNITTEST_UDPCLOSE void mDNSPlatformUDPClose(UDPSocket *sock) \
114     { \
115         (void)(sock); \
116     }
117
118 #define UNITTEST_FAIL_ASSERT { assert(((void*)__func__) == 0); }
119
120 #ifdef __cplusplus
121 }
122 #endif
123
124 #endif // ndef _UNITTEST_H_