Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/parse_number.h" | 5 #include "net/base/parse_number.h" |
| 6 | 6 |
| 7 #include <limits> | |
| 8 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 10 |
| 9 namespace net { | 11 namespace net { |
| 10 namespace { | 12 namespace { |
| 11 | 13 |
| 12 TEST(ParseNumberTest, IntValidInputs) { | 14 // Common tests to run for each of the overloaded versions of |
| 15 // ParseNonNegativeInteger(). | |
| 16 template <typename T> | |
| 17 void TestParseNonNegativeInteger() { | |
| 18 // Test valid inputs | |
| 13 const struct { | 19 const struct { |
| 14 const char* input; | 20 const char* input; |
| 15 int output; | 21 T output; |
| 16 } kTests[] = { | 22 } kValidTests[] = { |
| 17 {"0", 0}, {"00000", 0}, {"003", 3}, {"003", 3}, {"1234566", 1234566}, | 23 {"0", 0}, {"00000", 0}, {"003", 3}, {"003", 3}, {"1234566", 1234566}, |
| 18 {"987", 987}, {"010", 10}, | 24 {"987", 987}, {"010", 10}, |
| 19 }; | 25 }; |
| 20 | 26 |
| 21 for (const auto& test : kTests) { | 27 for (const auto& test : kValidTests) { |
| 22 int result; | 28 T result; |
| 23 ASSERT_TRUE(ParseNonNegativeDecimalInt(test.input, &result)) | 29 ASSERT_TRUE(ParseNonNegativeInteger(test.input, &result)) |
| 24 << "Failed to parse: " << test.input; | 30 << "Failed to parse: " << test.input; |
| 25 EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; | 31 EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; |
| 26 } | 32 } |
| 33 | |
| 34 // Test invalid inputs. | |
| 35 const char* kInvalidTests[] = { | |
| 36 "", "-23", "+42", " 123", "123 ", "123\n", "0xFF", "0x11", "x11", "F11", | |
|
eroman
2016/03/24 01:24:19
clang-format did something very different this tim
| |
| 37 "AF", "0AF", "0.0", "13.", "13,000", "13.000", "13/5", | |
| 38 // Generic overflow test. | |
| 39 "9999999999999999999999999999999999999999999999999999999999999999", "Inf", | |
| 40 "NaN", "null", "dog", | |
| 41 }; | |
| 42 | |
| 43 T kFailResult = T(3); | |
| 44 | |
| 45 for (const auto& input : kInvalidTests) { | |
| 46 T result = kFailResult; | |
| 47 ASSERT_FALSE(ParseNonNegativeInteger(input, &result)) | |
| 48 << "Succeeded to parse: " << input; | |
| 49 EXPECT_EQ(kFailResult, result) << "Modified output for failed parsing"; | |
| 50 } | |
| 51 | |
| 52 // Test that NUL in the input causes a rejection. | |
| 53 T result = kFailResult; | |
| 54 ASSERT_FALSE(ParseNonNegativeInteger(base::StringPiece("123\0", 4), &result)); | |
| 55 EXPECT_EQ(kFailResult, result); | |
| 27 } | 56 } |
| 28 | 57 |
| 29 TEST(ParseNumberTest, IntInvalidInputs) { | 58 // Common tests to run for each of the overloaded versions of |
| 30 const char* kTests[] = { | 59 // ParseSignedInteger(). |
| 31 "", | 60 template <typename T> |
| 32 "-23", | 61 void TestParseSignedInteger() { |
| 33 "+42", | 62 // Test valid inputs |
| 34 " 123", | 63 const struct { |
| 35 "123 ", | 64 const char* input; |
| 36 "123\n", | 65 T output; |
| 37 "0xFF", | 66 } kValidTests[] = { |
| 38 "0x11", | 67 {"0", 0}, {"-0", 0}, {"00000", 0}, |
|
eroman
2016/03/24 01:24:19
The difference in these inputs is -0 and other neg
| |
| 39 "x11", | 68 {"003", 3}, {"003", 3}, {"1234566", 1234566}, |
| 40 "F11", | 69 {"987", 987}, {"010", 10}, {"-632", -632}, |
| 41 "AF", | |
| 42 "0AF", | |
| 43 "0.0", | |
| 44 "13.", | |
| 45 "13,000", | |
| 46 "13.000", | |
| 47 "13/5", | |
| 48 "9999999999999999999999999999999999999999999999999999999999999999", | |
| 49 "Inf", | |
| 50 "NaN", | |
| 51 "null", | |
| 52 "dog", | |
| 53 }; | 70 }; |
| 54 | 71 |
| 55 for (const auto& input : kTests) { | 72 for (const auto& test : kValidTests) { |
| 56 int result = 0xDEAD; | 73 T result; |
| 57 ASSERT_FALSE(ParseNonNegativeDecimalInt(input, &result)) | 74 ASSERT_TRUE(ParseSignedInteger(test.input, &result)) << "Failed to parse: " |
| 58 << "Succeeded to parse: " << input; | 75 << test.input; |
| 59 EXPECT_EQ(0xDEAD, result) << "Modified output for failed parsing"; | 76 EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; |
| 60 } | 77 } |
| 78 | |
| 79 // Test invalid inputs. | |
| 80 const char* kInvalidTests[] = { | |
| 81 "", "+42", " 123", "123 ", "123\n", "0xFF", "0x11", "x11", "F11", "AF", | |
| 82 "0AF", "0.0", "13.", "13,000", "13.000", "13/5", | |
| 83 // Generic overflow test (none of types used can hold this) | |
| 84 "9999999999999999999999999999999999999999999999999999999999999999", | |
| 85 // Generic under test (none of types used can hold this) | |
| 86 "-9999999999999999999999999999999999999999999999999999999999999999", | |
| 87 "Inf", "NaN", "null", "dog", | |
| 88 }; | |
| 89 | |
| 90 T kFailResult = T(3); | |
| 91 | |
| 92 for (const auto& input : kInvalidTests) { | |
| 93 T result = kFailResult; | |
| 94 ASSERT_FALSE(ParseSignedInteger(input, &result)) << "Succeeded to parse: " | |
| 95 << input; | |
| 96 EXPECT_EQ(kFailResult, result) << "Modified output for failed parsing"; | |
| 97 } | |
| 98 | |
| 99 // Test that NUL in the input causes a rejection. | |
| 100 T result = kFailResult; | |
| 101 ASSERT_FALSE(ParseSignedInteger(base::StringPiece("123\0", 4), &result)); | |
| 102 EXPECT_EQ(kFailResult, result); | |
| 61 } | 103 } |
| 62 | 104 |
| 63 TEST(ParseNumberTest, IntInvalidInputsContainsNul) { | 105 TEST(ParseNumberTest, ParseNonNegativeIntegerInt32) { |
| 64 int result = 0xDEAD; | 106 TestParseNonNegativeInteger<int32_t>(); |
| 65 ASSERT_FALSE( | 107 } |
| 66 ParseNonNegativeDecimalInt(base::StringPiece("123\0", 4), &result)); | 108 |
| 67 EXPECT_EQ(0xDEAD, result); | 109 TEST(ParseNumberTest, ParseNonNegativeIntegerInt64) { |
| 110 TestParseNonNegativeInteger<int64_t>(); | |
| 111 } | |
| 112 | |
| 113 TEST(ParseNumberTest, ParseNonNegativeIntegerUint32) { | |
| 114 TestParseNonNegativeInteger<uint32_t>(); | |
| 115 } | |
| 116 | |
| 117 TEST(ParseNumberTest, ParseNonNegativeIntegerUint64) { | |
| 118 TestParseNonNegativeInteger<uint64_t>(); | |
| 119 } | |
| 120 | |
| 121 TEST(ParseNumberTest, ParseNonNegativeIntegerSizeT) { | |
| 122 // This will effectively be a duplicate of either the uint64_t or uint32_t | |
| 123 // test, depending on bitedness of build. Yet it is worth testing nonetheless. | |
| 124 TestParseNonNegativeInteger<size_t>(); | |
| 125 } | |
| 126 | |
| 127 TEST(ParseNumberTest, ParseSignedIntegerInt32) { | |
| 128 TestParseSignedInteger<int32_t>(); | |
| 129 } | |
| 130 | |
| 131 TEST(ParseNumberTest, ParseSignedIntegerInt64) { | |
| 132 TestParseSignedInteger<int64_t>(); | |
| 68 } | 133 } |
| 69 | 134 |
| 70 } // namespace | 135 } // namespace |
| 71 } // namespace net | 136 } // namespace net |
| OLD | NEW |