tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestBufferReader.cpp (1712B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "BufferReader.h"
      8 #include "gtest/gtest.h"
      9 
     10 using namespace mozilla;
     11 
     12 TEST(BufferReader, ReaderCursor)
     13 {
     14  // Allocate a buffer and create a BufferReader.
     15  const size_t BUFFER_SIZE = 10;
     16  uint8_t buffer[BUFFER_SIZE] = {0};
     17 
     18  const uint8_t* const HEAD = reinterpret_cast<uint8_t*>(buffer);
     19  const uint8_t* const TAIL = HEAD + BUFFER_SIZE;
     20 
     21  BufferReader reader(HEAD, BUFFER_SIZE);
     22  ASSERT_EQ(reader.Offset(), static_cast<size_t>(0));
     23  ASSERT_EQ(reader.Peek(BUFFER_SIZE), HEAD);
     24 
     25  // Keep reading to the end, and make sure the final read failed.
     26  const size_t READ_SIZE = 4;
     27  ASSERT_NE(BUFFER_SIZE % READ_SIZE, static_cast<size_t>(0));
     28  for (const uint8_t* ptr = reader.Peek(0); ptr != nullptr;
     29       ptr = reader.Read(READ_SIZE)) {
     30  }
     31 
     32  // Check the reading cursor of the BufferReader is correct
     33  // after reading and seeking.
     34  const uint8_t* tail = reader.Peek(0);
     35  const uint8_t* head = reader.Seek(0);
     36 
     37  EXPECT_EQ(head, HEAD);
     38  EXPECT_EQ(tail, TAIL);
     39 }
     40 
     41 TEST(BufferReader, UnalignedRead)
     42 {
     43  // Allocate a buffer and create a BufferReader.
     44  const size_t BUFFER_SIZE = 5;
     45  uint8_t buffer[BUFFER_SIZE] = {0};
     46 
     47  const uint8_t* const HEAD = reinterpret_cast<uint8_t*>(buffer);
     48 
     49  BufferReader reader(HEAD, BUFFER_SIZE);
     50  // adjust the offset so that it's unaligned
     51  reader.Read(1);
     52  // read an int which needs 4 byte alignment
     53  reader.ReadType<uint32_t>();
     54 }