canonicalize-test.c++ 12.4 KB
Newer Older
1
// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors
Matthew Maurer's avatar
Matthew Maurer committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "message.h"
#include "any.h"
#include <kj/debug.h>
#include <kj/test.h>
#include "test-util.h"

namespace capnp {
namespace _ {  // private
30
using test::TestLists;
Matthew Maurer's avatar
Matthew Maurer committed
31 32
namespace {

33

34
KJ_TEST("canonicalize yields canonical message") {
Matthew Maurer's avatar
Matthew Maurer committed
35 36 37 38 39
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestAllTypes>();

  initTestMessage(root);

40 41 42 43 44 45 46 47
  auto canonicalWords = canonicalize(root.asReader());
  // Throws an exception on canonicalization failure.

  kj::ArrayPtr<const capnp::word> canonicalSegments[1] = {canonicalWords.asPtr()};
  capnp::SegmentArrayMessageReader canonicalReader(kj::arrayPtr(canonicalSegments, 1));

  KJ_ASSERT(AnyStruct::Reader(root.asReader()) ==
            AnyStruct::Reader(canonicalReader.getRoot<TestAllTypes>()));
Matthew Maurer's avatar
Matthew Maurer committed
48 49
}

50 51 52 53 54 55 56
KJ_TEST("canonicalize succeeds on empty struct") {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestAllTypes>();

  canonicalize(root.asReader());  // Throws an exception on canoncalization failure.
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
KJ_TEST("data word with only its most significant bit set does not get truncated") {
  AlignedData<3> segment = {{
    // Struct pointer, body immediately follows, two data words
    0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,

    // First data word
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,

    // Second data word, all zero except most significant bit
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(segment.words, 3)};
  SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1));

  KJ_ASSERT(messageReader.isCanonical());
  auto canonicalWords = canonicalize(messageReader.getRoot<TestAllTypes>());

  // At one point this failed because an off-by-one bug in canonicalization
  // caused the second word of the data section to be truncated.
  ASSERT_EQ(canonicalWords.asBytes(), kj::arrayPtr(segment.bytes, 3 * 8));
}

KJ_TEST("INLINE_COMPOSITE data word with only its most significant bit set does not get truncated") {
  AlignedData<5> segment = {{
    // Struct pointer, body immediately follows, one pointer
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,

    // List pointer, no offset, inline composite, two words long
    0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,

    // Tag word, list has one element with two data words and no pointers
    0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,

    // First data word
    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,

    // Second data word, all zero except most significant bit
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(segment.words, 5)};
  SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1));

  KJ_ASSERT(messageReader.isCanonical());
  auto canonicalWords = canonicalize(messageReader.getRoot<TestLists>());

  // At one point this failed because an off-by-one bug in canonicalization
  // caused the second word of the data section to be truncated.
  ASSERT_EQ(canonicalWords.asBytes(), kj::arrayPtr(segment.bytes, 5 * 8));
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
KJ_TEST("canonical non-null empty struct field") {
  AlignedData<4> nonNullEmptyStruct = {{
    // Struct pointer, body immediately follows, two pointer fields, no data.
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,

    // First pointer field, struct, offset of 1, data size 1, no pointers.
    0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,

    // Non-null pointer to empty struct.
    0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,

    // Body of struct filled with non-zero data.
    0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(nonNullEmptyStruct.words, 4)};
  SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1));

  KJ_ASSERT(messageReader.isCanonical());
}

KJ_TEST("for pointers to empty structs, preorder is not canonical") {
  AlignedData<4> nonNullEmptyStruct = {{
    // Struct pointer, body immediately follows, two pointer fields, no data.
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,

    // First pointer field, struct, offset of 1, data size 1, no pointers.
    0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,

    // Non-null pointer to empty struct. Offset puts it in "preorder". Would need to have
    // an offset of -1 to be canonical.
    0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

    // Body of struct filled with non-zero data.
    0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(nonNullEmptyStruct.words, 4)};
  SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!messageReader.isCanonical());
}

Matthew Maurer's avatar
Matthew Maurer committed
148 149 150 151 152
KJ_TEST("isCanonical requires pointer preorder") {
  AlignedData<5> misorderedSegment = {{
    //Struct pointer, data immediately follows, two pointer fields, no data
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
    //Pointer field 1, pointing to the last entry, data size 1, no pointer
153
    0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
Matthew Maurer's avatar
Matthew Maurer committed
154 155 156 157 158 159 160 161
    //Pointer field 2, pointing to the next entry, data size 2, no pointer
    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    //Data for field 2
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    //Data for field 1
    0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(misorderedSegment.words,
162
                                                       5)};
Matthew Maurer's avatar
Matthew Maurer committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  SegmentArrayMessageReader outOfOrder(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!outOfOrder.isCanonical());
}

KJ_TEST("isCanonical requires dense packing") {
   AlignedData<3> gapSegment = {{
    //Struct pointer, data after a gap
    0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    //The gap
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //Data for field 1
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(gapSegment.words,
                                                       3)};
  SegmentArrayMessageReader gap(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!gap.isCanonical());
}

KJ_TEST("isCanonical rejects multi-segment messages") {
  AlignedData<1> farPtr = {{
    //Far pointer to next segment
    0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  }};

  AlignedData<2> farTarget = {{
    //Struct pointer (needed to make the far pointer legal)
    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    //Dummy data
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  }};

  kj::ArrayPtr<const word> segments[2] = {
    kj::arrayPtr(farPtr.words, 1),
    kj::arrayPtr(farTarget.words, 2)
  };

  SegmentArrayMessageReader multiSegmentMessage(kj::arrayPtr(segments, 2));
  KJ_ASSERT(!multiSegmentMessage.isCanonical());
}

KJ_TEST("isCanonical rejects zero segment messages") {
  SegmentArrayMessageReader zero(kj::arrayPtr((kj::ArrayPtr<const word>*)NULL,
                                               0));
  KJ_ASSERT(!zero.isCanonical());
}

KJ_TEST("isCanonical requires truncation of 0-valued struct fields") {
  AlignedData<2> nonTruncatedSegment = {{
    //Struct pointer, data immediately follows
    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    //Default data value, should have been truncated
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  }};
  kj::ArrayPtr<const word> segments[1] = {
    kj::arrayPtr(nonTruncatedSegment.words, 3)
  };
  SegmentArrayMessageReader nonTruncated(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!nonTruncated.isCanonical());
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
KJ_TEST("isCanonical rejects unused trailing words") {
   AlignedData<3> segment = {{
    // Struct pointer, data in next word
    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,

    // Data section of struct
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

    // Trailing zero word
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(segment.words, 3)};
  SegmentArrayMessageReader message(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!message.isCanonical());
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
KJ_TEST("isCanonical accepts empty inline composite list of zero-sized structs") {
   AlignedData<3> segment = {{
    // Struct pointer, pointer in next word
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,

    // List pointer, inline composite, zero words long
    0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,

    // Tag word
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(segment.words, 3)};
  SegmentArrayMessageReader message(kj::arrayPtr(segments, 1));

  KJ_ASSERT(message.isCanonical());
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
KJ_TEST("isCanonical rejects inline composite list with inaccurate word-length") {
   AlignedData<6> segment = {{
    // Struct pointer, no offset, pointer section has two entries
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,

    // List pointer, offset of one, inline composite, two words long
    // (The list only needs to be one word long to hold its actual elements;
    // therefore this message is not canonical.)
    0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,

    // Struct pointer, offset two, data section has one word
    0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,

    // Tag word, struct, one element, one word data section
    0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,

    // Data section of struct element of list
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

    // Data section of struct field in top-level struct
    0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
  }};
  kj::ArrayPtr<const word> segments[1] = {kj::arrayPtr(segment.words, 6)};
  SegmentArrayMessageReader message(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!message.isCanonical());
}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
KJ_TEST("upgraded lists can be canonicalized") {
  AlignedData<7> upgradedList = {{
    //Struct pointer, data immediately follows, 4 pointer fields, no data
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
    //Three words of default pointers to get to the int16 list
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //List pointer, 3 int16s.
    0x01, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
    //First two elements
    0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x05, 0x06,
    //Last element
    0x07, 0x08, 0x09, 0x10, 0x00, 0x00, 0x00, 0x00
  }};
  kj::ArrayPtr<const word> segments[1] = {
    kj::arrayPtr(upgradedList.words, 7)
  };
  SegmentArrayMessageReader upgraded(kj::arrayPtr(segments, 1));

  auto root = upgraded.getRoot<TestLists>();
310
  canonicalize(root);
311 312
}

313
KJ_TEST("isCanonical requires truncation of 0-valued struct fields in all list members") {
Matthew Maurer's avatar
Matthew Maurer committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  AlignedData<6> nonTruncatedList = {{
    //List pointer, composite,
    0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
    //Struct tag word, 2 structs, 2 data words per struct
    0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
    //Data word non-null
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    //Null trailing word
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //Data word non-null
    0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
    //Null trailing word
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  }};
  kj::ArrayPtr<const word> segments[1] = {
    kj::arrayPtr(nonTruncatedList.words, 6)
  };
  SegmentArrayMessageReader nonTruncated(kj::arrayPtr(segments, 1));

  KJ_ASSERT(!nonTruncated.isCanonical());
}
}  // namespace
}  // namespace _ (private)
}  // namespace capnp