aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/stack/ut/stack_ut.cpp
blob: 7b02d0cfd49fa5a663c0e2ba782f3ead058c2813 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <library/cpp/coroutine/engine/stack/stack.h>
#include <library/cpp/coroutine/engine/stack/stack_common.h>
#include <library/cpp/coroutine/engine/stack/stack_guards.h>
#include <library/cpp/coroutine/engine/stack/stack_utils.h>
#include <library/cpp/testing/gtest/gtest.h>


using namespace testing;

namespace NCoro::NStack::Tests {

    constexpr size_t StackSizeInPages = 4;

    template <class TGuard>
    class TStackFixture : public Test {
    protected: // methods
        TStackFixture()
            : Guard_(GetGuard<TGuard>())
            , StackSize_(StackSizeInPages * PageSize)
        {}

        void SetUp() override {
            ASSERT_TRUE(GetAlignedMemory(StackSizeInPages, RawMemory_, AlignedMemory_));
            Stack_ = MakeHolder<NDetails::TStack>(RawMemory_, AlignedMemory_, StackSize_, "test_stack");
            Guard_.Protect(AlignedMemory_, StackSize_, false);
        }

        void TearDown() override {
            Guard_.RemoveProtection(AlignedMemory_, StackSize_);
            free(Stack_->GetRawMemory());
            Stack_->Reset();
            EXPECT_EQ(Stack_->GetRawMemory(), nullptr);
        }

    protected: // data
        const TGuard& Guard_;
        const size_t StackSize_ = 0;
        char* RawMemory_ = nullptr;
        char* AlignedMemory_ = nullptr;
        THolder<NDetails::TStack> Stack_;
    };

    typedef Types<TCanaryGuard, TPageGuard> Implementations;
    TYPED_TEST_SUITE(TStackFixture, Implementations);

    TYPED_TEST(TStackFixture, PointersAndSize) {
        EXPECT_EQ(this->Stack_->GetRawMemory(), this->RawMemory_);
        EXPECT_EQ(this->Stack_->GetAlignedMemory(), this->AlignedMemory_);
        EXPECT_EQ(this->Stack_->GetSize(), this->StackSize_);
    }

    TYPED_TEST(TStackFixture, WriteStack) {
        auto workspace = this->Guard_.GetWorkspace(this->Stack_->GetAlignedMemory(), this->Stack_->GetSize());
        for (size_t i = 0; i < workspace.size(); i += 512) {
            workspace[i] = 42;
        }
        EXPECT_TRUE(this->Guard_.CheckOverride(this->Stack_->GetAlignedMemory(), this->Stack_->GetSize()));
    }

}