tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

tracing_strong_test.cc (3705B)


      1 // Copyright 2024 The Abseil 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 //     https://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 #include <tuple>
     15 #include <vector>
     16 
     17 #include "gmock/gmock.h"
     18 #include "gtest/gtest.h"
     19 #include "absl/base/attributes.h"
     20 #include "absl/base/config.h"
     21 #include "absl/base/internal/tracing.h"
     22 
     23 #if ABSL_HAVE_ATTRIBUTE_WEAK
     24 
     25 namespace {
     26 
     27 using ::testing::ElementsAre;
     28 
     29 using ::absl::base_internal::ObjectKind;
     30 
     31 enum Function { kWait, kContinue, kSignal, kObserved };
     32 
     33 using Record = std::tuple<Function, const void*, ObjectKind>;
     34 
     35 thread_local std::vector<Record>* tls_records = nullptr;
     36 
     37 }  // namespace
     38 
     39 namespace absl {
     40 ABSL_NAMESPACE_BEGIN
     41 namespace base_internal {
     42 
     43 // Strong extern "C" implementation.
     44 extern "C" {
     45 
     46 void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(const void* object,
     47                                                   ObjectKind kind) {
     48  if (tls_records != nullptr) {
     49    tls_records->push_back({kWait, object, kind});
     50  }
     51 }
     52 
     53 void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(const void* object,
     54                                                       ObjectKind kind) {
     55  if (tls_records != nullptr) {
     56    tls_records->push_back({kContinue, object, kind});
     57  }
     58 }
     59 
     60 void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(const void* object,
     61                                                     ObjectKind kind) {
     62  if (tls_records != nullptr) {
     63    tls_records->push_back({kSignal, object, kind});
     64  }
     65 }
     66 
     67 void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(const void* object,
     68                                                       ObjectKind kind) {
     69  if (tls_records != nullptr) {
     70    tls_records->push_back({kObserved, object, kind});
     71  }
     72 }
     73 
     74 }  // extern "C"
     75 
     76 }  // namespace base_internal
     77 ABSL_NAMESPACE_END
     78 }  // namespace absl
     79 
     80 namespace {
     81 
     82 TEST(TracingInternal, InvokesStrongFunctionWithNullptr) {
     83  std::vector<Record> records;
     84  tls_records = &records;
     85  auto kind = absl::base_internal::ObjectKind::kUnknown;
     86  absl::base_internal::TraceWait(nullptr, kind);
     87  absl::base_internal::TraceContinue(nullptr, kind);
     88  absl::base_internal::TraceSignal(nullptr, kind);
     89  absl::base_internal::TraceObserved(nullptr, kind);
     90  tls_records = nullptr;
     91 
     92  EXPECT_THAT(records, ElementsAre(Record{kWait, nullptr, kind},
     93                                   Record{kContinue, nullptr, kind},
     94                                   Record{kSignal, nullptr, kind},
     95                                   Record{kObserved, nullptr, kind}));
     96 }
     97 
     98 TEST(TracingInternal, InvokesStrongFunctionWithObjectAddress) {
     99  int object = 0;
    100  std::vector<Record> records;
    101  tls_records = &records;
    102  auto kind = absl::base_internal::ObjectKind::kUnknown;
    103  absl::base_internal::TraceWait(&object, kind);
    104  absl::base_internal::TraceContinue(&object, kind);
    105  absl::base_internal::TraceSignal(&object, kind);
    106  absl::base_internal::TraceObserved(&object, kind);
    107  tls_records = nullptr;
    108 
    109  EXPECT_THAT(records, ElementsAre(Record{kWait, &object, kind},
    110                                   Record{kContinue, &object, kind},
    111                                   Record{kSignal, &object, kind},
    112                                   Record{kObserved, &object, kind}));
    113 }
    114 
    115 }  // namespace
    116 
    117 #endif  // ABSL_HAVE_ATTRIBUTE_WEAK