Loading...
Searching...
No Matches
signature_verifier_test.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-2022 ExpoLab, UC Davis
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 */
25
27
28#include <glog/logging.h>
29#include <gmock/gmock.h>
30#include <gtest/gtest.h>
31
34
35namespace resdb {
36namespace {
37
38using ::resdb::testing::EqualsProto;
39
40TEST(SignatureVerifyTest, CalculateHash) {
41 std::string expected_str =
42 "\x9F\x86\xD0\x81\x88L}e\x9A/"
43 "\xEA\xA0\xC5Z\xD0\x15\xA3\xBFO\x1B+\v\x82,\xD1]l\x15\xB0\xF0\n\b";
44 EXPECT_EQ(SignatureVerifier::CalculateHash("test"), expected_str);
45}
46
47KeyInfo GetKeyInfo(SecretKey key) {
48 KeyInfo info;
49 info.set_key(key.private_key());
50 info.set_hash_type(key.hash_type());
51 return info;
52}
53
54class SignatureVerifyPTest
55 : public ::testing::TestWithParam<SignatureInfo::HashType> {
56 public:
57 SignatureVerifyPTest() {
58 admin_key_ = KeyGenerator ::GeneratorKeys(SignatureInfo::RSA);
59 admin_verifier_ = std::make_unique<SignatureVerifier>(
60 GetKeyInfo(admin_key_), GetCertInfo(0));
61 }
62
63 CertificateInfo GetCertInfo(int64_t node_id) {
64 CertificateInfo cert_info;
65 cert_info.set_node_id(node_id);
66
67 cert_info.mutable_admin_public_key()->set_key(admin_key_.public_key());
68 cert_info.mutable_admin_public_key()->set_hash_type(admin_key_.hash_type());
69
70 return cert_info;
71 }
72
73 CertificateKey GetPublicKeyInfo(SecretKey key, int64_t node_id) {
74 CertificateKey cert_key;
75 cert_key.mutable_public_key_info()->mutable_key()->set_key(
76 key.public_key());
77 cert_key.mutable_public_key_info()->mutable_key()->set_hash_type(
78 key.hash_type());
79 cert_key.mutable_public_key_info()->set_node_id(node_id);
80
81 auto resp_info =
82 admin_verifier_->SignCertificateKeyInfo(cert_key.public_key_info());
83 assert(resp_info.ok());
84 *cert_key.mutable_certificate() = *resp_info;
85 return cert_key;
86 }
87
88 private:
89 SecretKey admin_key_;
90 std::unique_ptr<SignatureVerifier> admin_verifier_;
91};
92
93TEST_P(SignatureVerifyPTest, SignAndVerify) {
94 SignatureInfo::HashType type = GetParam();
95 absl::StatusOr<SignatureInfo> s_info;
96 std::string message = "test_message";
97 SecretKey my_key = KeyGenerator ::GeneratorKeys(type);
98 int64_t my_node_id = 1, your_node_id = 2;
99 {
100 SignatureVerifier verifier(GetKeyInfo(my_key), GetCertInfo(my_node_id));
101 s_info = verifier.SignMessage(message);
102 EXPECT_TRUE(s_info.ok());
103 EXPECT_EQ(s_info->node_id(), my_node_id);
104 }
105 {
106 SecretKey your_key = KeyGenerator ::GeneratorKeys(type);
107 SignatureVerifier verifier(GetKeyInfo(your_key), GetCertInfo(your_node_id));
108 verifier.AddPublicKey(GetPublicKeyInfo(my_key, my_node_id));
109 EXPECT_TRUE(verifier.VerifyMessage(message, *s_info));
110 }
111}
112
113TEST_P(SignatureVerifyPTest, KeyNotFound) {
114 SignatureInfo::HashType type = GetParam();
115
116 absl::StatusOr<SignatureInfo> s_info;
117 std::string message = "test_message";
118 SecretKey my_key = KeyGenerator ::GeneratorKeys(type);
119 int64_t my_node_id = 1, your_node_id = 2;
120
121 {
122 SignatureVerifier verifier(GetKeyInfo(my_key), GetCertInfo(my_node_id));
123 s_info = verifier.SignMessage(message);
124 EXPECT_TRUE(s_info.ok());
125 EXPECT_EQ(s_info->node_id(), my_node_id);
126 }
127
128 {
129 SecretKey your_key = KeyGenerator ::GeneratorKeys(type);
130 SignatureVerifier verifier(GetKeyInfo(your_key), GetCertInfo(your_node_id));
131 EXPECT_FALSE(verifier.VerifyMessage(message, *s_info));
132 }
133}
134
135TEST_P(SignatureVerifyPTest, KeyNotMatch) {
136 SignatureInfo::HashType type = GetParam();
137
138 absl::StatusOr<SignatureInfo> s_info;
139 std::string message = "test_message";
140 SecretKey my_key = KeyGenerator ::GeneratorKeys(type);
141 SecretKey your_key = KeyGenerator ::GeneratorKeys(type);
142 int64_t my_node_id = 1, your_node_id = 2;
143
144 {
145 SignatureVerifier verifier(GetKeyInfo(my_key), GetCertInfo(my_node_id));
146 s_info = verifier.SignMessage(message);
147 EXPECT_TRUE(s_info.ok());
148 EXPECT_EQ(s_info->node_id(), my_node_id);
149 }
150
151 {
152 SignatureVerifier verifier(GetKeyInfo(your_key), GetCertInfo(your_node_id));
153 verifier.AddPublicKey(GetPublicKeyInfo(your_key, my_node_id));
154 EXPECT_FALSE(verifier.VerifyMessage(message, *s_info));
155 }
156}
157
158INSTANTIATE_TEST_SUITE_P(SignatureVerifyPTest, SignatureVerifyPTest,
159 ::testing::Values(SignatureInfo::RSA,
160 SignatureInfo::ED25519,
161 SignatureInfo::CMAC_AES,
162 SignatureInfo::ECDSA));
163
164} // namespace
165
166} // namespace resdb
static std::string CalculateHash(const std::string &str)
TEST(NetworkUtilsTest, GetDNSName)