xref: /aosp_15_r20/external/federated-compute/fcp/secagg/server/secret_sharing_complete_graph_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2020 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 <memory>
18 
19 #include "absl/status/status.h"
20 #include "fcp/secagg/server/secret_sharing_graph.h"
21 #include "fcp/secagg/server/secret_sharing_graph_factory.h"
22 #include "fcp/testing/testing.h"
23 
24 namespace fcp {
25 namespace secagg {
26 namespace {
27 
28 static constexpr int kNumNodes = 10;
29 static constexpr int kThreshold = 5;
30 
TEST(SecretSharingCompleteGraphTest,GetNumNodes)31 TEST(SecretSharingCompleteGraphTest, GetNumNodes) {
32   SecretSharingGraphFactory factory;
33   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
34   EXPECT_EQ(graph->GetNumNodes(), kNumNodes);
35 }
36 
TEST(SecretSharingCompleteGraphTest,GetDegree)37 TEST(SecretSharingCompleteGraphTest, GetDegree) {
38   SecretSharingGraphFactory factory;
39   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
40   EXPECT_EQ(graph->GetDegree(), kNumNodes);
41 }
42 
TEST(SecretSharingCompleteGraphTest,GetThreshold_Valid)43 TEST(SecretSharingCompleteGraphTest, GetThreshold_Valid) {
44   SecretSharingGraphFactory factory;
45   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
46   EXPECT_EQ(graph->GetThreshold(), kThreshold);
47 }
48 
TEST(SecretSharingCompleteGraphTest,Threshold_OutOfRange)49 TEST(SecretSharingCompleteGraphTest, Threshold_OutOfRange) {
50   SecretSharingGraphFactory factory;
51   EXPECT_DEATH(factory.CreateCompleteGraph(kNumNodes, kNumNodes + 1), "");
52 }
53 
TEST(SecretSharingCompleteGraphTest,GetNeighbor_Valid)54 TEST(SecretSharingCompleteGraphTest, GetNeighbor_Valid) {
55   SecretSharingGraphFactory factory;
56   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
57   for (int i = 0; i < graph->GetDegree(); i++) {
58     EXPECT_EQ(graph->GetNeighbor(0, i), i);
59   }
60 }
61 
TEST(SecretSharingCompleteGraphTest,GetNeighbor_OutOfRange)62 TEST(SecretSharingCompleteGraphTest, GetNeighbor_OutOfRange) {
63   SecretSharingGraphFactory factory;
64   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
65   EXPECT_DEATH(graph->GetNeighbor(0, -1), "");
66   EXPECT_DEATH(graph->GetNeighbor(0, kNumNodes), "");
67 }
68 
TEST(SecretSharingCompleteGraphTest,AreNeighbors_Valid)69 TEST(SecretSharingCompleteGraphTest, AreNeighbors_Valid) {
70   SecretSharingGraphFactory factory;
71   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
72   for (int i = 0; i < graph->GetDegree(); i++) {
73     EXPECT_TRUE(graph->AreNeighbors(0, i));
74   }
75 }
76 
TEST(SecretSharingCompleteGraphTest,AreNeighbors_OutOfRange)77 TEST(SecretSharingCompleteGraphTest, AreNeighbors_OutOfRange) {
78   SecretSharingGraphFactory factory;
79   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
80   EXPECT_DEATH(graph->AreNeighbors(0, -1), "");
81   EXPECT_DEATH(graph->AreNeighbors(0, kNumNodes), "");
82 }
83 
TEST(SecretSharingCompleteGraphTest,GetNeighborIndex)84 TEST(SecretSharingCompleteGraphTest, GetNeighborIndex) {
85   SecretSharingGraphFactory factory;
86   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
87   for (int i = 0; i < graph->GetNumNodes(); i++) {
88     for (int j = 0; j < graph->GetDegree(); j++) {
89       EXPECT_EQ(graph->GetNeighborIndex(i, j), j);
90     }
91   }
92 }
93 
TEST(SecretSharingCompleteGraphTest,IsOutgoingNeighbor)94 TEST(SecretSharingCompleteGraphTest, IsOutgoingNeighbor) {
95   SecretSharingGraphFactory factory;
96   auto graph = factory.CreateCompleteGraph(kNumNodes, kThreshold);
97   for (int i = 0; i < graph->GetNumNodes(); i++) {
98     for (int j = 0; j < graph->GetDegree(); j++) {
99       EXPECT_EQ(graph->IsOutgoingNeighbor(i, j), i <= j);
100     }
101   }
102 }
103 
104 }  // namespace
105 }  // namespace secagg
106 }  // namespace fcp
107