xref: /aosp_15_r20/external/cronet/net/log/net_log_values_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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 "net/log/net_log_values.h"
6 
7 #include <limits>
8 
9 #include "base/values.h"
10 #include "net/log/file_net_log_observer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace net {
14 
15 // Calls NetLogASCIIStringValue() on |raw| and returns the resulting string
16 // (rather than the base::Value).
GetNetLogString(std::string_view raw)17 std::string GetNetLogString(std::string_view raw) {
18   base::Value value = NetLogStringValue(raw);
19   EXPECT_TRUE(value.is_string());
20   return value.GetString();
21 }
22 
TEST(NetLogValuesTest,NetLogASCIIStringValue)23 TEST(NetLogValuesTest, NetLogASCIIStringValue) {
24   // ASCII strings should not be transformed.
25   EXPECT_EQ("ascii\nstrin\0g", GetNetLogString("ascii\nstrin\0g"));
26 
27   // Non-ASCII UTF-8 strings should be escaped.
28   EXPECT_EQ("%ESCAPED:\xE2\x80\x8B utf-8 string %E2%98%83",
29             GetNetLogString("utf-8 string \xE2\x98\x83"));
30 
31   // The presence of percent should not trigger escaping.
32   EXPECT_EQ("%20", GetNetLogString("%20"));
33 
34   // However if the value to be escaped contains percent, it should be escaped
35   // (so can unescape to restore the original string).
36   EXPECT_EQ("%ESCAPED:\xE2\x80\x8B %E2%98%83 %2520",
37             GetNetLogString("\xE2\x98\x83 %20"));
38 
39   // Test that when percent escaping, no ASCII value is escaped (excluding %).
40   for (uint8_t c = 0; c <= 0x7F; ++c) {
41     if (c == '%')
42       continue;
43 
44     std::string s;
45     s.push_back(c);
46 
47     EXPECT_EQ("%ESCAPED:\xE2\x80\x8B %E2 " + s, GetNetLogString("\xE2 " + s));
48   }
49 }
50 
TEST(NetLogValuesTest,NetLogBinaryValue)51 TEST(NetLogValuesTest, NetLogBinaryValue) {
52   // Test the encoding for empty bytes.
53   auto value1 = NetLogBinaryValue(nullptr, 0);
54   ASSERT_TRUE(value1.is_string());
55   EXPECT_EQ("", value1.GetString());
56 
57   // Test the encoding for a non-empty sequence (which needs padding).
58   const uint8_t kBytes[] = {0x00, 0xF3, 0xF8, 0xFF};
59   auto value2 = NetLogBinaryValue(kBytes, std::size(kBytes));
60   ASSERT_TRUE(value2.is_string());
61   EXPECT_EQ("APP4/w==", value2.GetString());
62 }
63 
64 template <typename T>
SerializedNetLogNumber(T num)65 std::string SerializedNetLogNumber(T num) {
66   auto value = NetLogNumberValue(num);
67 
68   EXPECT_TRUE(value.is_string() || value.is_int() || value.is_double());
69 
70   return SerializeNetLogValueToJson(value);
71 }
72 
SerializedNetLogInt64(int64_t num)73 std::string SerializedNetLogInt64(int64_t num) {
74   return SerializedNetLogNumber(num);
75 }
76 
SerializedNetLogUint64(uint64_t num)77 std::string SerializedNetLogUint64(uint64_t num) {
78   return SerializedNetLogNumber(num);
79 }
80 
TEST(NetLogValuesTest,NetLogNumberValue)81 TEST(NetLogValuesTest, NetLogNumberValue) {
82   const int64_t kMinInt = std::numeric_limits<int32_t>::min();
83   const int64_t kMaxInt = std::numeric_limits<int32_t>::max();
84 
85   // Numbers which can be represented by an INTEGER base::Value().
86   EXPECT_EQ("0", SerializedNetLogInt64(0));
87   EXPECT_EQ("0", SerializedNetLogUint64(0));
88   EXPECT_EQ("-1", SerializedNetLogInt64(-1));
89   EXPECT_EQ("-2147483648", SerializedNetLogInt64(kMinInt));
90   EXPECT_EQ("2147483647", SerializedNetLogInt64(kMaxInt));
91 
92   // Numbers which are outside of the INTEGER range, but fit within a DOUBLE.
93   EXPECT_EQ("-2147483649", SerializedNetLogInt64(kMinInt - 1));
94   EXPECT_EQ("2147483648", SerializedNetLogInt64(kMaxInt + 1));
95   EXPECT_EQ("4294967294", SerializedNetLogInt64(0xFFFFFFFF - 1));
96 
97   // kMaxSafeInteger is the same as JavaScript's Numbers.MAX_SAFE_INTEGER.
98   const int64_t kMaxSafeInteger = 9007199254740991;  // 2^53 - 1
99 
100   // Numbers that can be represented with full precision by a DOUBLE.
101   EXPECT_EQ("-9007199254740991", SerializedNetLogInt64(-kMaxSafeInteger));
102   EXPECT_EQ("9007199254740991", SerializedNetLogInt64(kMaxSafeInteger));
103   EXPECT_EQ("9007199254740991", SerializedNetLogUint64(kMaxSafeInteger));
104 
105   // Numbers that are just outside of the range of a DOUBLE need to be encoded
106   // as strings.
107   EXPECT_EQ("\"-9007199254740992\"",
108             SerializedNetLogInt64(-kMaxSafeInteger - 1));
109   EXPECT_EQ("\"9007199254740992\"", SerializedNetLogInt64(kMaxSafeInteger + 1));
110   EXPECT_EQ("\"9007199254740992\"",
111             SerializedNetLogUint64(kMaxSafeInteger + 1));
112 
113   // Test the 64-bit maximums.
114   EXPECT_EQ("\"9223372036854775807\"",
115             SerializedNetLogInt64(std::numeric_limits<int64_t>::max()));
116   EXPECT_EQ("\"18446744073709551615\"",
117             SerializedNetLogUint64(std::numeric_limits<uint64_t>::max()));
118 }
119 
120 }  // namespace net
121