gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  #include <cerrno>
    21  
    22  namespace gvisor {
    23  namespace testing {
    24  
    25  void CheckFailure(const char* cond, size_t cond_size, const char* msg,
    26                    size_t msg_size, int errno_value);
    27  
    28  // If cond is false, aborts the current process.
    29  //
    30  // This macro is async-signal-safe.
    31  #define TEST_CHECK(cond)                                                       \
    32    do {                                                                         \
    33      if (!(cond)) {                                                             \
    34        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, nullptr, \
    35                                              0, 0);                             \
    36      }                                                                          \
    37    } while (0)
    38  
    39  // If cond is false, logs msg then aborts the current process.
    40  //
    41  // This macro is async-signal-safe.
    42  #define TEST_CHECK_MSG(cond, msg)                                          \
    43    do {                                                                     \
    44      if (!(cond)) {                                                         \
    45        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, msg, \
    46                                              sizeof(msg) - 1, 0);           \
    47      }                                                                      \
    48    } while (0)
    49  
    50  // If cond is false, logs errno, then aborts the current process.
    51  //
    52  // This macro is async-signal-safe.
    53  #define TEST_PCHECK(cond)                                                      \
    54    do {                                                                         \
    55      if (!(cond)) {                                                             \
    56        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, nullptr, \
    57                                              0, errno);                         \
    58      }                                                                          \
    59    } while (0)
    60  
    61  // If cond is false, logs msg and errno, then aborts the current process.
    62  //
    63  // This macro is async-signal-safe.
    64  #define TEST_PCHECK_MSG(cond, msg)                                         \
    65    do {                                                                     \
    66      if (!(cond)) {                                                         \
    67        ::gvisor::testing::CheckFailure(#cond, sizeof(#cond) - 1, msg, \
    68                                              sizeof(msg) - 1, errno);       \
    69      }                                                                      \
    70    } while (0)
    71  
    72  // expr must return PosixErrorOr<T>. The current process is aborted if
    73  // !PosixError<T>.ok().
    74  //
    75  // This macro is async-signal-safe.
    76  #define TEST_CHECK_NO_ERRNO(expr)               \
    77    ({                                            \
    78      auto _expr_result = (expr);                 \
    79      if (!_expr_result.ok()) {                   \
    80        ::gvisor::testing::CheckFailure(    \
    81            #expr, sizeof(#expr) - 1, nullptr, 0, \
    82            _expr_result.error().errno_value());  \
    83      }                                           \
    84    })
    85  
    86  // expr must return PosixErrorOr<T>. The current process is aborted if
    87  // !PosixError<T>.ok(). Otherwise, PosixErrorOr<T> value is returned.
    88  //
    89  // This macro is async-signal-safe.
    90  #define TEST_CHECK_NO_ERRNO_AND_VALUE(expr)     \
    91    ({                                            \
    92      auto _expr_result = (expr);                 \
    93      if (!_expr_result.ok()) {                   \
    94        ::gvisor::testing::CheckFailure(    \
    95            #expr, sizeof(#expr) - 1, nullptr, 0, \
    96            _expr_result.error().errno_value());  \
    97      }                                           \
    98      std::move(_expr_result).ValueOrDie();       \
    99    })
   100  
   101  // cond must be greater or equal than 0. Used to test result of syscalls.
   102  //
   103  // This macro is async-signal-safe.
   104  #define TEST_CHECK_SUCCESS(cond) TEST_PCHECK((cond) >= 0)
   105  
   106  // cond must be -1 and errno must match errno_value. Used to test errors from
   107  // syscalls.
   108  //
   109  // This macro is async-signal-safe.
   110  #define TEST_CHECK_ERRNO(cond, errno_value)                                   \
   111    do {                                                                        \
   112      TEST_PCHECK((cond) == -1);                                                \
   113      TEST_PCHECK_MSG(errno == (errno_value), #cond " expected " #errno_value); \
   114    } while (0)
   115  
   116  }  // namespace testing
   117  }  // namespace gvisor
   118  
   119  #endif  // GVISOR_TEST_UTIL_LOGGING_H_