canonicalize-test.c++ 5.29 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 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
// 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
namespace {

KJ_TEST("canonicalize yields cannonical message") {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestAllTypes>();

  initTestMessage(root);

  MallocMessageBuilder canonicalMessage;
  canonicalMessage.canonicalRoot(builder.getRoot<AnyPointer>().asReader());
  KJ_ASSERT(canonicalMessage.isCanonical());
}

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
    0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    //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,
                                                       3)};
  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());
}

KJ_TEST("isCanonical requires truncation of 0-valued struct fields\
          in all list members") {
  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