2023-01-02 23:35:21 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
static bool mem_is_null(const void *block, size_t size)
|
2023-01-02 23:35:21 +00:00
|
|
|
{
|
|
|
|
const unsigned char *bytes = (const unsigned char *)block;
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if(bytes[i] != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Memory, BaseTypes)
|
|
|
|
{
|
|
|
|
void *aVoid[123];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aVoid, sizeof(aVoid));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aVoid, sizeof(aVoid)));
|
|
|
|
mem_zero(&aVoid, sizeof(aVoid));
|
|
|
|
EXPECT_TRUE(mem_is_null(aVoid, sizeof(aVoid)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aVoid, sizeof(aVoid));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aVoid, sizeof(aVoid)));
|
|
|
|
mem_zero(&aVoid[0], 123 * sizeof(void *));
|
|
|
|
EXPECT_TRUE(mem_is_null(aVoid, sizeof(aVoid)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aVoid, sizeof(aVoid));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aVoid, sizeof(aVoid)));
|
|
|
|
mem_zero(aVoid, sizeof(aVoid));
|
|
|
|
EXPECT_TRUE(mem_is_null(aVoid, sizeof(aVoid)));
|
|
|
|
|
|
|
|
int aInt[512];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aInt, sizeof(aInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aInt, sizeof(aInt)));
|
|
|
|
mem_zero(&aInt, sizeof(aInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aInt, sizeof(aInt)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aInt, sizeof(aInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aInt, sizeof(aInt)));
|
|
|
|
mem_zero(&aInt[0], sizeof(aInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aInt, sizeof(aInt)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aInt, sizeof(aInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aInt, sizeof(aInt)));
|
|
|
|
mem_zero(aInt, sizeof(aInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aInt, sizeof(aInt)));
|
|
|
|
|
|
|
|
int *apInt[512];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(apInt, sizeof(apInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(apInt, sizeof(apInt)));
|
|
|
|
mem_zero(&apInt, sizeof(apInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(apInt, sizeof(apInt)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(apInt, sizeof(apInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(apInt, sizeof(apInt)));
|
|
|
|
mem_zero(apInt, sizeof(apInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(apInt, sizeof(apInt)));
|
|
|
|
|
|
|
|
int aaInt[10][20];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaInt, sizeof(aaInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
mem_zero(&aaInt, sizeof(aaInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaInt, sizeof(aaInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
mem_zero(&aaInt[0], sizeof(aaInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaInt, sizeof(aaInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
mem_zero(&aaInt[0][0], sizeof(aaInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaInt, sizeof(aaInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
mem_zero(aaInt, sizeof(aaInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaInt, sizeof(aaInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
mem_zero(aaInt[0], sizeof(aaInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
|
|
|
|
|
|
|
|
int *aapInt[10][20];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapInt, sizeof(aapInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
|
|
|
|
mem_zero(&aapInt, sizeof(aapInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapInt, sizeof(aapInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
|
|
|
|
mem_zero(&aapInt[0], sizeof(aapInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapInt, sizeof(aapInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
|
|
|
|
mem_zero(aapInt, sizeof(aapInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapInt, sizeof(aapInt));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
|
|
|
|
mem_zero(aapInt[0], sizeof(aapInt));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
|
|
|
|
}
|
|
|
|
|
2023-01-08 15:27:25 +00:00
|
|
|
TEST(Memory, PodTypes)
|
2023-01-02 23:35:21 +00:00
|
|
|
{
|
|
|
|
NETADDR aAddr[123];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aAddr, sizeof(aAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aAddr, sizeof(aAddr)));
|
|
|
|
mem_zero(&aAddr, sizeof(aAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aAddr, sizeof(aAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aAddr, sizeof(aAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aAddr, sizeof(aAddr)));
|
|
|
|
mem_zero(&aAddr[0], sizeof(aAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aAddr, sizeof(aAddr)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aAddr, sizeof(aAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aAddr, sizeof(aAddr)));
|
|
|
|
mem_zero(aAddr, sizeof(aAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aAddr, sizeof(aAddr)));
|
|
|
|
|
|
|
|
NETADDR *apAddr[123];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(apAddr, sizeof(apAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
|
|
|
|
mem_zero((NETADDR **)apAddr, sizeof(apAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(apAddr, sizeof(apAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
|
|
|
|
mem_zero(&apAddr, sizeof(apAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(apAddr, sizeof(apAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
|
|
|
|
mem_zero(&apAddr[0], sizeof(apAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(apAddr, sizeof(apAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
|
|
|
|
mem_zero(apAddr, sizeof(apAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
|
|
|
|
|
|
|
|
// 2D arrays
|
|
|
|
NETADDR aaAddr[10][20];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaAddr, sizeof(aaAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
mem_zero(&aaAddr, sizeof(aaAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaAddr, sizeof(aaAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
mem_zero(&aaAddr[0], sizeof(aaAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaAddr, sizeof(aaAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
mem_zero(&aaAddr[0][0], sizeof(aaAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaAddr, sizeof(aaAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
mem_zero(aaAddr, sizeof(aaAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aaAddr, sizeof(aaAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
mem_zero(aaAddr[0], sizeof(aaAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
|
|
|
|
|
|
|
|
// 2D pointer arrays
|
|
|
|
NETADDR *aapAddr[10][20];
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapAddr, sizeof(aapAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
mem_zero(&aapAddr, sizeof(aapAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapAddr, sizeof(aapAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
mem_zero(&aapAddr[0], sizeof(aapAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapAddr, sizeof(aapAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
mem_zero(&aapAddr[0][0], sizeof(aapAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapAddr, sizeof(aapAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
mem_zero(aapAddr, sizeof(aapAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
2023-01-08 15:28:01 +00:00
|
|
|
secure_random_fill(aapAddr, sizeof(aapAddr));
|
2023-01-02 23:35:21 +00:00
|
|
|
EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
mem_zero(aapAddr[0], sizeof(aapAddr));
|
|
|
|
EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
|
|
|
|
}
|