Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Roger McFarlane | 3da6db273 | 2021-10-06 13:41:04 | [diff] [blame] | 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/variations/variations_test_utils.h" |
| 6 | |
| 7 | #include "base/base64.h" |
| 8 | #include "base/ranges/algorithm.h" |
| 9 | #include "components/variations/metrics.h" |
| 10 | #include "components/variations/proto/variations_seed.pb.h" |
| 11 | #include "components/variations/variations_seed_store.h" |
| 12 | #include "testing/gmock/include/gmock/gmock.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "third_party/zlib/google/compression_utils.h" |
| 15 | |
| 16 | namespace variations { |
| 17 | |
| 18 | using SignedSeedDataTest = ::testing::TestWithParam<SignedSeedData>; |
| 19 | |
| 20 | TEST_P(SignedSeedDataTest, HasValidBase64Data) { |
| 21 | const auto& signed_seed_data = GetParam(); |
| 22 | |
| 23 | std::string decoded_compressed_data; |
| 24 | ASSERT_TRUE(base::Base64Decode(signed_seed_data.base64_compressed_data, |
| 25 | &decoded_compressed_data)); |
| 26 | |
| 27 | std::string actual_uncompressed_data; |
| 28 | ASSERT_TRUE(compression::GzipUncompress(decoded_compressed_data, |
| 29 | &actual_uncompressed_data)); |
| 30 | |
Tom Sepez | 207184ff | 2024-02-01 16:58:00 | [diff] [blame] | 31 | std::string actual_encoded_uncompressed_data = |
| 32 | base::Base64Encode(actual_uncompressed_data); |
Roger McFarlane | 3da6db273 | 2021-10-06 13:41:04 | [diff] [blame] | 33 | EXPECT_EQ(actual_encoded_uncompressed_data, |
| 34 | signed_seed_data.base64_uncompressed_data); |
| 35 | |
| 36 | std::string decoded_uncompressed_data; |
| 37 | ASSERT_TRUE(base::Base64Decode(signed_seed_data.base64_uncompressed_data, |
| 38 | &decoded_uncompressed_data)); |
| 39 | |
| 40 | EXPECT_EQ(decoded_uncompressed_data, actual_uncompressed_data); |
| 41 | } |
| 42 | |
| 43 | TEST_P(SignedSeedDataTest, HasValidSignature) { |
| 44 | const auto& signed_seed_data = GetParam(); |
| 45 | std::string decoded_uncompressed_data; |
| 46 | ASSERT_TRUE(base::Base64Decode(signed_seed_data.base64_uncompressed_data, |
| 47 | &decoded_uncompressed_data)); |
| 48 | |
| 49 | const auto verify_signature_result = |
| 50 | VariationsSeedStore::VerifySeedSignatureForTesting( |
| 51 | decoded_uncompressed_data, signed_seed_data.base64_signature); |
| 52 | EXPECT_EQ(VerifySignatureResult::VALID_SIGNATURE, verify_signature_result); |
| 53 | } |
| 54 | |
| 55 | TEST_P(SignedSeedDataTest, HasStudyNames) { |
| 56 | const auto& signed_seed_data = GetParam(); |
| 57 | std::string decoded_uncompressed_data; |
| 58 | ASSERT_TRUE(base::Base64Decode(signed_seed_data.base64_uncompressed_data, |
| 59 | &decoded_uncompressed_data)); |
| 60 | VariationsSeed seed; |
| 61 | ASSERT_TRUE(seed.ParseFromString(decoded_uncompressed_data)); |
| 62 | std::vector<std::string> parsed_study_names; |
| 63 | base::ranges::transform(seed.study(), std::back_inserter(parsed_study_names), |
| 64 | [](const Study& s) { return s.name(); }); |
| 65 | EXPECT_THAT(parsed_study_names, ::testing::UnorderedElementsAreArray( |
| 66 | signed_seed_data.study_names)); |
| 67 | } |
| 68 | |
Sylvain Defresne | 346fb2a7 | 2021-10-11 17:54:58 | [diff] [blame] | 69 | INSTANTIATE_TEST_SUITE_P(VariationsTestUtils, |
| 70 | SignedSeedDataTest, |
| 71 | ::testing::Values(kTestSeedData, kCrashingSeedData)); |
Roger McFarlane | 3da6db273 | 2021-10-06 13:41:04 | [diff] [blame] | 72 | |
Sylvain Defresne | 346fb2a7 | 2021-10-11 17:54:58 | [diff] [blame] | 73 | } // namespace variations |