diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-06 23:32:58 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-06 23:32:58 +0000 |
commit | ad24b1827fe58c4a22c0cddb5791a95f2ab1b21b (patch) | |
tree | 7e5e4727d7ab5e3a96bc95ee890aebb1b6c2d608 /net/base/big_endian_unittest.cc | |
parent | d7de57877613a63e36facbd485245918c1131f61 (diff) | |
download | chromium_src-ad24b1827fe58c4a22c0cddb5791a95f2ab1b21b.zip chromium_src-ad24b1827fe58c4a22c0cddb5791a95f2ab1b21b.tar.gz chromium_src-ad24b1827fe58c4a22c0cddb5791a95f2ab1b21b.tar.bz2 |
Isolates generic DnsClient from AsyncHostResolver.
DnsClient provides a generic DNS client that allows fetching resource records.
DnsClient is very lightweight and does not support aggregation, queuing or
prioritization of requests.
This is the first CL in a series to merge AsyncHostResolver into
HostResolverImpl.
Also introduces general-purpose BigEndianReader/Writer.
BUG=90881
TEST=./net_unittests
Review URL: http://codereview.chromium.org/8762001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113282 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/big_endian_unittest.cc')
-rw-r--r-- | net/base/big_endian_unittest.cc | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/net/base/big_endian_unittest.cc b/net/base/big_endian_unittest.cc new file mode 100644 index 0000000..cb4a6d4 --- /dev/null +++ b/net/base/big_endian_unittest.cc @@ -0,0 +1,100 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/string_piece.h" +#include "net/base/big_endian.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace net { + +TEST(BigEndianReaderTest, ReadsValues) { + char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC }; + char buf[2]; + uint8 u8; + uint16 u16; + uint32 u32; + base::StringPiece piece; + BigEndianReader reader(data, sizeof(data)); + + EXPECT_TRUE(reader.Skip(2)); + EXPECT_EQ(data + 2, reader.ptr()); + EXPECT_EQ(reader.remaining(), static_cast<int>(sizeof(data)) - 2); + EXPECT_TRUE(reader.ReadBytes(buf, sizeof(buf))); + EXPECT_EQ(0x2, buf[0]); + EXPECT_EQ(0x3, buf[1]); + EXPECT_TRUE(reader.ReadU8(&u8)); + EXPECT_EQ(0x4, u8); + EXPECT_TRUE(reader.ReadU16(&u16)); + EXPECT_EQ(0x0506, u16); + EXPECT_TRUE(reader.ReadU32(&u32)); + EXPECT_EQ(0x0708090Au, u32); + base::StringPiece expected(reader.ptr(), 2); + EXPECT_TRUE(reader.ReadPiece(&piece, 2)); + EXPECT_EQ(2u, piece.size()); + EXPECT_EQ(expected.data(), piece.data()); +} + +TEST(BigEndianReaderTest, RespectsLength) { + char data[4]; + char buf[2]; + uint8 u8; + uint16 u16; + uint32 u32; + base::StringPiece piece; + BigEndianReader reader(data, sizeof(data)); + // 4 left + EXPECT_FALSE(reader.Skip(6)); + EXPECT_TRUE(reader.Skip(1)); + // 3 left + EXPECT_FALSE(reader.ReadU32(&u32)); + EXPECT_FALSE(reader.ReadPiece(&piece, 4)); + EXPECT_TRUE(reader.Skip(2)); + // 1 left + EXPECT_FALSE(reader.ReadU16(&u16)); + EXPECT_FALSE(reader.ReadBytes(buf, 2)); + EXPECT_TRUE(reader.Skip(1)); + // 0 left + EXPECT_FALSE(reader.ReadU8(&u8)); + EXPECT_EQ(0, reader.remaining()); +} + +TEST(BigEndianWriterTest, WritesValues) { + char expected[] = { 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0xA }; + char data[sizeof(expected)]; + char buf[] = { 0x2, 0x3 }; + memset(data, 0, sizeof(data)); + BigEndianWriter writer(data, sizeof(data)); + + EXPECT_TRUE(writer.Skip(2)); + EXPECT_TRUE(writer.WriteBytes(buf, sizeof(buf))); + EXPECT_TRUE(writer.WriteU8(0x4)); + EXPECT_TRUE(writer.WriteU16(0x0506)); + EXPECT_TRUE(writer.WriteU32(0x0708090A)); + EXPECT_EQ(0, memcmp(expected, data, sizeof(expected))); +} + +TEST(BigEndianWriterTest, RespectsLength) { + char data[4]; + char buf[2]; + uint8 u8 = 0; + uint16 u16 = 0; + uint32 u32 = 0; + BigEndianWriter writer(data, sizeof(data)); + // 4 left + EXPECT_FALSE(writer.Skip(6)); + EXPECT_TRUE(writer.Skip(1)); + // 3 left + EXPECT_FALSE(writer.WriteU32(u32)); + EXPECT_TRUE(writer.Skip(2)); + // 1 left + EXPECT_FALSE(writer.WriteU16(u16)); + EXPECT_FALSE(writer.WriteBytes(buf, 2)); + EXPECT_TRUE(writer.Skip(1)); + // 0 left + EXPECT_FALSE(writer.WriteU8(u8)); + EXPECT_EQ(0, writer.remaining()); +} + +} // namespace net + |