Newer
Older
Joshua Colp
committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
*
* This is called when an async query completes, either because it resolved or
* because it was canceled. In our case, this callback is used to signal to the
* test that it can continue
*
* \param query The DNS query that has completed
*/
static void async_callback(const struct ast_dns_query *query)
{
struct async_resolution_data *async_data = ast_dns_query_get_data(query);
ast_mutex_lock(&async_data->lock);
async_data->complete = 1;
ast_cond_signal(&async_data->cond);
ast_mutex_unlock(&async_data->lock);
}
AST_TEST_DEFINE(resolver_resolve_async)
{
RAII_VAR(struct async_resolution_data *, async_data, NULL, ao2_cleanup);
RAII_VAR(struct ast_dns_query_active *, active, NULL, ao2_cleanup);
struct ast_dns_result *result;
enum ast_test_result_state res = AST_TEST_PASS;
struct timespec timeout;
switch (cmd) {
case TEST_INIT:
info->name = "resolver_resolve_async";
info->category = "/main/dns/";
info->summary = "Test a nominal asynchronous DNS resolution";
info->description =
"This test performs an asynchronous DNS resolution of a domain. The goal of this\n"
"test is not to check the records for accuracy. Rather, the goal is to ensure that\n"
"the resolver is called into as expected, that we regain control before the query\n"
"is completed, and to ensure that nothing tried to cancel the resolution.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (ast_dns_resolver_register(&test_resolver)) {
ast_test_status_update(test, "Unable to register test resolver\n");
return AST_TEST_FAIL;
}
resolver_data_init();
async_data = async_data_alloc();
if (!async_data) {
ast_test_status_update(test, "Failed to allocate asynchronous data\n");
res = AST_TEST_FAIL;
goto cleanup;
}
active = ast_dns_resolve_async("asterisk.org", T_A, C_IN, async_callback, async_data);
Joshua Colp
committed
if (!active) {
ast_test_status_update(test, "Asynchronous resolution of address failed\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (!test_resolver_data.resolve_called) {
ast_test_status_update(test, "DNS resolution did not call resolver's resolve() method\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (test_resolver_data.canceled) {
ast_test_status_update(test, "Resolver's cancel() method called for no reason\n");
res = AST_TEST_FAIL;
goto cleanup;
}
Joshua Colp
committed
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
timeout.tv_sec += 10;
ast_mutex_lock(&async_data->lock);
while (!async_data->complete) {
if (ast_cond_timedwait(&async_data->cond, &async_data->lock, &timeout) == ETIMEDOUT) {
break;
}
}
ast_mutex_unlock(&async_data->lock);
if (!async_data->complete) {
ast_test_status_update(test, "Asynchronous resolution timed out\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (!test_resolver_data.resolution_complete) {
ast_test_status_update(test, "Asynchronous resolution completed early?\n");
res = AST_TEST_FAIL;
goto cleanup;
}
result = ast_dns_query_get_result(active->query);
if (!result) {
ast_test_status_update(test, "Asynchronous resolution yielded no result\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (!ast_dns_result_get_records(result)) {
ast_test_status_update(test, "Asynchronous result had no records\n");
res = AST_TEST_FAIL;
goto cleanup;
}
cleanup:
ast_dns_resolver_unregister(&test_resolver);
resolver_data_cleanup();
return res;
}
/*! Stub async resolution callback */
static void stub_callback(const struct ast_dns_query *query)
{
return;
}
AST_TEST_DEFINE(resolver_resolve_async_off_nominal)
{
struct ast_dns_resolver terrible_resolver = {
.name = "Ed Wood's Filmography",
.priority = 0,
.resolve = fail_resolve,
.cancel = stub_cancel,
};
struct dns_resolve_data {
const char *name;
int rr_type;
int rr_class;
ast_dns_resolve_callback callback;
} resolves [] = {
{ NULL, T_A, C_IN, stub_callback },
{ "asterisk.org", -1, C_IN, stub_callback },
{ "asterisk.org", 65536 + 1, C_IN, stub_callback },
{ "asterisk.org", T_A, -1, stub_callback },
{ "asterisk.org", T_A, 65536 + 1, stub_callback },
{ "asterisk.org", T_A, C_IN, NULL },
Joshua Colp
committed
};
struct ast_dns_query_active *active;
enum ast_test_result_state res = AST_TEST_PASS;
int i;
switch (cmd) {
case TEST_INIT:
info->name = "resolver_resolve_async_off_nominal";
info->category = "/main/dns/";
info->summary = "Test off-nominal asynchronous DNS resolution";
info->description =
"This test performs several off-nominal asynchronous DNS resolutions:\n"
"\t* Attempt resolution with NULL name\n"
"\t* Attempt resolution with invalid RR type\n"
"\t* Attempt resolution with invalid RR class\n"
"\t* Attempt resolution with NULL callback pointer\n"
"\t* Attempt resolution with resolver that returns an error";
Joshua Colp
committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (ast_dns_resolver_register(&test_resolver)) {
ast_test_status_update(test, "Failed to register test resolver\n");
return AST_TEST_FAIL;
}
for (i = 0; i < ARRAY_LEN(resolves); ++i) {
active = ast_dns_resolve_async(resolves[i].name, resolves[i].rr_type, resolves[i].rr_class,
resolves[i].callback, NULL);
if (active) {
ast_test_status_update(test, "Successfully performed asynchronous resolution with invalid data\n");
ao2_ref(active, -1);
res = AST_TEST_FAIL;
}
}
ast_dns_resolver_unregister(&test_resolver);
if (ast_dns_resolver_register(&terrible_resolver)) {
ast_test_status_update(test, "Failed to register the DNS resolver\n");
return AST_TEST_FAIL;
}
active = ast_dns_resolve_async("asterisk.org", T_A, C_IN, stub_callback, NULL);
Joshua Colp
committed
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
ast_dns_resolver_unregister(&terrible_resolver);
if (active) {
ast_test_status_update(test, "Successfully performed asynchronous resolution with invalid data\n");
ao2_ref(active, -1);
return AST_TEST_FAIL;
}
return res;
}
AST_TEST_DEFINE(resolver_resolve_async_cancel)
{
RAII_VAR(struct async_resolution_data *, async_data, NULL, ao2_cleanup);
RAII_VAR(struct ast_dns_query_active *, active, NULL, ao2_cleanup);
struct ast_dns_result *result;
enum ast_test_result_state res = AST_TEST_PASS;
struct timespec timeout;
switch (cmd) {
case TEST_INIT:
info->name = "resolver_resolve_async_cancel";
info->category = "/main/dns/";
info->summary = "Test canceling an asynchronous DNS resolution";
info->description =
"This test performs an asynchronous DNS resolution of a domain and then cancels\n"
"the resolution. The goal of this test is to ensure that the cancel() callback of\n"
"the resolver is called and that it properly interrupts the resolution such that no\n"
"records are returned.";
Joshua Colp
committed
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (ast_dns_resolver_register(&test_resolver)) {
ast_test_status_update(test, "Unable to register test resolver\n");
return AST_TEST_FAIL;
}
resolver_data_init();
async_data = async_data_alloc();
if (!async_data) {
ast_test_status_update(test, "Failed to allocate asynchronous data\n");
res = AST_TEST_FAIL;
goto cleanup;
}
active = ast_dns_resolve_async("asterisk.org", T_A, C_IN, async_callback, async_data);
Joshua Colp
committed
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
if (!active) {
ast_test_status_update(test, "Asynchronous resolution of address failed\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (!test_resolver_data.resolve_called) {
ast_test_status_update(test, "DNS resolution did not call resolver's resolve() method\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (test_resolver_data.canceled) {
ast_test_status_update(test, "Resolver's cancel() method called for no reason\n");
res = AST_TEST_FAIL;
goto cleanup;
}
ast_dns_resolve_cancel(active);
if (!test_resolver_data.canceled) {
ast_test_status_update(test, "Resolver's cancel() method was not called\n");
res = AST_TEST_FAIL;
goto cleanup;
}
Joshua Colp
committed
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
timeout.tv_sec += 10;
ast_mutex_lock(&async_data->lock);
while (!async_data->complete) {
if (ast_cond_timedwait(&async_data->cond, &async_data->lock, &timeout) == ETIMEDOUT) {
break;
}
}
ast_mutex_unlock(&async_data->lock);
if (!async_data->complete) {
ast_test_status_update(test, "Asynchronous resolution timed out\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (test_resolver_data.resolution_complete) {
ast_test_status_update(test, "Resolution completed without cancelation\n");
res = AST_TEST_FAIL;
goto cleanup;
}
result = ast_dns_query_get_result(active->query);
if (result) {
ast_test_status_update(test, "Canceled resolution had a result\n");
res = AST_TEST_FAIL;
goto cleanup;
}
cleanup:
ast_dns_resolver_unregister(&test_resolver);
resolver_data_cleanup();
return res;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(resolver_register_unregister);
AST_TEST_UNREGISTER(resolver_register_off_nominal);
AST_TEST_UNREGISTER(resolver_unregister_off_nominal);
AST_TEST_UNREGISTER(resolver_data);
AST_TEST_UNREGISTER(resolver_set_result);
AST_TEST_UNREGISTER(resolver_set_result_off_nominal);
AST_TEST_UNREGISTER(resolver_add_record);
AST_TEST_UNREGISTER(resolver_add_record_off_nominal);
AST_TEST_UNREGISTER(resolver_resolve_sync);
AST_TEST_UNREGISTER(resolver_resolve_sync_off_nominal);
AST_TEST_UNREGISTER(resolver_resolve_async);
AST_TEST_UNREGISTER(resolver_resolve_async_off_nominal);
AST_TEST_UNREGISTER(resolver_resolve_async_cancel);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(resolver_register_unregister);
AST_TEST_REGISTER(resolver_register_off_nominal);
AST_TEST_REGISTER(resolver_unregister_off_nominal);
AST_TEST_REGISTER(resolver_data);
AST_TEST_REGISTER(resolver_set_result);
AST_TEST_REGISTER(resolver_set_result_off_nominal);
AST_TEST_REGISTER(resolver_add_record);
AST_TEST_REGISTER(resolver_add_record_off_nominal);
AST_TEST_REGISTER(resolver_resolve_sync);
AST_TEST_REGISTER(resolver_resolve_sync_off_nominal);
AST_TEST_REGISTER(resolver_resolve_async);
AST_TEST_REGISTER(resolver_resolve_async_off_nominal);
AST_TEST_REGISTER(resolver_resolve_async_cancel);
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DNS API Tests");