1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_TESTS_VALIDATION_TEST_INPUT_PARSER_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_VALIDATION_TEST_INPUT_PARSER_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <string> 12 #include <vector> 13 14 namespace mojo { 15 namespace test { 16 17 // Input Format of Mojo Message Validation Tests. 18 // 19 // Data items are separated by whitespaces: 20 // - ' ' (0x20) space; 21 // - '\t' (0x09) horizontal tab; 22 // - '\n' (0x0a) newline; 23 // - '\r' (0x0d) carriage return. 24 // A comment starts with //, extending to the end of the line. 25 // Each data item is of the format [<type>]<value>. The types defined and the 26 // corresponding value formats are described below. 27 // 28 // Type: u1 / u2 / u4 / u8 29 // Description: Little-endian 1/2/4/8-byte unsigned integer. 30 // Value Format: 31 // - Decimal integer: 0|[1-9][0-9]* 32 // - Hexadecimal integer: 0[xX][0-9a-fA-F]+ 33 // - The type prefix (including the square brackets) of 1-byte unsigned 34 // integer is optional. 35 // 36 // Type: s1 / s2 / s4 / s8 37 // Description: Little-endian 1/2/4/8-byte signed integer. 38 // Value Format: 39 // - Decimal integer: [-+]?(0|[1-9][0-9]*) 40 // - Hexadecimal integer: [-+]?0[xX][0-9a-fA-F]+ 41 // 42 // Type: b 43 // Description: Binary sequence of 1 byte. 44 // Value Format: [01]{8} 45 // 46 // Type: f / d 47 // Description: Little-endian IEEE-754 format of float (4 bytes) and double (8 48 // bytes). 49 // Value Format: [-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)? 50 // 51 // Type: dist4 / dist8 52 // Description: Little-endian 4/8-byte unsigned integer. The actual value is set 53 // to the byte distance from the location of this integer to the location of the 54 // anchr item with the same ID. A dist8 and anchr pair can be used to easily 55 // represent an encoded pointer. A dist4 and anchr pair can be used to easily 56 // calculate struct/array size. 57 // Value Format: The value is an ID: [0-9a-zA-Z_]+ 58 // 59 // Type: anchr 60 // Description: Mark an anchor location. It doesnt translate into any actual 61 // data. 62 // Value Format: The value is an ID of the same format as that of dist4/8. 63 // 64 // Type: handles 65 // Description: The number of handles that are associated with the message. This 66 // special item is not part of the message data. If specified, it should be the 67 // first item. 68 // Value Format: The same format as u1/2/4/8. 69 // 70 // EXAMPLE: 71 // 72 // Suppose you have the following Mojo types defined: 73 // struct Bar { 74 // int32_t a; 75 // bool b; 76 // bool c; 77 // }; 78 // struct Foo { 79 // Bar x; 80 // uint32_t y; 81 // }; 82 // 83 // The following describes a valid message whose payload is a Foo struct: 84 // // message header 85 // [dist4]message_header // num_bytes 86 // [u4]3 // version 87 // [u4]0 // type 88 // [u4]1 // flags 89 // [u8]1234 // request_id 90 // [anchr]message_header 91 // 92 // // payload 93 // [dist4]foo // num_bytes 94 // [u4]2 // version 95 // [dist8]bar_ptr // x 96 // [u4]0xABCD // y 97 // [u4]0 // padding 98 // [anchr]foo 99 // 100 // [anchr]bar_ptr 101 // [dist4]bar // num_bytes 102 // [u4]3 // version 103 // [s4]-1 // a 104 // [b]00000010 // b and c 105 // 0 0 0 // padding 106 // [anchr]bar 107 108 // Parses validation test input. 109 // On success, |data| and |num_handles| store the parsing result, 110 // |error_message| is cleared; on failure, |error_message| is set to a message 111 // describing the error, |data| is cleared and |num_handles| set to 0. 112 // Note: For now, this method only works on little-endian platforms. 113 bool ParseValidationTestInput(const std::string& input, 114 std::vector<uint8_t>* data, 115 size_t* num_handles, 116 std::string* error_message); 117 118 } // namespace test 119 } // namespace mojo 120 121 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_VALIDATION_TEST_INPUT_PARSER_H_ 122