Asterisk - The Open Source Telephony Project GIT-master-f36a736
Macros | Functions | Variables
test_time.c File Reference

Timezone tests. More...

#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
Include dependency graph for test_time.c:

Go to the source code of this file.

Macros

#define TZDIR   "/usr/share/zoneinfo"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (test_time_create_by_unit)
 
 AST_TEST_DEFINE (test_time_create_by_unit_str)
 
 AST_TEST_DEFINE (test_time_str_to_unit)
 
 AST_TEST_DEFINE (test_time_tv_to_usec)
 
 AST_TEST_DEFINE (test_timezone_watch)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Time Tests" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Timezone tests.

Author
Tilghman Lesher <tlesher AT digium DOT com> 

Definition in file test_time.c.

Macro Definition Documentation

◆ TZDIR

#define TZDIR   "/usr/share/zoneinfo"

Definition at line 44 of file test_time.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 300 of file test_time.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 300 of file test_time.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 300 of file test_time.c.

◆ AST_TEST_DEFINE() [1/5]

AST_TEST_DEFINE ( test_time_create_by_unit  )

Definition at line 170 of file test_time.c.

171{
172 struct timeval tv;
173
174 switch (cmd) {
175 case TEST_INIT:
176 info->name = "time_create_by_unit";
177 info->category = "/main/stdtime/";
178 info->summary = "Verify unit value to timeval conversions";
179 info->description = info->summary;
180 return AST_TEST_NOT_RUN;
181 case TEST_EXECUTE:
182 break;
183 }
184
185 /* Nominal */
186 ast_test_validate(test, ast_time_create_by_unit(1000, TIME_UNIT_NANOSECOND).tv_usec == 1);
187 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_MICROSECOND).tv_usec == 1);
188 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_MILLISECOND).tv_usec == 1000);
189 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_SECOND).tv_sec == 1);
190 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_MINUTE).tv_sec == 60);
191 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_HOUR).tv_sec == 3600);
192 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_DAY).tv_sec == 86400);
193 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_WEEK).tv_sec == 604800);
194 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_MONTH).tv_sec == 2629746);
195 ast_test_validate(test, ast_time_create_by_unit(1, TIME_UNIT_YEAR).tv_sec == 31556952);
196
197 /* timeval normalization */
199 ast_test_validate(test, tv.tv_sec == 1 && tv.tv_usec == 500000);
200
202 ast_test_validate(test, tv.tv_sec == 1 && tv.tv_usec == 500000);
203
205 ast_test_validate(test, tv.tv_sec == 1 && tv.tv_usec == 500000);
206
207 return AST_TEST_PASS;
208}
def info(msg)
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_NOT_RUN
Definition: test.h:194
@ TIME_UNIT_MONTH
Definition: time.h:348
@ TIME_UNIT_MICROSECOND
Definition: time.h:341
@ TIME_UNIT_WEEK
Definition: time.h:347
@ TIME_UNIT_MINUTE
Definition: time.h:344
@ TIME_UNIT_SECOND
Definition: time.h:343
@ TIME_UNIT_YEAR
Definition: time.h:349
@ TIME_UNIT_MILLISECOND
Definition: time.h:342
@ TIME_UNIT_HOUR
Definition: time.h:345
@ TIME_UNIT_DAY
Definition: time.h:346
@ TIME_UNIT_NANOSECOND
Definition: time.h:340
struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit)
Convert the given unit value, and create a timeval object from it.
Definition: time.c:113

References AST_TEST_NOT_RUN, AST_TEST_PASS, ast_time_create_by_unit(), sip_to_pjsip::info(), TEST_EXECUTE, TEST_INIT, TIME_UNIT_DAY, TIME_UNIT_HOUR, TIME_UNIT_MICROSECOND, TIME_UNIT_MILLISECOND, TIME_UNIT_MINUTE, TIME_UNIT_MONTH, TIME_UNIT_NANOSECOND, TIME_UNIT_SECOND, TIME_UNIT_WEEK, and TIME_UNIT_YEAR.

◆ AST_TEST_DEFINE() [2/5]

AST_TEST_DEFINE ( test_time_create_by_unit_str  )

Definition at line 210 of file test_time.c.

211{
212 struct timeval tv;
213
214 switch (cmd) {
215 case TEST_INIT:
216 info->name = "time_create_by_unit_str";
217 info->category = "/main/stdtime/";
218 info->summary = "Verify value with unit as a string to timeval conversions";
219 info->description = info->summary;
220 return AST_TEST_NOT_RUN;
221 case TEST_EXECUTE:
222 break;
223 }
224
225 /* Nominal */
226 ast_test_validate(test, ast_time_create_by_unit_str(1000, "ns").tv_usec == 1);
227 ast_test_validate(test, ast_time_create_by_unit_str(1, "us").tv_usec == 1);
228 ast_test_validate(test, ast_time_create_by_unit_str(1, "ms").tv_usec == 1000);
229 ast_test_validate(test, ast_time_create_by_unit_str(1, "s").tv_sec == 1);
230 ast_test_validate(test, ast_time_create_by_unit_str(1, "m").tv_sec == 60);
231 ast_test_validate(test, ast_time_create_by_unit_str(1, "h").tv_sec == 3600);
232 ast_test_validate(test, ast_time_create_by_unit_str(1, "d").tv_sec == 86400);
233 ast_test_validate(test, ast_time_create_by_unit_str(1, "w").tv_sec == 604800);
234 ast_test_validate(test, ast_time_create_by_unit_str(1, "mo").tv_sec == 2629746);
235 ast_test_validate(test, ast_time_create_by_unit_str(1, "yr").tv_sec == 31556952);
236
237 /* timeval normalization */
238 tv = ast_time_create_by_unit_str(1500000000, "ns");
239 ast_test_validate(test, tv.tv_sec == 1 && tv.tv_usec == 500000);
240
241 tv = ast_time_create_by_unit_str(1500000, "us");
242 ast_test_validate(test, tv.tv_sec == 1 && tv.tv_usec == 500000);
243
244 tv = ast_time_create_by_unit_str(1500, "ms");
245 ast_test_validate(test, tv.tv_sec == 1 && tv.tv_usec == 500000);
246
247 return AST_TEST_PASS;
248}
struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
Convert the given unit value, and create a timeval object from it.
Definition: time.c:143

References AST_TEST_NOT_RUN, AST_TEST_PASS, ast_time_create_by_unit_str(), sip_to_pjsip::info(), TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [3/5]

AST_TEST_DEFINE ( test_time_str_to_unit  )

Definition at line 118 of file test_time.c.

119{
120 switch (cmd) {
121 case TEST_INIT:
122 info->name = "time_str_to_unit";
123 info->category = "/main/stdtime/";
124 info->summary = "Verify string to time unit conversions";
125 info->description = info->summary;
126 return AST_TEST_NOT_RUN;
127 case TEST_EXECUTE:
128 break;
129 }
130
131 /* Nominal */
132 ast_test_validate(test, ast_time_str_to_unit("ns") == TIME_UNIT_NANOSECOND);
133 ast_test_validate(test, ast_time_str_to_unit("us") == TIME_UNIT_MICROSECOND);
134 ast_test_validate(test, ast_time_str_to_unit("ms") == TIME_UNIT_MILLISECOND);
135 ast_test_validate(test, ast_time_str_to_unit("s") == TIME_UNIT_SECOND);
136 ast_test_validate(test, ast_time_str_to_unit("m") == TIME_UNIT_MINUTE);
137 ast_test_validate(test, ast_time_str_to_unit("h") == TIME_UNIT_HOUR);
138 ast_test_validate(test, ast_time_str_to_unit("d") == TIME_UNIT_DAY);
139 ast_test_validate(test, ast_time_str_to_unit("w") == TIME_UNIT_WEEK);
140 ast_test_validate(test, ast_time_str_to_unit("mo") == TIME_UNIT_MONTH);
141 ast_test_validate(test, ast_time_str_to_unit("y") == TIME_UNIT_YEAR);
142
143 /* Plural */
144 ast_test_validate(test, ast_time_str_to_unit("nanoseconds") == TIME_UNIT_NANOSECOND);
145 ast_test_validate(test, ast_time_str_to_unit("microseconds") == TIME_UNIT_MICROSECOND);
146 ast_test_validate(test, ast_time_str_to_unit("milliseconds") == TIME_UNIT_MILLISECOND);
147 ast_test_validate(test, ast_time_str_to_unit("seconds") == TIME_UNIT_SECOND);
148 ast_test_validate(test, ast_time_str_to_unit("minutes") == TIME_UNIT_MINUTE);
149 ast_test_validate(test, ast_time_str_to_unit("hours") == TIME_UNIT_HOUR);
150 ast_test_validate(test, ast_time_str_to_unit("days") == TIME_UNIT_DAY);
151 ast_test_validate(test, ast_time_str_to_unit("weeks") == TIME_UNIT_WEEK);
152 ast_test_validate(test, ast_time_str_to_unit("months") == TIME_UNIT_MONTH);
153 ast_test_validate(test, ast_time_str_to_unit("years") == TIME_UNIT_YEAR);
154
155 /* Case */
156 ast_test_validate(test, ast_time_str_to_unit("Nsec") == TIME_UNIT_NANOSECOND);
157 ast_test_validate(test, ast_time_str_to_unit("Usec") == TIME_UNIT_MICROSECOND);
158 ast_test_validate(test, ast_time_str_to_unit("Msec") == TIME_UNIT_MILLISECOND);
159 ast_test_validate(test, ast_time_str_to_unit("Sec") == TIME_UNIT_SECOND);
160 ast_test_validate(test, ast_time_str_to_unit("Min") == TIME_UNIT_MINUTE);
161 ast_test_validate(test, ast_time_str_to_unit("Hr") == TIME_UNIT_HOUR);
162 ast_test_validate(test, ast_time_str_to_unit("Day") == TIME_UNIT_DAY);
163 ast_test_validate(test, ast_time_str_to_unit("Wk") == TIME_UNIT_WEEK);
164 ast_test_validate(test, ast_time_str_to_unit("Mth") == TIME_UNIT_MONTH);
165 ast_test_validate(test, ast_time_str_to_unit("Yr") == TIME_UNIT_YEAR);
166
167 return AST_TEST_PASS;
168}
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
Definition: time.c:66

References AST_TEST_NOT_RUN, AST_TEST_PASS, ast_time_str_to_unit(), sip_to_pjsip::info(), TEST_EXECUTE, TEST_INIT, TIME_UNIT_DAY, TIME_UNIT_HOUR, TIME_UNIT_MICROSECOND, TIME_UNIT_MILLISECOND, TIME_UNIT_MINUTE, TIME_UNIT_MONTH, TIME_UNIT_NANOSECOND, TIME_UNIT_SECOND, TIME_UNIT_WEEK, and TIME_UNIT_YEAR.

◆ AST_TEST_DEFINE() [4/5]

AST_TEST_DEFINE ( test_time_tv_to_usec  )

Definition at line 250 of file test_time.c.

251{
252 struct timeval tv;
253
254 switch (cmd) {
255 case TEST_INIT:
256 info->name = "time_tv_to_usec";
257 info->category = "/main/stdtime/";
258 info->summary = "Verify conversion of a timeval structure to microseconds";
259 info->description = info->summary;
260 return AST_TEST_NOT_RUN;
261 case TEST_EXECUTE:
262 break;
263 }
264
265 tv = ast_time_create(0, 0);
266 ast_test_validate(test, ast_time_tv_to_usec(&tv) == 0);
267
268 tv = ast_time_create(0, 1);
269 ast_test_validate(test, ast_time_tv_to_usec(&tv) == 1);
270
271 tv = ast_time_create(1, 0);
272 ast_test_validate(test, ast_time_tv_to_usec(&tv) == 1000000);
273
274 tv = ast_time_create(1, 1);
275 ast_test_validate(test, ast_time_tv_to_usec(&tv) == 1000001);
276
277 return AST_TEST_PASS;
278}
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
Definition: time.c:95
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
Definition: time.c:90

References AST_TEST_NOT_RUN, AST_TEST_PASS, ast_time_create(), ast_time_tv_to_usec(), sip_to_pjsip::info(), TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [5/5]

AST_TEST_DEFINE ( test_timezone_watch  )

Definition at line 48 of file test_time.c.

49{
50 const char *zones[] = { "America/Chicago", "America/New_York" };
51 int type, i, res = AST_TEST_PASS;
52 struct timeval tv = ast_tvnow();
53 struct ast_tm atm[ARRAY_LEN(zones)];
54 char tmpdir[] = "/tmp/timezone.XXXXXX";
55 char tzfile[50], syscmd[256];
56
57 switch (cmd) {
58 case TEST_INIT:
59 info->name = "timezone_watch";
60 info->category = "/main/stdtime/";
61 info->summary = "Verify deleting timezone file purges cache";
62 info->description =
63 "Verifies that the caching engine properly destroys a timezone entry when its file is deleted.";
64 return AST_TEST_NOT_RUN;
65 case TEST_EXECUTE:
66 break;
67 }
68
69 if (!mkdtemp(tmpdir)) {
70 ast_test_status_update(test, "Unable to create working directory: %s\n", strerror(errno));
71 return AST_TEST_NOT_RUN;
72 }
73 snprintf(tzfile, sizeof(tzfile), "%s/test", tmpdir);
74
75 for (type = 0; type <
76#ifdef SOLARIS
77 1 /* Solaris doesn't use symlinks for timezones */
78#else
79 2
80#endif
81 ; type++) {
82 ast_test_status_update(test, "Executing %s test...\n", type == 0 ? "deletion" : "symlink");
83 for (i = 0; i < ARRAY_LEN(zones); i++) {
84 int system_res;
85 snprintf(syscmd, sizeof(syscmd), "%s " TZDIR "/%s %s", type == 0 ? "cp" : "ln -sf", zones[i], tzfile);
86 if ((system_res = ast_safe_system(syscmd))) {
87 ast_log(LOG_WARNING, "system(%s) returned non-zero: %d\n", syscmd, system_res);
88 }
90 ast_test_status_update(test, "Querying timezone %s\n", tzfile);
91 ast_localtime(&tv, &atm[i], tzfile);
92 if (i != 0) {
93 if (atm[i].tm_hour == atm[i - 1].tm_hour) {
94 if (atm[i].tm_isdst == atm[i - 1].tm_isdst) {
95 res = AST_TEST_FAIL;
96 ast_test_status_update(test, "Failed %s test: %d(%s) = %d(%s)\n", type == 0 ? "deletion" : "symlink", atm[i].tm_hour, zones[i], atm[i-1].tm_hour, zones[i-1]);
97 } else {
98 ast_log(LOG_WARNING, "DST transition during %s test: %d(%s/%d) != %d(%s/%d)\n", type == 0 ? "deletion" : "symlink", atm[i].tm_hour, zones[i], atm[i].tm_isdst, atm[i-1].tm_hour, zones[i-1], atm[i-1].tm_isdst);
99 }
100 }
101 }
102
103 if (i + 1 != ARRAY_LEN(zones)) {
104 /* stat(2) only has resolution to 1 second - must wait, or the mtime is the same */
105 usleep(1100000);
106 }
107 }
108 }
109
110 snprintf(syscmd, sizeof(syscmd), "rm -rf %s", tmpdir);
111 if (ast_safe_system(syscmd)) {
112 ast_log(LOG_WARNING, "system(%s) returned non-zero.\n", syscmd);
113 }
114
115 return res;
116}
#define ast_log
Definition: astobj2.c:42
static const char type[]
Definition: chan_ooh323.c:109
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: extconf.c:829
char * mkdtemp(char *template_s)
#define LOG_WARNING
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
void ast_localtime_wakeup_monitor(struct ast_test *info)
Definition: localtime.c:795
int errno
int tm_hour
Definition: localtime.h:38
int tm_isdst
Definition: localtime.h:44
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
@ AST_TEST_FAIL
Definition: test.h:196
#define TZDIR
Definition: test_time.c:44
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_localtime(), ast_localtime_wakeup_monitor(), ast_log, ast_safe_system(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, ast_tvnow(), errno, sip_to_pjsip::info(), LOG_WARNING, mkdtemp(), TEST_EXECUTE, TEST_INIT, ast_tm::tm_hour, ast_tm::tm_isdst, type, and TZDIR.

◆ load_module()

static int load_module ( void  )
static

Definition at line 290 of file test_time.c.

291{
292 AST_TEST_REGISTER(test_timezone_watch);
293 AST_TEST_REGISTER(test_time_tv_to_usec);
294 AST_TEST_REGISTER(test_time_str_to_unit);
295 AST_TEST_REGISTER(test_time_create_by_unit);
296 AST_TEST_REGISTER(test_time_create_by_unit_str);
298}
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 280 of file test_time.c.

281{
282 AST_TEST_UNREGISTER(test_time_create_by_unit_str);
283 AST_TEST_UNREGISTER(test_time_create_by_unit);
284 AST_TEST_UNREGISTER(test_time_str_to_unit);
285 AST_TEST_UNREGISTER(test_time_tv_to_usec);
286 AST_TEST_UNREGISTER(test_timezone_watch);
287 return 0;
288}
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Time Tests" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 300 of file test_time.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 300 of file test_time.c.