blob: 5d4d9c823b23e64fc0584e3bf389dee11b5b53cd [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2021 The Chromium Authors
Roger McFarlane3da6db2732021-10-06 13:41:042// 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
16namespace variations {
17
18using SignedSeedDataTest = ::testing::TestWithParam<SignedSeedData>;
19
20TEST_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 Sepez207184ff2024-02-01 16:58:0031 std::string actual_encoded_uncompressed_data =
32 base::Base64Encode(actual_uncompressed_data);
Roger McFarlane3da6db2732021-10-06 13:41:0433 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
43TEST_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
55TEST_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 Defresne346fb2a72021-10-11 17:54:5869INSTANTIATE_TEST_SUITE_P(VariationsTestUtils,
70 SignedSeedDataTest,
71 ::testing::Values(kTestSeedData, kCrashingSeedData));
Roger McFarlane3da6db2732021-10-06 13:41:0472
Sylvain Defresne346fb2a72021-10-11 17:54:5873} // namespace variations