signed_web_bundle_id_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2022 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. #include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
  5. #include <array>
  6. #include <tuple>
  7. #include <utility>
  8. #include "base/strings/string_piece.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace web_package {
  12. class SignedWebBundleIdValidTest
  13. : public ::testing::TestWithParam<
  14. std::tuple<std::string,
  15. SignedWebBundleId::Type,
  16. absl::optional<std::array<uint8_t, 32>>>> {
  17. public:
  18. SignedWebBundleIdValidTest()
  19. : raw_id_(std::get<0>(GetParam())),
  20. type_(std::get<1>(GetParam())),
  21. public_key_(std::get<2>(GetParam())) {}
  22. protected:
  23. std::string raw_id_;
  24. SignedWebBundleId::Type type_;
  25. absl::optional<std::array<uint8_t, 32>> public_key_;
  26. };
  27. TEST_P(SignedWebBundleIdValidTest, ValidIDs) {
  28. const auto parsed_id = SignedWebBundleId::Create(raw_id_);
  29. EXPECT_TRUE(parsed_id.has_value());
  30. EXPECT_EQ(parsed_id->type(), type_);
  31. if (type_ == SignedWebBundleId::Type::kEd25519PublicKey) {
  32. EXPECT_EQ(parsed_id->GetEd25519PublicKey().bytes(), *public_key_);
  33. }
  34. }
  35. INSTANTIATE_TEST_SUITE_P(
  36. All,
  37. SignedWebBundleIdValidTest,
  38. ::testing::Values(
  39. // Development-only key suffix
  40. std::make_tuple(
  41. "aerugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaac",
  42. SignedWebBundleId::Type::kDevelopment,
  43. absl::nullopt),
  44. // Ed25519 key suffix
  45. std::make_tuple(
  46. "aerugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaic",
  47. SignedWebBundleId::Type::kEd25519PublicKey,
  48. std::array<uint8_t, 32>({0x01, 0x23, 0x43, 0x43, 0x33, 0x42, 0x7A,
  49. 0x14, 0x42, 0x14, 0xa2, 0xb6, 0xc2, 0xd9,
  50. 0xf2, 0x02, 0x03, 0x42, 0x18, 0x10, 0x12,
  51. 0x26, 0x62, 0x88, 0xf6, 0xa3, 0xa5, 0x47,
  52. 0x14, 0x69, 0x00, 0x73}))),
  53. [](const testing::TestParamInfo<SignedWebBundleIdValidTest::ParamType>&
  54. info) { return std::get<0>(info.param); });
  55. class SignedWebBundleIdInvalidTest
  56. : public ::testing::TestWithParam<std::pair<std::string, std::string>> {};
  57. TEST_P(SignedWebBundleIdInvalidTest, InvalidIDs) {
  58. EXPECT_FALSE(SignedWebBundleId::Create(GetParam().second).has_value());
  59. }
  60. INSTANTIATE_TEST_SUITE_P(
  61. All,
  62. SignedWebBundleIdInvalidTest,
  63. ::testing::Values(
  64. std::make_pair("emptyKey", ""),
  65. std::make_pair(
  66. "oneCharacterShort",
  67. "erugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaic"),
  68. std::make_pair(
  69. "invalidSuffix",
  70. "aerugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaayc"),
  71. std::make_pair(
  72. "usesPadding",
  73. "aerugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdj74aagaa="),
  74. std::make_pair(
  75. "validKeyButInUppercase",
  76. "AERUGQZTIJ5BIQQUUK3MFWPSAIBUEGAQCITGFCHWUOSUOFDJABZQAAIC"),
  77. std::make_pair(
  78. "invalidCharacter9",
  79. "9erugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaac")),
  80. [](const testing::TestParamInfo<SignedWebBundleIdInvalidTest::ParamType>&
  81. info) { return info.param.first; });
  82. TEST(SignedWebBundleIdTest, Comparators) {
  83. const auto a1 = *SignedWebBundleId::Create(
  84. "aerugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaac");
  85. const auto a2 = *SignedWebBundleId::Create(
  86. "aerugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaac");
  87. const auto b = *SignedWebBundleId::Create(
  88. "berugqztij5biqquuk3mfwpsaibuegaqcitgfchwuosuofdjabzqaaac");
  89. EXPECT_TRUE(a1 == a1);
  90. EXPECT_TRUE(a1 == a2);
  91. EXPECT_FALSE(a1 == b);
  92. EXPECT_TRUE(a1 < b);
  93. EXPECT_FALSE(b < a2);
  94. }
  95. } // namespace web_package