0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-19 19:52:15 +02:00
openvpn3/test/unittests/test_splitlines.cpp
Frank Lichtenheld cb06f9e330
SplitLines/UserPass: Review fixes
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
2023-11-08 21:05:06 +01:00

125 lines
3.5 KiB
C++

// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2023 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
#include "test_common.h"
#include <openvpn/common/splitlines.hpp>
using namespace openvpn;
const std::string short_text = "Lorem\nipsum\r\ndolor\n\r\nsit";
const std::vector<std::string> short_lines{"Lorem\n", "ipsum\r\n", "dolor\n", "\r\n", "sit"};
const std::vector<std::string> short_lines_trim{"Lorem", "ipsum", "dolor", "", "sit"};
TEST(SplitLines, no_max_length_no_trim)
{
SplitLines in(short_text, 0);
size_t index = 0;
while (in(false))
{
ASSERT_EQ(in.line_ref(), short_lines[index++]);
}
}
TEST(SplitLines, next_no_max_length_no_trim)
{
SplitLines in(short_text, 0);
size_t index = 0;
std::string line;
SplitLines::Status ret = SplitLines::S_OKAY;
while ((ret = in.next(line, false)) != SplitLines::S_EOF)
{
ASSERT_EQ(ret, SplitLines::S_OKAY);
ASSERT_EQ(line, short_lines[index++]);
}
}
TEST(SplitLines, no_max_length_trim)
{
SplitLines in(short_text, 0);
size_t index = 0;
while (in(true))
{
ASSERT_FALSE(in.line_overflow());
ASSERT_EQ(in.line_ref(), short_lines_trim[index++]);
}
}
TEST(SplitLines, next_no_max_length_trim)
{
SplitLines in(short_text, 0);
size_t index = 0;
std::string line;
SplitLines::Status ret = SplitLines::S_OKAY;
while ((ret = in.next(line, true)) != SplitLines::S_EOF)
{
ASSERT_EQ(ret, SplitLines::S_OKAY);
ASSERT_EQ(line, short_lines_trim[index++]);
}
}
TEST(SplitLines, max_length)
{
SplitLines in(short_text, 24);
size_t index = 0;
while (in(true))
{
ASSERT_FALSE(in.line_overflow());
ASSERT_EQ(in.line_ref(), short_lines_trim[index++]);
}
}
TEST(SplitLines, next_max_length)
{
SplitLines in(short_text, 24);
size_t index = 0;
std::string line;
SplitLines::Status ret = SplitLines::S_OKAY;
while ((ret = in.next(line, true)) != SplitLines::S_EOF)
{
ASSERT_EQ(ret, SplitLines::S_OKAY);
ASSERT_EQ(line, short_lines_trim[index++]);
}
}
TEST(SplitLines, max_length_overflow)
{
SplitLines in(short_text, 3);
ASSERT_TRUE(in(true));
ASSERT_TRUE(in.line_overflow());
ASSERT_THROW(in.line_ref(), SplitLines::overflow_error);
}
TEST(SplitLines, next_max_length_overflow)
{
SplitLines in(short_text, 3);
std::string line;
ASSERT_EQ(in.next(line, true), SplitLines::S_ERROR);
}
TEST(SplitLines, moved_error)
{
SplitLines in(short_text);
ASSERT_TRUE(in(true));
ASSERT_FALSE(in.line_overflow());
std::string line = in.line_move();
ASSERT_THROW(in.line_ref(), SplitLines::moved_error);
}