Chromium Code Reviews| Index: components/payments/content/payment_manifest_section_table_unittest.cc |
| diff --git a/components/payments/content/payment_manifest_section_table_unittest.cc b/components/payments/content/payment_manifest_section_table_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8ca4bc083c41005231636e1961cc1a034094ed59 |
| --- /dev/null |
| +++ b/components/payments/content/payment_manifest_section_table_unittest.cc |
| @@ -0,0 +1,244 @@ |
| +// Copyright 2017 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 "components/payments/content/payment_manifest_section_table.h" |
| + |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace payments { |
| +namespace { |
|
please use gerrit instead
2017/04/05 20:33:31
Newline below.
gogerald1
2017/04/05 23:38:49
Done.
|
| +const uint32_t kFingerPrintLength = 32; |
|
please use gerrit instead
2017/04/05 20:33:31
size_t
gogerald1
2017/04/05 23:38:49
Done.
|
| + |
| +class PaymentManifestSectionTableTest : public testing::Test { |
| + public: |
| + PaymentManifestSectionTableTest() {} |
| + ~PaymentManifestSectionTableTest() override {} |
| + |
| + protected: |
| + void SetUp() override { |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
|
please use gerrit instead
2017/04/05 20:33:31
Can you put this in the constructor instead?
gogerald1
2017/04/05 23:38:49
Why would we do that? we are not supposed to use t
please use gerrit instead
2017/04/06 14:35:28
Both constructor and SetUp() are called for every
gogerald1
2017/04/06 16:42:09
Ah, Great to know, but it still looks slightly bet
|
| + file_ = temp_dir_.GetPath().AppendASCII("TestWebDatabase"); |
| + |
| + table_.reset(new PaymentManifestSectionTable); |
| + db_.reset(new WebDatabase); |
| + db_->AddTable(table_.get()); |
| + ASSERT_EQ(sql::INIT_OK, db_->Init(file_)); |
| + } |
| + |
| + void TearDown() override {} |
| + |
| + base::FilePath file_; |
| + base::ScopedTempDir temp_dir_; |
| + std::unique_ptr<PaymentManifestSectionTable> table_; |
| + std::unique_ptr<WebDatabase> db_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(PaymentManifestSectionTableTest); |
| +}; |
| + |
| +TEST_F(PaymentManifestSectionTableTest, GetManifestWithoutSection) { |
| + PaymentManifestSectionTable* manifest_section_table = |
| + PaymentManifestSectionTable::FromWebDatabase(db_.get()); |
| + std::string bobpay_method_name = "https://bobpay.com"; |
| + std::vector<mojom::PaymentManifestSectionPtr> retrieved_manifest = |
| + manifest_section_table->GetPaymentManifestSections(bobpay_method_name); |
| + ASSERT_TRUE(retrieved_manifest.empty()); |
| +} |
| + |
| +TEST_F(PaymentManifestSectionTableTest, AddAndGetManifestWithSingleSection) { |
| + std::vector<mojom::PaymentManifestSectionPtr> manifest; |
| + |
| + // Adds a section to the manifest. |
| + mojom::PaymentManifestSectionPtr section = |
| + mojom::PaymentManifestSection::New(); |
| + section->package_name = "com.bobpay"; |
| + section->version = static_cast<int64_t>(1); |
| + // Adds two finger prints. |
| + for (uint32_t i = 0; i < 2; i++) { |
| + std::vector<uint8_t> finger_print; |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + finger_print.push_back(i * kFingerPrintLength + j); |
| + } |
| + section->sha256_cert_fingerprints.push_back(finger_print); |
| + } |
| + manifest.push_back(std::move(section)); |
| + |
| + // Adds the manifest to the table. |
| + PaymentManifestSectionTable* manifest_section_table = |
| + PaymentManifestSectionTable::FromWebDatabase(db_.get()); |
| + std::string bobpay_method_name = "https://bobpay.com"; |
| + ASSERT_TRUE(manifest_section_table->AddPaymentManifestSections( |
| + bobpay_method_name, manifest)); |
| + |
| + // Gets and verifys the manifest. |
| + std::vector<mojom::PaymentManifestSectionPtr> retrieved_manifest = |
| + manifest_section_table->GetPaymentManifestSections(bobpay_method_name); |
| + ASSERT_EQ(retrieved_manifest.size(), 1U); |
| + ASSERT_EQ(retrieved_manifest[0]->package_name, "com.bobpay"); |
| + ASSERT_EQ(retrieved_manifest[0]->version, 1); |
| + ASSERT_EQ(retrieved_manifest[0]->sha256_cert_fingerprints.size(), 2U); |
| + // Verify the two finger prints. |
| + for (uint32_t i = 0; i < 2; i++) { |
| + ASSERT_EQ(retrieved_manifest[0]->sha256_cert_fingerprints[i].size(), |
| + kFingerPrintLength); |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + ASSERT_EQ(retrieved_manifest[0]->sha256_cert_fingerprints[i][j], |
| + i * kFingerPrintLength + j); |
| + } |
| + } |
| +} |
| + |
| +TEST_F(PaymentManifestSectionTableTest, AddAndGetManifestWithMultipleSections) { |
| + std::vector<mojom::PaymentManifestSectionPtr> manifest; |
| + |
| + // Adds section one to the manifest. |
| + mojom::PaymentManifestSectionPtr section_1 = |
| + mojom::PaymentManifestSection::New(); |
| + section_1->package_name = "com.bobpay"; |
| + section_1->version = static_cast<int64_t>(1); |
| + // Adds two finger prints. |
| + for (uint32_t i = 0; i < 2; i++) { |
| + std::vector<uint8_t> finger_print; |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + finger_print.push_back(i * kFingerPrintLength + j); |
| + } |
| + section_1->sha256_cert_fingerprints.push_back(finger_print); |
| + } |
| + manifest.push_back(std::move(section_1)); |
| + |
| + // Adds section two to the manifest. |
| + mojom::PaymentManifestSectionPtr section_2 = |
| + mojom::PaymentManifestSection::New(); |
| + section_2->package_name = "com.alicepay"; |
| + section_2->version = static_cast<int64_t>(2); |
| + // Adds two finger prints. |
| + for (uint32_t i = 0; i < 2; i++) { |
| + std::vector<uint8_t> finger_print; |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + finger_print.push_back(2 * i * kFingerPrintLength + j); |
| + } |
| + section_2->sha256_cert_fingerprints.push_back(finger_print); |
| + } |
| + manifest.push_back(std::move(section_2)); |
| + |
| + // Adds the manifest to the table. |
| + PaymentManifestSectionTable* manifest_section_table = |
| + PaymentManifestSectionTable::FromWebDatabase(db_.get()); |
| + std::string bobpay_method_name = "https://bobpay.com"; |
| + ASSERT_TRUE(manifest_section_table->AddPaymentManifestSections( |
| + bobpay_method_name, manifest)); |
| + |
| + // Retrieves the manifest. |
| + std::vector<mojom::PaymentManifestSectionPtr> retrieved_manifest = |
| + manifest_section_table->GetPaymentManifestSections(bobpay_method_name); |
| + ASSERT_EQ(retrieved_manifest.size(), 2U); |
| + |
| + // Verifys section one. |
| + ASSERT_EQ(retrieved_manifest[0]->package_name, "com.bobpay"); |
| + ASSERT_EQ(retrieved_manifest[0]->version, 1); |
| + ASSERT_EQ(retrieved_manifest[0]->sha256_cert_fingerprints.size(), 2U); |
| + for (uint32_t i = 0; i < 2; i++) { |
| + ASSERT_EQ(retrieved_manifest[0]->sha256_cert_fingerprints[i].size(), |
| + kFingerPrintLength); |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + ASSERT_EQ(retrieved_manifest[0]->sha256_cert_fingerprints[i][j], |
| + i * kFingerPrintLength + j); |
| + } |
| + } |
| + |
| + // Verifys section two. |
| + ASSERT_EQ(retrieved_manifest[1]->package_name, "com.alicepay"); |
| + ASSERT_EQ(retrieved_manifest[1]->version, 2); |
| + ASSERT_EQ(retrieved_manifest[1]->sha256_cert_fingerprints.size(), 2U); |
| + for (uint32_t i = 0; i < 2; i++) { |
| + ASSERT_EQ(retrieved_manifest[1]->sha256_cert_fingerprints[i].size(), |
| + kFingerPrintLength); |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + ASSERT_EQ(retrieved_manifest[1]->sha256_cert_fingerprints[i][j], |
| + 2 * i * kFingerPrintLength + j); |
| + } |
| + } |
| +} |
| + |
| +TEST_F(PaymentManifestSectionTableTest, AddAndGetMultipleManifests) { |
| + PaymentManifestSectionTable* manifest_section_table = |
| + PaymentManifestSectionTable::FromWebDatabase(db_.get()); |
| + |
| + // Adds bobpay manifest to the table. |
| + std::vector<mojom::PaymentManifestSectionPtr> manifest_1; |
| + mojom::PaymentManifestSectionPtr section_1 = |
| + mojom::PaymentManifestSection::New(); |
| + section_1->package_name = "com.bobpay"; |
| + section_1->version = static_cast<int64_t>(1); |
| + // Adds two finger prints. |
| + for (uint32_t i = 0; i < 2; i++) { |
| + std::vector<uint8_t> finger_print; |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + finger_print.push_back(i * kFingerPrintLength + j); |
| + } |
| + section_1->sha256_cert_fingerprints.push_back(finger_print); |
| + } |
| + manifest_1.push_back(std::move(section_1)); |
| + |
| + std::string bobpay_method_name = "https://bobpay.com"; |
| + ASSERT_TRUE(manifest_section_table->AddPaymentManifestSections( |
| + bobpay_method_name, manifest_1)); |
| + |
| + // Adds alicepay manifest to the table. |
| + std::vector<mojom::PaymentManifestSectionPtr> manifest_2; |
| + mojom::PaymentManifestSectionPtr section_2 = |
| + mojom::PaymentManifestSection::New(); |
| + section_2->package_name = "com.alicepay"; |
| + section_2->version = static_cast<int64_t>(2); |
| + // Adds two finger prints. |
| + for (uint32_t i = 0; i < 2; i++) { |
| + std::vector<uint8_t> finger_print; |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + finger_print.push_back(2 * i * kFingerPrintLength + j); |
| + } |
| + section_2->sha256_cert_fingerprints.push_back(finger_print); |
| + } |
| + manifest_2.push_back(std::move(section_2)); |
| + |
| + std::string alicepay_method_name = "https://alicepay.com"; |
| + ASSERT_TRUE(manifest_section_table->AddPaymentManifestSections( |
| + alicepay_method_name, manifest_2)); |
| + |
| + // Verifys bobpay manifest. |
| + std::vector<mojom::PaymentManifestSectionPtr> bobpay_manifest = |
| + manifest_section_table->GetPaymentManifestSections(bobpay_method_name); |
| + ASSERT_EQ(bobpay_manifest.size(), 1U); |
| + |
| + ASSERT_EQ(bobpay_manifest[0]->package_name, "com.bobpay"); |
| + ASSERT_EQ(bobpay_manifest[0]->version, 1); |
| + ASSERT_EQ(bobpay_manifest[0]->sha256_cert_fingerprints.size(), 2U); |
| + for (uint32_t i = 0; i < 2; i++) { |
| + ASSERT_EQ(bobpay_manifest[0]->sha256_cert_fingerprints[i].size(), |
| + kFingerPrintLength); |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + ASSERT_EQ(bobpay_manifest[0]->sha256_cert_fingerprints[i][j], |
| + i * kFingerPrintLength + j); |
| + } |
| + } |
| + |
| + // Verifys alicepay manifest. |
| + std::vector<mojom::PaymentManifestSectionPtr> alicepay_manifest = |
| + manifest_section_table->GetPaymentManifestSections(alicepay_method_name); |
| + ASSERT_EQ(alicepay_manifest.size(), 1U); |
| + |
| + ASSERT_EQ(alicepay_manifest[0]->package_name, "com.alicepay"); |
| + ASSERT_EQ(alicepay_manifest[0]->version, 2); |
| + ASSERT_EQ(alicepay_manifest[0]->sha256_cert_fingerprints.size(), 2U); |
| + for (uint32_t i = 0; i < 2; i++) { |
| + ASSERT_EQ(alicepay_manifest[0]->sha256_cert_fingerprints[i].size(), |
| + kFingerPrintLength); |
| + for (uint32_t j = 0; j < kFingerPrintLength; j++) { |
| + ASSERT_EQ(alicepay_manifest[0]->sha256_cert_fingerprints[i][j], |
| + 2 * i * kFingerPrintLength + j); |
| + } |
| + } |
| +} |
| +} |
| +} // payments |