| 1 | // Copyright (c) 2016 Sandstorm Development Group, Inc. and contributorscxx . . $ `<file>/src/capnp/canonicalize-test.c++`/ |
| 2 | // Licensed under the MIT License: |
| 3 | // |
| 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | // of this software and associated documentation files (the "Software"), to deal |
| 6 | // in the Software without restriction, including without limitation the rights |
| 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | // copies of the Software, and to permit persons to whom the Software is |
| 9 | // furnished to do so, subject to the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included in |
| 12 | // all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | // THE SOFTWARE. |
| 21 | |
| 22 | #include "message.h"cxx . . $ `<file>/src/capnp/message.h`/ |
| 23 | #include "any.h"cxx . . $ `<file>/src/capnp/any.h`/ |
| 24 | #include <kj/debug.h>cxx . . $ `<file>/src/kj/debug.h`/ |
| 25 | #include <kj/test.h>cxx . . $ `<file>/src/kj/test.h`/ |
| 26 | #include "test-util.h"cxx . . $ `<file>/src/capnp/test-util.h`/ |
| 27 | |
| 28 | namespace capnpcxx . . $ capnp/ { |
| 29 | namespace _cxx . . $ capnp/_/ { // private |
| 30 | using test::TestListscxx . . $ capnp/_/TestLists#; |
| 31 | namespacecxx . . $ capnp/_/`$anonymous_namespace_src/capnp/canonicalize-test.c++`/ { |
| 32 | |
| 33 | |
| 34 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("canonicalize yields canonical message") { |
| 35 | MallocMessageBuildercxx . . $ capnp/MallocMessageBuilder# buildercxx . . $ capnp/MallocMessageBuilder#MallocMessageBuilder(10fb5f1879a95591).; |
| 36 | auto root = builder.initRootcxx . . $ capnp/MessageBuilder#initRoot(f86d3b46b49947da).<TestAllTypescxx . . $ capnp/_/TestAllTypes#>(); |
| 37 | |
| 38 | initTestMessagecxx . . $ capnp/_/initTestMessage(c152dab085ce445f).(root); |
| 39 | |
| 40 | auto canonicalWords = canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(root.asReadercxx . . $ capnproto_test/capnp/test/TestAllTypes#Builder#asReader(671e6dd9e649d589).()); |
| 41 | // Throws an exception on canonicalization failure. |
| 42 | |
| 43 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const capnpcxx . . $ capnp/::wordcxx . . $ capnp/word#> canonicalSegments[1] = {canonicalWords.asPtrcxx . . $ kj/Array#asPtr(3a275942fa90435e).()}; |
| 44 | capnpcxx . . $ capnp/::SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# canonicalReadercxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(canonicalSegments, 1)); |
| 45 | |
| 46 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(AnyStruct::Reader(root.asReader()) == |
| 47 | AnyStruct::Reader(canonicalReader.getRoot<TestAllTypes>())); |
| 48 | } |
| 49 | |
| 50 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("canonicalize succeeds on empty struct") { |
| 51 | MallocMessageBuildercxx . . $ capnp/MallocMessageBuilder# buildercxx . . $ capnp/MallocMessageBuilder#MallocMessageBuilder(10fb5f1879a95591).; |
| 52 | auto root = builder.initRootcxx . . $ capnp/MessageBuilder#initRoot(f86d3b46b49947da).<TestAllTypescxx . . $ capnp/_/TestAllTypes#>(); |
| 53 | |
| 54 | canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(root.asReadercxx . . $ capnproto_test/capnp/test/TestAllTypes#Builder#asReader(671e6dd9e649d589).()); // Throws an exception on canoncalization failure. |
| 55 | } |
| 56 | |
| 57 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("data word with only its most significant bit set does not get truncated") { |
| 58 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> segment = {{ |
| 59 | // Struct pointer, body immediately follows, two data words |
| 60 | 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, |
| 61 | |
| 62 | // First data word |
| 63 | 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 64 | |
| 65 | // Second data word, all zero except most significant bit |
| 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, |
| 67 | }}; |
| 68 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 3)}; |
| 69 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messageReadercxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 70 | |
| 71 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(messageReader.isCanonical()); |
| 72 | auto canonicalWords = canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(messageReader.getRootcxx . . $ capnp/MessageReader#getRoot(9c3d92e2fc371030).<TestAllTypescxx . . $ capnp/_/TestAllTypes#>()); |
| 73 | |
| 74 | // At one point this failed because an off-by-one bug in canonicalization |
| 75 | // caused the second word of the data section to be truncated. |
| 76 | ASSERT_EQcxx . . $ `src/kj/compat/gtest.h:68:9`!(canonicalWords.asBytes(), kj::arrayPtr(segment.bytes, 3 * 8)); |
| 77 | } |
| 78 | |
| 79 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("INLINE_COMPOSITE data word with only its most significant bit set does not get truncated") { |
| 80 | AlignedDatacxx . . $ capnp/_/AlignedData#<5> segment = {{ |
| 81 | // Struct pointer, body immediately follows, one pointer |
| 82 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 83 | |
| 84 | // List pointer, no offset, inline composite, two words long |
| 85 | 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, |
| 86 | |
| 87 | // Tag word, list has one element with two data words and no pointers |
| 88 | 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, |
| 89 | |
| 90 | // First data word |
| 91 | 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, |
| 92 | |
| 93 | // Second data word, all zero except most significant bit |
| 94 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, |
| 95 | }}; |
| 96 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 5)}; |
| 97 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messageReadercxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 98 | |
| 99 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(messageReader.isCanonical()); |
| 100 | auto canonicalWords = canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(messageReader.getRootcxx . . $ capnp/MessageReader#getRoot(9c3d92e2fc371030).<TestListscxx . . $ capnp/_/TestLists#>()); |
| 101 | |
| 102 | // At one point this failed because an off-by-one bug in canonicalization |
| 103 | // caused the second word of the data section to be truncated. |
| 104 | ASSERT_EQcxx . . $ `src/kj/compat/gtest.h:68:9`!(canonicalWords.asBytes(), kj::arrayPtr(segment.bytes, 5 * 8)); |
| 105 | } |
| 106 | |
| 107 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("canonical non-null empty struct field") { |
| 108 | AlignedDatacxx . . $ capnp/_/AlignedData#<4> nonNullEmptyStruct = {{ |
| 109 | // Struct pointer, body immediately follows, two pointer fields, no data. |
| 110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, |
| 111 | |
| 112 | // First pointer field, struct, offset of 1, data size 1, no pointers. |
| 113 | 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 114 | |
| 115 | // Non-null pointer to empty struct. |
| 116 | 0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 117 | |
| 118 | // Body of struct filled with non-zero data. |
| 119 | 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, |
| 120 | }}; |
| 121 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(nonNullEmptyStruct.wordscxx . . $ capnp/_/AlignedData#words., 4)}; |
| 122 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messageReadercxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 123 | |
| 124 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(messageReader.isCanonical()); |
| 125 | } |
| 126 | |
| 127 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("for pointers to empty structs, preorder is not canonical") { |
| 128 | AlignedDatacxx . . $ capnp/_/AlignedData#<4> nonNullEmptyStruct = {{ |
| 129 | // Struct pointer, body immediately follows, two pointer fields, no data. |
| 130 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, |
| 131 | |
| 132 | // First pointer field, struct, offset of 1, data size 1, no pointers. |
| 133 | 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 134 | |
| 135 | // Non-null pointer to empty struct. Offset puts it in "preorder". Would need to have |
| 136 | // an offset of -1 to be canonical. |
| 137 | 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 138 | |
| 139 | // Body of struct filled with non-zero data. |
| 140 | 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, |
| 141 | }}; |
| 142 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(nonNullEmptyStruct.wordscxx . . $ capnp/_/AlignedData#words., 4)}; |
| 143 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messageReadercxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 144 | |
| 145 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!messageReader.isCanonical()); |
| 146 | } |
| 147 | |
| 148 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical requires pointer preorder") { |
| 149 | AlignedDatacxx . . $ capnp/_/AlignedData#<5> misorderedSegment = {{ |
| 150 | //Struct pointer, data immediately follows, two pointer fields, no data |
| 151 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, |
| 152 | //Pointer field 1, pointing to the last entry, data size 1, no pointer |
| 153 | 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 154 | //Pointer field 2, pointing to the next entry, data size 2, no pointer |
| 155 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 156 | //Data for field 2 |
| 157 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 158 | //Data for field 1 |
| 159 | 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 |
| 160 | }}; |
| 161 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(misorderedSegment.wordscxx . . $ capnp/_/AlignedData#words., |
| 162 | 5)}; |
| 163 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# outOfOrdercxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 164 | |
| 165 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!outOfOrder.isCanonical()); |
| 166 | } |
| 167 | |
| 168 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical requires dense packing") { |
| 169 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> gapSegment = {{ |
| 170 | //Struct pointer, data after a gap |
| 171 | 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 172 | //The gap |
| 173 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 174 | //Data for field 1 |
| 175 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 176 | }}; |
| 177 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(gapSegment.wordscxx . . $ capnp/_/AlignedData#words., |
| 178 | 3)}; |
| 179 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# gapcxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 180 | |
| 181 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!gap.isCanonical()); |
| 182 | } |
| 183 | |
| 184 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical rejects multi-segment messages") { |
| 185 | AlignedDatacxx . . $ capnp/_/AlignedData#<1> farPtr = {{ |
| 186 | //Far pointer to next segment |
| 187 | 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 188 | }}; |
| 189 | |
| 190 | AlignedDatacxx . . $ capnp/_/AlignedData#<2> farTarget = {{ |
| 191 | //Struct pointer (needed to make the far pointer legal) |
| 192 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 193 | //Dummy data |
| 194 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 195 | }}; |
| 196 | |
| 197 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[2] = { |
| 198 | kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(farPtr.wordscxx . . $ capnp/_/AlignedData#words., 1), |
| 199 | kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(farTarget.wordscxx . . $ capnp/_/AlignedData#words., 2) |
| 200 | }; |
| 201 | |
| 202 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# multiSegmentMessagecxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 2)); |
| 203 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!multiSegmentMessage.isCanonical()); |
| 204 | } |
| 205 | |
| 206 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical rejects zero segment messages") { |
| 207 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# zerocxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).((kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#>*)NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, |
| 208 | 0)); |
| 209 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!zero.isCanonical()); |
| 210 | } |
| 211 | |
| 212 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical requires truncation of 0-valued struct fields") { |
| 213 | AlignedDatacxx . . $ capnp/_/AlignedData#<2> nonTruncatedSegment = {{ |
| 214 | //Struct pointer, data immediately follows |
| 215 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 216 | //Default data value, should have been truncated |
| 217 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 218 | }}; |
| 219 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = { |
| 220 | kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(nonTruncatedSegment.wordscxx . . $ capnp/_/AlignedData#words., 3) |
| 221 | }; |
| 222 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# nonTruncatedcxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 223 | |
| 224 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!nonTruncated.isCanonical()); |
| 225 | } |
| 226 | |
| 227 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical rejects unused trailing words") { |
| 228 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> segment = {{ |
| 229 | // Struct pointer, data in next word |
| 230 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 231 | |
| 232 | // Data section of struct |
| 233 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 234 | |
| 235 | // Trailing zero word |
| 236 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 237 | }}; |
| 238 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 3)}; |
| 239 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messagecxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 240 | |
| 241 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!message.isCanonical()); |
| 242 | } |
| 243 | |
| 244 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical accepts empty inline composite list of zero-sized structs") { |
| 245 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> segment = {{ |
| 246 | // Struct pointer, pointer in next word |
| 247 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 248 | |
| 249 | // List pointer, inline composite, zero words long |
| 250 | 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, |
| 251 | |
| 252 | // Tag word |
| 253 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 254 | }}; |
| 255 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 3)}; |
| 256 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messagecxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 257 | |
| 258 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(message.isCanonical()); |
| 259 | } |
| 260 | |
| 261 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical rejects inline composite list with inaccurate word-length") { |
| 262 | AlignedDatacxx . . $ capnp/_/AlignedData#<6> segment = {{ |
| 263 | // Struct pointer, no offset, pointer section has two entries |
| 264 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, |
| 265 | |
| 266 | // List pointer, offset of one, inline composite, two words long |
| 267 | // (The list only needs to be one word long to hold its actual elements; |
| 268 | // therefore this message is not canonical.) |
| 269 | 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, |
| 270 | |
| 271 | // Struct pointer, offset two, data section has one word |
| 272 | 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 273 | |
| 274 | // Tag word, struct, one element, one word data section |
| 275 | 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 276 | |
| 277 | // Data section of struct element of list |
| 278 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 279 | |
| 280 | // Data section of struct field in top-level struct |
| 281 | 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, |
| 282 | }}; |
| 283 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 6)}; |
| 284 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messagecxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 285 | |
| 286 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!message.isCanonical()); |
| 287 | } |
| 288 | |
| 289 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("upgraded lists can be canonicalized") { |
| 290 | AlignedDatacxx . . $ capnp/_/AlignedData#<7> upgradedList = {{ |
| 291 | //Struct pointer, data immediately follows, 4 pointer fields, no data |
| 292 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, |
| 293 | //Three words of default pointers to get to the int16 list |
| 294 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 295 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 296 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 297 | //List pointer, 3 int16s. |
| 298 | 0x01, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, |
| 299 | //First two elements |
| 300 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x05, 0x06, |
| 301 | //Last element |
| 302 | 0x07, 0x08, 0x09, 0x10, 0x00, 0x00, 0x00, 0x00 |
| 303 | }}; |
| 304 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = { |
| 305 | kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(upgradedList.wordscxx . . $ capnp/_/AlignedData#words., 7) |
| 306 | }; |
| 307 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# upgradedcxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 308 | |
| 309 | auto root = upgraded.getRootcxx . . $ capnp/MessageReader#getRoot(9c3d92e2fc371030).<TestListscxx . . $ capnp/_/TestLists#>(); |
| 310 | canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(root); |
| 311 | } |
| 312 | |
| 313 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("isCanonical requires truncation of 0-valued struct fields in all list members") { |
| 314 | AlignedDatacxx . . $ capnp/_/AlignedData#<6> nonTruncatedList = {{ |
| 315 | //List pointer, composite, |
| 316 | 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, |
| 317 | //Struct tag word, 2 structs, 2 data words per struct |
| 318 | 0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, |
| 319 | //Data word non-null |
| 320 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 321 | //Null trailing word |
| 322 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 323 | //Data word non-null |
| 324 | 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, |
| 325 | //Null trailing word |
| 326 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 327 | }}; |
| 328 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = { |
| 329 | kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(nonTruncatedList.wordscxx . . $ capnp/_/AlignedData#words., 6) |
| 330 | }; |
| 331 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# nonTruncatedcxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 332 | |
| 333 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!nonTruncated.isCanonical()); |
| 334 | } |
| 335 | |
| 336 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("primitive list with nonzero padding") { |
| 337 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> segment = {{ |
| 338 | // Struct, one pointer field. |
| 339 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 340 | |
| 341 | // List of three byte-sized elements. |
| 342 | 0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, |
| 343 | |
| 344 | // Fourth byte is non-zero! |
| 345 | 0x01, 0x02, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 346 | }}; |
| 347 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 3)}; |
| 348 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messagecxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 349 | |
| 350 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!message.isCanonical()); |
| 351 | |
| 352 | auto canonicalWords = canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(message.getRootcxx . . $ capnp/MessageReader#getRoot(9c3d92e2fc371030).<test::TestAnyPointercxx . . $ capnproto_test/capnp/test/TestAnyPointer#>()); |
| 353 | |
| 354 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> canonicalSegment = {{ |
| 355 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 356 | 0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, |
| 357 | 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 358 | }}; |
| 359 | |
| 360 | ASSERT_EQcxx . . $ `src/kj/compat/gtest.h:68:9`!(canonicalWords.asBytes(), kj::arrayPtr(canonicalSegment.bytes, 3 * 8)); |
| 361 | } |
| 362 | |
| 363 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("bit list with nonzero padding") { |
| 364 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> segment = {{ |
| 365 | // Struct, one pointer field. |
| 366 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 367 | |
| 368 | // List of eleven bit-sized elements. |
| 369 | 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, |
| 370 | |
| 371 | // Twelfth bit is non-zero! |
| 372 | 0xee, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 373 | }}; |
| 374 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const wordcxx . . $ capnp/word#> segments[1] = {kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segment.wordscxx . . $ capnp/_/AlignedData#words., 3)}; |
| 375 | SegmentArrayMessageReadercxx . . $ capnp/SegmentArrayMessageReader# messagecxx . . $ capnp/SegmentArrayMessageReader#SegmentArrayMessageReader(2c04efb88a49d2d6).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(segments, 1)); |
| 376 | |
| 377 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(!message.isCanonical()); |
| 378 | |
| 379 | auto canonicalWords = canonicalizecxx . . $ capnp/canonicalize(e0e7eec87cd0b62c).(message.getRootcxx . . $ capnp/MessageReader#getRoot(9c3d92e2fc371030).<test::TestAnyPointercxx . . $ capnproto_test/capnp/test/TestAnyPointer#>()); |
| 380 | |
| 381 | AlignedDatacxx . . $ capnp/_/AlignedData#<3> canonicalSegment = {{ |
| 382 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 383 | 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, |
| 384 | 0xee, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 385 | }}; |
| 386 | |
| 387 | ASSERT_EQcxx . . $ `src/kj/compat/gtest.h:68:9`!(canonicalWords.asBytes(), kj::arrayPtr(canonicalSegment.bytes, 3 * 8)); |
| 388 | } |
| 389 | |
| 390 | } // namespace |
| 391 | } // namespace _ (private) |
| 392 | } // namespace capnp |