-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_saga_logger.cpp
More file actions
222 lines (182 loc) · 8.33 KB
/
test_saga_logger.cpp
File metadata and controls
222 lines (182 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: test_saga_logger.cpp ║
Version: 0.0.37 ║
Last Modified: 2026-04-06 04:34:25 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 220 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 5bee4e8e41 2026-04-03 Implement Disaster Recovery Manager and associated tests ║
• 25f9a09910 2026-04-02 Refactor tests and improve assertions ║
• 2a1fb04231 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
#include <gtest/gtest.h>
#include "utils/saga_logger.h"
#include "utils/lek_manager.h"
#include "security/pki_key_provider.h"
#include "security/mock_key_provider.h"
#include "themis/edition.h"
#include "utils/pki_client.h"
#include "storage/rocksdb_wrapper.h"
#include <filesystem>
using namespace themis;
using namespace themis::utils;
using namespace themis::security;
namespace {
bool IsFieldEncryptionAvailable() {
return themis::edition::IsFeatureEnabled("field_encryption");
}
}
class SAGALoggerTest : public ::testing::Test {
protected:
void SetUp() override {
if (!IsFieldEncryptionAvailable()) {
GTEST_SKIP() << "Field encryption unavailable in Community edition";
}
// Clean test directories
std::filesystem::remove_all("data/test_saga");
std::filesystem::create_directories("data/test_saga");
// Setup components
key_provider_ = std::make_shared<MockKeyProvider>();
PKIConfig pki_cfg;
pki_cfg.service_id = "test-saga";
pki_cfg.endpoint = "https://localhost:8443";
pki_client_ = std::make_shared<VCCPKIClient>(pki_cfg);
enc_ = std::make_shared<FieldEncryption>(key_provider_);
// Create LEK for testing
key_provider_->createKey("saga_lek", 32);
}
void TearDown() override {
std::filesystem::remove_all("data/test_saga");
}
std::shared_ptr<MockKeyProvider> key_provider_;
std::shared_ptr<VCCPKIClient> pki_client_;
std::shared_ptr<FieldEncryption> enc_;
};
TEST_F(SAGALoggerTest, LogAndFlush_CreatesSignedBatch) {
if (!IsFieldEncryptionAvailable()) {
GTEST_SKIP() << "Field encryption unavailable in Community edition";
}
SAGALoggerConfig cfg;
cfg.batch_size = 2;
cfg.batch_interval = std::chrono::minutes(60);
cfg.log_path = "data/test_saga/saga.jsonl";
cfg.signature_path = "data/test_saga/signatures.jsonl";
cfg.key_id = "saga_lek";
cfg.encrypt_then_sign = true;
SAGALogger logger(enc_, pki_client_, cfg);
// Log 2 steps to trigger flush
SAGAStep step1;
step1.saga_id = "tx_001";
step1.step_name = "create_user";
step1.action = "forward";
step1.entity_id = "user_123";
step1.payload = {{"email", "test@example.com"}};
step1.status = "success";
step1.timestamp = std::chrono::system_clock::now();
SAGAStep step2;
step2.saga_id = "tx_001";
step2.step_name = "send_email";
step2.action = "forward";
step2.entity_id = "email_456";
step2.payload = {{"to", "test@example.com"}};
step2.status = "success";
step2.timestamp = std::chrono::system_clock::now();
logger.logStep(step1);
logger.logStep(step2); // Triggers flush at batch_size=2
// Verify files exist
EXPECT_TRUE(std::filesystem::exists(cfg.log_path));
EXPECT_TRUE(std::filesystem::exists(cfg.signature_path));
// Verify batch can be listed
auto batches = logger.listBatches();
ASSERT_EQ(batches.size(), 1);
}
TEST_F(SAGALoggerTest, VerifyBatch_ValidSignature_ReturnsTrue) {
if (!IsFieldEncryptionAvailable()) {
GTEST_SKIP() << "Field encryption unavailable in Community edition";
}
SAGALoggerConfig cfg;
cfg.batch_size = 1;
cfg.log_path = "data/test_saga/saga2.jsonl";
cfg.signature_path = "data/test_saga/signatures2.jsonl";
cfg.key_id = "saga_lek";
cfg.encrypt_then_sign = true;
SAGALogger logger(enc_, pki_client_, cfg);
SAGAStep step;
step.saga_id = "tx_verify";
step.step_name = "test_step";
step.action = "forward";
step.entity_id = "entity_789";
step.payload = {{"data", "test"}};
step.status = "success";
step.timestamp = std::chrono::system_clock::now();
logger.logStep(step); // Triggers flush
auto batches = logger.listBatches();
ASSERT_FALSE(batches.empty());
// Verify first batch
bool verified = logger.verifyBatch(batches[0]);
EXPECT_TRUE(verified);
}
TEST_F(SAGALoggerTest, LoadBatch_DecryptsAndReturnsSteps) {
if (!IsFieldEncryptionAvailable()) {
GTEST_SKIP() << "Field encryption unavailable in Community edition";
}
SAGALoggerConfig cfg;
cfg.batch_size = 2;
cfg.log_path = "data/test_saga/saga3.jsonl";
cfg.signature_path = "data/test_saga/signatures3.jsonl";
cfg.key_id = "saga_lek";
cfg.encrypt_then_sign = true;
SAGALogger logger(enc_, pki_client_, cfg);
SAGAStep step1;
step1.saga_id = "tx_load";
step1.step_name = "step_a";
step1.action = "forward";
step1.entity_id = "ent_a";
step1.payload = {{"field", "value_a"}};
step1.status = "success";
step1.timestamp = std::chrono::system_clock::now();
SAGAStep step2;
step2.saga_id = "tx_load";
step2.step_name = "step_b";
step2.action = "compensate";
step2.entity_id = "ent_b";
step2.payload = {{"field", "value_b"}};
step2.status = "success";
step2.timestamp = std::chrono::system_clock::now();
logger.logStep(step1);
logger.logStep(step2);
auto batches = logger.listBatches();
ASSERT_FALSE(batches.empty());
// Load and verify decryption
auto loaded_steps = logger.loadBatch(batches[0]);
ASSERT_EQ(loaded_steps.size(), 2);
EXPECT_EQ(loaded_steps[0].saga_id, "tx_load");
EXPECT_EQ(loaded_steps[0].step_name, "step_a");
EXPECT_EQ(loaded_steps[0].action, "forward");
EXPECT_EQ(loaded_steps[1].saga_id, "tx_load");
EXPECT_EQ(loaded_steps[1].step_name, "step_b");
EXPECT_EQ(loaded_steps[1].action, "compensate");
}
TEST_F(SAGALoggerTest, Flush_EmptyBuffer_DoesNothing) {
SAGALoggerConfig cfg;
cfg.batch_size = 100;
cfg.log_path = "data/test_saga/saga4.jsonl";
cfg.signature_path = "data/test_saga/signatures4.jsonl";
cfg.key_id = "saga_lek";
SAGALogger logger(enc_, pki_client_, cfg);
logger.flush(); // Should not crash or create files
auto batches = logger.listBatches();
EXPECT_TRUE(batches.empty());
}