CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/tests/test_expandingstring_failure.cpp
Views: 1799
1
#include <AP_gtest.h>
2
#include <stdlib.h>
3
#include <AP_Common/ExpandingString.h>
4
#include <AP_HAL/AP_HAL.h>
5
6
/**
7
* This file test realloc failure on ExpandingString
8
*/
9
10
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
11
12
static uint32_t count = 0;
13
14
void *realloc(void *ptr, size_t new_size) {
15
count++;
16
if (count < 3) {
17
if (new_size == 0) {
18
free(ptr);
19
return nullptr;
20
}
21
if (ptr == nullptr) {
22
return malloc(new_size);
23
}
24
void *new_mem = malloc(new_size);
25
if (new_mem != nullptr) {
26
memcpy(new_mem, ptr, new_size);
27
free(ptr);
28
}
29
return new_mem;
30
} else {
31
return nullptr;
32
}
33
}
34
35
36
// THAT IS UGLY HACK BUT IT WORKS ... it is just used to make print_vprintf return negative value.
37
class BufferPrinter : public AP_HAL::BetterStream {
38
public:
39
BufferPrinter(char* str, size_t size) :
40
_offs(0), _str(str), _size(size) {}
41
42
size_t write(uint8_t c) override { return 1; }
43
size_t write(const uint8_t *buffer, size_t size) override { return 1; }
44
45
size_t _offs;
46
char* const _str;
47
const size_t _size;
48
49
uint32_t available() override { return 0; }
50
bool read(uint8_t &b) override { return false; }
51
uint32_t txspace() override { return 0; }
52
bool discard_input() override { return false; }
53
};
54
55
void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap);
56
void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap) {
57
BufferPrinter* p = static_cast<BufferPrinter*>(s);
58
if (count < 2) {
59
p->_offs = -1;
60
return;
61
}
62
if (count == 2) {
63
p->_offs = p->_size * 2;
64
} else {
65
p->_offs = p->_size;
66
}
67
return;
68
}
69
70
TEST(ExpandingString, Tests)
71
{
72
// Test print_vprintf failure.
73
ExpandingString *test_string = NEW_NOTHROW ExpandingString();
74
test_string->printf("Test\n");
75
EXPECT_STREQ("", test_string->get_string());
76
EXPECT_STREQ("", test_string->get_writeable_string());
77
EXPECT_EQ(0u, test_string->get_length());
78
EXPECT_FALSE(test_string->has_failed_allocation());
79
// test failure on second printf expand()
80
test_string = NEW_NOTHROW ExpandingString();
81
test_string->printf("Test\n");
82
EXPECT_STREQ("", test_string->get_string());
83
EXPECT_STREQ("", test_string->get_writeable_string());
84
EXPECT_EQ(0u, test_string->get_length());
85
EXPECT_TRUE(test_string->has_failed_allocation());
86
// Test realloc failure
87
test_string = NEW_NOTHROW ExpandingString();
88
test_string->printf("Test\n");
89
EXPECT_STREQ(nullptr, test_string->get_string());
90
EXPECT_STREQ(nullptr, test_string->get_writeable_string());
91
EXPECT_EQ(0u, test_string->get_length());
92
EXPECT_TRUE(test_string->has_failed_allocation());
93
// test append failure
94
EXPECT_FALSE(test_string->append("Test2\n", 6));
95
// test failure on first printf realloc
96
test_string->printf("Test\n");
97
EXPECT_STREQ(nullptr, test_string->get_string());
98
EXPECT_STREQ(nullptr, test_string->get_writeable_string());
99
EXPECT_EQ(0u, test_string->get_length());
100
EXPECT_TRUE(test_string->has_failed_allocation());
101
// test failure on append realloc
102
test_string = NEW_NOTHROW ExpandingString();
103
EXPECT_FALSE(test_string->append("Test2\n", 6));
104
EXPECT_TRUE(test_string->has_failed_allocation());
105
EXPECT_STREQ(nullptr, test_string->get_string());
106
EXPECT_EQ(0u, test_string->get_length());
107
108
test_string->~ExpandingString();
109
EXPECT_STRNE("Test\n", test_string->get_string());
110
}
111
112
TEST(ExpandingString, TestsFailure)
113
{
114
115
116
}
117
AP_GTEST_MAIN()
118
119