1 /* 2 * Copyright 2015 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 using System; 18 19 namespace FlatBuffers.Test 20 { 21 [FlatBuffersTestClass] 22 public class FlatBuffersFuzzTests 23 { 24 private readonly Lcg _lcg = new Lcg(); 25 26 [FlatBuffersTestMethod] TestObjects()27 public void TestObjects() 28 { 29 CheckObjects(11, 100); 30 } 31 32 [FlatBuffersTestMethod] TestNumbers()33 public void TestNumbers() 34 { 35 var builder = new FlatBufferBuilder(1); 36 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 37 builder.AddBool(true); 38 Assert.ArrayEqual(new byte[] { 1 }, builder.DataBuffer.ToFullArray()); 39 builder.AddSbyte(-127); 40 Assert.ArrayEqual(new byte[] { 129, 1 }, builder.DataBuffer.ToFullArray()); 41 builder.AddByte(255); 42 Assert.ArrayEqual(new byte[] { 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // First pad 43 builder.AddShort(-32222); 44 Assert.ArrayEqual(new byte[] { 0, 0, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // Second pad 45 builder.AddUshort(0xFEEE); 46 Assert.ArrayEqual(new byte[] { 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // no pad 47 builder.AddInt(-53687092); 48 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // third pad 49 builder.AddUint(0x98765432); 50 Assert.ArrayEqual(new byte[] { 0x32, 0x54, 0x76, 0x98, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.ToFullArray()); // no pad 51 } 52 53 [FlatBuffersTestMethod] TestNumbers64()54 public void TestNumbers64() 55 { 56 var builder = new FlatBufferBuilder(1); 57 builder.AddUlong(0x1122334455667788); 58 Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.ToFullArray()); 59 60 builder = new FlatBufferBuilder(1); 61 builder.AddLong(0x1122334455667788); 62 Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.ToFullArray()); 63 } 64 65 [FlatBuffersTestMethod] TestVector_1xUInt8()66 public void TestVector_1xUInt8() 67 { 68 var builder = new FlatBufferBuilder(1); 69 builder.StartVector(sizeof(byte), 1, 1); 70 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 71 builder.AddByte(1); 72 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 73 builder.EndVector(); 74 Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 75 } 76 77 [FlatBuffersTestMethod] TestVector_2xUint8()78 public void TestVector_2xUint8() 79 { 80 var builder = new FlatBufferBuilder(1); 81 builder.StartVector(sizeof(byte), 2, 1); 82 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 83 builder.AddByte(1); 84 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, builder.DataBuffer.ToFullArray()); 85 builder.AddByte(2); 86 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.ToFullArray()); 87 builder.EndVector(); 88 Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.ToFullArray()); 89 } 90 91 [FlatBuffersTestMethod] TestVector_1xUInt16()92 public void TestVector_1xUInt16() 93 { 94 var builder = new FlatBufferBuilder(1); 95 builder.StartVector(sizeof(ushort), 1, 1); 96 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 97 builder.AddUshort(1); 98 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 99 builder.EndVector(); 100 Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 101 } 102 103 [FlatBuffersTestMethod] TestVector_2xUInt16()104 public void TestVector_2xUInt16() 105 { 106 var builder = new FlatBufferBuilder(1); 107 builder.StartVector(sizeof(ushort), 2, 1); 108 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.ToFullArray()); 109 builder.AddUshort(0xABCD); 110 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0xCD, 0xAB }, builder.DataBuffer.ToFullArray()); 111 builder.AddUshort(0xDCBA); 112 Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.ToFullArray()); 113 builder.EndVector(); 114 Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.ToFullArray()); 115 } 116 117 [FlatBuffersTestMethod] TestCreateAsciiString()118 public void TestCreateAsciiString() 119 { 120 var builder = new FlatBufferBuilder(1); 121 builder.CreateString("foo"); 122 Assert.ArrayEqual(new byte[] { 3, 0, 0, 0, (byte)'f', (byte)'o', (byte)'o', 0 }, builder.DataBuffer.ToFullArray()); 123 124 builder.CreateString("moop"); 125 Assert.ArrayEqual(new byte[] 126 { 127 0, 0, 0, 0, 128 0, 0, 0, 0, 129 0, 0, 0, 0, // Padding to 32 bytes 130 4, 0, 0, 0, 131 (byte)'m', (byte)'o', (byte)'o', (byte)'p', 132 0, 0, 0, 0, // zero terminator with 3 byte pad 133 3, 0, 0, 0, 134 (byte)'f', (byte)'o', (byte)'o', 0 135 }, builder.DataBuffer.ToFullArray()); 136 } 137 138 [FlatBuffersTestMethod] TestCreateSharedAsciiString()139 public void TestCreateSharedAsciiString() 140 { 141 var builder = new FlatBufferBuilder(1); 142 builder.CreateSharedString("foo"); 143 Assert.ArrayEqual(new byte[] { 3, 0, 0, 0, (byte)'f', (byte)'o', (byte)'o', 0 }, builder.DataBuffer.ToFullArray()); 144 145 builder.CreateSharedString("foo"); 146 Assert.ArrayEqual(new byte[] { 3, 0, 0, 0, (byte)'f', (byte)'o', (byte)'o', 0 }, builder.DataBuffer.ToFullArray()); 147 } 148 149 [FlatBuffersTestMethod] TestCreateArbitarytring()150 public void TestCreateArbitarytring() 151 { 152 var builder = new FlatBufferBuilder(1); 153 builder.CreateString("\x01\x02\x03"); 154 Assert.ArrayEqual(new byte[] 155 { 156 3, 0, 0, 0, 157 0x01, 0x02, 0x03, 0 158 }, builder.DataBuffer.ToFullArray()); // No padding 159 builder.CreateString("\x04\x05\x06\x07"); 160 Assert.ArrayEqual(new byte[] 161 { 162 0, 0, 0, 0, 163 0, 0, 0, 0, 164 0, 0, 0, 0, // Padding to 32 bytes 165 4, 0, 0, 0, 166 0x04, 0x05, 0x06, 0x07, 167 0, 0, 0, 0, // zero terminator with 3 byte pad 168 3, 0, 0, 0, 169 0x01, 0x02, 0x03, 0 170 }, builder.DataBuffer.ToFullArray()); // No padding 171 } 172 173 [FlatBuffersTestMethod] TestEmptyVTable()174 public void TestEmptyVTable() 175 { 176 var builder = new FlatBufferBuilder(1); 177 builder.StartTable(0); 178 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 179 builder.EndTable(); 180 Assert.ArrayEqual(new byte[] 181 { 182 4, 0, 4, 0, 183 4, 0, 0, 0 184 }, 185 builder.DataBuffer.ToFullArray()); 186 } 187 188 [FlatBuffersTestMethod] TestVTableWithOneBool()189 public void TestVTableWithOneBool() 190 { 191 var builder = new FlatBufferBuilder(1); 192 builder.StartTable(1); 193 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 194 builder.AddBool(0, true, false); 195 builder.EndTable(); 196 Assert.ArrayEqual(new byte[] 197 { 198 0, 0, // padding to 16 bytes 199 6, 0, // vtable bytes 200 8, 0, // object length inc vtable offset 201 7, 0, // start of bool value 202 6, 0, 0, 0, // int32 offset for start of vtable 203 0, 0, 0, // padding 204 1, // value 0 205 }, 206 builder.DataBuffer.ToFullArray()); 207 } 208 209 [FlatBuffersTestMethod] TestVTableWithOneBool_DefaultValue()210 public void TestVTableWithOneBool_DefaultValue() 211 { 212 var builder = new FlatBufferBuilder(1); 213 builder.StartTable(1); 214 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 215 builder.AddBool(0, false, false); 216 builder.EndTable(); 217 Assert.ArrayEqual(new byte[] 218 { 219 // No padding. 220 4, 0, // vtable bytes 221 4, 0, // end of object from here 222 // entry 0 is not stored (trimmed end of vtable) 223 4, 0, 0, 0, // int32 offset for start of vtable 224 }, 225 builder.DataBuffer.ToFullArray()); 226 } 227 228 [FlatBuffersTestMethod] TestVTableWithOneInt16()229 public void TestVTableWithOneInt16() 230 { 231 var builder = new FlatBufferBuilder(1); 232 builder.StartTable(1); 233 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 234 builder.AddShort(0, 0x789A, 0); 235 builder.EndTable(); 236 Assert.ArrayEqual(new byte[] 237 { 238 0, 0, // padding to 16 bytes 239 6, 0, // vtable bytes 240 8, 0, // object length inc vtable offset 241 6, 0, // start of int16 value 242 6, 0, 0, 0, // int32 offset for start of vtable 243 0, 0, // padding 244 0x9A, 0x78, //value 0 245 }, 246 builder.DataBuffer.ToFullArray()); 247 } 248 249 [FlatBuffersTestMethod] TestVTableWithTwoInt16()250 public void TestVTableWithTwoInt16() 251 { 252 var builder = new FlatBufferBuilder(1); 253 builder.StartTable(2); 254 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 255 builder.AddShort(0, 0x3456, 0); 256 builder.AddShort(1, 0x789A, 0); 257 builder.EndTable(); 258 Assert.ArrayEqual(new byte[] 259 { 260 8, 0, // vtable bytes 261 8, 0, // object length inc vtable offset 262 6, 0, // start of int16 value 0 263 4, 0, // start of int16 value 1 264 8, 0, 0, 0, // int32 offset for start of vtable 265 0x9A, 0x78, // value 1 266 0x56, 0x34, // value 0 267 }, 268 builder.DataBuffer.ToFullArray()); 269 } 270 271 [FlatBuffersTestMethod] TestVTableWithInt16AndBool()272 public void TestVTableWithInt16AndBool() 273 { 274 var builder = new FlatBufferBuilder(1); 275 builder.StartTable(2); 276 Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.ToFullArray()); 277 builder.AddShort(0, 0x3456, 0); 278 builder.AddBool(1, true, false); 279 builder.EndTable(); 280 Assert.ArrayEqual(new byte[] 281 { 282 8, 0, // vtable bytes 283 8, 0, // object length inc vtable offset 284 6, 0, // start of int16 value 0 285 5, 0, // start of bool value 1 286 8, 0, 0, 0, // int32 offset for start of vtable 287 0, 1, // padding + value 1 288 0x56, 0x34, // value 0 289 }, 290 builder.DataBuffer.ToFullArray()); 291 } 292 293 [FlatBuffersTestMethod] TestVTableWithEmptyVector()294 public void TestVTableWithEmptyVector() 295 { 296 var builder = new FlatBufferBuilder(1); 297 builder.StartVector(sizeof(byte), 0, 1); 298 var vecEnd = builder.EndVector(); 299 300 builder.StartTable(1); 301 302 builder.AddOffset(0, vecEnd.Value, 0); 303 builder.EndTable(); 304 Assert.ArrayEqual(new byte[] 305 { 306 0, 0, 0, 0, 307 0, 0, 0, 0, 308 0, 0, 0, 0, 309 0, 0, // Padding to 32 bytes 310 6, 0, // vtable bytes 311 8, 0, // object length inc vtable offset 312 4, 0, // start of vector offset value 0 313 6, 0, 0, 0, // int32 offset for start of vtable 314 4, 0, 0, 0, 315 0, 0, 0, 0, 316 }, 317 builder.DataBuffer.ToFullArray()); 318 } 319 320 [FlatBuffersTestMethod] TestVTableWithEmptyVectorAndScalars()321 public void TestVTableWithEmptyVectorAndScalars() 322 { 323 var builder = new FlatBufferBuilder(1); 324 builder.StartVector(sizeof(byte), 0, 1); 325 var vecEnd = builder.EndVector(); 326 327 builder.StartTable(2); 328 builder.AddShort(0, 55, 0); 329 builder.AddOffset(1, vecEnd.Value, 0); 330 builder.EndTable(); 331 Assert.ArrayEqual(new byte[] 332 { 333 0, 0, 0, 0, 334 0, 0, 0, 0, // Padding to 32 bytes 335 8, 0, // vtable bytes 336 12, 0, // object length inc vtable offset 337 10, 0, // offset to int16 value 0 338 4, 0, // start of vector offset value 1 339 8, 0, 0, 0, // int32 offset for start of vtable 340 8, 0, 0, 0, // value 1 341 0, 0, 55, 0, // value 0 342 0, 0, 0, 0, // length of vector (not in sctruc) 343 }, 344 builder.DataBuffer.ToFullArray()); 345 } 346 347 348 [FlatBuffersTestMethod] TestVTableWith_1xInt16_and_Vector_or_2xInt16()349 public void TestVTableWith_1xInt16_and_Vector_or_2xInt16() 350 { 351 var builder = new FlatBufferBuilder(1); 352 builder.StartVector(sizeof(short), 2, 1); 353 builder.AddShort(0x1234); 354 builder.AddShort(0x5678); 355 var vecEnd = builder.EndVector(); 356 357 builder.StartTable(2); 358 builder.AddOffset(1, vecEnd.Value, 0); 359 builder.AddShort(0, 55, 0); 360 builder.EndTable(); 361 Assert.ArrayEqual(new byte[] 362 { 363 0, 0, 0, 0, // Padding to 32 bytes 364 8, 0, // vtable bytes 365 12, 0, // object length 366 6, 0, // start of value 0 from end of vtable 367 8, 0, // start of value 1 from end of buffer 368 8, 0, 0, 0, // int32 offset for start of vtable 369 0, 0, 55, 0, // padding + value 0 370 4, 0, 0, 0, // position of vector from here 371 2, 0, 0, 0, // length of vector 372 0x78, 0x56, // vector value 0 373 0x34, 0x12, // vector value 1 374 }, 375 builder.DataBuffer.ToFullArray()); 376 } 377 378 [FlatBuffersTestMethod] TestVTableWithAStruct_of_int8_int16_int32()379 public void TestVTableWithAStruct_of_int8_int16_int32() 380 { 381 var builder = new FlatBufferBuilder(1); 382 builder.StartTable(1); 383 builder.Prep(4+4+4, 0); 384 builder.AddSbyte(55); 385 builder.Pad(3); 386 builder.AddShort(0x1234); 387 builder.Pad(2); 388 builder.AddInt(0x12345678); 389 var structStart = builder.Offset; 390 builder.AddStruct(0, structStart, 0); 391 builder.EndTable(); 392 Assert.ArrayEqual(new byte[] 393 { 394 0, 0, 0, 0, 395 0, 0, 0, 0, 396 0, 0, // Padding to 32 bytes 397 6, 0, // vtable bytes 398 16, 0, // object length 399 4, 0, // start of struct from here 400 6, 0, 0, 0, // int32 offset for start of vtable 401 0x78, 0x56, 0x34, 0x12, // struct value 2 402 0x00, 0x00, 0x34, 0x12, // struct value 1 403 0x00, 0x00, 0x00, 55, // struct value 0 404 }, 405 builder.DataBuffer.ToFullArray()); 406 } 407 408 [FlatBuffersTestMethod] TestVTableWithAVectorOf_2xStructOf_2xInt8()409 public void TestVTableWithAVectorOf_2xStructOf_2xInt8() 410 { 411 var builder = new FlatBufferBuilder(1); 412 builder.StartVector(sizeof(byte)*2, 2, 1); 413 builder.AddByte(33); 414 builder.AddByte(44); 415 builder.AddByte(55); 416 builder.AddByte(66); 417 var vecEnd = builder.EndVector(); 418 419 builder.StartTable(1); 420 builder.AddOffset(0, vecEnd.Value, 0); 421 builder.EndTable(); 422 423 Assert.ArrayEqual(new byte[] 424 { 425 0, 0, 0, 0, 426 0, 0, 0, 0, 427 0, 0, // Padding to 32 bytes 428 6, 0, // vtable bytes 429 8, 0, // object length 430 4, 0, // offset of vector offset 431 6, 0, 0, 0, // int32 offset for start of vtable 432 4, 0, 0, 0, // Vector start offset 433 2, 0, 0, 0, // Vector len 434 66, // vector 1, 1 435 55, // vector 1, 0 436 44, // vector 0, 1 437 33, // vector 0, 0 438 }, 439 builder.DataBuffer.ToFullArray()); 440 } 441 442 [FlatBuffersTestMethod] TestVTableWithSomeElements()443 public void TestVTableWithSomeElements() 444 { 445 var builder = new FlatBufferBuilder(1); 446 builder.StartTable(2); 447 builder.AddByte(0, 33, 0); 448 builder.AddShort(1, 66, 0); 449 var off = builder.EndTable(); 450 builder.Finish(off); 451 452 byte[] padded = new byte[] 453 { 454 0, 0, 0, 0, 455 0, 0, 0, 0, 456 0, 0, 0, 0, //Padding to 32 bytes 457 12, 0, 0, 0, // root of table, pointing to vtable offset 458 8, 0, // vtable bytes 459 8, 0, // object length 460 7, 0, // start of value 0 461 4, 0, // start of value 1 462 8, 0, 0, 0, // int32 offset for start of vtable 463 66, 0, // value 1 464 0, 33, // value 0 465 466 }; 467 Assert.ArrayEqual(padded, builder.DataBuffer.ToFullArray()); 468 469 // no padding in sized array 470 byte[] unpadded = new byte[padded.Length - 12]; 471 Buffer.BlockCopy(padded, 12, unpadded, 0, unpadded.Length); 472 Assert.ArrayEqual(unpadded, builder.DataBuffer.ToSizedArray()); 473 } 474 475 [FlatBuffersTestMethod] TestTwoFinishTable()476 public void TestTwoFinishTable() 477 { 478 var builder = new FlatBufferBuilder(1); 479 builder.StartTable(2); 480 builder.AddByte(0, 33, 0); 481 builder.AddByte(1, 44, 0); 482 var off0 = builder.EndTable(); 483 builder.Finish(off0); 484 485 builder.StartTable(3); 486 builder.AddByte(0, 55, 0); 487 builder.AddByte(1, 66, 0); 488 builder.AddByte(2, 77, 0); 489 var off1 = builder.EndTable(); 490 builder.Finish(off1); 491 492 Assert.ArrayEqual(new byte[] 493 { 494 0, 0, 0, 0, 495 0, 0, 0, 0, 496 0, 0, 0, 0, 497 0, 0, 0, 0, 498 0, 0, 0, 0, // padding to 64 bytes 499 16, 0, 0, 0, // root of table, pointing to vtable offset (obj1) 500 0, 0, // padding 501 502 10, 0, // vtable bytes 503 8, 0, // object length 504 7, 0, // start of value 0 505 6, 0, // start of value 1 506 5, 0, // start of value 2 507 10, 0, 0, 0, // int32 offset for start of vtable 508 0, // pad 509 77, // values 2, 1, 0 510 66, 511 55, 512 513 12, 0, 0, 0, // root of table, pointing to vtable offset (obj0) 514 8, 0, // vtable bytes 515 8, 0, // object length 516 7, 0, // start of value 0 517 6, 0, // start of value 1 518 8, 0, 0, 0, // int32 offset for start of vtable 519 0, 0, // pad 520 44, // value 1, 0 521 33, 522 }, 523 builder.DataBuffer.ToFullArray()); 524 } 525 526 [FlatBuffersTestMethod] TestBunchOfBools()527 public void TestBunchOfBools() 528 { 529 var builder = new FlatBufferBuilder(1); 530 builder.StartTable(8); 531 for (var i = 0; i < 8; i++) 532 { 533 builder.AddBool(i, true, false); 534 } 535 var off = builder.EndTable(); 536 builder.Finish(off); 537 538 byte[] padded = new byte[] 539 { 540 0, 0, 0, 0, 541 0, 0, 0, 0, 542 0, 0, 0, 0, 543 0, 0, 0, 0, 544 0, 0, 0, 0, 545 0, 0, 0, 0, 546 0, 0, 0, 0, // padding to 64 bytes 547 548 24, 0, 0, 0, // root of table, pointing to vtable offset (obj0) 549 20, 0, // vtable bytes 550 12, 0, // object length 551 11, 0, // start of value 0 552 10, 0, // start of value 1 553 9, 0, // start of value 2 554 8, 0, // start of value 3 555 7, 0, // start of value 4 556 6, 0, // start of value 5 557 5, 0, // start of value 6 558 4, 0, // start of value 7 559 560 20, 0, 0, 0, // int32 offset for start of vtable 561 562 1, 1, 1, 1, // values 563 1, 1, 1, 1, 564 565 }; 566 Assert.ArrayEqual(padded, builder.DataBuffer.ToFullArray()); 567 568 // no padding in sized array 569 byte[] unpadded = new byte[padded.Length - 28]; 570 Buffer.BlockCopy(padded, 28, unpadded, 0, unpadded.Length); 571 Assert.ArrayEqual(unpadded, builder.DataBuffer.ToSizedArray()); 572 } 573 574 [FlatBuffersTestMethod] TestBunchOfBoolsSizePrefixed()575 public void TestBunchOfBoolsSizePrefixed() 576 { 577 var builder = new FlatBufferBuilder(1); 578 builder.StartTable(8); 579 for (var i = 0; i < 8; i++) 580 { 581 builder.AddBool(i, true, false); 582 } 583 var off = builder.EndTable(); 584 builder.FinishSizePrefixed(off); 585 586 byte[] padded = new byte[] 587 { 588 0, 0, 0, 0, 589 0, 0, 0, 0, 590 0, 0, 0, 0, 591 0, 0, 0, 0, 592 0, 0, 0, 0, 593 0, 0, 0, 0, // padding to 64 bytes 594 595 36, 0, 0, 0, // size prefix 596 24, 0, 0, 0, // root of table, pointing to vtable offset (obj0) 597 20, 0, // vtable bytes 598 12, 0, // object length 599 11, 0, // start of value 0 600 10, 0, // start of value 1 601 9, 0, // start of value 2 602 8, 0, // start of value 3 603 7, 0, // start of value 4 604 6, 0, // start of value 5 605 5, 0, // start of value 6 606 4, 0, // start of value 7 607 608 20, 0, 0, 0, // int32 offset for start of vtable 609 610 1, 1, 1, 1, // values 611 1, 1, 1, 1, 612 613 }; 614 Assert.ArrayEqual(padded, builder.DataBuffer.ToFullArray()); 615 616 // no padding in sized array 617 byte[] unpadded = new byte[padded.Length - 24]; 618 Buffer.BlockCopy(padded, 24, unpadded, 0, unpadded.Length); 619 Assert.ArrayEqual(unpadded, builder.DataBuffer.ToSizedArray()); 620 } 621 622 [FlatBuffersTestMethod] TestWithFloat()623 public void TestWithFloat() 624 { 625 var builder = new FlatBufferBuilder(1); 626 builder.StartTable(1); 627 builder.AddFloat(0, 1, 0); 628 builder.EndTable(); 629 630 631 Assert.ArrayEqual(new byte[] 632 { 633 0, 0, 634 6, 0, // vtable bytes 635 8, 0, // object length 636 4, 0, // start of value 0 637 6, 0, 0, 0, // int32 offset for start of vtable 638 0, 0, 128, 63, // value 639 640 }, 641 builder.DataBuffer.ToFullArray()); 642 } 643 CheckObjects(int fieldCount, int objectCount)644 private void CheckObjects(int fieldCount, int objectCount) 645 { 646 _lcg.Reset(); 647 648 const int testValuesMax = 11; 649 650 var builder = new FlatBufferBuilder(1); 651 652 var objects = new int[objectCount]; 653 654 for (var i = 0; i < objectCount; ++i) 655 { 656 builder.StartTable(fieldCount); 657 658 for (var j = 0; j < fieldCount; ++j) 659 { 660 var fieldType = _lcg.Next()%testValuesMax; 661 662 switch (fieldType) 663 { 664 case 0: 665 { 666 builder.AddBool(j, FuzzTestData.BoolValue, false); 667 break; 668 } 669 case 1: 670 { 671 builder.AddSbyte(j, FuzzTestData.Int8Value, 0); 672 break; 673 } 674 case 2: 675 { 676 builder.AddByte(j, FuzzTestData.UInt8Value, 0); 677 break; 678 } 679 case 3: 680 { 681 builder.AddShort(j, FuzzTestData.Int16Value, 0); 682 break; 683 } 684 case 4: 685 { 686 builder.AddUshort(j, FuzzTestData.UInt16Value, 0); 687 break; 688 } 689 case 5: 690 { 691 builder.AddInt(j, FuzzTestData.Int32Value, 0); 692 break; 693 } 694 case 6: 695 { 696 builder.AddUint(j, FuzzTestData.UInt32Value, 0); 697 break; 698 } 699 case 7: 700 { 701 builder.AddLong(j, FuzzTestData.Int64Value, 0); 702 break; 703 } 704 case 8: 705 { 706 builder.AddUlong(j, FuzzTestData.UInt64Value, 0); 707 break; 708 } 709 case 9: 710 { 711 builder.AddFloat(j, FuzzTestData.Float32Value, 0); 712 break; 713 } 714 case 10: 715 { 716 builder.AddDouble(j, FuzzTestData.Float64Value, 0); 717 break; 718 } 719 default: 720 throw new Exception("Unreachable"); 721 } 722 723 } 724 725 var offset = builder.EndTable(); 726 727 // Store the object offset 728 objects[i] = offset; 729 } 730 731 _lcg.Reset(); 732 733 // Test all objects are readable and return expected values... 734 for (var i = 0; i < objectCount; ++i) 735 { 736 var table = new TestTable(builder.DataBuffer, builder.DataBuffer.Length - objects[i]); 737 738 for (var j = 0; j < fieldCount; ++j) 739 { 740 var fieldType = _lcg.Next() % testValuesMax; 741 var fc = 2 + j; // 2 == VtableMetadataFields 742 var f = fc * 2; 743 744 switch (fieldType) 745 { 746 case 0: 747 { 748 Assert.AreEqual(FuzzTestData.BoolValue, table.GetSlot(f, false)); 749 break; 750 } 751 case 1: 752 { 753 Assert.AreEqual(FuzzTestData.Int8Value, table.GetSlot(f, (sbyte)0)); 754 break; 755 } 756 case 2: 757 { 758 Assert.AreEqual(FuzzTestData.UInt8Value, table.GetSlot(f, (byte)0)); 759 break; 760 } 761 case 3: 762 { 763 Assert.AreEqual(FuzzTestData.Int16Value, table.GetSlot(f, (short)0)); 764 break; 765 } 766 case 4: 767 { 768 Assert.AreEqual(FuzzTestData.UInt16Value, table.GetSlot(f, (ushort)0)); 769 break; 770 } 771 case 5: 772 { 773 Assert.AreEqual(FuzzTestData.Int32Value, table.GetSlot(f, (int)0)); 774 break; 775 } 776 case 6: 777 { 778 Assert.AreEqual(FuzzTestData.UInt32Value, table.GetSlot(f, (uint)0)); 779 break; 780 } 781 case 7: 782 { 783 Assert.AreEqual(FuzzTestData.Int64Value, table.GetSlot(f, (long)0)); 784 break; 785 } 786 case 8: 787 { 788 Assert.AreEqual(FuzzTestData.UInt64Value, table.GetSlot(f, (ulong)0)); 789 break; 790 } 791 case 9: 792 { 793 Assert.AreEqual(FuzzTestData.Float32Value, table.GetSlot(f, (float)0)); 794 break; 795 } 796 case 10: 797 { 798 Assert.AreEqual(FuzzTestData.Float64Value, table.GetSlot(f, (double)0)); 799 break; 800 } 801 default: 802 throw new Exception("Unreachable"); 803 } 804 805 } 806 807 } 808 809 } 810 } 811 } 812