Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 1 | #include "parser_test.h" |
| 2 | |
| 3 | #include <cmath> |
| 4 | #include <string> |
| 5 | |
| 6 | #include "flatbuffers/idl.h" |
| 7 | #include "test_assert.h" |
| 8 | |
| 9 | namespace flatbuffers { |
| 10 | namespace tests { |
| 11 | namespace { |
| 12 | |
| 13 | // Shortcuts for the infinity. |
| 14 | static const auto infinity_f = std::numeric_limits<float>::infinity(); |
| 15 | static const auto infinity_d = std::numeric_limits<double>::infinity(); |
| 16 | |
| 17 | // Test that parser errors are actually generated. |
| 18 | static void TestError_(const char *src, const char *error_substr, bool strict_json, |
| 19 | const char *file, int line, const char *func) { |
| 20 | flatbuffers::IDLOptions opts; |
| 21 | opts.strict_json = strict_json; |
| 22 | flatbuffers::Parser parser(opts); |
| 23 | if (parser.Parse(src)) { |
| 24 | TestFail("true", "false", |
| 25 | ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line, |
| 26 | func); |
| 27 | } else if (!strstr(parser.error_.c_str(), error_substr)) { |
| 28 | TestFail(error_substr, parser.error_.c_str(), |
| 29 | ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line, |
| 30 | func); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static void TestError_(const char *src, const char *error_substr, const char *file, |
| 35 | int line, const char *func) { |
| 36 | TestError_(src, error_substr, false, file, line, func); |
| 37 | } |
| 38 | |
| 39 | #ifdef _WIN32 |
| 40 | # define TestError(src, ...) \ |
| 41 | TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__) |
| 42 | #else |
| 43 | # define TestError(src, ...) \ |
| 44 | TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
| 45 | #endif |
| 46 | |
| 47 | static bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | // Test that parsing errors occur as we'd expect. |
| 52 | // Also useful for coverage, making sure these paths are run. |
| 53 | void ErrorTest() { |
| 54 | // In order they appear in idl_parser.cpp |
| 55 | TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit"); |
| 56 | TestError("\"\0", "illegal"); |
| 57 | TestError("\"\\q", "escape code"); |
| 58 | TestError("table ///", "documentation"); |
| 59 | TestError("@", "illegal"); |
| 60 | TestError("table 1", "expecting"); |
| 61 | TestError("table X { Y:[[int]]; }", "nested vector"); |
| 62 | TestError("table X { Y:1; }", "illegal type"); |
| 63 | TestError("table X { Y:int; Y:int; }", "field already"); |
| 64 | TestError("table Y {} table X { Y:int; }", "same as table"); |
| 65 | TestError("struct X { Y:string; }", "only scalar"); |
| 66 | TestError("struct X { a:uint = 42; }", "default values"); |
| 67 | TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum"); |
| 68 | TestError("struct X { Y:int (deprecated); }", "deprecate"); |
| 69 | TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }", |
| 70 | "missing type field"); |
| 71 | TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {", |
| 72 | "type id"); |
| 73 | TestError("table X { Y:int; } root_type X; { Z:", "unknown field"); |
| 74 | TestError("table X { Y:int; } root_type X; { Y:", "string constant", true); |
| 75 | TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant", |
| 76 | true); |
| 77 | TestError( |
| 78 | "struct X { Y:int; Z:int; } table W { V:X; } root_type W; " |
| 79 | "{ V:{ Y:1 } }", |
| 80 | "wrong number"); |
| 81 | TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }", |
| 82 | "unknown enum value"); |
| 83 | TestError("table X { Y:byte; } root_type X; { Y:; }", "starting"); |
| 84 | TestError("enum X:byte { Y } enum X {", "enum already"); |
| 85 | TestError("enum X:float {}", "underlying"); |
| 86 | TestError("enum X:byte { Y, Y }", "value already"); |
| 87 | TestError("enum X:byte { Y=2, Z=2 }", "unique"); |
| 88 | TestError("enum X:byte (force_align: 4) { Y }", "force_align"); |
| 89 | TestError("table X { Y:int; } table X {", "datatype already"); |
| 90 | TestError("table X { } union X { }", "datatype already"); |
| 91 | TestError("union X { } table X { }", "datatype already"); |
| 92 | TestError("namespace A; table X { } namespace A; union X { }", |
| 93 | "datatype already"); |
| 94 | TestError("namespace A; union X { } namespace A; table X { }", |
| 95 | "datatype already"); |
| 96 | TestError("struct X (force_align: 7) { Y:int; }", "force_align"); |
| 97 | TestError("struct X {}", "size 0"); |
| 98 | TestError("{}", "no root"); |
| 99 | TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "end of file"); |
| 100 | TestError("table X { Y:byte; } root_type X; { Y:1 } table Y{ Z:int }", |
| 101 | "end of file"); |
| 102 | TestError("root_type X;", "unknown root"); |
| 103 | TestError("struct X { Y:int; } root_type X;", "a table"); |
| 104 | TestError("union X { Y }", "referenced"); |
| 105 | TestError("union Z { X } struct X { Y:int; }", "only tables"); |
| 106 | TestError("table X { Y:[int]; YLength:int; }", "clash"); |
| 107 | TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once"); |
| 108 | // float to integer conversion is forbidden |
| 109 | TestError("table X { Y:int; } root_type X; { Y:1.0 }", "float"); |
| 110 | TestError("table X { Y:bool; } root_type X; { Y:1.0 }", "float"); |
| 111 | TestError("enum X:bool { Y = true }", "must be integral"); |
| 112 | // Array of non-scalar |
| 113 | TestError("table X { x:int; } struct Y { y:[X:2]; }", |
| 114 | "may contain only scalar or struct fields"); |
| 115 | // Non-snake case field names |
| 116 | TestError("table X { Y: int; } root_type Y: {Y:1.0}", "snake_case"); |
| 117 | // Complex defaults |
| 118 | TestError("table X { y: string = 1; }", "expecting: string"); |
| 119 | TestError("table X { y: string = []; }", " Cannot assign token"); |
| 120 | TestError("table X { y: [int] = [1]; }", "Expected `]`"); |
| 121 | TestError("table X { y: [int] = [; }", "Expected `]`"); |
| 122 | TestError("table X { y: [int] = \"\"; }", "type mismatch"); |
| 123 | // An identifier can't start from sign (+|-) |
| 124 | TestError("table X { -Y: int; } root_type Y: {Y:1.0}", "identifier"); |
| 125 | TestError("table X { +Y: int; } root_type Y: {Y:1.0}", "identifier"); |
| 126 | } |
| 127 | |
| 128 | void EnumOutOfRangeTest() { |
| 129 | TestError("enum X:byte { Y = 128 }", "enum value does not fit"); |
| 130 | TestError("enum X:byte { Y = -129 }", "enum value does not fit"); |
| 131 | TestError("enum X:byte { Y = 126, Z0, Z1 }", "enum value does not fit"); |
| 132 | TestError("enum X:ubyte { Y = -1 }", "enum value does not fit"); |
| 133 | TestError("enum X:ubyte { Y = 256 }", "enum value does not fit"); |
| 134 | TestError("enum X:ubyte { Y = 255, Z }", "enum value does not fit"); |
| 135 | TestError("table Y{} union X { Y = -1 }", "enum value does not fit"); |
| 136 | TestError("table Y{} union X { Y = 256 }", "enum value does not fit"); |
| 137 | TestError("table Y{} union X { Y = 255, Z:Y }", "enum value does not fit"); |
| 138 | TestError("enum X:int { Y = -2147483649 }", "enum value does not fit"); |
| 139 | TestError("enum X:int { Y = 2147483648 }", "enum value does not fit"); |
| 140 | TestError("enum X:uint { Y = -1 }", "enum value does not fit"); |
| 141 | TestError("enum X:uint { Y = 4294967297 }", "enum value does not fit"); |
| 142 | TestError("enum X:long { Y = 9223372036854775808 }", "does not fit"); |
| 143 | TestError("enum X:long { Y = 9223372036854775807, Z }", |
| 144 | "enum value does not fit"); |
| 145 | TestError("enum X:ulong { Y = -1 }", "does not fit"); |
| 146 | TestError("enum X:ubyte (bit_flags) { Y=8 }", "bit flag out"); |
| 147 | TestError("enum X:byte (bit_flags) { Y=7 }", "must be unsigned"); // -128 |
| 148 | // bit_flgs out of range |
| 149 | TestError("enum X:ubyte (bit_flags) { Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8 }", |
| 150 | "out of range"); |
| 151 | } |
| 152 | |
| 153 | void IntegerOutOfRangeTest() { |
| 154 | TestError("table T { F:byte; } root_type T; { F:128 }", |
| 155 | "constant does not fit"); |
| 156 | TestError("table T { F:byte; } root_type T; { F:-129 }", |
| 157 | "constant does not fit"); |
| 158 | TestError("table T { F:ubyte; } root_type T; { F:256 }", |
| 159 | "constant does not fit"); |
| 160 | TestError("table T { F:ubyte; } root_type T; { F:-1 }", |
| 161 | "constant does not fit"); |
| 162 | TestError("table T { F:short; } root_type T; { F:32768 }", |
| 163 | "constant does not fit"); |
| 164 | TestError("table T { F:short; } root_type T; { F:-32769 }", |
| 165 | "constant does not fit"); |
| 166 | TestError("table T { F:ushort; } root_type T; { F:65536 }", |
| 167 | "constant does not fit"); |
| 168 | TestError("table T { F:ushort; } root_type T; { F:-1 }", |
| 169 | "constant does not fit"); |
| 170 | TestError("table T { F:int; } root_type T; { F:2147483648 }", |
| 171 | "constant does not fit"); |
| 172 | TestError("table T { F:int; } root_type T; { F:-2147483649 }", |
| 173 | "constant does not fit"); |
| 174 | TestError("table T { F:uint; } root_type T; { F:4294967296 }", |
| 175 | "constant does not fit"); |
| 176 | TestError("table T { F:uint; } root_type T; { F:-1 }", |
| 177 | "constant does not fit"); |
| 178 | // Check fixed width aliases |
| 179 | TestError("table X { Y:uint8; } root_type X; { Y: -1 }", "does not fit"); |
| 180 | TestError("table X { Y:uint8; } root_type X; { Y: 256 }", "does not fit"); |
| 181 | TestError("table X { Y:uint16; } root_type X; { Y: -1 }", "does not fit"); |
| 182 | TestError("table X { Y:uint16; } root_type X; { Y: 65536 }", "does not fit"); |
| 183 | TestError("table X { Y:uint32; } root_type X; { Y: -1 }", ""); |
| 184 | TestError("table X { Y:uint32; } root_type X; { Y: 4294967296 }", |
| 185 | "does not fit"); |
| 186 | TestError("table X { Y:uint64; } root_type X; { Y: -1 }", ""); |
| 187 | TestError("table X { Y:uint64; } root_type X; { Y: -9223372036854775809 }", |
| 188 | "does not fit"); |
| 189 | TestError("table X { Y:uint64; } root_type X; { Y: 18446744073709551616 }", |
| 190 | "does not fit"); |
| 191 | |
| 192 | TestError("table X { Y:int8; } root_type X; { Y: -129 }", "does not fit"); |
| 193 | TestError("table X { Y:int8; } root_type X; { Y: 128 }", "does not fit"); |
| 194 | TestError("table X { Y:int16; } root_type X; { Y: -32769 }", "does not fit"); |
| 195 | TestError("table X { Y:int16; } root_type X; { Y: 32768 }", "does not fit"); |
| 196 | TestError("table X { Y:int32; } root_type X; { Y: -2147483649 }", ""); |
| 197 | TestError("table X { Y:int32; } root_type X; { Y: 2147483648 }", |
| 198 | "does not fit"); |
| 199 | TestError("table X { Y:int64; } root_type X; { Y: -9223372036854775809 }", |
| 200 | "does not fit"); |
| 201 | TestError("table X { Y:int64; } root_type X; { Y: 9223372036854775808 }", |
| 202 | "does not fit"); |
| 203 | // check out-of-int64 as int8 |
| 204 | TestError("table X { Y:int8; } root_type X; { Y: -9223372036854775809 }", |
| 205 | "does not fit"); |
| 206 | TestError("table X { Y:int8; } root_type X; { Y: 9223372036854775808 }", |
| 207 | "does not fit"); |
| 208 | |
| 209 | // Check default values |
| 210 | TestError("table X { Y:int64=-9223372036854775809; } root_type X; {}", |
| 211 | "does not fit"); |
| 212 | TestError("table X { Y:int64= 9223372036854775808; } root_type X; {}", |
| 213 | "does not fit"); |
| 214 | TestError("table X { Y:uint64; } root_type X; { Y: -1 }", ""); |
| 215 | TestError("table X { Y:uint64=-9223372036854775809; } root_type X; {}", |
| 216 | "does not fit"); |
| 217 | TestError("table X { Y:uint64= 18446744073709551616; } root_type X; {}", |
| 218 | "does not fit"); |
| 219 | } |
| 220 | |
| 221 | void InvalidFloatTest() { |
| 222 | auto invalid_msg = "invalid number"; |
| 223 | auto comma_msg = "expecting: ,"; |
| 224 | TestError("table T { F:float; } root_type T; { F:1,0 }", ""); |
| 225 | TestError("table T { F:float; } root_type T; { F:. }", ""); |
| 226 | TestError("table T { F:float; } root_type T; { F:- }", invalid_msg); |
| 227 | TestError("table T { F:float; } root_type T; { F:+ }", invalid_msg); |
| 228 | TestError("table T { F:float; } root_type T; { F:-. }", invalid_msg); |
| 229 | TestError("table T { F:float; } root_type T; { F:+. }", invalid_msg); |
| 230 | TestError("table T { F:float; } root_type T; { F:.e }", ""); |
| 231 | TestError("table T { F:float; } root_type T; { F:-e }", invalid_msg); |
| 232 | TestError("table T { F:float; } root_type T; { F:+e }", invalid_msg); |
| 233 | TestError("table T { F:float; } root_type T; { F:-.e }", invalid_msg); |
| 234 | TestError("table T { F:float; } root_type T; { F:+.e }", invalid_msg); |
| 235 | TestError("table T { F:float; } root_type T; { F:-e1 }", invalid_msg); |
| 236 | TestError("table T { F:float; } root_type T; { F:+e1 }", invalid_msg); |
| 237 | TestError("table T { F:float; } root_type T; { F:1.0e+ }", invalid_msg); |
| 238 | TestError("table T { F:float; } root_type T; { F:1.0e- }", invalid_msg); |
| 239 | // exponent pP is mandatory for hex-float |
| 240 | TestError("table T { F:float; } root_type T; { F:0x0 }", invalid_msg); |
| 241 | TestError("table T { F:float; } root_type T; { F:-0x. }", invalid_msg); |
| 242 | TestError("table T { F:float; } root_type T; { F:0x. }", invalid_msg); |
| 243 | TestError("table T { F:float; } root_type T; { F:0Xe }", invalid_msg); |
| 244 | TestError("table T { F:float; } root_type T; { F:\"0Xe\" }", invalid_msg); |
| 245 | TestError("table T { F:float; } root_type T; { F:\"nan(1)\" }", invalid_msg); |
| 246 | // eE not exponent in hex-float! |
| 247 | TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg); |
| 248 | TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg); |
| 249 | TestError("table T { F:float; } root_type T; { F:0x0.0p }", invalid_msg); |
| 250 | TestError("table T { F:float; } root_type T; { F:0x0.0p+ }", invalid_msg); |
| 251 | TestError("table T { F:float; } root_type T; { F:0x0.0p- }", invalid_msg); |
| 252 | TestError("table T { F:float; } root_type T; { F:0x0.0pa1 }", invalid_msg); |
| 253 | TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg); |
| 254 | TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg); |
| 255 | TestError("table T { F:float; } root_type T; { F:0x0.0e+0 }", invalid_msg); |
| 256 | TestError("table T { F:float; } root_type T; { F:0x0.0e-0 }", invalid_msg); |
| 257 | TestError("table T { F:float; } root_type T; { F:0x0.0ep+ }", invalid_msg); |
| 258 | TestError("table T { F:float; } root_type T; { F:0x0.0ep- }", invalid_msg); |
| 259 | TestError("table T { F:float; } root_type T; { F:1.2.3 }", invalid_msg); |
| 260 | TestError("table T { F:float; } root_type T; { F:1.2.e3 }", invalid_msg); |
| 261 | TestError("table T { F:float; } root_type T; { F:1.2e.3 }", invalid_msg); |
| 262 | TestError("table T { F:float; } root_type T; { F:1.2e0.3 }", invalid_msg); |
| 263 | TestError("table T { F:float; } root_type T; { F:1.2e3. }", invalid_msg); |
| 264 | TestError("table T { F:float; } root_type T; { F:1.2e3.0 }", invalid_msg); |
| 265 | TestError("table T { F:float; } root_type T; { F:+-1.0 }", invalid_msg); |
| 266 | TestError("table T { F:float; } root_type T; { F:1.0e+-1 }", invalid_msg); |
| 267 | TestError("table T { F:float; } root_type T; { F:\"1.0e+-1\" }", invalid_msg); |
| 268 | TestError("table T { F:float; } root_type T; { F:1.e0e }", comma_msg); |
| 269 | TestError("table T { F:float; } root_type T; { F:0x1.p0e }", comma_msg); |
| 270 | TestError("table T { F:float; } root_type T; { F:\" 0x10 \" }", invalid_msg); |
| 271 | // floats in string |
| 272 | TestError("table T { F:float; } root_type T; { F:\"1,2.\" }", invalid_msg); |
| 273 | TestError("table T { F:float; } root_type T; { F:\"1.2e3.\" }", invalid_msg); |
| 274 | TestError("table T { F:float; } root_type T; { F:\"0x1.p0e\" }", invalid_msg); |
| 275 | TestError("table T { F:float; } root_type T; { F:\"0x1.0\" }", invalid_msg); |
| 276 | TestError("table T { F:float; } root_type T; { F:\" 0x1.0\" }", invalid_msg); |
| 277 | TestError("table T { F:float; } root_type T; { F:\"+ 0\" }", invalid_msg); |
| 278 | // disable escapes for "number-in-string" |
| 279 | TestError("table T { F:float; } root_type T; { F:\"\\f1.2e3.\" }", "invalid"); |
| 280 | TestError("table T { F:float; } root_type T; { F:\"\\t1.2e3.\" }", "invalid"); |
| 281 | TestError("table T { F:float; } root_type T; { F:\"\\n1.2e3.\" }", "invalid"); |
| 282 | TestError("table T { F:float; } root_type T; { F:\"\\r1.2e3.\" }", "invalid"); |
| 283 | TestError("table T { F:float; } root_type T; { F:\"4\\x005\" }", "invalid"); |
| 284 | TestError("table T { F:float; } root_type T; { F:\"\'12\'\" }", invalid_msg); |
| 285 | // null is not a number constant! |
| 286 | TestError("table T { F:float; } root_type T; { F:\"null\" }", invalid_msg); |
| 287 | TestError("table T { F:float; } root_type T; { F:null }", invalid_msg); |
| 288 | } |
| 289 | |
| 290 | void UnicodeInvalidSurrogatesTest() { |
| 291 | TestError( |
| 292 | "table T { F:string; }" |
| 293 | "root_type T;" |
| 294 | "{ F:\"\\uD800\"}", |
| 295 | "unpaired high surrogate"); |
| 296 | TestError( |
| 297 | "table T { F:string; }" |
| 298 | "root_type T;" |
| 299 | "{ F:\"\\uD800abcd\"}", |
| 300 | "unpaired high surrogate"); |
| 301 | TestError( |
| 302 | "table T { F:string; }" |
| 303 | "root_type T;" |
| 304 | "{ F:\"\\uD800\\n\"}", |
| 305 | "unpaired high surrogate"); |
| 306 | TestError( |
| 307 | "table T { F:string; }" |
| 308 | "root_type T;" |
| 309 | "{ F:\"\\uD800\\uD800\"}", |
| 310 | "multiple high surrogates"); |
| 311 | TestError( |
| 312 | "table T { F:string; }" |
| 313 | "root_type T;" |
| 314 | "{ F:\"\\uDC00\"}", |
| 315 | "unpaired low surrogate"); |
| 316 | } |
| 317 | |
| 318 | void InvalidUTF8Test() { |
| 319 | // "1 byte" pattern, under min length of 2 bytes |
| 320 | TestError( |
| 321 | "table T { F:string; }" |
| 322 | "root_type T;" |
| 323 | "{ F:\"\x80\"}", |
| 324 | "illegal UTF-8 sequence"); |
| 325 | // 2 byte pattern, string too short |
| 326 | TestError( |
| 327 | "table T { F:string; }" |
| 328 | "root_type T;" |
| 329 | "{ F:\"\xDF\"}", |
| 330 | "illegal UTF-8 sequence"); |
| 331 | // 3 byte pattern, string too short |
| 332 | TestError( |
| 333 | "table T { F:string; }" |
| 334 | "root_type T;" |
| 335 | "{ F:\"\xEF\xBF\"}", |
| 336 | "illegal UTF-8 sequence"); |
| 337 | // 4 byte pattern, string too short |
| 338 | TestError( |
| 339 | "table T { F:string; }" |
| 340 | "root_type T;" |
| 341 | "{ F:\"\xF7\xBF\xBF\"}", |
| 342 | "illegal UTF-8 sequence"); |
| 343 | // "5 byte" pattern, string too short |
| 344 | TestError( |
| 345 | "table T { F:string; }" |
| 346 | "root_type T;" |
| 347 | "{ F:\"\xFB\xBF\xBF\xBF\"}", |
| 348 | "illegal UTF-8 sequence"); |
| 349 | // "6 byte" pattern, string too short |
| 350 | TestError( |
| 351 | "table T { F:string; }" |
| 352 | "root_type T;" |
| 353 | "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}", |
| 354 | "illegal UTF-8 sequence"); |
| 355 | // "7 byte" pattern, string too short |
| 356 | TestError( |
| 357 | "table T { F:string; }" |
| 358 | "root_type T;" |
| 359 | "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}", |
| 360 | "illegal UTF-8 sequence"); |
| 361 | // "5 byte" pattern, over max length of 4 bytes |
| 362 | TestError( |
| 363 | "table T { F:string; }" |
| 364 | "root_type T;" |
| 365 | "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}", |
| 366 | "illegal UTF-8 sequence"); |
| 367 | // "6 byte" pattern, over max length of 4 bytes |
| 368 | TestError( |
| 369 | "table T { F:string; }" |
| 370 | "root_type T;" |
| 371 | "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}", |
| 372 | "illegal UTF-8 sequence"); |
| 373 | // "7 byte" pattern, over max length of 4 bytes |
| 374 | TestError( |
| 375 | "table T { F:string; }" |
| 376 | "root_type T;" |
| 377 | "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}", |
| 378 | "illegal UTF-8 sequence"); |
| 379 | |
| 380 | // Three invalid encodings for U+000A (\n, aka NEWLINE) |
| 381 | TestError( |
| 382 | "table T { F:string; }" |
| 383 | "root_type T;" |
| 384 | "{ F:\"\xC0\x8A\"}", |
| 385 | "illegal UTF-8 sequence"); |
| 386 | TestError( |
| 387 | "table T { F:string; }" |
| 388 | "root_type T;" |
| 389 | "{ F:\"\xE0\x80\x8A\"}", |
| 390 | "illegal UTF-8 sequence"); |
| 391 | TestError( |
| 392 | "table T { F:string; }" |
| 393 | "root_type T;" |
| 394 | "{ F:\"\xF0\x80\x80\x8A\"}", |
| 395 | "illegal UTF-8 sequence"); |
| 396 | |
| 397 | // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL) |
| 398 | TestError( |
| 399 | "table T { F:string; }" |
| 400 | "root_type T;" |
| 401 | "{ F:\"\xE0\x81\xA9\"}", |
| 402 | "illegal UTF-8 sequence"); |
| 403 | TestError( |
| 404 | "table T { F:string; }" |
| 405 | "root_type T;" |
| 406 | "{ F:\"\xF0\x80\x81\xA9\"}", |
| 407 | "illegal UTF-8 sequence"); |
| 408 | |
| 409 | // Invalid encoding for U+20AC (EURO SYMBOL) |
| 410 | TestError( |
| 411 | "table T { F:string; }" |
| 412 | "root_type T;" |
| 413 | "{ F:\"\xF0\x82\x82\xAC\"}", |
| 414 | "illegal UTF-8 sequence"); |
| 415 | |
| 416 | // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in |
| 417 | // UTF-8 |
| 418 | TestError( |
| 419 | "table T { F:string; }" |
| 420 | "root_type T;" |
| 421 | // U+10400 "encoded" as U+D801 U+DC00 |
| 422 | "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}", |
| 423 | "illegal UTF-8 sequence"); |
| 424 | |
| 425 | // Check independence of identifier from locale. |
| 426 | std::string locale_ident; |
| 427 | locale_ident += "table T { F"; |
| 428 | locale_ident += static_cast<char>(-32); // unsigned 0xE0 |
| 429 | locale_ident += " :string; }"; |
| 430 | locale_ident += "root_type T;"; |
| 431 | locale_ident += "{}"; |
| 432 | TestError(locale_ident.c_str(), ""); |
| 433 | } |
| 434 | |
| 435 | template<typename T> |
| 436 | T TestValue(const char *json, const char *type_name, |
| 437 | const char *decls = nullptr) { |
| 438 | flatbuffers::Parser parser; |
| 439 | parser.builder_.ForceDefaults(true); // return defaults |
| 440 | auto check_default = json ? false : true; |
| 441 | if (check_default) { parser.opts.output_default_scalars_in_json = true; } |
| 442 | // Simple schema. |
| 443 | std::string schema = std::string(decls ? decls : "") + "\n" + |
| 444 | "table X { y:" + std::string(type_name) + |
| 445 | "; } root_type X;"; |
| 446 | auto schema_done = parser.Parse(schema.c_str()); |
| 447 | TEST_EQ_STR(parser.error_.c_str(), ""); |
| 448 | TEST_EQ(schema_done, true); |
| 449 | |
| 450 | auto done = parser.Parse(check_default ? "{}" : json); |
| 451 | TEST_EQ_STR(parser.error_.c_str(), ""); |
| 452 | TEST_EQ(done, true); |
| 453 | |
| 454 | // Check with print. |
| 455 | std::string print_back; |
| 456 | parser.opts.indent_step = -1; |
| 457 | TEST_EQ(GenerateText(parser, parser.builder_.GetBufferPointer(), &print_back), |
| 458 | true); |
| 459 | // restore value from its default |
| 460 | if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); } |
| 461 | |
| 462 | auto root = flatbuffers::GetRoot<flatbuffers::Table>( |
| 463 | parser.builder_.GetBufferPointer()); |
| 464 | return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0); |
| 465 | } |
| 466 | |
| 467 | // Additional parser testing not covered elsewhere. |
| 468 | void ValueTest() { |
| 469 | // Test scientific notation numbers. |
| 470 | TEST_EQ( |
| 471 | FloatCompare(TestValue<float>("{ y:0.0314159e+2 }", "float"), 3.14159f), |
| 472 | true); |
| 473 | // number in string |
| 474 | TEST_EQ(FloatCompare(TestValue<float>("{ y:\"0.0314159e+2\" }", "float"), |
| 475 | 3.14159f), |
| 476 | true); |
| 477 | |
| 478 | // Test conversion functions. |
| 479 | TEST_EQ(FloatCompare(TestValue<float>("{ y:cos(rad(180)) }", "float"), -1), |
| 480 | true); |
| 481 | |
| 482 | // int embedded to string |
| 483 | TEST_EQ(TestValue<int>("{ y:\"-876\" }", "int=-123"), -876); |
| 484 | TEST_EQ(TestValue<int>("{ y:\"876\" }", "int=-123"), 876); |
| 485 | |
| 486 | // Test negative hex constant. |
| 487 | TEST_EQ(TestValue<int>("{ y:-0x8ea0 }", "int=-0x8ea0"), -36512); |
| 488 | TEST_EQ(TestValue<int>(nullptr, "int=-0x8ea0"), -36512); |
| 489 | |
| 490 | // positive hex constant |
| 491 | TEST_EQ(TestValue<int>("{ y:0x1abcdef }", "int=0x1"), 0x1abcdef); |
| 492 | // with optional '+' sign |
| 493 | TEST_EQ(TestValue<int>("{ y:+0x1abcdef }", "int=+0x1"), 0x1abcdef); |
| 494 | // hex in string |
| 495 | TEST_EQ(TestValue<int>("{ y:\"0x1abcdef\" }", "int=+0x1"), 0x1abcdef); |
| 496 | |
| 497 | // Make sure we do unsigned 64bit correctly. |
| 498 | TEST_EQ(TestValue<uint64_t>("{ y:12335089644688340133 }", "ulong"), |
| 499 | 12335089644688340133ULL); |
| 500 | |
| 501 | // bool in string |
| 502 | TEST_EQ(TestValue<bool>("{ y:\"false\" }", "bool=true"), false); |
| 503 | TEST_EQ(TestValue<bool>("{ y:\"true\" }", "bool=\"true\""), true); |
| 504 | TEST_EQ(TestValue<bool>("{ y:'false' }", "bool=true"), false); |
| 505 | TEST_EQ(TestValue<bool>("{ y:'true' }", "bool=\"true\""), true); |
| 506 | |
| 507 | // check comments before and after json object |
| 508 | TEST_EQ(TestValue<int>("/*before*/ { y:1 } /*after*/", "int"), 1); |
| 509 | TEST_EQ(TestValue<int>("//before \n { y:1 } //after", "int"), 1); |
| 510 | } |
| 511 | |
| 512 | void NestedListTest() { |
| 513 | flatbuffers::Parser parser1; |
| 514 | TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }" |
| 515 | "root_type T;" |
| 516 | "{ F:[ [10,20], [30,40]] }"), |
| 517 | true); |
| 518 | } |
| 519 | |
| 520 | void EnumStringsTest() { |
| 521 | flatbuffers::Parser parser1; |
| 522 | TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }" |
| 523 | "root_type T;" |
| 524 | "{ F:[ A, B, \"C\", \"A B C\" ] }"), |
| 525 | true); |
| 526 | flatbuffers::Parser parser2; |
| 527 | TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }" |
| 528 | "root_type T;" |
| 529 | "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"), |
| 530 | true); |
| 531 | // unsigned bit_flags |
| 532 | flatbuffers::Parser parser3; |
| 533 | TEST_EQ( |
| 534 | parser3.Parse("enum E:uint16 (bit_flags) { F0, F07=7, F08, F14=14, F15 }" |
| 535 | " table T { F: E = \"F15 F08\"; }" |
| 536 | "root_type T;"), |
| 537 | true); |
| 538 | } |
| 539 | |
| 540 | void EnumValueTest() { |
| 541 | // json: "{ Y:0 }", schema: table X { y: "E"} |
| 542 | // 0 in enum (V=0) E then Y=0 is valid. |
| 543 | TEST_EQ(TestValue<int>("{ y:0 }", "E", "enum E:int { V }"), 0); |
| 544 | TEST_EQ(TestValue<int>("{ y:V }", "E", "enum E:int { V }"), 0); |
| 545 | // A default value of Y is 0. |
| 546 | TEST_EQ(TestValue<int>("{ }", "E", "enum E:int { V }"), 0); |
| 547 | TEST_EQ(TestValue<int>("{ y:5 }", "E=V", "enum E:int { V=5 }"), 5); |
| 548 | // Generate json with defaults and check. |
| 549 | TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { V=5 }"), 5); |
| 550 | // 5 in enum |
| 551 | TEST_EQ(TestValue<int>("{ y:5 }", "E", "enum E:int { Z, V=5 }"), 5); |
| 552 | TEST_EQ(TestValue<int>("{ y:5 }", "E=V", "enum E:int { Z, V=5 }"), 5); |
| 553 | // Generate json with defaults and check. |
| 554 | TEST_EQ(TestValue<int>(nullptr, "E", "enum E:int { Z, V=5 }"), 0); |
| 555 | TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { Z, V=5 }"), 5); |
| 556 | // u84 test |
| 557 | TEST_EQ(TestValue<uint64_t>(nullptr, "E=V", |
| 558 | "enum E:ulong { V = 13835058055282163712 }"), |
| 559 | 13835058055282163712ULL); |
| 560 | TEST_EQ(TestValue<uint64_t>(nullptr, "E=V", |
| 561 | "enum E:ulong { V = 18446744073709551615 }"), |
| 562 | 18446744073709551615ULL); |
| 563 | // Assign non-enum value to enum field. Is it right? |
| 564 | TEST_EQ(TestValue<int>("{ y:7 }", "E", "enum E:int { V = 0 }"), 7); |
| 565 | // Check that non-ascending values are valid. |
| 566 | TEST_EQ(TestValue<int>("{ y:5 }", "E=V", "enum E:int { Z=10, V=5 }"), 5); |
| 567 | } |
| 568 | |
| 569 | void IntegerBoundaryTest() { |
| 570 | // Check numerical compatibility with non-C++ languages. |
| 571 | // By the C++ standard, std::numerical_limits<int64_t>::min() == |
| 572 | // -9223372036854775807 (-2^63+1) or less* The Flatbuffers grammar and most of |
| 573 | // the languages (C#, Java, Rust) expect that minimum values are: -128, |
| 574 | // -32768,.., -9223372036854775808. Since C++20, |
| 575 | // static_cast<int64>(0x8000000000000000ULL) is well-defined two's complement |
| 576 | // cast. Therefore -9223372036854775808 should be valid negative value. |
| 577 | TEST_EQ(flatbuffers::numeric_limits<int8_t>::min(), -128); |
| 578 | TEST_EQ(flatbuffers::numeric_limits<int8_t>::max(), 127); |
| 579 | TEST_EQ(flatbuffers::numeric_limits<int16_t>::min(), -32768); |
| 580 | TEST_EQ(flatbuffers::numeric_limits<int16_t>::max(), 32767); |
| 581 | TEST_EQ(flatbuffers::numeric_limits<int32_t>::min() + 1, -2147483647); |
| 582 | TEST_EQ(flatbuffers::numeric_limits<int32_t>::max(), 2147483647ULL); |
| 583 | TEST_EQ(flatbuffers::numeric_limits<int64_t>::min() + 1LL, |
| 584 | -9223372036854775807LL); |
| 585 | TEST_EQ(flatbuffers::numeric_limits<int64_t>::max(), 9223372036854775807ULL); |
| 586 | TEST_EQ(flatbuffers::numeric_limits<uint8_t>::max(), 255); |
| 587 | TEST_EQ(flatbuffers::numeric_limits<uint16_t>::max(), 65535); |
| 588 | TEST_EQ(flatbuffers::numeric_limits<uint32_t>::max(), 4294967295ULL); |
| 589 | TEST_EQ(flatbuffers::numeric_limits<uint64_t>::max(), |
| 590 | 18446744073709551615ULL); |
| 591 | |
| 592 | TEST_EQ(TestValue<int8_t>("{ y:127 }", "byte"), 127); |
| 593 | TEST_EQ(TestValue<int8_t>("{ y:-128 }", "byte"), -128); |
| 594 | TEST_EQ(TestValue<uint8_t>("{ y:255 }", "ubyte"), 255); |
| 595 | TEST_EQ(TestValue<uint8_t>("{ y:0 }", "ubyte"), 0); |
| 596 | TEST_EQ(TestValue<int16_t>("{ y:32767 }", "short"), 32767); |
| 597 | TEST_EQ(TestValue<int16_t>("{ y:-32768 }", "short"), -32768); |
| 598 | TEST_EQ(TestValue<uint16_t>("{ y:65535 }", "ushort"), 65535); |
| 599 | TEST_EQ(TestValue<uint16_t>("{ y:0 }", "ushort"), 0); |
| 600 | TEST_EQ(TestValue<int32_t>("{ y:2147483647 }", "int"), 2147483647); |
| 601 | TEST_EQ(TestValue<int32_t>("{ y:-2147483648 }", "int") + 1, -2147483647); |
| 602 | TEST_EQ(TestValue<uint32_t>("{ y:4294967295 }", "uint"), 4294967295); |
| 603 | TEST_EQ(TestValue<uint32_t>("{ y:0 }", "uint"), 0); |
| 604 | TEST_EQ(TestValue<int64_t>("{ y:9223372036854775807 }", "long"), |
| 605 | 9223372036854775807LL); |
| 606 | TEST_EQ(TestValue<int64_t>("{ y:-9223372036854775808 }", "long") + 1LL, |
| 607 | -9223372036854775807LL); |
| 608 | TEST_EQ(TestValue<uint64_t>("{ y:18446744073709551615 }", "ulong"), |
| 609 | 18446744073709551615ULL); |
| 610 | TEST_EQ(TestValue<uint64_t>("{ y:0 }", "ulong"), 0); |
| 611 | TEST_EQ(TestValue<uint64_t>("{ y: 18446744073709551615 }", "uint64"), |
| 612 | 18446744073709551615ULL); |
| 613 | // check that the default works |
| 614 | TEST_EQ(TestValue<uint64_t>(nullptr, "uint64 = 18446744073709551615"), |
| 615 | 18446744073709551615ULL); |
| 616 | } |
| 617 | |
| 618 | void ValidFloatTest() { |
| 619 | // check rounding to infinity |
| 620 | TEST_EQ(TestValue<float>("{ y:+3.4029e+38 }", "float"), +infinity_f); |
| 621 | TEST_EQ(TestValue<float>("{ y:-3.4029e+38 }", "float"), -infinity_f); |
| 622 | TEST_EQ(TestValue<double>("{ y:+1.7977e+308 }", "double"), +infinity_d); |
| 623 | TEST_EQ(TestValue<double>("{ y:-1.7977e+308 }", "double"), -infinity_d); |
| 624 | |
| 625 | TEST_EQ( |
| 626 | FloatCompare(TestValue<float>("{ y:0.0314159e+2 }", "float"), 3.14159f), |
| 627 | true); |
| 628 | // float in string |
| 629 | TEST_EQ(FloatCompare(TestValue<float>("{ y:\" 0.0314159e+2 \" }", "float"), |
| 630 | 3.14159f), |
| 631 | true); |
| 632 | |
| 633 | TEST_EQ(TestValue<float>("{ y:1 }", "float"), 1.0f); |
| 634 | TEST_EQ(TestValue<float>("{ y:1.0 }", "float"), 1.0f); |
| 635 | TEST_EQ(TestValue<float>("{ y:1. }", "float"), 1.0f); |
| 636 | TEST_EQ(TestValue<float>("{ y:+1. }", "float"), 1.0f); |
| 637 | TEST_EQ(TestValue<float>("{ y:-1. }", "float"), -1.0f); |
| 638 | TEST_EQ(TestValue<float>("{ y:1.e0 }", "float"), 1.0f); |
| 639 | TEST_EQ(TestValue<float>("{ y:1.e+0 }", "float"), 1.0f); |
| 640 | TEST_EQ(TestValue<float>("{ y:1.e-0 }", "float"), 1.0f); |
| 641 | TEST_EQ(TestValue<float>("{ y:0.125 }", "float"), 0.125f); |
| 642 | TEST_EQ(TestValue<float>("{ y:.125 }", "float"), 0.125f); |
| 643 | TEST_EQ(TestValue<float>("{ y:-.125 }", "float"), -0.125f); |
| 644 | TEST_EQ(TestValue<float>("{ y:+.125 }", "float"), +0.125f); |
| 645 | TEST_EQ(TestValue<float>("{ y:5 }", "float"), 5.0f); |
| 646 | TEST_EQ(TestValue<float>("{ y:\"5\" }", "float"), 5.0f); |
| 647 | |
| 648 | #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0) |
| 649 | // Old MSVC versions may have problem with this check. |
| 650 | // https://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/ |
| 651 | TEST_EQ(TestValue<double>("{ y:6.9294956446009195e15 }", "double"), |
| 652 | 6929495644600920.0); |
| 653 | // check nan's |
| 654 | TEST_EQ(std::isnan(TestValue<double>("{ y:nan }", "double")), true); |
| 655 | TEST_EQ(std::isnan(TestValue<float>("{ y:nan }", "float")), true); |
| 656 | TEST_EQ(std::isnan(TestValue<float>("{ y:\"nan\" }", "float")), true); |
| 657 | TEST_EQ(std::isnan(TestValue<float>("{ y:\"+nan\" }", "float")), true); |
| 658 | TEST_EQ(std::isnan(TestValue<float>("{ y:\"-nan\" }", "float")), true); |
| 659 | TEST_EQ(std::isnan(TestValue<float>("{ y:+nan }", "float")), true); |
| 660 | TEST_EQ(std::isnan(TestValue<float>("{ y:-nan }", "float")), true); |
| 661 | TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=nan")), true); |
| 662 | TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=-nan")), true); |
| 663 | // check inf |
| 664 | TEST_EQ(TestValue<float>("{ y:inf }", "float"), infinity_f); |
| 665 | TEST_EQ(TestValue<float>("{ y:\"inf\" }", "float"), infinity_f); |
| 666 | TEST_EQ(TestValue<float>("{ y:\"-inf\" }", "float"), -infinity_f); |
| 667 | TEST_EQ(TestValue<float>("{ y:\"+inf\" }", "float"), infinity_f); |
| 668 | TEST_EQ(TestValue<float>("{ y:+inf }", "float"), infinity_f); |
| 669 | TEST_EQ(TestValue<float>("{ y:-inf }", "float"), -infinity_f); |
| 670 | TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinity_f); |
| 671 | TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinity_f); |
| 672 | TestValue<double>( |
| 673 | "{ y: [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, " |
| 674 | "3.0e2] }", |
| 675 | "[double]"); |
| 676 | TestValue<float>( |
| 677 | "{ y: [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, " |
| 678 | "3.0e2] }", |
| 679 | "[float]"); |
| 680 | |
| 681 | // Test binary format of float point. |
| 682 | // https://en.cppreference.com/w/cpp/language/floating_literal |
| 683 | // 0x11.12p-1 = (1*16^1 + 2*16^0 + 3*16^-1 + 4*16^-2) * 2^-1 = |
| 684 | TEST_EQ(TestValue<double>("{ y:0x12.34p-1 }", "double"), 9.1015625); |
| 685 | // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0 |
| 686 | TEST_EQ(TestValue<float>("{ y:-0x0.2p0 }", "float"), -0.125f); |
| 687 | TEST_EQ(TestValue<float>("{ y:-0x.2p1 }", "float"), -0.25f); |
| 688 | TEST_EQ(TestValue<float>("{ y:0x1.2p3 }", "float"), 9.0f); |
| 689 | TEST_EQ(TestValue<float>("{ y:0x10.1p0 }", "float"), 16.0625f); |
| 690 | TEST_EQ(TestValue<double>("{ y:0x1.2p3 }", "double"), 9.0); |
| 691 | TEST_EQ(TestValue<double>("{ y:0x10.1p0 }", "double"), 16.0625); |
| 692 | TEST_EQ(TestValue<double>("{ y:0xC.68p+2 }", "double"), 49.625); |
| 693 | TestValue<double>("{ y: [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[double]"); |
| 694 | TestValue<float>("{ y: [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[float]"); |
| 695 | |
| 696 | #else // FLATBUFFERS_HAS_NEW_STRTOD |
| 697 | TEST_OUTPUT_LINE("FLATBUFFERS_HAS_NEW_STRTOD tests skipped"); |
| 698 | #endif // !FLATBUFFERS_HAS_NEW_STRTOD |
| 699 | } |
| 700 | |
| 701 | void UnicodeTest() { |
| 702 | flatbuffers::Parser parser; |
| 703 | // Without setting allow_non_utf8 = true, we treat \x sequences as byte |
| 704 | // sequences which are then validated as UTF-8. |
| 705 | TEST_EQ(parser.Parse("table T { F:string; }" |
| 706 | "root_type T;" |
| 707 | "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC" |
| 708 | "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD8" |
| 709 | "3D\\uDE0E\" }"), |
| 710 | true); |
| 711 | std::string jsongen; |
| 712 | parser.opts.indent_step = -1; |
| 713 | auto result = |
| 714 | GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); |
| 715 | TEST_EQ(result, true); |
| 716 | TEST_EQ_STR(jsongen.c_str(), |
| 717 | "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC" |
| 718 | "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}"); |
| 719 | } |
| 720 | |
| 721 | void UnicodeTestAllowNonUTF8() { |
| 722 | flatbuffers::Parser parser; |
| 723 | parser.opts.allow_non_utf8 = true; |
| 724 | TEST_EQ( |
| 725 | parser.Parse( |
| 726 | "table T { F:string; }" |
| 727 | "root_type T;" |
| 728 | "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC" |
| 729 | "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"), |
| 730 | true); |
| 731 | std::string jsongen; |
| 732 | parser.opts.indent_step = -1; |
| 733 | auto result = |
| 734 | GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); |
| 735 | TEST_EQ(result, true); |
| 736 | TEST_EQ_STR( |
| 737 | jsongen.c_str(), |
| 738 | "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC" |
| 739 | "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}"); |
| 740 | } |
| 741 | |
| 742 | void UnicodeTestGenerateTextFailsOnNonUTF8() { |
| 743 | flatbuffers::Parser parser; |
| 744 | // Allow non-UTF-8 initially to model what happens when we load a binary |
| 745 | // flatbuffer from disk which contains non-UTF-8 strings. |
| 746 | parser.opts.allow_non_utf8 = true; |
| 747 | TEST_EQ( |
| 748 | parser.Parse( |
| 749 | "table T { F:string; }" |
| 750 | "root_type T;" |
| 751 | "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC" |
| 752 | "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"), |
| 753 | true); |
| 754 | std::string jsongen; |
| 755 | parser.opts.indent_step = -1; |
| 756 | // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates |
| 757 | // failure. |
| 758 | parser.opts.allow_non_utf8 = false; |
| 759 | auto result = |
| 760 | GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); |
| 761 | TEST_EQ(result, false); |
| 762 | } |
| 763 | |
| 764 | void UnicodeSurrogatesTest() { |
| 765 | flatbuffers::Parser parser; |
| 766 | |
| 767 | TEST_EQ(parser.Parse("table T { F:string (id: 0); }" |
| 768 | "root_type T;" |
| 769 | "{ F:\"\\uD83D\\uDCA9\"}"), |
| 770 | true); |
| 771 | auto root = flatbuffers::GetRoot<flatbuffers::Table>( |
| 772 | parser.builder_.GetBufferPointer()); |
| 773 | auto string = root->GetPointer<flatbuffers::String *>( |
| 774 | flatbuffers::FieldIndexToOffset(0)); |
| 775 | TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9"); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | |
| 780 | void UnknownFieldsTest() { |
| 781 | flatbuffers::IDLOptions opts; |
| 782 | opts.skip_unexpected_fields_in_json = true; |
| 783 | flatbuffers::Parser parser(opts); |
| 784 | |
| 785 | TEST_EQ(parser.Parse("table T { str:string; i:int;}" |
| 786 | "root_type T;" |
| 787 | "{ str:\"test\"," |
| 788 | "unknown_string:\"test\"," |
| 789 | "\"unknown_string\":\"test\"," |
| 790 | "unknown_int:10," |
| 791 | "unknown_float:1.0," |
| 792 | "unknown_array: [ 1, 2, 3, 4]," |
| 793 | "unknown_object: { i: 10 }," |
| 794 | "\"unknown_object\": { \"i\": 10 }," |
| 795 | "i:10}"), |
| 796 | true); |
| 797 | |
| 798 | std::string jsongen; |
| 799 | parser.opts.indent_step = -1; |
| 800 | auto result = |
| 801 | GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); |
| 802 | TEST_EQ(result, true); |
| 803 | TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}"); |
| 804 | } |
| 805 | |
| 806 | void ParseUnionTest() { |
| 807 | // Unions must be parseable with the type field following the object. |
| 808 | flatbuffers::Parser parser; |
| 809 | TEST_EQ(parser.Parse("table T { A:int; }" |
| 810 | "union U { T }" |
| 811 | "table V { X:U; }" |
| 812 | "root_type V;" |
| 813 | "{ X:{ A:1 }, X_type: T }"), |
| 814 | true); |
| 815 | // Unions must be parsable with prefixed namespace. |
| 816 | flatbuffers::Parser parser2; |
| 817 | TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }" |
| 818 | "table B { e:U; } root_type B;" |
| 819 | "{ e_type: N_A, e: {} }"), |
| 820 | true); |
| 821 | } |
| 822 | |
| 823 | void ValidSameNameDifferentNamespaceTest() { |
| 824 | // Duplicate table names in different namespaces must be parsable |
| 825 | TEST_ASSERT(flatbuffers::Parser().Parse( |
| 826 | "namespace A; table X {} namespace B; table X {}")); |
| 827 | // Duplicate union names in different namespaces must be parsable |
| 828 | TEST_ASSERT(flatbuffers::Parser().Parse( |
| 829 | "namespace A; union X {} namespace B; union X {}")); |
| 830 | // Clashing table and union names in different namespaces must be parsable |
| 831 | TEST_ASSERT(flatbuffers::Parser().Parse( |
| 832 | "namespace A; table X {} namespace B; union X {}")); |
| 833 | TEST_ASSERT(flatbuffers::Parser().Parse( |
| 834 | "namespace A; union X {} namespace B; table X {}")); |
| 835 | } |
| 836 | |
| 837 | void WarningsAsErrorsTest() { |
| 838 | { |
| 839 | flatbuffers::IDLOptions opts; |
| 840 | // opts.warnings_as_errors should default to false |
| 841 | flatbuffers::Parser parser(opts); |
| 842 | TEST_EQ(parser.Parse("table T { THIS_NAME_CAUSES_A_WARNING:string;}\n" |
| 843 | "root_type T;"), |
| 844 | true); |
| 845 | } |
| 846 | { |
| 847 | flatbuffers::IDLOptions opts; |
| 848 | opts.warnings_as_errors = true; |
| 849 | flatbuffers::Parser parser(opts); |
| 850 | TEST_EQ(parser.Parse("table T { THIS_NAME_CAUSES_A_WARNING:string;}\n" |
| 851 | "root_type T;"), |
| 852 | false); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | void StringVectorDefaultsTest() { |
| 857 | std::vector<std::string> schemas; |
| 858 | schemas.push_back("table Monster { mana: string = \"\"; }"); |
| 859 | schemas.push_back("table Monster { mana: string = \"mystr\"; }"); |
| 860 | schemas.push_back("table Monster { mana: string = \" \"; }"); |
| 861 | schemas.push_back("table Monster { mana: string = \"null\"; }"); |
| 862 | schemas.push_back("table Monster { mana: [int] = []; }"); |
| 863 | schemas.push_back("table Monster { mana: [uint] = [ ]; }"); |
| 864 | schemas.push_back("table Monster { mana: [byte] = [\t\t\n]; }"); |
| 865 | schemas.push_back("enum E:int{}table Monster{mana:[E]=[];}"); |
| 866 | for (auto s = schemas.begin(); s < schemas.end(); s++) { |
| 867 | flatbuffers::Parser parser; |
| 868 | TEST_ASSERT(parser.Parse(s->c_str())); |
| 869 | const auto *mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana"); |
| 870 | TEST_EQ(mana->IsDefault(), true); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | void FieldIdentifierTest() { |
| 875 | using flatbuffers::Parser; |
| 876 | TEST_EQ(true, Parser().Parse("table T{ f: int (id:0); }")); |
| 877 | // non-integer `id` should be rejected |
| 878 | TEST_EQ(false, Parser().Parse("table T{ f: int (id:text); }")); |
| 879 | TEST_EQ(false, Parser().Parse("table T{ f: int (id:\"text\"); }")); |
| 880 | TEST_EQ(false, Parser().Parse("table T{ f: int (id:0text); }")); |
| 881 | TEST_EQ(false, Parser().Parse("table T{ f: int (id:1.0); }")); |
| 882 | TEST_EQ(false, Parser().Parse("table T{ f: int (id:-1); g: int (id:0); }")); |
| 883 | TEST_EQ(false, Parser().Parse("table T{ f: int (id:129496726); }")); |
| 884 | // A unuion filed occupys two ids: enumerator + pointer (offset). |
| 885 | TEST_EQ(false, |
| 886 | Parser().Parse("union X{} table T{ u: X(id:0); table F{x:int;\n}")); |
| 887 | // Positive tests for unions |
| 888 | TEST_EQ(true, Parser().Parse("union X{} table T{ u: X (id:1); }")); |
| 889 | TEST_EQ(true, Parser().Parse("union X{} table T{ u: X; }")); |
| 890 | // Test using 'inf' and 'nan' words both as identifiers and as default values. |
| 891 | TEST_EQ(true, Parser().Parse("table T{ nan: string; }")); |
| 892 | TEST_EQ(true, Parser().Parse("table T{ inf: string; }")); |
| 893 | #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0) |
| 894 | TEST_EQ(true, Parser().Parse("table T{ inf: float = inf; }")); |
| 895 | TEST_EQ(true, Parser().Parse("table T{ nan: float = inf; }")); |
| 896 | #endif |
| 897 | } |
| 898 | |
| 899 | } // namespace tests |
| 900 | } // namespace flatbuffers |