OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "components/payments/content/android/utility/payment_manifest_parser.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace payments { |
| 10 namespace { |
| 11 |
| 12 void ExpectUnableToParse(const std::string& input) { |
| 13 std::vector<mojom::PaymentManifestSectionPtr> actual_output = |
| 14 PaymentManifestParser::ParseIntoVector(input); |
| 15 EXPECT_TRUE(actual_output.empty()); |
| 16 } |
| 17 |
| 18 void ExpectParsed( |
| 19 const std::string& input, |
| 20 const std::string& expected_package_name, |
| 21 int expected_version, |
| 22 const std::vector<std::vector<uint8_t>>& expected_fingerprints = {}) { |
| 23 std::vector<mojom::PaymentManifestSectionPtr> actual_output = |
| 24 PaymentManifestParser::ParseIntoVector(input); |
| 25 ASSERT_EQ(1U, actual_output.size()); |
| 26 EXPECT_EQ(expected_package_name, actual_output.front()->package_name); |
| 27 EXPECT_EQ(expected_version, actual_output.front()->version); |
| 28 EXPECT_EQ(expected_fingerprints, |
| 29 actual_output.front()->sha256_cert_fingerprints); |
| 30 } |
| 31 |
| 32 TEST(PaymentManifestParserTest, NullContentIsMalformed) { |
| 33 ExpectUnableToParse(std::string()); |
| 34 } |
| 35 |
| 36 TEST(PaymentManifestParserTest, NonJsonContentIsMalformed) { |
| 37 ExpectUnableToParse("this is not json"); |
| 38 } |
| 39 |
| 40 TEST(PaymentManifestParserTest, StringContentIsMalformed) { |
| 41 ExpectUnableToParse("\"this is a string\""); |
| 42 } |
| 43 |
| 44 TEST(PaymentManifestParserTest, EmptyDictionaryIsMalformed) { |
| 45 ExpectUnableToParse("{}"); |
| 46 } |
| 47 |
| 48 TEST(PaymentManifestParserTest, NullAndroidSectionIsMalformed) { |
| 49 ExpectUnableToParse("{\"android\": null}"); |
| 50 } |
| 51 |
| 52 TEST(PaymentManifestParserTest, NumberAndroidSectionIsMalformed) { |
| 53 ExpectUnableToParse("{\"android\": 0}"); |
| 54 } |
| 55 |
| 56 TEST(PaymentManifestParserTest, ListOfNumbersAndroidSectionIsMalformed) { |
| 57 ExpectUnableToParse("{\"android\": [0]}"); |
| 58 } |
| 59 |
| 60 TEST(PaymentManifestParserTest, |
| 61 ListOfEmptyDictionariesAndroidSectionIsMalformed) { |
| 62 ExpectUnableToParse("{\"android\": [{}]}"); |
| 63 } |
| 64 |
| 65 TEST(PaymentManifestParserTest, NoPackageNameIsMalformed) { |
| 66 ExpectUnableToParse("{\"android\": [{\"version\": 3}]}"); |
| 67 } |
| 68 |
| 69 TEST(PaymentManifestParserTest, OnlyPackageNameIsWellFormed) { |
| 70 ExpectParsed("{\"android\": [{\"package\": \"*\"}]}", "*", 0); |
| 71 } |
| 72 |
| 73 TEST(PaymentManifestParserTest, WellFormed) { |
| 74 ExpectParsed( |
| 75 "{\"android\": [{" |
| 76 "\"package\": \"com.bobpay.app\"," |
| 77 "\"version\": 3," |
| 78 "\"sha256_cert_fingerprints\": " |
| 79 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 80 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"]}]}", |
| 81 "com.bobpay.app", 3, |
| 82 {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xA0, |
| 83 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xB0, 0xB1, |
| 84 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xC0, 0xC1}}); |
| 85 } |
| 86 |
| 87 TEST(PaymentManifestParserTest, ValuesShouldNotHaveNulCharacters) { |
| 88 ExpectUnableToParse( |
| 89 "{\"android\": [{" |
| 90 "\"package\": \"com.bob\0pay.app\"," |
| 91 "\"version\": 3," |
| 92 "\"sha256_cert_fingerprints\": " |
| 93 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 94 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"]}]}"); |
| 95 } |
| 96 |
| 97 TEST(PaymentManifestParserTest, StarPackageShouldBeTheOnlySection) { |
| 98 ExpectUnableToParse( |
| 99 "{\"android\": [{" |
| 100 "\"package\": \"com.bobpay.app\"," |
| 101 "\"version\": 3," |
| 102 "\"sha256_cert_fingerprints\": " |
| 103 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 104 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"]}, {" |
| 105 "\"package\": \"*\"}]}"); |
| 106 } |
| 107 |
| 108 TEST(PaymentManifestParserTest, DuplicateSignaturesWellFormed) { |
| 109 ExpectParsed( |
| 110 "{\"android\": [{" |
| 111 "\"package\": \"com.bobpay.app\"," |
| 112 "\"version\": 3," |
| 113 "\"sha256_cert_fingerprints\": " |
| 114 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 115 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"," |
| 116 "\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 117 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"]}]}", |
| 118 "com.bobpay.app", 3, |
| 119 {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xA0, |
| 120 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xB0, 0xB1, |
| 121 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xC0, 0xC1}, |
| 122 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xA0, |
| 123 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xB0, 0xB1, |
| 124 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xC0, 0xC1}}); |
| 125 } |
| 126 |
| 127 TEST(PaymentManifestParserTest, KeysShouldBeLowerCase) { |
| 128 ExpectUnableToParse( |
| 129 "{\"android\": [{" |
| 130 "\"package\": \"com.bobpay.app\"," |
| 131 "\"version\": 3," |
| 132 "\"sha256_CERT_fingerprints\": " |
| 133 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 134 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"]}]}"); |
| 135 } |
| 136 |
| 137 TEST(PaymentManifestParserTest, FingerprintsShouldBeUpperCase) { |
| 138 ExpectUnableToParse( |
| 139 "{\"android\": [{" |
| 140 "\"package\": \"com.bobpay.app\"," |
| 141 "\"version\": 3," |
| 142 "\"sha256_cert_fingerprints\": " |
| 143 "[\"00:01:02:03:04:05:06:07:08:09:a0:a1:a2:a3:a4:a5:a6:a7:a8:a9:b0:b1:b2:" |
| 144 "b3:b4:b5:b6:b7:b8:b9:c0:c1\"]}]}"); |
| 145 } |
| 146 |
| 147 TEST(PaymentManifestParserTest, FingerprintBytesShouldBeColonSeparated) { |
| 148 ExpectUnableToParse( |
| 149 "{\"android\": [{" |
| 150 "\"package\": \"com.bobpay.app\"," |
| 151 "\"version\": 3," |
| 152 "\"sha256_cert_fingerprints\":" |
| 153 "[\"00010203040506070809A0A1A2A3A4A5A6A7A8A9B0B1B2B3B4B5B6B7B8B9C0C1\"]}]" |
| 154 "}"); |
| 155 } |
| 156 |
| 157 TEST(PaymentManifestParserTest, FingerprintsShouldBeHex) { |
| 158 ExpectUnableToParse( |
| 159 "{\"android\": [{" |
| 160 "\"package\": \"com.bobpay.app\"," |
| 161 "\"version\": 3," |
| 162 "\"sha256_cert_fingerprints\": " |
| 163 "[\"GG:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 164 "B3:B4:B5:B6:B7:B8:B9:C0:C1\"]}]}"); |
| 165 } |
| 166 |
| 167 TEST(PaymentManifestParserTest, FingerprintsShouldContainsThirtyTwoBytes) { |
| 168 ExpectUnableToParse( |
| 169 "{\"android\": [{" |
| 170 "\"package\": \"com.bobpay.app\"," |
| 171 "\"version\": 3," |
| 172 "\"sha256_cert_fingerprints\": " |
| 173 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 174 "B3:B4:B5:B6:B7:B8:B9:C0\"]}]}"); |
| 175 ExpectUnableToParse( |
| 176 "{\"android\": [{" |
| 177 "\"package\": \"com.bobpay.app\"," |
| 178 "\"version\": 3," |
| 179 "\"sha256_cert_fingerprints\": " |
| 180 "[\"00:01:02:03:04:05:06:07:08:09:A0:A1:A2:A3:A4:A5:A6:A7:A8:A9:B0:B1:B2:" |
| 181 "B3:B4:B5:B6:B7:B8:B9:C0:C1:C2\"]}]}"); |
| 182 } |
| 183 |
| 184 } // namespace |
| 185 } // namespace payments |
OLD | NEW |