canonicalize-test.c++ 6.09 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

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

  initTestMessage(root);

40
  canonicalize(root.asReader());
41
  //Will assert if canonicalize failed to do so
Matthew Maurer's avatar
Matthew Maurer committed
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
}

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());
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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>();
144
  canonicalize(root);
145 146
}

147
KJ_TEST("isCanonical requires truncation of 0-valued struct fields in all list members") {
Matthew Maurer's avatar
Matthew Maurer committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  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