Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / net / probe_message_unittest.cc
blobf386089dcd5a2d33b8cac3a5524900c515bf97dd
1 // Copyright 2013 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.
5 #include <string.h>
7 #include "chrome/browser/net/probe_message.h"
8 #include "chrome/browser/net/probe_message.pb.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace chrome_browser_net {
13 class ProbeMessageTest : public ::testing::Test {
14 protected:
15 ProbeMessageTest() {}
17 ~ProbeMessageTest() override {}
20 TEST_F(ProbeMessageTest, TestGenerateProbeRequest) {
21 ProbeMessage pm;
22 ProbePacket_Token token;
23 token.set_timestamp_micros(1000000U);
24 token.mutable_hash()->assign("1x1x");
25 uint32 group_id = 1;
26 uint32 probe_size = 500;
27 uint32 pacing_interval_micros = 1000000;
28 uint32 number_probe_packets = 21;
29 ProbePacket probe_packet;
30 pm.GenerateProbeRequest(token,
31 group_id,
32 probe_size,
33 pacing_interval_micros,
34 number_probe_packets,
35 &probe_packet);
37 EXPECT_EQ(probe_packet.header().type(), ProbePacket_Type_PROBE_REQUEST);
38 EXPECT_EQ(probe_packet.header().version(), ProbeMessage::kVersion);
39 EXPECT_EQ(probe_packet.group_id(), group_id);
40 EXPECT_EQ(probe_packet.probe_size_bytes(), probe_size);
41 EXPECT_EQ(probe_packet.pacing_interval_micros(), pacing_interval_micros);
42 EXPECT_EQ(probe_packet.number_probe_packets(), number_probe_packets);
43 EXPECT_GE(probe_packet.ByteSize(), static_cast<int>(probe_size));
46 TEST_F(ProbeMessageTest, TestSetPacketHeader) {
47 ProbeMessage pm;
48 ProbePacket probe_packet;
49 pm.SetPacketHeader(ProbePacket_Type_HELLO_REQUEST, &probe_packet);
50 EXPECT_EQ(probe_packet.header().type(), ProbePacket_Type_HELLO_REQUEST);
51 EXPECT_EQ(probe_packet.header().version(), ProbeMessage::kVersion);
53 pm.SetPacketHeader(ProbePacket_Type_PROBE_REPLY, &probe_packet);
54 EXPECT_EQ(probe_packet.header().type(), ProbePacket_Type_PROBE_REPLY);
57 TEST_F(ProbeMessageTest, TestMakeEncodePacketAndParseInput) {
58 ProbeMessage pm;
59 ProbePacket in_packet;
60 uint32 version = 2;
61 ProbePacket_Type type = ProbePacket_Type_HELLO_REPLY;
62 uint32 number_probe_packets = 2;
63 uint32 group_id = 5;
64 in_packet.mutable_header()->set_version(version);
65 in_packet.mutable_header()->set_type(type);
66 in_packet.set_number_probe_packets(number_probe_packets);
67 in_packet.set_group_id(group_id);
69 // Encode it to string.
70 std::string output = pm.MakeEncodedPacket(in_packet);
71 // Parse to ProbePacket.
72 ProbePacket out_packet;
73 pm.ParseInput(output, &out_packet);
75 EXPECT_EQ(out_packet.header().type(), type);
76 EXPECT_EQ(out_packet.header().version(), version);
77 EXPECT_EQ(out_packet.number_probe_packets(), number_probe_packets);
78 EXPECT_EQ(out_packet.group_id(), group_id);
81 TEST_F(ProbeMessageTest, TestChecksum) {
82 ProbeMessage pm;
83 std::string str("ABC");
84 uint32 computed_checksum = pm.Checksum(str);
85 uint32 expected_sum = 0;
86 for (unsigned i = 0; i < str.size(); ++i)
87 expected_sum += static_cast<uint8>(str[i]);
88 EXPECT_EQ(computed_checksum, expected_sum);
91 TEST_F(ProbeMessageTest, TestEncode) {
92 ProbeMessage pm;
93 std::string original("ABC");
94 std::string output = pm.Encode(original);
95 std::string expected_str(original.size(), 0);
96 for (unsigned i = 0; i < original.size(); ++i) {
97 expected_str[i] = original[i] ^ ProbeMessage::kEncodingString[i];
99 EXPECT_EQ(output, expected_str);
101 // Do it again to decode.
102 std::string twice_encoded = pm.Encode(output);
103 EXPECT_EQ(twice_encoded, original);
106 } // namespace chrome_browser_net