/**
 * Check that strings do the right thing
 */
void MessageDeserializerTest::testString() {
  vector<const FieldDescriptor*> fields;
  fields.push_back(new StringFieldDescriptor("string", 10, 10));
  fields.push_back(new StringFieldDescriptor("string", 0, 32));
  Descriptor descriptor("Test Descriptor", fields);

  // now setup the data
  const uint8_t data[] = "0123456789this is a longer string";

  // try to inflate with too little
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      0));
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      9));

  // try to inflat with too much data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      43));

  // now to the right amount
  auto_ptr<const Message> message(m_deserializer.InflateMessage(
      &descriptor,
      data,
      sizeof(data)));
  CPPUNIT_ASSERT(message.get());
  CPPUNIT_ASSERT_EQUAL(2u, message->FieldCount());

  const string expected = (
      "string: 0123456789\nstring: this is a longer string\n");
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(message.get()));

  // now try with different sizes
  auto_ptr<const Message> message2(m_deserializer.InflateMessage(
      &descriptor,
      data,
      19));
  CPPUNIT_ASSERT(message2.get());
  CPPUNIT_ASSERT_EQUAL(2u, message2->FieldCount());

  const string expected2 = (
      "string: 0123456789\nstring: this is a\n");
  CPPUNIT_ASSERT_EQUAL(expected2, m_printer.AsString(message2.get()));
}
Exemplo n.º 2
0
/*
 * Test the MessagePrinter
 */
void GenericMessagePrinterTest::testSimplePrinter() {
  // setup some fields
  BoolFieldDescriptor bool_descriptor("On/Off");
  IPV4FieldDescriptor ipv4_descriptor("ip");
  UIDFieldDescriptor uid_descriptor("uid");
  StringFieldDescriptor string_descriptor("Name", 0, 32);
  UInt32FieldDescriptor uint32_descriptor("Id");
  UInt8FieldDescriptor uint8_descriptor("Count", false, -3);
  Int8FieldDescriptor int8_descriptor("Delta", false, 1);
  Int16FieldDescriptor int16_descriptor("Rate", false, -1);

  // try a simple print first
  vector<const ola::messaging::MessageFieldInterface*> fields;
  fields.push_back(new BoolMessageField(&bool_descriptor, false));
  fields.push_back(new IPV4MessageField(&ipv4_descriptor, 0x0100000a));
  fields.push_back(new UIDMessageField(&uid_descriptor, UID(0x7a70, 1)));
  fields.push_back(new StringMessageField(&string_descriptor, "foobar"));
  fields.push_back(new UInt32MessageField(&uint32_descriptor, 42));
  fields.push_back(new UInt8MessageField(&uint8_descriptor, 4));
  fields.push_back(new Int8MessageField(&int8_descriptor, 10));
  fields.push_back(new Int16MessageField(&int16_descriptor, 10));

  Message message(fields);
  string expected = (
      "On/Off: false\nip: 10.0.0.1\nuid: 7a70:00000001\nName: foobar\nId: 42\n"
      "Count: 4 x 10 ^ -3\nDelta: 10 x 10 ^ 1\nRate: 10 x 10 ^ -1\n");
  OLA_ASSERT_EQ(expected, m_printer.AsString(&message));
}
/**
 * Test that simple (no variable sized fields) work big endian style.
 */
void MessageDeserializerTest::testSimpleBigEndian() {
  // build the descriptor
  vector<const FieldDescriptor*> fields;
  // All multi-byte fields default to big endian
  fields.push_back(new BoolFieldDescriptor("bool"));
  fields.push_back(new UInt8FieldDescriptor("uint8"));
  fields.push_back(new Int8FieldDescriptor("int8"));
  fields.push_back(new UInt16FieldDescriptor("uint16"));
  fields.push_back(new Int16FieldDescriptor("int16"));
  fields.push_back(new UInt32FieldDescriptor("uint32"));
  fields.push_back(new Int32FieldDescriptor("int32"));
  Descriptor descriptor("Test Descriptor", fields);

  // now setup the data
  const uint8_t big_endian_data[] = {
    0, 10, -10, 1, 0x2c, 0xfe, 10,
    1, 2, 3, 4, 0xfe, 6, 7, 8};

  // try to inflate with no data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      NULL,
      0));

  // now inflate with too little data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      big_endian_data,
      1));

  // now inflate with too much data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      big_endian_data,
      sizeof(big_endian_data) + 1));

  // now the correct amount & verify
  auto_ptr<const Message> message(m_deserializer.InflateMessage(
      &descriptor,
      big_endian_data,
      sizeof(big_endian_data)));
  CPPUNIT_ASSERT(message.get());
  CPPUNIT_ASSERT_EQUAL(7u, message->FieldCount());

  const string expected = (
      "bool: false\nuint8: 10\nint8: -10\nuint16: 300\nint16: -502\n"
      "uint32: 16909060\nint32: -33159416\n");
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(message.get()));
}
/**
 * Test that simple (no variable sized fields) work little endian style.
 */
void MessageDeserializerTest::testSimpleLittleEndian() {
  // build the descriptor
  vector<const FieldDescriptor*> fields;
  fields.push_back(new BoolFieldDescriptor("bool"));
  fields.push_back(new UInt8FieldDescriptor("uint8"));
  fields.push_back(new Int8FieldDescriptor("int8"));
  fields.push_back(new UInt16FieldDescriptor("uint16", true));
  fields.push_back(new Int16FieldDescriptor("int16", true));
  fields.push_back(new UInt32FieldDescriptor("uint32", true));
  fields.push_back(new Int32FieldDescriptor("int32", true));
  Descriptor descriptor("Test Descriptor", fields);

  // now setup the data
  const uint8_t little_endian_data[] = {
    1, 10, 246, 0x2c, 1, 10, 0xfe,
    4, 3, 2, 1, 8, 7, 6, 0xfe};

  // try to inflate with no data
  OLA_ASSERT_NULL(m_deserializer.InflateMessage(
      &descriptor,
      NULL,
      0));

  // now inflate with too little data
  OLA_ASSERT_NULL(m_deserializer.InflateMessage(
      &descriptor,
      little_endian_data,
      1));

  // now inflate with too much data
  OLA_ASSERT_NULL(m_deserializer.InflateMessage(
      &descriptor,
      little_endian_data,
      sizeof(little_endian_data) + 1));

  // now the correct amount & verify
  auto_ptr<const Message> message(m_deserializer.InflateMessage(
      &descriptor,
      little_endian_data,
      sizeof(little_endian_data)));
  OLA_ASSERT_NOT_NULL(message.get());
  OLA_ASSERT_EQ(7u, message->FieldCount());

  const string expected = (
      "bool: true\nuint8: 10\nint8: -10\nuint16: 300\nint16: -502\n"
      "uint32: 16909060\nint32: -33159416\n");
  OLA_ASSERT_EQ(expected, m_printer.AsString(message.get()));
}
Exemplo n.º 5
0
/**
 * Check that labels are added
 */
void GenericMessagePrinterTest::testLabeledPrinter() {
  UInt8FieldDescriptor::IntervalVector intervals;
  intervals.push_back(UInt8FieldDescriptor::Interval(0, 2));

  UInt8FieldDescriptor::LabeledValues labels;
  labels["off"] = 0;
  labels["on"] = 1;
  labels["auto"] = 2;

  UInt8FieldDescriptor uint8_descriptor("State", intervals, labels);

  vector<const ola::messaging::MessageFieldInterface*> fields;
  fields.push_back(new UInt8MessageField(&uint8_descriptor, 0));
  fields.push_back(new UInt8MessageField(&uint8_descriptor, 1));
  fields.push_back(new UInt8MessageField(&uint8_descriptor, 2));

  Message message(fields);
  string expected = "State: off\nState: on\nState: auto\n";
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(&message));
}
/**
 * Test UID inflation.
 */
void MessageDeserializerTest::testUID() {
  // build the descriptor
  vector<const FieldDescriptor*> fields;
  fields.push_back(new UIDFieldDescriptor("Address"));
  Descriptor descriptor("Test Descriptor", fields);

  // now setup the data
  const uint8_t big_endian_data[] = {0x70, 0x7a, 0, 0, 0, 1};

  // now the correct amount & verify
  auto_ptr<const Message> message(m_deserializer.InflateMessage(
      &descriptor,
      big_endian_data,
      sizeof(big_endian_data)));
  CPPUNIT_ASSERT(message.get());
  CPPUNIT_ASSERT_EQUAL(1u, message->FieldCount());

  const string expected = "Address: 707a:00000001\n";
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(message.get()));
}
Exemplo n.º 7
0
void GenericMessagePrinterTest::testNestedPrinter() {
  // this holds some information on people
  StringFieldDescriptor *string_descriptor = new StringFieldDescriptor(
      "Name", 0, 32);
  BoolFieldDescriptor *bool_descriptor = new BoolFieldDescriptor("Female");
  UInt8FieldDescriptor *uint8_descriptor = new UInt8FieldDescriptor("Age");

  vector<const FieldDescriptor*> person_fields;
  person_fields.push_back(string_descriptor);
  person_fields.push_back(bool_descriptor);
  person_fields.push_back(uint8_descriptor);
  FieldDescriptorGroup group_descriptor("Person", person_fields, 0, 10);

  // setup the first person
  vector<const MessageFieldInterface*> person1;
  person1.push_back(new StringMessageField(string_descriptor, "Lisa"));
  person1.push_back(new BoolMessageField(bool_descriptor, true));
  person1.push_back(new UInt8MessageField(uint8_descriptor, 21));

  // setup the second person
  vector<const MessageFieldInterface*> person2;
  person2.push_back(new StringMessageField(string_descriptor, "Simon"));
  person2.push_back(new BoolMessageField(bool_descriptor, false));
  person2.push_back(new UInt8MessageField(uint8_descriptor, 26));

  vector<const ola::messaging::MessageFieldInterface*> messages;
  messages.push_back(new GroupMessageField(&group_descriptor, person1));
  messages.push_back(new GroupMessageField(&group_descriptor, person2));

  Message message(messages);

  string expected = (
      "Person {\n  Name: Lisa\n  Female: true\n  Age: 21\n}\n"
      "Person {\n  Name: Simon\n  Female: false\n  Age: 26\n}\n");
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(&message));
}
/*
 * Test MessageSerializer with nested fixed groups.
 */
void MessageDeserializerTest::testWithNestedFixedGroups() {
  vector<const FieldDescriptor*> fields, group_fields, group_fields2;
  group_fields.push_back(new BoolFieldDescriptor("bool"));
  group_fields2.push_back(new UInt8FieldDescriptor("uint8"));
  group_fields2.push_back(new FieldDescriptorGroup("bar", group_fields, 2, 2));
  fields.push_back(new FieldDescriptorGroup("", group_fields2, 0, 4));
  Descriptor descriptor("Test Descriptor", fields);

  // now setup the inputs
  const uint8_t data[] = {0, 0, 0, 1, 0, 1, 2, 1, 0, 3, 1, 1};

  // an empty mesage
  auto_ptr<const Message> message(m_deserializer.InflateMessage(
      &descriptor,
      data,
      0));
  CPPUNIT_ASSERT(message.get());
  CPPUNIT_ASSERT_EQUAL(0u, message->FieldCount());

  // message with not enough data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      1));
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      2));

  // a single instance of a group
  auto_ptr<const Message> message2(m_deserializer.InflateMessage(
      &descriptor,
      data,
      3));
  CPPUNIT_ASSERT(message2.get());
  CPPUNIT_ASSERT_EQUAL(1u, message2->FieldCount());

  const string expected = (
      " {\n  uint8: 0\n  bar {\n    bool: false\n  }\n  bar {\n"
      "    bool: false\n  }\n}\n");
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(message2.get()));

  // four instances
  auto_ptr<const Message> message3(m_deserializer.InflateMessage(
      &descriptor,
      data,
      sizeof(data)));
  CPPUNIT_ASSERT(message3.get());
  CPPUNIT_ASSERT_EQUAL(4u, message3->FieldCount());

  const string expected2 = (
      " {\n  uint8: 0\n  bar {\n    bool: false\n  }\n  bar {\n"
      "    bool: false\n  }\n}\n"
      " {\n  uint8: 1\n  bar {\n    bool: false\n  }\n  bar {\n"
      "    bool: true\n  }\n}\n"
      " {\n  uint8: 2\n  bar {\n    bool: true\n  }\n  bar {\n"
      "    bool: false\n  }\n}\n"
      " {\n  uint8: 3\n  bar {\n    bool: true\n  }\n  bar {\n"
      "    bool: true\n  }\n}\n");
  CPPUNIT_ASSERT_EQUAL(expected2, m_printer.AsString(message3.get()));

  // too much data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      sizeof(data) + 1));
}
/*
 * Check the MessageSerializer works with variable sized groups.
 */
void MessageDeserializerTest::testWithGroups() {
  // build the descriptor
  vector<const FieldDescriptor*> group_fields;
  group_fields.push_back(new BoolFieldDescriptor("bool"));
  group_fields.push_back(new UInt8FieldDescriptor("uint8"));

  vector<const FieldDescriptor*> fields;
  fields.push_back(new FieldDescriptorGroup("group", group_fields, 0, 3));
  Descriptor descriptor("Test Descriptor", fields);

  // now setup the inputs
  const uint8_t data[] = {0, 10, 1, 3, 0, 20, 1, 40};

  // an empty message
  auto_ptr<const Message> message(m_deserializer.InflateMessage(
      &descriptor,
      data,
      0));
  CPPUNIT_ASSERT(message.get());
  CPPUNIT_ASSERT_EQUAL(0u, message->FieldCount());

  // message with not enough data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      1));

  // a single instance of a group
  auto_ptr<const Message> message2(m_deserializer.InflateMessage(
      &descriptor,
      data,
      2));
  CPPUNIT_ASSERT(message2.get());
  CPPUNIT_ASSERT_EQUAL(1u,
                       message2->FieldCount());

  const string expected = "group {\n  bool: false\n  uint8: 10\n}\n";
  CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(message2.get()));

  // another message with not enough data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      3));

  // two instances of the group
  auto_ptr<const Message> message3(m_deserializer.InflateMessage(
      &descriptor,
      data,
      4));
  CPPUNIT_ASSERT(message3.get());
  CPPUNIT_ASSERT_EQUAL(2u, message3->FieldCount());

  const string expected2 = (
      "group {\n  bool: false\n  uint8: 10\n}\n"
      "group {\n  bool: true\n  uint8: 3\n}\n");
  CPPUNIT_ASSERT_EQUAL(expected2, m_printer.AsString(message3.get()));

  // trhee instances of the group
  auto_ptr<const Message> message4(m_deserializer.InflateMessage(
      &descriptor,
      data,
      6));
  CPPUNIT_ASSERT(message4.get());
  CPPUNIT_ASSERT_EQUAL(3u,
                       message4->FieldCount());

  const string expected3 = (
      "group {\n  bool: false\n  uint8: 10\n}\n"
      "group {\n  bool: true\n  uint8: 3\n}\n"
      "group {\n  bool: false\n  uint8: 20\n}\n");
  CPPUNIT_ASSERT_EQUAL(expected3, m_printer.AsString(message4.get()));

  // message with too much data
  CPPUNIT_ASSERT(!m_deserializer.InflateMessage(
      &descriptor,
      data,
      sizeof(data)));
}