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 (116 lines) | stat: -rw-r--r-- 3,325 bytes parent folder | download | duplicates (3)
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
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Fuzzing tests for streams read operations that involve parsing of data.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/
#include "stdafx.h"

using namespace concurrency::streams;

namespace tests
{
namespace functional
{
namespace streams
{
using namespace utility;
using namespace ::pplx;

SUITE(streams_fuzz_tests)
{
    std::string get_fuzzed_file_path(std::string requires_str)
    {
        std::string ipfile;

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

        return ipfile;
    }

    concurrency::streams::basic_istream<char> get_input_stream(std::string requires_str)
    {
        utility::string_t ipfile = utility::conversions::to_string_t(get_fuzzed_file_path(requires_str));
        concurrency::streams::basic_istream<char> ifs;

        if (true == ipfile.empty())
        {
            VERIFY_IS_TRUE(false, "Input file is empty");
            return ifs;
        }

        ifs = concurrency::streams::file_stream<char>::open_istream(ipfile, std::ios::in).get();

        // Look for UTF-8 BOM
        if (ifs.read().get() != 0xEF || ifs.read().get() != 0xBB || ifs.read().get() != 0xBF)
        {
            VERIFY_IS_TRUE(false, "Input file encoding is not UTF-8. Test will not parse the file.");
            ifs.close().get();
        }
        return ifs;
    }

    TEST(fuzz_read_line, "Requires", "fuzz_read_line_ipfile", "Timeout", "600000")
    {
        auto ifs = get_input_stream("fuzz_read_line_ipfile");
        if (!ifs.is_valid() || !ifs.is_open()) return;

        size_t num_lines = 0;
        while (false == ifs.is_eof())
        {
            container_buffer<std::vector<uint8_t>> buf;
            ifs.read_line(buf).get();
            num_lines++;
        }
        ifs.close().get();
        std::wcout << U("Number of lines read:") << num_lines;
    }

    template<class T>
    void extract(const basic_istream<char>& ifs)
    {
        try
        {
            ifs.extract<T>().get();
        }
        catch (std::exception)
        {
        }
        return;
    }

    TEST(fuzz_extract, "Requires", "fuzz_extract_ipfile", "Timeout", "600000")
    {
        auto ifs = get_input_stream("fuzz_extract_ipfile");
        if (!ifs.is_valid() || !ifs.is_open()) return;

        int num_lines = 0;
        while (false == ifs.is_eof())
        {
            extract<std::string>(ifs);
            extract<std::string>(ifs);
            extract<unsigned int>(ifs);
            extract<uint64_t>(ifs);
            extract<bool>(ifs);
            extract<std::string>(ifs);
            extract<int>(ifs);
            container_buffer<std::vector<uint8_t>> buf;
            ifs.read_line(buf).get();
            num_lines++;
        }
        ifs.close().get();
        std::wcout << L"Number of lines read:" << num_lines << std::endl;
    }

} // SUITE(streams_fuzz_tests)

} // namespace streams
} // namespace functional
} // namespace tests