OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_splitlines.cpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13#include "test_common.hpp"
14
16
17using namespace openvpn;
18
19const std::string short_text = "Lorem\nipsum\r\ndolor\n\r\nsit";
20const std::vector<std::string> short_lines{"Lorem\n", "ipsum\r\n", "dolor\n", "\r\n", "sit"};
21const std::vector<std::string> short_lines_trim{"Lorem", "ipsum", "dolor", "", "sit"};
22
23TEST(SplitLines, no_max_length_no_trim)
24{
26 size_t index = 0;
27 while (in(false))
28 {
29 ASSERT_EQ(in.line_ref(), short_lines[index++]);
30 }
31}
32
33TEST(SplitLines, next_no_max_length_no_trim)
34{
36 size_t index = 0;
37 std::string line;
39 while ((ret = in.next(line, false)) != SplitLines::S_EOF)
40 {
41 ASSERT_EQ(ret, SplitLines::S_OKAY);
42 ASSERT_EQ(line, short_lines[index++]);
43 }
44}
45
46TEST(SplitLines, no_max_length_trim)
47{
49 size_t index = 0;
50 while (in(true))
51 {
52 ASSERT_FALSE(in.line_overflow());
53 ASSERT_EQ(in.line_ref(), short_lines_trim[index++]);
54 }
55}
56
57TEST(SplitLines, next_no_max_length_trim)
58{
60 size_t index = 0;
61 std::string line;
63 while ((ret = in.next(line, true)) != SplitLines::S_EOF)
64 {
65 ASSERT_EQ(ret, SplitLines::S_OKAY);
66 ASSERT_EQ(line, short_lines_trim[index++]);
67 }
68}
69
70TEST(SplitLines, max_length)
71{
72 SplitLines in(short_text, 24);
73 size_t index = 0;
74 while (in(true))
75 {
76 ASSERT_FALSE(in.line_overflow());
77 ASSERT_EQ(in.line_ref(), short_lines_trim[index++]);
78 }
79}
80
81TEST(SplitLines, next_max_length)
82{
83 SplitLines in(short_text, 24);
84 size_t index = 0;
85 std::string line;
87 while ((ret = in.next(line, true)) != SplitLines::S_EOF)
88 {
89 ASSERT_EQ(ret, SplitLines::S_OKAY);
90 ASSERT_EQ(line, short_lines_trim[index++]);
91 }
92}
93
94TEST(SplitLines, max_length_overflow)
95{
97 ASSERT_TRUE(in(true));
98 ASSERT_TRUE(in.line_overflow());
99 ASSERT_THROW(in.line_ref(), SplitLines::overflow_error);
100}
101
102TEST(SplitLines, next_max_length_overflow)
103{
104 SplitLines in(short_text, 3);
105 std::string line;
106 ASSERT_EQ(in.next(line, true), SplitLines::S_ERROR);
107}
108
109TEST(SplitLines, moved_error)
110{
112 ASSERT_TRUE(in(true));
113 ASSERT_FALSE(in.line_overflow());
114 std::string line = in.line_move();
115 ASSERT_THROW(in.line_ref(), SplitLines::moved_error);
116}
bool line_overflow() const
Status next(std::string &ln, const bool trim=true)
std::string & line_ref()
@ S_ERROR
line was longer than allowed
@ S_EOF
no further characters are available
@ S_OKAY
next line was successfully read
std::string ret
const std::vector< std::string > short_lines
const std::vector< std::string > short_lines_trim
const std::string short_text
TEST(SplitLines, no_max_length_no_trim)