xref: /aosp_15_r20/external/federated-compute/fcp/secagg/testing/test_matchers.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2019 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/secagg/testing/test_matchers.h"
18 
19 #include <string>
20 
21 namespace fcp {
22 namespace secagg {
23 namespace testing {
24 
ToSecAggVectorData(const SecAggVector & vector)25 SecAggVectorData ToSecAggVectorData(const SecAggVector& vector){
26   return std::make_pair(vector.modulus(), vector.GetAsUint64Vector());
27 }
28 
ToDataMap(const SecAggVectorMap & secagg_vector_map)29 SecAggVectorDataMap ToDataMap(const SecAggVectorMap& secagg_vector_map) {
30   SecAggVectorDataMap result;
31   for (const auto& item : secagg_vector_map) {
32     result.emplace(item.first, ToSecAggVectorData(item.second));
33   }
34   return result;
35 }
36 
37 class SecAggVectorMapMatcherImpl
38     : public ::testing::MatcherInterface<const SecAggVectorMap&> {
39  public:
SecAggVectorMapMatcherImpl(SecAggVectorDataMap expected)40   explicit SecAggVectorMapMatcherImpl(SecAggVectorDataMap expected)
41       : expected_(expected) {}
DescribeTo(::std::ostream * os) const42   void DescribeTo(::std::ostream* os) const override {
43     for (const auto& item : expected_) {
44       *os << "{name: \"" << item.first << "\", modulus: " << item.second.first
45           << ", vector:";
46       for (uint64_t val : item.second.second) {
47         *os << " " << val;
48       }
49       *os << "} ";
50     }
51   }
52 
MatchAndExplain(const SecAggVectorMap & arg,::testing::MatchResultListener * listener) const53   bool MatchAndExplain(
54       const SecAggVectorMap& arg,
55       ::testing::MatchResultListener* listener) const override {
56     return ::testing::ExplainMatchResult(
57         ::testing::UnorderedElementsAreArray(expected_), ToDataMap(arg),
58         listener);
59   }
60 
61  private:
62   SecAggVectorDataMap expected_;
63 };
64 
operator ::testing::Matcher<const SecAggVectorMap&>() const65 SecAggVectorMapMatcher::operator ::testing::Matcher<const SecAggVectorMap&>()
66     const {
67   return ::testing::MakeMatcher(new SecAggVectorMapMatcherImpl(expected_));
68 }
69 
MatchesSecAggVectorMap(const SecAggVectorMap & expected)70 SecAggVectorMapMatcher MatchesSecAggVectorMap(const SecAggVectorMap& expected) {
71   return SecAggVectorMapMatcher(ToDataMap(expected));
72 }
73 
MatchesSecAggVector(const std::string & name,const SecAggVector & vector)74 SecAggVectorMapMatcher MatchesSecAggVector(const std::string& name,
75                                            const SecAggVector& vector) {
76   SecAggVectorDataMap expected;
77   expected.emplace(name, ToSecAggVectorData(vector));
78   return SecAggVectorMapMatcher(expected);
79 }
80 
81 }  // namespace testing
82 }  // namespace secagg
83 }  // namespace fcp
84