henrike@webrtc.org | f7795df | 2014-05-13 18:00:26 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/bytebuffer.h" |
| 12 | #include "webrtc/base/byteorder.h" |
| 13 | #include "webrtc/base/common.h" |
| 14 | #include "webrtc/base/gunit.h" |
| 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | TEST(ByteBufferTest, TestByteOrder) { |
| 19 | uint16 n16 = 1; |
| 20 | uint32 n32 = 1; |
| 21 | uint64 n64 = 1; |
| 22 | |
| 23 | EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16))); |
| 24 | EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32))); |
| 25 | EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64))); |
| 26 | |
| 27 | if (IsHostBigEndian()) { |
| 28 | // The host is the network (big) endian. |
| 29 | EXPECT_EQ(n16, HostToNetwork16(n16)); |
| 30 | EXPECT_EQ(n32, HostToNetwork32(n32)); |
| 31 | EXPECT_EQ(n64, HostToNetwork64(n64)); |
| 32 | |
| 33 | // GetBE converts big endian to little endian here. |
| 34 | EXPECT_EQ(n16 >> 8, GetBE16(&n16)); |
| 35 | EXPECT_EQ(n32 >> 24, GetBE32(&n32)); |
| 36 | EXPECT_EQ(n64 >> 56, GetBE64(&n64)); |
| 37 | } else { |
| 38 | // The host is little endian. |
| 39 | EXPECT_NE(n16, HostToNetwork16(n16)); |
| 40 | EXPECT_NE(n32, HostToNetwork32(n32)); |
| 41 | EXPECT_NE(n64, HostToNetwork64(n64)); |
| 42 | |
| 43 | // GetBE converts little endian to big endian here. |
| 44 | EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16)); |
| 45 | EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32)); |
| 46 | EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64)); |
| 47 | |
| 48 | // GetBE converts little endian to big endian here. |
| 49 | EXPECT_EQ(n16 << 8, GetBE16(&n16)); |
| 50 | EXPECT_EQ(n32 << 24, GetBE32(&n32)); |
| 51 | EXPECT_EQ(n64 << 56, GetBE64(&n64)); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | TEST(ByteBufferTest, TestBufferLength) { |
| 56 | ByteBuffer buffer; |
| 57 | size_t size = 0; |
| 58 | EXPECT_EQ(size, buffer.Length()); |
| 59 | |
| 60 | buffer.WriteUInt8(1); |
| 61 | ++size; |
| 62 | EXPECT_EQ(size, buffer.Length()); |
| 63 | |
| 64 | buffer.WriteUInt16(1); |
| 65 | size += 2; |
| 66 | EXPECT_EQ(size, buffer.Length()); |
| 67 | |
| 68 | buffer.WriteUInt24(1); |
| 69 | size += 3; |
| 70 | EXPECT_EQ(size, buffer.Length()); |
| 71 | |
| 72 | buffer.WriteUInt32(1); |
| 73 | size += 4; |
| 74 | EXPECT_EQ(size, buffer.Length()); |
| 75 | |
| 76 | buffer.WriteUInt64(1); |
| 77 | size += 8; |
| 78 | EXPECT_EQ(size, buffer.Length()); |
| 79 | |
| 80 | EXPECT_TRUE(buffer.Consume(0)); |
| 81 | EXPECT_EQ(size, buffer.Length()); |
| 82 | |
| 83 | EXPECT_TRUE(buffer.Consume(4)); |
| 84 | size -= 4; |
| 85 | EXPECT_EQ(size, buffer.Length()); |
| 86 | } |
| 87 | |
| 88 | TEST(ByteBufferTest, TestGetSetReadPosition) { |
| 89 | ByteBuffer buffer("ABCDEF", 6); |
| 90 | EXPECT_EQ(6U, buffer.Length()); |
| 91 | ByteBuffer::ReadPosition pos(buffer.GetReadPosition()); |
| 92 | EXPECT_TRUE(buffer.SetReadPosition(pos)); |
| 93 | EXPECT_EQ(6U, buffer.Length()); |
| 94 | std::string read; |
| 95 | EXPECT_TRUE(buffer.ReadString(&read, 3)); |
| 96 | EXPECT_EQ("ABC", read); |
| 97 | EXPECT_EQ(3U, buffer.Length()); |
| 98 | EXPECT_TRUE(buffer.SetReadPosition(pos)); |
| 99 | EXPECT_EQ(6U, buffer.Length()); |
| 100 | read.clear(); |
| 101 | EXPECT_TRUE(buffer.ReadString(&read, 3)); |
| 102 | EXPECT_EQ("ABC", read); |
| 103 | EXPECT_EQ(3U, buffer.Length()); |
| 104 | // For a resize by writing Capacity() number of bytes. |
| 105 | size_t capacity = buffer.Capacity(); |
| 106 | buffer.ReserveWriteBuffer(buffer.Capacity()); |
| 107 | EXPECT_EQ(capacity + 3U, buffer.Length()); |
| 108 | EXPECT_FALSE(buffer.SetReadPosition(pos)); |
| 109 | read.clear(); |
| 110 | EXPECT_TRUE(buffer.ReadString(&read, 3)); |
| 111 | EXPECT_EQ("DEF", read); |
| 112 | } |
| 113 | |
| 114 | TEST(ByteBufferTest, TestReadWriteBuffer) { |
| 115 | ByteBuffer::ByteOrder orders[2] = { ByteBuffer::ORDER_HOST, |
| 116 | ByteBuffer::ORDER_NETWORK }; |
| 117 | for (size_t i = 0; i < ARRAY_SIZE(orders); i++) { |
| 118 | ByteBuffer buffer(orders[i]); |
| 119 | EXPECT_EQ(orders[i], buffer.Order()); |
| 120 | uint8 ru8; |
| 121 | EXPECT_FALSE(buffer.ReadUInt8(&ru8)); |
| 122 | |
| 123 | // Write and read uint8. |
| 124 | uint8 wu8 = 1; |
| 125 | buffer.WriteUInt8(wu8); |
| 126 | EXPECT_TRUE(buffer.ReadUInt8(&ru8)); |
| 127 | EXPECT_EQ(wu8, ru8); |
| 128 | EXPECT_EQ(0U, buffer.Length()); |
| 129 | |
| 130 | // Write and read uint16. |
| 131 | uint16 wu16 = (1 << 8) + 1; |
| 132 | buffer.WriteUInt16(wu16); |
| 133 | uint16 ru16; |
| 134 | EXPECT_TRUE(buffer.ReadUInt16(&ru16)); |
| 135 | EXPECT_EQ(wu16, ru16); |
| 136 | EXPECT_EQ(0U, buffer.Length()); |
| 137 | |
| 138 | // Write and read uint24. |
| 139 | uint32 wu24 = (3 << 16) + (2 << 8) + 1; |
| 140 | buffer.WriteUInt24(wu24); |
| 141 | uint32 ru24; |
| 142 | EXPECT_TRUE(buffer.ReadUInt24(&ru24)); |
| 143 | EXPECT_EQ(wu24, ru24); |
| 144 | EXPECT_EQ(0U, buffer.Length()); |
| 145 | |
| 146 | // Write and read uint32. |
| 147 | uint32 wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1; |
| 148 | buffer.WriteUInt32(wu32); |
| 149 | uint32 ru32; |
| 150 | EXPECT_TRUE(buffer.ReadUInt32(&ru32)); |
| 151 | EXPECT_EQ(wu32, ru32); |
| 152 | EXPECT_EQ(0U, buffer.Length()); |
| 153 | |
| 154 | // Write and read uint64. |
| 155 | uint32 another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5; |
| 156 | uint64 wu64 = (static_cast<uint64>(another32) << 32) + wu32; |
| 157 | buffer.WriteUInt64(wu64); |
| 158 | uint64 ru64; |
| 159 | EXPECT_TRUE(buffer.ReadUInt64(&ru64)); |
| 160 | EXPECT_EQ(wu64, ru64); |
| 161 | EXPECT_EQ(0U, buffer.Length()); |
| 162 | |
| 163 | // Write and read string. |
| 164 | std::string write_string("hello"); |
| 165 | buffer.WriteString(write_string); |
| 166 | std::string read_string; |
| 167 | EXPECT_TRUE(buffer.ReadString(&read_string, write_string.size())); |
| 168 | EXPECT_EQ(write_string, read_string); |
| 169 | EXPECT_EQ(0U, buffer.Length()); |
| 170 | |
| 171 | // Write and read bytes |
| 172 | char write_bytes[] = "foo"; |
| 173 | buffer.WriteBytes(write_bytes, 3); |
| 174 | char read_bytes[3]; |
| 175 | EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3)); |
| 176 | for (int i = 0; i < 3; ++i) { |
| 177 | EXPECT_EQ(write_bytes[i], read_bytes[i]); |
| 178 | } |
| 179 | EXPECT_EQ(0U, buffer.Length()); |
| 180 | |
| 181 | // Write and read reserved buffer space |
| 182 | char* write_dst = buffer.ReserveWriteBuffer(3); |
| 183 | memcpy(write_dst, write_bytes, 3); |
| 184 | memset(read_bytes, 0, 3); |
| 185 | EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3)); |
| 186 | for (int i = 0; i < 3; ++i) { |
| 187 | EXPECT_EQ(write_bytes[i], read_bytes[i]); |
| 188 | } |
| 189 | EXPECT_EQ(0U, buffer.Length()); |
| 190 | |
| 191 | // Write and read in order. |
| 192 | buffer.WriteUInt8(wu8); |
| 193 | buffer.WriteUInt16(wu16); |
| 194 | buffer.WriteUInt24(wu24); |
| 195 | buffer.WriteUInt32(wu32); |
| 196 | buffer.WriteUInt64(wu64); |
| 197 | EXPECT_TRUE(buffer.ReadUInt8(&ru8)); |
| 198 | EXPECT_EQ(wu8, ru8); |
| 199 | EXPECT_TRUE(buffer.ReadUInt16(&ru16)); |
| 200 | EXPECT_EQ(wu16, ru16); |
| 201 | EXPECT_TRUE(buffer.ReadUInt24(&ru24)); |
| 202 | EXPECT_EQ(wu24, ru24); |
| 203 | EXPECT_TRUE(buffer.ReadUInt32(&ru32)); |
| 204 | EXPECT_EQ(wu32, ru32); |
| 205 | EXPECT_TRUE(buffer.ReadUInt64(&ru64)); |
| 206 | EXPECT_EQ(wu64, ru64); |
| 207 | EXPECT_EQ(0U, buffer.Length()); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | } // namespace rtc |