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