| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "test_util.h" |
| 5 |
#include "../error.h" |
| 6 |
|
| 7 |
static struct drgn_error *no_recursion_allowed(int *counter) |
| 8 |
{ |
| 9 |
drgn_recursion_guard(0, "recursive call detected"); |
| 10 |
(*counter)++; |
| 11 |
return no_recursion_allowed(counter); |
| 12 |
} |
| 13 |
|
| 14 |
static struct drgn_error *limited_recursion_allowed(int *counter) |
| 15 |
{ |
| 16 |
drgn_recursion_guard(10, "maximum recursion depth exceeded"); |
| 17 |
(*counter)++; |
| 18 |
return limited_recursion_allowed(counter); |
| 19 |
} |
| 20 |
|
| 21 |
#suite recursion_guard |
| 22 |
|
| 23 |
#test no_recursion |
| 24 |
{ |
| 25 |
int counter = 0; |
| 26 |
struct drgn_error *err = no_recursion_allowed(&counter); |
| 27 |
ck_assert_ptr_nonnull(err); |
| 28 |
ck_assert_int_eq(err->code, DRGN_ERROR_RECURSION); |
| 29 |
ck_assert_str_eq(err->message, "recursive call detected"); |
| 30 |
drgn_error_destroy(err); |
| 31 |
ck_assert_int_eq(counter, 1); |
| 32 |
} |
| 33 |
|
| 34 |
#test limited_recursion |
| 35 |
{ |
| 36 |
int counter = 0; |
| 37 |
struct drgn_error *err = limited_recursion_allowed(&counter); |
| 38 |
ck_assert_ptr_nonnull(err); |
| 39 |
ck_assert_int_eq(err->code, DRGN_ERROR_RECURSION); |
| 40 |
ck_assert_str_eq(err->message, "maximum recursion depth exceeded"); |
| 41 |
drgn_error_destroy(err); |
| 42 |
ck_assert_int_eq(counter, 11); |
| 43 |
} |