File: fuzz_tests.cpp

package info (click to toggle)
cpprest 2.10.18-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,896 kB
  • sloc: cpp: 70,952; sh: 275; makefile: 170; javascript: 147
file content (83 lines) | stat: -rw-r--r-- 2,355 bytes parent folder | download | duplicates (2)
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
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 * fuzz_tests.cpp
 *
 * Fuzz tests for the JSON library.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "cpprest/json.h"
#include "cpprest/containerstream.h"
#include "cpprest/filestream.h"
#include "unittestpp.h"

using namespace web;

namespace tests
{
namespace functional
{
namespace json_tests
{
#ifdef _WIN32

SUITE(json_fuzz_tests)
{
    std::string get_fuzzed_file_path()
    {
        std::string ipfile;

        if (UnitTest::GlobalSettings::Has("fuzzedinputfile"))
        {
            ipfile = UnitTest::GlobalSettings::Get("fuzzedinputfile");
        }

        return ipfile;
    }

    TEST(fuzz_json_parser, "Requires", "fuzzedinputfile")
    {
        std::wstring ipfile = utility::conversions::to_utf16string(get_fuzzed_file_path());
        if (true == ipfile.empty())
        {
            VERIFY_IS_TRUE(false, "Input file is empty");
            return;
        }

        auto fs = Concurrency::streams::file_stream<uint8_t>::open_istream(ipfile).get();
        concurrency::streams::container_buffer<std::string> cbuf;
        fs.read_to_end(cbuf).get();
        fs.close().get();
        auto json_str = cbuf.collection();

        // Look for UTF-8 BOM
        if ((uint8_t)json_str[0] != 0xEF || (uint8_t)json_str[1] != 0xBB || (uint8_t)json_str[2] != 0xBF)
        {
            VERIFY_IS_TRUE(false, "Input file encoding is not UTF-8. Test will not parse the file.");
            return;
        }

        auto utf16_json_str = utility::conversions::utf8_to_utf16(json_str);
        // UTF8 to UTF16 conversion will retain the BOM, remove it.
        if (utf16_json_str.front() == 0xFEFF) utf16_json_str.erase(0, 1);

        try
        {
            json::value::parse(std::move(utf16_json_str));
            std::cout << "Input file parsed successfully.";
        }
        catch (const json::json_exception& ex)
        {
            std::cout << "json exception:" << ex.what();
        }
    }
}

#endif
} // namespace json_tests
} // namespace functional
} // namespace tests