• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/big_endian.h"
6 
7 #include <stdint.h>
8 
9 #include "base/strings/string_piece.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace base {
13 
TEST(BigEndianReaderTest,ReadsValues)14 TEST(BigEndianReaderTest, ReadsValues) {
15   char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
16                   0x1A, 0x2B, 0x3C, 0x4D, 0x5E };
17   char buf[2];
18   uint8_t u8;
19   uint16_t u16;
20   uint32_t u32;
21   uint64_t u64;
22   base::StringPiece piece;
23   BigEndianReader reader(data, sizeof(data));
24 
25   EXPECT_TRUE(reader.Skip(2));
26   EXPECT_EQ(data + 2, reader.ptr());
27   EXPECT_EQ(reader.remaining(), static_cast<int>(sizeof(data)) - 2);
28   EXPECT_TRUE(reader.ReadBytes(buf, sizeof(buf)));
29   EXPECT_EQ(0x2, buf[0]);
30   EXPECT_EQ(0x3, buf[1]);
31   EXPECT_TRUE(reader.ReadU8(&u8));
32   EXPECT_EQ(0x4, u8);
33   EXPECT_TRUE(reader.ReadU16(&u16));
34   EXPECT_EQ(0x0506, u16);
35   EXPECT_TRUE(reader.ReadU32(&u32));
36   EXPECT_EQ(0x0708090Au, u32);
37   EXPECT_TRUE(reader.ReadU64(&u64));
38   EXPECT_EQ(0x0B0C0D0E0F1A2B3Cllu, u64);
39   base::StringPiece expected(reader.ptr(), 2);
40   EXPECT_TRUE(reader.ReadPiece(&piece, 2));
41   EXPECT_EQ(2u, piece.size());
42   EXPECT_EQ(expected.data(), piece.data());
43 }
44 
TEST(BigEndianReaderTest,RespectsLength)45 TEST(BigEndianReaderTest, RespectsLength) {
46   char data[8];
47   char buf[2];
48   uint8_t u8;
49   uint16_t u16;
50   uint32_t u32;
51   uint64_t u64;
52   base::StringPiece piece;
53   BigEndianReader reader(data, sizeof(data));
54   // 8 left
55   EXPECT_FALSE(reader.Skip(9));
56   EXPECT_TRUE(reader.Skip(1));
57   // 7 left
58   EXPECT_FALSE(reader.ReadU64(&u64));
59   EXPECT_TRUE(reader.Skip(4));
60   // 3 left
61   EXPECT_FALSE(reader.ReadU32(&u32));
62   EXPECT_FALSE(reader.ReadPiece(&piece, 4));
63   EXPECT_TRUE(reader.Skip(2));
64   // 1 left
65   EXPECT_FALSE(reader.ReadU16(&u16));
66   EXPECT_FALSE(reader.ReadBytes(buf, 2));
67   EXPECT_TRUE(reader.Skip(1));
68   // 0 left
69   EXPECT_FALSE(reader.ReadU8(&u8));
70   EXPECT_EQ(0, reader.remaining());
71 }
72 
TEST(BigEndianWriterTest,WritesValues)73 TEST(BigEndianWriterTest, WritesValues) {
74   char expected[] = { 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE,
75                       0xF, 0x1A, 0x2B, 0x3C };
76   char data[sizeof(expected)];
77   char buf[] = { 0x2, 0x3 };
78   memset(data, 0, sizeof(data));
79   BigEndianWriter writer(data, sizeof(data));
80 
81   EXPECT_TRUE(writer.Skip(2));
82   EXPECT_TRUE(writer.WriteBytes(buf, sizeof(buf)));
83   EXPECT_TRUE(writer.WriteU8(0x4));
84   EXPECT_TRUE(writer.WriteU16(0x0506));
85   EXPECT_TRUE(writer.WriteU32(0x0708090A));
86   EXPECT_TRUE(writer.WriteU64(0x0B0C0D0E0F1A2B3Cllu));
87   EXPECT_EQ(0, memcmp(expected, data, sizeof(expected)));
88 }
89 
TEST(BigEndianWriterTest,RespectsLength)90 TEST(BigEndianWriterTest, RespectsLength) {
91   char data[8];
92   char buf[2];
93   uint8_t u8 = 0;
94   uint16_t u16 = 0;
95   uint32_t u32 = 0;
96   uint64_t u64 = 0;
97   BigEndianWriter writer(data, sizeof(data));
98   // 8 left
99   EXPECT_FALSE(writer.Skip(9));
100   EXPECT_TRUE(writer.Skip(1));
101   // 7 left
102   EXPECT_FALSE(writer.WriteU64(u64));
103   EXPECT_TRUE(writer.Skip(4));
104   // 3 left
105   EXPECT_FALSE(writer.WriteU32(u32));
106   EXPECT_TRUE(writer.Skip(2));
107   // 1 left
108   EXPECT_FALSE(writer.WriteU16(u16));
109   EXPECT_FALSE(writer.WriteBytes(buf, 2));
110   EXPECT_TRUE(writer.Skip(1));
111   // 0 left
112   EXPECT_FALSE(writer.WriteU8(u8));
113   EXPECT_EQ(0, writer.remaining());
114 }
115 
116 }  // namespace base
117