summaryrefslogtreecommitdiffstats
path: root/tests/src/exception-tests.cpp
blob: 318b2b85befa0ad8fa75da4d44084b88a09d1c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
 *
 * Copyright 2021-2023 Ribose Inc. (https://www.ribose.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "sexp-tests.h"

using namespace sexp;

namespace {
class ExceptionTests : public testing::Test {
  protected:
    static void do_scan_with_exception(const char *str_in, const char *msg)
    {
        try {
            std::istringstream  iss(str_in);
            sexp_input_stream_t is(&iss);
            is.set_byte_size(8)->get_char()->scan_object();
            FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
        } catch (sexp::sexp_exception_t &e) {
            EXPECT_STREQ(e.what(), msg);
        }
    }
};

TEST_F(ExceptionTests, UnexpectedEof)
{
    do_scan_with_exception("(4:This2:is1:a4:test",
                           "SEXP ERROR: unexpected end of file at position 20");
}

TEST_F(ExceptionTests, UnexpectedCharacter4bit)
{
    do_scan_with_exception(
      "(4:This2:is1:a4:test #)",
      "SEXP ERROR: character ')' found in 4-bit coding region at position 22");
}

TEST_F(ExceptionTests, IllegalCharacter)
{
    do_scan_with_exception("(This is a test ?)",
                           "SEXP ERROR: illegal character '?' (0x3f) at position 16");
}

TEST_F(ExceptionTests, UnexpectedEofAfterQoute)
{
    do_scan_with_exception("(\")\n", "SEXP ERROR: unexpected end of file at position 4");
}

TEST_F(ExceptionTests, IllegalCharacterBase64)
{
    do_scan_with_exception("(Test {KDQ6VGhpczI6aXMxOmE0OnRlc3Qq})",
                           "SEXP ERROR: illegal character '}' (0x7d) at position 35");
}

TEST_F(ExceptionTests, InvalidHex)
{
    do_scan_with_exception("(\"\\x1U\")",
                           "SEXP ERROR: Hex character \\x1... too short at position 5");
}

TEST_F(ExceptionTests, InvalidOctal)
{
    do_scan_with_exception("(\"\\12U\")",
                           "SEXP ERROR: Octal character \\12... too short at position 5");
}

TEST_F(ExceptionTests, TooBigOctal)
{
    do_scan_with_exception("(\"\\666U\")",
                           "SEXP ERROR: Octal character \\666... too big at position 5");
}

TEST_F(ExceptionTests, InvalidEscape)
{
    do_scan_with_exception("(\"\\?\")",
                           "SEXP ERROR: Unknown escape sequence \\? at position 3");
}

TEST_F(ExceptionTests, StringTooShortQuoted)
{
    do_scan_with_exception(
      "(4\"ABC\")",
      "SEXP ERROR: Declared length was 4, but quoted string ended too early at position 6");
}

TEST_F(ExceptionTests, StringTooShortBase64)
{
    sexp::sexp_exception_t::set_verbosity(sexp::sexp_exception_t::warning);
    do_scan_with_exception("(8|NDpBQkNE|)",
                           "SEXP WARNING: Base64 string has length 6 different than declared "
                           "length 8 at position 12");
    sexp::sexp_exception_t::set_verbosity(sexp::sexp_exception_t::error);
}

TEST_F(ExceptionTests, StringTooShortHex)
{
    sexp::sexp_exception_t::set_verbosity(sexp::sexp_exception_t::warning);
    do_scan_with_exception(
      "(8#AAABFCAD#)",
      "SEXP WARNING: Hex string has length 4 different than declared length 8 at position 12");
    sexp::sexp_exception_t::set_verbosity(sexp::sexp_exception_t::error);
}

TEST_F(ExceptionTests, StringBadLength)
{
    do_scan_with_exception("(1A:AAABFCAD)",
                           "SEXP ERROR: illegal character 'A' (0x41) at position 2");
}

TEST_F(ExceptionTests, DecimalTooLong)
{
    do_scan_with_exception("(1234567890:AAABFCAD)",
                           "SEXP ERROR: Decimal number is too long at position 11");
}

TEST_F(ExceptionTests, Base64CurlyBracket)
{
    // "ey..." in base64 encoding translates to "{..."
    do_scan_with_exception("({ey})", "SEXP ERROR: illegal character '{' (0x7b) at position 3");
}

TEST_F(ExceptionTests, UnusedBits)
{
    sexp::sexp_exception_t::set_verbosity(sexp::sexp_exception_t::warning);
    do_scan_with_exception(
      "(Test |AABBCCDD11|)",
      "SEXP WARNING: 6-bit region ended with 4 unused bits left-over at position 17");
    sexp::sexp_exception_t::set_verbosity(sexp::sexp_exception_t::error);
}

TEST_F(ExceptionTests, NotAListWhenExpected)
{
    try {
        std::istringstream iss(
          "|d738/4ghP9rFZ0gAIYZ5q9y6iskDJwASi5rEQpEQq8ZyMZeIZzIAR2I5iGE=|");
        sexp_input_stream_t is(&iss);

        sexp_list_t a_list;
        a_list.parse(is.set_byte_size(8)->get_char());
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(e.what(),
                     "SEXP ERROR: character '|' found where '(' was expected at position 0");
    }
}

TEST_F(ExceptionTests, InvalidByteSizeAndMode)
{
    try {
        std::istringstream             iss("(3:a\011c)");
        sexp_input_stream_t            is(&iss);
        std::shared_ptr<sexp_object_t> obj = is.set_byte_size(8)->get_char()->scan_object();

        std::ostringstream   oss(std::ios_base::binary);
        sexp_output_stream_t os(&oss);
        os.change_output_byte_size(4, sexp_output_stream_t::advanced)->print_advanced(obj);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(
          e.what(),
          "SEXP ERROR: Can't print in advanced mode with restricted output character set");
    }
}

TEST_F(ExceptionTests, SexpWarning)
{
    testing::internal::CaptureStdout();
    sexp::sexp_exception_t::set_interactive(true);
    sexp_error(sexp_exception_t::warning, "Test warning", 0, 0, 200);
    std::string output = testing::internal::GetCapturedStdout();
    EXPECT_EQ(output, "\n*** SEXP WARNING: Test warning at position 200 ***\n");
    sexp::sexp_exception_t::set_interactive(false);
}

static void do_parse_list_from_string(const char *str)
{
    std::istringstream  iss(str);
    sexp_input_stream_t is(&iss);
    sexp_list_t         lst;
    lst.parse(is.set_byte_size(8)->get_char());
}

static void do_parse_list_from_string_with_limit(const char *str, size_t m_depth)
{
    std::istringstream  iss(str);
    sexp_input_stream_t is(&iss, m_depth);
    sexp_list_t         lst;
    lst.parse(is.set_byte_size(8)->get_char());
}

TEST_F(ExceptionTests, MaxDepthParse)
{
    const char *depth_1 = "(sexp_list_1)";
    const char *depth_4 = "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4))))";
    const char *depth_4e = "(sexp_list_1 (sexp_list_2 (sexp_list_3 ())))";
    const char *depth_5 =
      "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4 (sexp_list_5)))))";
    const char *depth_5e = "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4 ()))))";

    do_parse_list_from_string(depth_1);
    do_parse_list_from_string(depth_4);
    do_parse_list_from_string(depth_4e);
    do_parse_list_from_string(depth_5);
    do_parse_list_from_string(depth_5e);

    do_parse_list_from_string_with_limit(depth_1, 4);
    do_parse_list_from_string_with_limit(depth_4, 4);
    do_parse_list_from_string_with_limit(depth_4e, 4);

    try {
        do_parse_list_from_string_with_limit(depth_5, 4);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(
          e.what(),
          "SEXP ERROR: Maximum allowed SEXP list depth (4) is exceeded at position 53");
    }

    try {
        do_parse_list_from_string_with_limit(depth_5e, 4);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(
          e.what(),
          "SEXP ERROR: Maximum allowed SEXP list depth (4) is exceeded at position 53");
    }
}

static void do_print_list_from_string(const char *str, bool advanced, size_t m_depth = 0)
{
    std::istringstream  iss(str);
    sexp_input_stream_t is(&iss);
    sexp_list_t         lst;
    lst.parse(is.set_byte_size(8)->get_char());

    std::ostringstream   oss(str);
    sexp_output_stream_t os(&oss, m_depth);
    if (advanced)
        lst.print_advanced(&os);
    else
        lst.print_canonical(&os);
}

TEST_F(ExceptionTests, MaxDepthPrintAdvanced)
{
    const char *depth_1 = "(sexp_list_1)";
    const char *depth_4 = "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4))))";
    const char *depth_4e = "(sexp_list_1 (sexp_list_2 (sexp_list_3 ())))";
    const char *depth_5 =
      "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4 (sexp_list_5)))))";
    const char *depth_5e = "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4 ()))))";

    do_print_list_from_string(depth_1, true);
    do_print_list_from_string(depth_4, true);
    do_print_list_from_string(depth_4e, true);
    do_print_list_from_string(depth_5, true);
    do_print_list_from_string(depth_5e, true);

    do_print_list_from_string(depth_1, true, 4);
    do_print_list_from_string(depth_4, true, 4);
    do_print_list_from_string(depth_4e, true, 4);

    try {
        do_print_list_from_string(depth_5, true, 4);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(e.what(), "SEXP ERROR: Maximum allowed SEXP list depth (4) is exceeded");
    }

    try {
        do_print_list_from_string(depth_5e, true, 4);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(e.what(), "SEXP ERROR: Maximum allowed SEXP list depth (4) is exceeded");
    }
}

TEST_F(ExceptionTests, MaxDepthPrintCanonical)
{
    const char *depth_1 = "(sexp_list_1)";
    const char *depth_4 = "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4))))";
    const char *depth_4e = "(sexp_list_1 (sexp_list_2 (sexp_list_3 ())))";
    const char *depth_5 =
      "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4 (sexp_list_5)))))";
    const char *depth_5e = "(sexp_list_1 (sexp_list_2 (sexp_list_3 (sexp_list_4 ()))))";

    do_print_list_from_string(depth_1, false);
    do_print_list_from_string(depth_4, false);
    do_print_list_from_string(depth_4e, false);
    do_print_list_from_string(depth_5, false);
    do_print_list_from_string(depth_5e, false);

    do_print_list_from_string(depth_1, false, 4);
    do_print_list_from_string(depth_4, false, 4);
    do_print_list_from_string(depth_4e, false, 4);

    try {
        do_print_list_from_string(depth_5, false, 4);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(e.what(), "SEXP ERROR: Maximum allowed SEXP list depth (4) is exceeded");
    }

    try {
        do_print_list_from_string(depth_5e, false, 4);
        FAIL() << "sexp::sexp_exception_t expected but has not been thrown";
    } catch (sexp::sexp_exception_t &e) {
        EXPECT_STREQ(e.what(), "SEXP ERROR: Maximum allowed SEXP list depth (4) is exceeded");
    }
}

} // namespace