summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/probe_message_unittest.cc
blob: f386089dcd5a2d33b8cac3a5524900c515bf97dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2013 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 <string.h>

#include "chrome/browser/net/probe_message.h"
#include "chrome/browser/net/probe_message.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chrome_browser_net {

class ProbeMessageTest : public ::testing::Test {
 protected:
  ProbeMessageTest() {}

  ~ProbeMessageTest() override {}
};

TEST_F(ProbeMessageTest, TestGenerateProbeRequest) {
  ProbeMessage pm;
  ProbePacket_Token token;
  token.set_timestamp_micros(1000000U);
  token.mutable_hash()->assign("1x1x");
  uint32 group_id = 1;
  uint32 probe_size = 500;
  uint32 pacing_interval_micros = 1000000;
  uint32 number_probe_packets = 21;
  ProbePacket probe_packet;
  pm.GenerateProbeRequest(token,
                          group_id,
                          probe_size,
                          pacing_interval_micros,
                          number_probe_packets,
                          &probe_packet);

  EXPECT_EQ(probe_packet.header().type(), ProbePacket_Type_PROBE_REQUEST);
  EXPECT_EQ(probe_packet.header().version(), ProbeMessage::kVersion);
  EXPECT_EQ(probe_packet.group_id(), group_id);
  EXPECT_EQ(probe_packet.probe_size_bytes(), probe_size);
  EXPECT_EQ(probe_packet.pacing_interval_micros(), pacing_interval_micros);
  EXPECT_EQ(probe_packet.number_probe_packets(), number_probe_packets);
  EXPECT_GE(probe_packet.ByteSize(), static_cast<int>(probe_size));
}

TEST_F(ProbeMessageTest, TestSetPacketHeader) {
  ProbeMessage pm;
  ProbePacket probe_packet;
  pm.SetPacketHeader(ProbePacket_Type_HELLO_REQUEST, &probe_packet);
  EXPECT_EQ(probe_packet.header().type(), ProbePacket_Type_HELLO_REQUEST);
  EXPECT_EQ(probe_packet.header().version(), ProbeMessage::kVersion);

  pm.SetPacketHeader(ProbePacket_Type_PROBE_REPLY, &probe_packet);
  EXPECT_EQ(probe_packet.header().type(), ProbePacket_Type_PROBE_REPLY);
}

TEST_F(ProbeMessageTest, TestMakeEncodePacketAndParseInput) {
  ProbeMessage pm;
  ProbePacket in_packet;
  uint32 version = 2;
  ProbePacket_Type type = ProbePacket_Type_HELLO_REPLY;
  uint32 number_probe_packets = 2;
  uint32 group_id = 5;
  in_packet.mutable_header()->set_version(version);
  in_packet.mutable_header()->set_type(type);
  in_packet.set_number_probe_packets(number_probe_packets);
  in_packet.set_group_id(group_id);

  // Encode it to string.
  std::string output = pm.MakeEncodedPacket(in_packet);
  // Parse to ProbePacket.
  ProbePacket out_packet;
  pm.ParseInput(output, &out_packet);

  EXPECT_EQ(out_packet.header().type(), type);
  EXPECT_EQ(out_packet.header().version(), version);
  EXPECT_EQ(out_packet.number_probe_packets(), number_probe_packets);
  EXPECT_EQ(out_packet.group_id(), group_id);
}

TEST_F(ProbeMessageTest, TestChecksum) {
  ProbeMessage pm;
  std::string str("ABC");
  uint32 computed_checksum = pm.Checksum(str);
  uint32 expected_sum = 0;
  for (unsigned i = 0; i < str.size(); ++i)
    expected_sum += static_cast<uint8>(str[i]);
  EXPECT_EQ(computed_checksum, expected_sum);
}

TEST_F(ProbeMessageTest, TestEncode) {
  ProbeMessage pm;
  std::string original("ABC");
  std::string output = pm.Encode(original);
  std::string expected_str(original.size(), 0);
  for (unsigned i = 0; i < original.size(); ++i) {
    expected_str[i] = original[i] ^ ProbeMessage::kEncodingString[i];
  }
  EXPECT_EQ(output, expected_str);

  // Do it again to decode.
  std::string twice_encoded = pm.Encode(output);
  EXPECT_EQ(twice_encoded, original);
}

}  // namespace chrome_browser_net