tor-browser

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

util_aligned_malloc_unittest.cc (2442B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 #include "scoped_ptrs_util.h"
      9 
     10 namespace nss_test {
     11 
     12 struct SomeContext {
     13  uint8_t some_buf[13];
     14  void *mem;
     15 };
     16 
     17 template <class T>
     18 struct ScopedDelete {
     19  void operator()(T *ptr) {
     20    if (ptr) {
     21      PORT_Free(ptr->mem);
     22    }
     23  }
     24 };
     25 typedef std::unique_ptr<SomeContext, ScopedDelete<SomeContext> >
     26    ScopedSomeContext;
     27 
     28 class AlignedMallocTest : public ::testing::Test,
     29                          public ::testing::WithParamInterface<size_t> {
     30 protected:
     31  ScopedSomeContext test_align_new(size_t alignment) {
     32    ScopedSomeContext ctx(PORT_ZNewAligned(SomeContext, alignment, mem));
     33    return ctx;
     34  };
     35  ScopedSomeContext test_align_alloc(size_t alignment) {
     36    void *mem = nullptr;
     37    ScopedSomeContext ctx((SomeContext *)PORT_ZAllocAligned(sizeof(SomeContext),
     38                                                            alignment, &mem));
     39    if (ctx) {
     40      ctx->mem = mem;
     41    }
     42    return ctx;
     43  }
     44 };
     45 
     46 TEST_P(AlignedMallocTest, TestNew) {
     47  size_t alignment = GetParam();
     48  ScopedSomeContext ctx = test_align_new(alignment);
     49  EXPECT_TRUE(ctx.get());
     50  EXPECT_EQ(0U, (uintptr_t)ctx.get() % alignment);
     51 }
     52 
     53 TEST_P(AlignedMallocTest, TestAlloc) {
     54  size_t alignment = GetParam();
     55  ScopedSomeContext ctx = test_align_alloc(alignment);
     56  EXPECT_TRUE(ctx.get());
     57  EXPECT_EQ(0U, (uintptr_t)ctx.get() % alignment);
     58 }
     59 
     60 class AlignedMallocTestBadSize : public AlignedMallocTest {};
     61 
     62 TEST_P(AlignedMallocTestBadSize, TestNew) {
     63  size_t alignment = GetParam();
     64  ScopedSomeContext ctx = test_align_new(alignment);
     65  EXPECT_FALSE(ctx.get());
     66 }
     67 
     68 TEST_P(AlignedMallocTestBadSize, TestAlloc) {
     69  size_t alignment = GetParam();
     70  ScopedSomeContext ctx = test_align_alloc(alignment);
     71  EXPECT_FALSE(ctx.get());
     72 }
     73 
     74 static const size_t kSizes[] = {1, 2, 4, 8, 16, 32, 64};
     75 static const size_t kBadSizes[] = {0, 7, 17, 24, 56};
     76 
     77 INSTANTIATE_TEST_SUITE_P(AllAligned, AlignedMallocTest,
     78                         ::testing::ValuesIn(kSizes));
     79 INSTANTIATE_TEST_SUITE_P(AllAlignedBadSize, AlignedMallocTestBadSize,
     80                         ::testing::ValuesIn(kBadSizes));
     81 
     82 }  // namespace nss_test