github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/util/logging.h (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #ifndef GVISOR_TEST_UTIL_LOGGING_H_
    16  #define GVISOR_TEST_UTIL_LOGGING_H_
    17  
    18  #include <stddef.h>
    19  
    20  namespace gvisor {
    21  namespace testing {
    22  
    23  void CheckFailure(const char* cond, size_t cond_size, const char* msg,
    24                    size_t msg_size, int errno_value);
    25  
    26  // If cond is false, aborts the current process.
    27  //
    28  // This macro is async-signal-safe.
    29  #define TEST_CHECK(cond)                                                       \
    30    do {                                                                         \
    31      if (!(cond)) {                                                             \
    32        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, nullptr, \
    33                                              0, 0);                             \
    34      }                                                                          \
    35    } while (0)
    36  
    37  // If cond is false, logs msg then aborts the current process.
    38  //
    39  // This macro is async-signal-safe.
    40  #define TEST_CHECK_MSG(cond, msg)                                          \
    41    do {                                                                     \
    42      if (!(cond)) {                                                         \
    43        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, msg, \
    44                                              sizeof(msg) - 1, 0);           \
    45      }                                                                      \
    46    } while (0)
    47  
    48  // If cond is false, logs errno, then aborts the current process.
    49  //
    50  // This macro is async-signal-safe.
    51  #define TEST_PCHECK(cond)                                                      \
    52    do {                                                                         \
    53      if (!(cond)) {                                                             \
    54        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, nullptr, \
    55                                              0, errno);                         \
    56      }                                                                          \
    57    } while (0)
    58  
    59  // If cond is false, logs msg and errno, then aborts the current process.
    60  //
    61  // This macro is async-signal-safe.
    62  #define TEST_PCHECK_MSG(cond, msg)                                         \
    63    do {                                                                     \
    64      if (!(cond)) {                                                         \
    65        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, msg, \
    66                                              sizeof(msg) - 1, errno);       \
    67      }                                                                      \
    68    } while (0)
    69  
    70  // expr must return PosixErrorOr<T>. The current process is aborted if
    71  // !PosixError<T>.ok().
    72  //
    73  // This macro is async-signal-safe.
    74  #define TEST_CHECK_NO_ERRNO(expr)               \
    75    ({                                            \
    76      auto _expr_result = (expr);                 \
    77      if (!_expr_result.ok()) {                   \
    78        ::gvisor::testing::CheckFailure(    \
    79            #expr, sizeof(#expr) - 1, nullptr, 0, \
    80            _expr_result.error().errno_value());  \
    81      }                                           \
    82    })
    83  
    84  // expr must return PosixErrorOr<T>. The current process is aborted if
    85  // !PosixError<T>.ok(). Otherwise, PosixErrorOr<T> value is returned.
    86  //
    87  // This macro is async-signal-safe.
    88  #define TEST_CHECK_NO_ERRNO_AND_VALUE(expr)     \
    89    ({                                            \
    90      auto _expr_result = (expr);                 \
    91      if (!_expr_result.ok()) {                   \
    92        ::gvisor::testing::CheckFailure(    \
    93            #expr, sizeof(#expr) - 1, nullptr, 0, \
    94            _expr_result.error().errno_value());  \
    95      }                                           \
    96      std::move(_expr_result).ValueOrDie();       \
    97    })
    98  
    99  // cond must be greater or equal than 0. Used to test result of syscalls.
   100  //
   101  // This macro is async-signal-safe.
   102  #define TEST_CHECK_SUCCESS(cond) TEST_PCHECK((cond) >= 0)
   103  
   104  // cond must be -1 and errno must match errno_value. Used to test errors from
   105  // syscalls.
   106  //
   107  // This macro is async-signal-safe.
   108  #define TEST_CHECK_ERRNO(cond, errno_value)                                   \
   109    do {                                                                        \
   110      TEST_PCHECK((cond) == -1);                                                \
   111      TEST_PCHECK_MSG(errno == (errno_value), #cond " expected " #errno_value); \
   112    } while (0)
   113  
   114  }  // namespace testing
   115  }  // namespace gvisor
   116  
   117  #endif  // GVISOR_TEST_UTIL_LOGGING_H_