File: read_test.h

package info (click to toggle)
prismlauncher 10.0.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,244 kB
  • sloc: cpp: 83,598; java: 1,293; python: 114; sh: 78; xml: 9; makefile: 7
file content (250 lines) | stat: -rw-r--r-- 8,926 bytes parent folder | download
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
/*
 * libnbt++ - A library for the Minecraft Named Binary Tag format.
 * Copyright (C) 2013, 2015  ljfa-ag
 *
 * This file is part of libnbt++.
 *
 * libnbt++ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * libnbt++ 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libnbt++.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <cxxtest/TestSuite.h>
#include "io/stream_reader.h"
#ifdef NBT_HAVE_ZLIB
#include "io/izlibstream.h"
#endif
#include "nbt_tags.h"
#include <iostream>
#include <fstream>
#include <sstream>

using namespace nbt;

#include "data.h"

class read_test : public CxxTest::TestSuite
{
public:
    void test_stream_reader_big()
    {
        std::string input{
            1, //tag_type::Byte
            0, //tag_type::End
            11, //tag_type::Int_Array

            0x0a, 0x0b, 0x0c, 0x0d, //0x0a0b0c0d in Big Endian

            0x00, 0x06, //String length in Big Endian
            'f', 'o', 'o', 'b', 'a', 'r',

            0 //tag_type::End (invalid with allow_end = false)
        };
        std::istringstream is(input);
        nbt::io::stream_reader reader(is);

        TS_ASSERT_EQUALS(&reader.get_istr(), &is);
        TS_ASSERT_EQUALS(reader.get_endian(), endian::big);

        TS_ASSERT_EQUALS(reader.read_type(), tag_type::Byte);
        TS_ASSERT_EQUALS(reader.read_type(true), tag_type::End);
        TS_ASSERT_EQUALS(reader.read_type(false), tag_type::Int_Array);

        int32_t i;
        reader.read_num(i);
        TS_ASSERT_EQUALS(i, 0x0a0b0c0d);

        TS_ASSERT_EQUALS(reader.read_string(), "foobar");

        TS_ASSERT_THROWS(reader.read_type(false), io::input_error);
        TS_ASSERT(!is);
        is.clear();

        //Test for invalid tag type 13
        is.str("\x0d");
        TS_ASSERT_THROWS(reader.read_type(), io::input_error);
        TS_ASSERT(!is);
        is.clear();

        //Test for unexpcted EOF on numbers (input too short for int32_t)
        is.str("\x03\x04");
        reader.read_num(i);
        TS_ASSERT(!is);
    }

    void test_stream_reader_little()
    {
        std::string input{
            0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, //0x0d0c0b0a09080706 in Little Endian

            0x06, 0x00, //String length in Little Endian
            'f', 'o', 'o', 'b', 'a', 'r',

            0x10, 0x00, //String length (intentionally too large)
            'a', 'b', 'c', 'd' //unexpected EOF
        };
        std::istringstream is(input);
        nbt::io::stream_reader reader(is, endian::little);

        TS_ASSERT_EQUALS(reader.get_endian(), endian::little);

        int64_t i;
        reader.read_num(i);
        TS_ASSERT_EQUALS(i, 0x0d0c0b0a09080706);

        TS_ASSERT_EQUALS(reader.read_string(), "foobar");

        TS_ASSERT_THROWS(reader.read_string(), io::input_error);
        TS_ASSERT(!is);
    }

    //Tests if comp equals an extended variant of Notch's bigtest NBT
    void verify_bigtest_structure(const tag_compound& comp)
    {
        TS_ASSERT_EQUALS(comp.size(), 13u);

        TS_ASSERT(comp.at("byteTest") == tag_byte(127));
        TS_ASSERT(comp.at("shortTest") == tag_short(32767));
        TS_ASSERT(comp.at("intTest") == tag_int(2147483647));
        TS_ASSERT(comp.at("longTest") == tag_long(9223372036854775807));
        TS_ASSERT(comp.at("floatTest") == tag_float(std::stof("0xff1832p-25"))); //0.4982315
        TS_ASSERT(comp.at("doubleTest") == tag_double(std::stod("0x1f8f6bbbff6a5ep-54"))); //0.493128713218231

        //From bigtest.nbt: "the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...)"
        tag_byte_array byteArrayTest;
        for(int n = 0; n < 1000; ++n)
            byteArrayTest.push_back((n*n*255 + n*7) % 100);
        TS_ASSERT(comp.at("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))") == byteArrayTest);

        TS_ASSERT(comp.at("stringTest") == tag_string("HELLO WORLD THIS IS A TEST STRING \u00C5\u00C4\u00D6!"));

        TS_ASSERT(comp.at("listTest (compound)") == tag_list::of<tag_compound>({
            {{"created-on", tag_long(1264099775885)}, {"name", "Compound tag #0"}},
            {{"created-on", tag_long(1264099775885)}, {"name", "Compound tag #1"}}
        }));
        TS_ASSERT(comp.at("listTest (long)") == tag_list::of<tag_long>({11, 12, 13, 14, 15}));
        TS_ASSERT(comp.at("listTest (end)") == tag_list());

        TS_ASSERT((comp.at("nested compound test") == tag_compound{
            {"egg", tag_compound{{"value", 0.5f},  {"name", "Eggbert"}}},
            {"ham", tag_compound{{"value", 0.75f}, {"name", "Hampus"}}}
        }));

        TS_ASSERT(comp.at("intArrayTest") == tag_int_array(
            {0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f}));
    }

    void test_read_bigtest()
    {
        //Uses an extended variant of Notch's original bigtest file
        std::string input(__binary_bigtest_uncompr_start, __binary_bigtest_uncompr_end);
        std::istringstream file(input, std::ios::binary);

        auto pair = nbt::io::read_compound(file);
        TS_ASSERT_EQUALS(pair.first, "Level");
        verify_bigtest_structure(*pair.second);
    }

    void test_read_littletest()
    {
        //Same as bigtest, but little endian
        std::string input(__binary_littletest_uncompr_start, __binary_littletest_uncompr_end);
        std::istringstream file(input, std::ios::binary);

        auto pair = nbt::io::read_compound(file, endian::little);
        TS_ASSERT_EQUALS(pair.first, "Level");
        TS_ASSERT_EQUALS(pair.second->get_type(), tag_type::Compound);
        verify_bigtest_structure(*pair.second);
    }

    void test_read_eof1()
    {
        std::string input(__binary_errortest_eof1_start, __binary_errortest_eof1_end);
        std::istringstream file(input, std::ios::binary);
        nbt::io::stream_reader reader(file);

        //EOF within a tag_double payload
        TS_ASSERT(file);
        TS_ASSERT_THROWS(reader.read_tag(), io::input_error);
        TS_ASSERT(!file);
    }

    void test_read_eof2()
    {
        std::string input(__binary_errortest_eof2_start, __binary_errortest_eof2_end);
        std::istringstream file(input, std::ios::binary);
        nbt::io::stream_reader reader(file);

        //EOF within a key in a compound
        TS_ASSERT(file);
        TS_ASSERT_THROWS(reader.read_tag(), io::input_error);
        TS_ASSERT(!file);
    }

    void test_read_errortest_noend()
    {
        std::string input(__binary_errortest_noend_start, __binary_errortest_noend_end);
        std::istringstream file(input, std::ios::binary);
        nbt::io::stream_reader reader(file);

        //Missing tag_end
        TS_ASSERT(file);
        TS_ASSERT_THROWS(reader.read_tag(), io::input_error);
        TS_ASSERT(!file);
    }

    void test_read_errortest_neg_length()
    {
        std::string input(__binary_errortest_neg_length_start, __binary_errortest_neg_length_end);
        std::istringstream file(input, std::ios::binary);
        nbt::io::stream_reader reader(file);

        //Negative list length
        TS_ASSERT(file);
        TS_ASSERT_THROWS(reader.read_tag(), io::input_error);
        TS_ASSERT(!file);
    }

    void test_read_misc()
    {
        std::string input(__binary_toplevel_string_start, __binary_toplevel_string_end);
        std::istringstream file(input, std::ios::binary);
        nbt::io::stream_reader reader(file);

        //Toplevel tag other than compound
        TS_ASSERT(file);
        TS_ASSERT_THROWS(reader.read_compound(), io::input_error);
        TS_ASSERT(!file);

        //Rewind and try again with read_tag
        file.clear();
        TS_ASSERT(file.seekg(0));
        auto pair = reader.read_tag();
        TS_ASSERT_EQUALS(pair.first, "Test (toplevel tag_string)");
        TS_ASSERT(*pair.second == tag_string(
            "Even though unprovided for by NBT, the library should also handle "
            "the case where the file consists of something else than tag_compound"));
    }
    void test_read_gzip()
    {
#ifdef NBT_HAVE_ZLIB
        std::string input(__binary_bigtest_nbt_start, __binary_bigtest_nbt_end);
        std::istringstream file(input, std::ios::binary);
        zlib::izlibstream igzs(file);
        TS_ASSERT(file && igzs);

        auto pair = nbt::io::read_compound(igzs);
        TS_ASSERT(igzs);
        TS_ASSERT_EQUALS(pair.first, "Level");
        verify_bigtest_structure(*pair.second);
#endif
    }
};