Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f23f285 authored by johnshamoon's avatar johnshamoon
Browse files

test_vendor: Add function to extract bytes from iterators

The extract function extracts a number of bytes from the type that the
iterator is pointing to. The iterator will be incremented to the value
after the last byte after extract is called.

Fixed the bounds of the for loop in the preIncrementTest so that it
does not try to dereference the sentinel after the last value.

Test: Passes unit tests in test/iterator_test.cc
Change-Id: I07944641b829c820bbe40d612d7f065e514516b3
parent ee9b3403
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
#pragma once
#include <iterator>
#include <memory>
#include <type_traits>

#include "base/logging.h"

namespace test_vendor_lib {

@@ -42,6 +45,21 @@ class Iterator
  uint8_t& operator*() const;
  uint8_t* operator->() const;

  // Get the next sizeof(FixedWidthIntegerType) bytes and return the filled type
  template <typename FixedWidthIntegerType>
  FixedWidthIntegerType extract() {
    static_assert(std::is_integral<FixedWidthIntegerType>::value,
                  "Iterator::extract requires an integral type.");
    FixedWidthIntegerType extracted_value = 0;

    for (size_t i = 0; i < sizeof(FixedWidthIntegerType); i++) {
      extracted_value |= static_cast<FixedWidthIntegerType>(**this) << i * 8;
      (*this)++;
    }

    return extracted_value;
  }

 private:
  std::shared_ptr<class HciPacket> hci_packet_;
  size_t index_;
+2 −0
Original line number Diff line number Diff line
@@ -99,6 +99,8 @@ bool Iterator::operator>=(Iterator& itr) {
}

uint8_t& Iterator::operator*() const {
  CHECK(index_ != hci_packet_->get_length());

  return hci_packet_->get_at_index(index_);
}

+25 −1
Original line number Diff line number Diff line
@@ -33,6 +33,30 @@ class IteratorTest : public ::testing::Test {
  std::shared_ptr<TestPacket> packet;
};

TEST_F(IteratorTest, extractTest) {
  Iterator general_case = packet->get_begin();

  ASSERT_EQ(0x95, general_case.extract<uint8_t>());
  ASSERT_EQ(0x471f, general_case.extract<uint16_t>());
  ASSERT_EQ(0x951f0200u, general_case.extract<uint32_t>());
  ASSERT_EQ(0x33000101000000cbu, general_case.extract<uint64_t>());
}

TEST_F(IteratorTest, extractBoundsDeathTest) {
  Iterator bounds_test = packet->get_end();

  ASSERT_DEATH(bounds_test.extract<uint8_t>(), "");
  ASSERT_DEATH(bounds_test.extract<uint16_t>(), "");
  ASSERT_DEATH(bounds_test.extract<uint32_t>(), "");
  ASSERT_DEATH(bounds_test.extract<uint64_t>(), "");
}

TEST_F(IteratorTest, dereferenceDeathTest) {
  Iterator dereference_test = packet->get_end();

  ASSERT_EQ(0x45, *(dereference_test - static_cast<size_t>(1)));
  ASSERT_DEATH(*dereference_test, "");
}
TEST_F(IteratorTest, plusEqTest) {
  Iterator plus_eq = packet->get_begin();
  for (size_t i = 0; i < complete_l2cap_packet.size(); i += 2) {
@@ -45,7 +69,7 @@ TEST_F(IteratorTest, plusEqTest) {

TEST_F(IteratorTest, preIncrementTest) {
  Iterator plus_plus = packet->get_begin();
  for (size_t i = 0; i < complete_l2cap_packet.size(); i++) {
  for (size_t i = 0; i < complete_l2cap_packet.size() - 1; i++) {
    ASSERT_EQ(complete_l2cap_packet[i + 1], *(++plus_plus))
        << "Pre-increment test: Dereferenced iterator does not equal expected "
        << "at index " << i;