| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/test.h" |
| 35 |
#include "asterisk/utils.h" |
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/astobj2.h" |
| 38 |
|
| 39 |
static int indicator; |
| 40 |
static struct ast_test *current_test; |
| 41 |
AST_MUTEX_DEFINE_STATIC(the_lock); |
| 42 |
|
| 43 |
static void lock_it(ast_mutex_t *lock) |
| 44 |
{ |
| 45 |
indicator = 1; |
| 46 |
ast_mutex_lock(lock); |
| 47 |
} |
| 48 |
|
| 49 |
static void unlock_it(ast_mutex_t *lock) |
| 50 |
{ |
| 51 |
indicator = 0; |
| 52 |
ast_mutex_unlock(lock); |
| 53 |
} |
| 54 |
|
| 55 |
AST_TEST_DEFINE(lock_test) |
| 56 |
{ |
| 57 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 58 |
int i; |
| 59 |
|
| 60 |
switch(cmd) { |
| 61 |
case TEST_INIT: |
| 62 |
info->name = "lock_test"; |
| 63 |
info->category = "/main/lock/"; |
| 64 |
info->summary = "SCOPED_LOCK test"; |
| 65 |
info->description = |
| 66 |
"Tests that scoped locks are scoped as they are expected to be"; |
| 67 |
return AST_TEST_NOT_RUN; |
| 68 |
case TEST_EXECUTE: |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
current_test = test; |
| 73 |
indicator = 0; |
| 74 |
{ |
| 75 |
SCOPED_LOCK(lock, &the_lock, lock_it, unlock_it); |
| 76 |
if (indicator != 1) { |
| 77 |
ast_log(LOG_ERROR, "The lock was not acquired via RAII"); |
| 78 |
res = AST_TEST_FAIL; |
| 79 |
} |
| 80 |
} |
| 81 |
if (indicator != 0) { |
| 82 |
ast_log(LOG_ERROR, "The lock was not released when the variable went out of scope"); |
| 83 |
res = AST_TEST_FAIL; |
| 84 |
} |
| 85 |
|
| 86 |
for (i = 0; i < 10; ++i) { |
| 87 |
SCOPED_LOCK(lock, &the_lock, lock_it, unlock_it); |
| 88 |
if (indicator != 1) { |
| 89 |
ast_log(LOG_ERROR, "The lock was not acquired via RAII"); |
| 90 |
res = AST_TEST_FAIL; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (indicator != 0) { |
| 95 |
ast_log(LOG_ERROR, "The lock was not released when the variable went out of scope"); |
| 96 |
res = AST_TEST_FAIL; |
| 97 |
} |
| 98 |
|
| 99 |
return res; |
| 100 |
} |
| 101 |
|
| 102 |
struct test_struct |
| 103 |
{ |
| 104 |
int locked; |
| 105 |
int reffed; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static void test_lock(struct test_struct *test) |
| 116 |
{ |
| 117 |
ast_test_status_update(current_test, "Lock is occurring\n"); |
| 118 |
ao2_lock(test); |
| 119 |
if (test->reffed) { |
| 120 |
test->locked = 1; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void test_unlock(struct test_struct *test) |
| 132 |
{ |
| 133 |
ast_test_status_update(current_test, "Unlock is occurring\n"); |
| 134 |
ao2_unlock(test); |
| 135 |
if (test->reffed) { |
| 136 |
test->locked = 0; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static struct test_struct *test_ref(struct test_struct *test) |
| 148 |
{ |
| 149 |
ast_test_status_update(current_test, "Ref is occurring\n"); |
| 150 |
ao2_ref(test, +1); |
| 151 |
if (!test->locked) { |
| 152 |
test->reffed = 1; |
| 153 |
} |
| 154 |
return test; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
static void test_unref(struct test_struct *test) |
| 165 |
{ |
| 166 |
ast_test_status_update(current_test, "Unref is occurring\n"); |
| 167 |
ao2_ref(test, -1); |
| 168 |
if (!test->locked) { |
| 169 |
test->reffed = 0; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static struct test_struct *test_iterator_next(struct ao2_iterator *iter) |
| 180 |
{ |
| 181 |
struct test_struct *test = ao2_iterator_next(iter); |
| 182 |
|
| 183 |
if (!test) { |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
ao2_ref(test, -1); |
| 192 |
test_ref(test); |
| 193 |
|
| 194 |
return test; |
| 195 |
} |
| 196 |
|
| 197 |
AST_TEST_DEFINE(cleanup_order) |
| 198 |
{ |
| 199 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 200 |
struct ao2_iterator iter; |
| 201 |
struct test_struct *object_iter; |
| 202 |
RAII_VAR(struct ao2_container*, container, NULL, ao2_cleanup); |
| 203 |
RAII_VAR(struct test_struct *, object, NULL, ao2_cleanup); |
| 204 |
|
| 205 |
switch(cmd) { |
| 206 |
case TEST_INIT: |
| 207 |
info->name = "cleanup_order_test"; |
| 208 |
info->category = "/main/lock/"; |
| 209 |
info->summary = "cleanup order test"; |
| 210 |
info->description = |
| 211 |
"Tests that variables with cleanup attributes are cleaned up\n" |
| 212 |
"in the reverse order they are declared."; |
| 213 |
return AST_TEST_NOT_RUN; |
| 214 |
case TEST_EXECUTE: |
| 215 |
break; |
| 216 |
} |
| 217 |
current_test = test; |
| 218 |
|
| 219 |
container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 13, NULL, NULL, NULL); |
| 220 |
object = ao2_alloc(sizeof(*object), NULL); |
| 221 |
if (!object || !container) { |
| 222 |
|
| 223 |
return AST_TEST_FAIL; |
| 224 |
} |
| 225 |
|
| 226 |
{ |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
RAII_VAR(struct test_struct *, object2, test_ref(object), test_unref); |
| 231 |
SCOPED_LOCK(lock, object, test_lock, test_unlock); |
| 232 |
if (!object->reffed || !object->locked) { |
| 233 |
ast_log(LOG_ERROR, "Test failed due to out of order initializations"); |
| 234 |
res = AST_TEST_FAIL; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (object->reffed || object->locked) { |
| 239 |
ast_log(LOG_ERROR, "Test failed due to out of order cleanups\n"); |
| 240 |
res = AST_TEST_FAIL; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
ao2_link(container, object); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
for (iter = ao2_iterator_init(container, 0); |
| 251 |
(object_iter = test_iterator_next(&iter)); |
| 252 |
test_unref(object_iter)) { |
| 253 |
SCOPED_LOCK(lock, object_iter, test_lock, test_unlock); |
| 254 |
if (!object->reffed || !object->locked) { |
| 255 |
ast_log(LOG_ERROR, "Test failed due to out of order initializations"); |
| 256 |
res = AST_TEST_FAIL; |
| 257 |
} |
| 258 |
} |
| 259 |
ao2_iterator_destroy(&iter); |
| 260 |
|
| 261 |
if (object->reffed || object->locked) { |
| 262 |
ast_log(LOG_ERROR, "Test failed due to out of order cleanups\n"); |
| 263 |
res = AST_TEST_FAIL; |
| 264 |
} |
| 265 |
|
| 266 |
return res; |
| 267 |
} |
| 268 |
|
| 269 |
static int unload_module(void) |
| 270 |
{ |
| 271 |
AST_TEST_UNREGISTER(lock_test); |
| 272 |
AST_TEST_UNREGISTER(cleanup_order); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
static int load_module(void) |
| 277 |
{ |
| 278 |
AST_TEST_REGISTER(lock_test); |
| 279 |
AST_TEST_REGISTER(cleanup_order); |
| 280 |
return AST_MODULE_LOAD_SUCCESS; |
| 281 |
} |
| 282 |
|
| 283 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SCOPED_LOCK test module"); |