2ae1e85cbf4ede646ca68f0fc5e71898258c15ae
[platform/core/system/dlog.git] / src / tests / limiter_wrap.c
1 /*
2  * Copyright (c) 2018-2020, Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // C
18 #include <assert.h>
19 #include <stdio.h>
20
21 // POSIX
22 #include <time.h>
23
24 // DLog
25 #include <dlog.h>
26 #include <loglimiter.h>
27 #include <loglimiter-internal.h>
28
29 static size_t get_rulecount()
30 {
31         size_t cnt = 0;
32         struct rule *r = NULL;
33         do {
34                 char buf[128];
35                 assert(!__log_limiter_dump_rule(&r, buf, sizeof buf));
36                 ++cnt;
37         } while (r);
38         return cnt;
39 }
40
41 static int fail_snprintf;
42 int __wrap_snprintf(char *str, size_t size, const char *format, ...)
43 {
44         if (fail_snprintf && !--fail_snprintf)
45                 return -1;
46
47         va_list va;
48         va_start(va, format);
49         const int ret = vsnprintf(str, size, format, va);
50         va_end(va);
51         return ret;
52 }
53
54 static int fail_malloc;
55 void *__real_malloc(size_t size);
56 void *__wrap_malloc(size_t size)
57 {
58         return (fail_malloc == 1 && size == sizeof(struct rule))
59             || (fail_malloc == 2 && size != sizeof(struct rule))
60                 ? NULL
61                 : __real_malloc(size)
62         ;
63 }
64
65 static bool fail_time;
66 static bool future_time;
67 time_t __real_time(time_t *t);
68 time_t __wrap_time(time_t *t)
69 {
70         if (fail_time)
71                 return -1;
72
73         return __real_time(t) + (future_time ? 40000 : 0);
74 }