Path: blob/master/libraries/AP_Common/tests/test_expandingstring_failure.cpp
9418 views
#include <AP_gtest.h>1#include <stdlib.h>2#include <AP_Common/ExpandingString.h>3#include <AP_HAL/AP_HAL.h>45/**6* This file test realloc failure on ExpandingString7*/89const AP_HAL::HAL& hal = AP_HAL::get_HAL();1011static uint32_t count = 0;1213void * WEAK mem_realloc(void *ptr, size_t old_size, size_t new_size)14{15if (++count >= 3) {16return nullptr;17}1819if (new_size == 0) {20free(ptr);21return nullptr;22}2324if (ptr == nullptr) {25return malloc(new_size);26}2728void *new_ptr = malloc(new_size);29if (new_ptr != nullptr) {30size_t copy_size = new_size > old_size ? old_size : new_size;31memcpy(new_ptr, ptr, copy_size);32free(ptr);33}3435return new_ptr;36}373839// THAT IS UGLY HACK BUT IT WORKS ... it is just used to make print_vprintf return negative value.40class BufferPrinter : public AP_HAL::BetterStream {41public:42BufferPrinter(char* str, size_t size) :43_offs(0), _str(str), _size(size) {}4445size_t write(uint8_t c) override { return 1; }46size_t write(const uint8_t *buffer, size_t size) override { return 1; }4748size_t _offs;49char* const _str;50const size_t _size;5152uint32_t available() override { return 0; }53bool read(uint8_t &b) override { return false; }54uint32_t txspace() override { return 0; }55bool discard_input() override { return false; }56};5758void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap);59void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap) {60BufferPrinter* p = static_cast<BufferPrinter*>(s);61if (count < 2) {62p->_offs = -1;63return;64}65if (count == 2) {66p->_offs = p->_size * 2;67} else {68p->_offs = p->_size;69}70return;71}7273TEST(ExpandingString, Tests)74{75// Test print_vprintf failure.76ExpandingString *test_string = NEW_NOTHROW ExpandingString();77test_string->printf("Test\n");78EXPECT_STREQ("", test_string->get_string());79EXPECT_STREQ("", test_string->get_writeable_string());80EXPECT_EQ(0u, test_string->get_length());81EXPECT_FALSE(test_string->has_failed_allocation());82// test failure on second printf expand()83test_string = NEW_NOTHROW ExpandingString();84test_string->printf("Test\n");85EXPECT_STREQ("", test_string->get_string());86EXPECT_STREQ("", test_string->get_writeable_string());87EXPECT_EQ(0u, test_string->get_length());88EXPECT_TRUE(test_string->has_failed_allocation());89// Test realloc failure90test_string = NEW_NOTHROW ExpandingString();91test_string->printf("Test\n");92EXPECT_STREQ(nullptr, test_string->get_string());93EXPECT_STREQ(nullptr, test_string->get_writeable_string());94EXPECT_EQ(0u, test_string->get_length());95EXPECT_TRUE(test_string->has_failed_allocation());96// test append failure97EXPECT_FALSE(test_string->append("Test2\n", 6));98// test failure on first printf realloc99test_string->printf("Test\n");100EXPECT_STREQ(nullptr, test_string->get_string());101EXPECT_STREQ(nullptr, test_string->get_writeable_string());102EXPECT_EQ(0u, test_string->get_length());103EXPECT_TRUE(test_string->has_failed_allocation());104// test failure on append realloc105test_string = NEW_NOTHROW ExpandingString();106EXPECT_FALSE(test_string->append("Test2\n", 6));107EXPECT_TRUE(test_string->has_failed_allocation());108EXPECT_STREQ(nullptr, test_string->get_string());109EXPECT_EQ(0u, test_string->get_length());110111test_string->~ExpandingString();112EXPECT_STRNE("Test\n", test_string->get_string());113}114115TEST(ExpandingString, TestsFailure)116{117118119}120AP_GTEST_MAIN()121122123