summaryrefslogtreecommitdiff
path: root/services/gpuservice/tests/unittests/GpuServiceTest.cpp
blob: 62b3e53f5347dbff7d7bc5d2f64f6612247e46dd (plain)
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
#undef LOG_TAG
#define LOG_TAG "gpuservice_unittest"

#include "gpuservice/GpuService.h"

#include <gtest/gtest.h>
#include <log/log_main.h>

#include <chrono>
#include <thread>

namespace android {
namespace {

class GpuServiceTest : public testing::Test {
public:
    GpuServiceTest() {
        const ::testing::TestInfo* const test_info =
                ::testing::UnitTest::GetInstance()->current_test_info();
        ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
    }

    ~GpuServiceTest() {
        const ::testing::TestInfo* const test_info =
            ::testing::UnitTest::GetInstance()->current_test_info();
        ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
    }

};


/*
* The behaviour before this test + fixes was UB caused by threads accessing deallocated memory.
*
* This test creates the service (which initializes the culprit threads),
* deallocates it immediately and sleeps.
*
* GpuService's destructor gets called and joins the threads.
* If we haven't crashed by the time the sleep time has elapsed, we're good
* Let the test pass.
*/
TEST_F(GpuServiceTest, onInitializeShouldNotCauseUseAfterFree) {
    sp<GpuService> service = new GpuService();
    service.clear();
    std::this_thread::sleep_for(std::chrono::seconds(3));

    // If we haven't crashed yet due to threads accessing freed up memory, let the test pass
    EXPECT_TRUE(true);
}

} // namespace
} // namespace android