mirror of
https://gitee.com/openharmony/commonlibrary_memory_utils
synced 2024-11-23 15:30:32 +00:00
commit
a763b7475d
29
libdmabufheap/test/BUILD.gn
Executable file
29
libdmabufheap/test/BUILD.gn
Executable file
@ -0,0 +1,29 @@
|
||||
# Copyright (C) 2022 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import("//build/test.gni")
|
||||
|
||||
module_output_path = "libdmabufheap/"
|
||||
|
||||
ohos_unittest("DmabufAllocTest") {
|
||||
module_out_path = module_output_path
|
||||
sources = [ "unittest/libdmabufheap/dmabuf_alloc_test.cpp" ]
|
||||
deps = [
|
||||
"//utils/memory/libdmabufheap:libdmabufheap",
|
||||
]
|
||||
}
|
||||
|
||||
group("unittest") {
|
||||
testonly = true
|
||||
deps = [ ":DmabufAllocTest" ]
|
||||
}
|
261
libdmabufheap/test/unittest/libdmabufheap/dmabuf_alloc_test.cpp
Normal file
261
libdmabufheap/test/unittest/libdmabufheap/dmabuf_alloc_test.cpp
Normal file
@ -0,0 +1,261 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/wait.h>
|
||||
#include <limits.h>
|
||||
#include "dmabuf_alloc.h"
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
|
||||
class DmabufAllocTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase();
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
};
|
||||
|
||||
void DmabufAllocTest::SetUpTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void DmabufAllocTest::TearDownTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void DmabufAllocTest::SetUp()
|
||||
{
|
||||
}
|
||||
|
||||
void DmabufAllocTest::TearDown()
|
||||
{
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, AllocSingleBuffer, TestSize.Level1)
|
||||
{
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = BUFFER_SIZE };
|
||||
ASSERT_EQ(0, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
void *ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_GE(sprintf((char *)ptr, "libdmabufheap"), 0);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_STREQ("libdmabufheap", (char *)ptr);
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, ShareBufferBetweenProcess, Function|MediumTest|Level1)
|
||||
{
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = BUFFER_SIZE };
|
||||
ASSERT_EQ(0, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
void *ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_GE(sprintf((char *)ptr, "parent"), 0);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
pid_t pid = fork();
|
||||
ASSERT_GE(pid, 0);
|
||||
/* child process */
|
||||
if (pid == 0) {
|
||||
ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_STREQ("parent", (char *)ptr);
|
||||
|
||||
ASSERT_GE(sprintf((char *)ptr, "child"), 0);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
/* parent process */
|
||||
waitpid(pid, NULL, 0);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_STREQ("child", (char *)ptr);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, OpenInvalidNameHeap, Function|MediumTest|Level1)
|
||||
{
|
||||
int i;
|
||||
std::string invalidName = "invalid";
|
||||
int heapFd = DmabufHeapOpen(invalidName.c_str());
|
||||
ASSERT_EQ(-EPERM, heapFd);
|
||||
|
||||
for (i = 0; i < 20; i++) {
|
||||
invalidName += "invalid";
|
||||
}
|
||||
heapFd = DmabufHeapOpen(invalidName.c_str());
|
||||
ASSERT_EQ(-EINVAL, heapFd);
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, AllocInvalidSizeBuffer, Function|MediumTest|Level1)
|
||||
{
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = 0 };
|
||||
ASSERT_EQ(-EINVAL, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, BufferSyncWithWrongFd, Function|MediumTest|Level1)
|
||||
{
|
||||
const unsigned int WRONG_FD = UINT_MAX;
|
||||
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = BUFFER_SIZE };
|
||||
ASSERT_EQ(0, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
void *ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_NE(0, DmabufHeapBufferSyncStart(WRONG_FD, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_NE(0, DmabufHeapBufferSyncEnd(WRONG_FD, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, BufferSyncWithWrongSyncType, Function|MediumTest|Level1)
|
||||
{
|
||||
const unsigned int WRONG_SYNC_TYPE = UINT_MAX;
|
||||
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = BUFFER_SIZE };
|
||||
ASSERT_EQ(0, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
void *ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_NE(0, DmabufHeapBufferSyncEnd(buffer.fd, (DmabufHeapBufferSyncType)WRONG_SYNC_TYPE));
|
||||
|
||||
ASSERT_NE(0, DmabufHeapBufferSyncStart(buffer.fd, (DmabufHeapBufferSyncType)WRONG_SYNC_TYPE));
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, SyncBufferTwice, Function|MediumTest|Level1)
|
||||
{
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = BUFFER_SIZE };
|
||||
ASSERT_EQ(0, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
void *ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_GE(sprintf((char *)ptr, "libdmabufheap"), 0);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_STREQ("libdmabufheap", (char *)ptr);
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
||||
|
||||
HWTEST_F(DmabufAllocTest, ExchangeBufferSyncOrder, Function|MediumTest|Level1)
|
||||
{
|
||||
int heapFd = DmabufHeapOpen("system");
|
||||
ASSERT_GE(heapFd, 0);
|
||||
|
||||
DmabufHeapBuffer buffer = { .size = BUFFER_SIZE };
|
||||
ASSERT_EQ(0, DmabufHeapBufferAlloc(heapFd, &buffer));
|
||||
|
||||
void *ptr = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, buffer.fd, 0);
|
||||
ASSERT_TRUE(ptr != NULL);
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncEnd(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferSyncStart(buffer.fd, DMA_BUF_HEAP_BUF_SYNC_RW));
|
||||
|
||||
ASSERT_EQ(0, munmap(ptr, BUFFER_SIZE));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapBufferFree(&buffer));
|
||||
|
||||
ASSERT_EQ(0, DmabufHeapClose(heapFd));
|
||||
}
|
@ -6,7 +6,9 @@
|
||||
"//utils/memory/libdmabufheap:libdmabufheap"
|
||||
],
|
||||
"inner_kits": [],
|
||||
"test_list": []
|
||||
"test_list": [
|
||||
"//utils/memory/libdmabufheap/test:unittest"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user