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/android/web_app_manifest_section_table.h" |
| 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace payments { |
| 11 namespace { |
| 12 |
| 13 class WebAppManifestSectionTableTest : public testing::Test { |
| 14 public: |
| 15 WebAppManifestSectionTableTest() {} |
| 16 ~WebAppManifestSectionTableTest() override {} |
| 17 |
| 18 protected: |
| 19 void SetUp() override { |
| 20 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 21 file_ = temp_dir_.GetPath().AppendASCII("TestWebDatabase"); |
| 22 |
| 23 table_.reset(new WebAppManifestSectionTable); |
| 24 db_.reset(new WebDatabase); |
| 25 db_->AddTable(table_.get()); |
| 26 ASSERT_EQ(sql::INIT_OK, db_->Init(file_)); |
| 27 } |
| 28 |
| 29 void TearDown() override {} |
| 30 |
| 31 std::vector<uint8_t> GenerateFingerprint(uint8_t seed) { |
| 32 std::vector<uint8_t> fingerprint; |
| 33 // Note that the fingerprint is calculated with SHA-256, so the length is |
| 34 // 32. |
| 35 for (size_t i = 0; i < 32U; i++) { |
| 36 fingerprint.push_back((seed + i) % 256U); |
| 37 } |
| 38 return fingerprint; |
| 39 } |
| 40 |
| 41 base::FilePath file_; |
| 42 base::ScopedTempDir temp_dir_; |
| 43 std::unique_ptr<WebAppManifestSectionTable> table_; |
| 44 std::unique_ptr<WebDatabase> db_; |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(WebAppManifestSectionTableTest); |
| 48 }; |
| 49 |
| 50 TEST_F(WebAppManifestSectionTableTest, GetNonExistManifest) { |
| 51 WebAppManifestSectionTable* web_app_manifest_section_table = |
| 52 WebAppManifestSectionTable::FromWebDatabase(db_.get()); |
| 53 mojom::WebAppManifestSectionPtr retrieved_manifest = |
| 54 web_app_manifest_section_table->GetWebAppManifest("https://bobpay.com"); |
| 55 ASSERT_TRUE(retrieved_manifest.get() == nullptr); |
| 56 } |
| 57 |
| 58 TEST_F(WebAppManifestSectionTableTest, AddAndGetManifest) { |
| 59 std::vector<uint8_t> fingerprint_one = GenerateFingerprint(1); |
| 60 std::vector<uint8_t> fingerprint_two = GenerateFingerprint(32); |
| 61 |
| 62 // create a bobpay web app manifest. |
| 63 mojom::WebAppManifestSectionPtr manifest = |
| 64 mojom::WebAppManifestSection::New(); |
| 65 manifest->id = "com.bobpay"; |
| 66 manifest->min_version = static_cast<int64_t>(1); |
| 67 manifest->fingerprints.push_back(fingerprint_one); |
| 68 manifest->fingerprints.push_back(fingerprint_two); |
| 69 |
| 70 // Adds the manifest to the table. |
| 71 WebAppManifestSectionTable* web_app_manifest_section_table = |
| 72 WebAppManifestSectionTable::FromWebDatabase(db_.get()); |
| 73 ASSERT_TRUE( |
| 74 web_app_manifest_section_table->AddWebAppManifest(manifest.get())); |
| 75 |
| 76 // Gets and verifys the manifest. |
| 77 mojom::WebAppManifestSectionPtr retrieved_manifest = |
| 78 web_app_manifest_section_table->GetWebAppManifest("com.bobpay"); |
| 79 ASSERT_EQ(retrieved_manifest->id, "com.bobpay"); |
| 80 ASSERT_EQ(retrieved_manifest->min_version, 1); |
| 81 ASSERT_EQ(retrieved_manifest->fingerprints.size(), 2U); |
| 82 |
| 83 // Verify the two fingerprints. |
| 84 ASSERT_TRUE(retrieved_manifest->fingerprints[0] == fingerprint_one); |
| 85 ASSERT_TRUE(retrieved_manifest->fingerprints[1] == fingerprint_two); |
| 86 } |
| 87 |
| 88 TEST_F(WebAppManifestSectionTableTest, AddAndGetMultipleManifests) { |
| 89 std::vector<uint8_t> fingerprint_one = GenerateFingerprint(1); |
| 90 std::vector<uint8_t> fingerprint_two = GenerateFingerprint(32); |
| 91 std::vector<uint8_t> fingerprint_three = GenerateFingerprint(2); |
| 92 std::vector<uint8_t> fingerprint_four = GenerateFingerprint(30); |
| 93 |
| 94 WebAppManifestSectionTable* web_app_manifest_section_table = |
| 95 WebAppManifestSectionTable::FromWebDatabase(db_.get()); |
| 96 |
| 97 // Adds bobpay manifest to the table. |
| 98 mojom::WebAppManifestSectionPtr manifest_1 = |
| 99 mojom::WebAppManifestSection::New(); |
| 100 manifest_1->id = "com.bobpay"; |
| 101 manifest_1->min_version = static_cast<int64_t>(1); |
| 102 // Adds two finger prints. |
| 103 manifest_1->fingerprints.push_back(fingerprint_one); |
| 104 manifest_1->fingerprints.push_back(fingerprint_two); |
| 105 ASSERT_TRUE( |
| 106 web_app_manifest_section_table->AddWebAppManifest(manifest_1.get())); |
| 107 |
| 108 // Adds alicepay manifest to the table. |
| 109 mojom::WebAppManifestSectionPtr manifest_2 = |
| 110 mojom::WebAppManifestSection::New(); |
| 111 manifest_2->id = "com.alicepay"; |
| 112 manifest_2->min_version = static_cast<int64_t>(2); |
| 113 // Adds two finger prints. |
| 114 manifest_2->fingerprints.push_back(fingerprint_three); |
| 115 manifest_2->fingerprints.push_back(fingerprint_four); |
| 116 ASSERT_TRUE( |
| 117 web_app_manifest_section_table->AddWebAppManifest(manifest_2.get())); |
| 118 |
| 119 // Verifys bobpay manifest. |
| 120 mojom::WebAppManifestSectionPtr bobpay_manifest = |
| 121 web_app_manifest_section_table->GetWebAppManifest("com.bobpay"); |
| 122 ASSERT_EQ(bobpay_manifest->id, "com.bobpay"); |
| 123 ASSERT_EQ(bobpay_manifest->min_version, 1); |
| 124 ASSERT_EQ(bobpay_manifest->fingerprints.size(), 2U); |
| 125 ASSERT_TRUE(bobpay_manifest->fingerprints[0] == fingerprint_one); |
| 126 ASSERT_TRUE(bobpay_manifest->fingerprints[1] == fingerprint_two); |
| 127 |
| 128 // Verifys alicepay manifest. |
| 129 mojom::WebAppManifestSectionPtr alicepay_manifest = |
| 130 web_app_manifest_section_table->GetWebAppManifest("com.alicepay"); |
| 131 ASSERT_EQ(alicepay_manifest->id, "com.alicepay"); |
| 132 ASSERT_EQ(alicepay_manifest->min_version, 2); |
| 133 ASSERT_EQ(alicepay_manifest->fingerprints.size(), 2U); |
| 134 ASSERT_TRUE(alicepay_manifest->fingerprints[0] == fingerprint_three); |
| 135 ASSERT_TRUE(alicepay_manifest->fingerprints[1] == fingerprint_four); |
| 136 } |
| 137 } |
| 138 } // payments |
OLD | NEW |