-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_pii_stream_scanner.cpp
More file actions
164 lines (136 loc) · 7.48 KB
/
test_pii_stream_scanner.cpp
File metadata and controls
164 lines (136 loc) · 7.48 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
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: test_pii_stream_scanner.cpp ║
Version: 0.0.3 ║
Last Modified: 2026-04-06 04:32:11 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟡 RELEASE-CANDIDATE ║
• Quality Score: 65.0/100 ║
• Total Lines: 163 ║
• Open Issues: TODOs: 0, Stubs: 11 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 25f9a09910 2026-04-02 Refactor tests and improve assertions ║
• edcfeb9848 2026-03-11 feat: add scripts for auditing and reconciling GitHub iss... ║
• eea8f803ba 2026-03-09 feat(utils): implement HashChainAuditWriter/AuditLogVerif... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ⚠️ Needs Work ║
╚═════════════════════════════════════════════════════════════════════╝
*/
#include <gtest/gtest.h>
#include "utils/pii_detection_engine.h"
#include <memory>
#include <string>
#include <vector>
using namespace themis::utils;
// ---------------------------------------------------------------------------
// Minimal stub engine that detects the literal token "SECRET" as PII
// ---------------------------------------------------------------------------
class StubEngine : public IPIIDetectionEngine {
public:
std::string getName() const override { return "stub"; }
std::string getVersion() const override { return "1.0.0"; }
bool isEnabled() const override { return true; }
PluginSignature getSignature() const override { return {}; }
bool initialize(const nlohmann::json&) override { return true; }
bool reload(const nlohmann::json&) override { return true; }
std::string getLastError() const override { return {}; }
nlohmann::json getMetadata() const override { return {}; }
PIIType classifyFieldName(const std::string&) const override { return PIIType::UNKNOWN; }
std::string getRedactionRecommendation(PIIType) const override { return "partial"; }
std::vector<PIIFinding> detectInText(const std::string& text) const override {
std::vector<PIIFinding> results;
const std::string token = "SECRET";
size_t pos = 0;
while ((pos = text.find(token, pos)) != std::string::npos) {
PIIFinding f;
f.type = PIIType::UNKNOWN;
f.value = token;
f.start_offset = pos;
f.end_offset = pos + token.size();
f.confidence = 1.0;
f.engine_name = "stub";
results.push_back(f);
pos += token.size();
}
return results;
}
};
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
// ============================================================================
// Construction
// ============================================================================
TEST(PIIStreamScanner, NullEngineThrows) {
EXPECT_THROW(PIIStreamScanner(nullptr), std::invalid_argument);
}
TEST(PIIStreamScanner, ValidConstructionDoesNotThrow) {
auto detector = std::make_shared<StubEngine>();
EXPECT_NO_THROW({
auto scanner = std::make_unique<PIIStreamScanner>(detector, PIIStreamScannerConfig{});
EXPECT_NE(scanner, nullptr);
});
}
// ============================================================================
// Single-chunk scan
// ============================================================================
TEST(PIIStreamScanner, DetectsTokenInSingleChunk) {
auto engine = std::make_shared<StubEngine>();
PIIStreamScanner scanner(engine);
auto findings = scanner.scan_chunk("Hello SECRET world", /*is_last=*/true);
ASSERT_EQ(findings.size(), 1u);
EXPECT_EQ(findings[0].value, "SECRET");
}
TEST(PIIStreamScanner, NoFindingsOnCleanText) {
auto engine = std::make_shared<StubEngine>();
PIIStreamScanner scanner(engine);
auto findings = scanner.scan_chunk("Hello clean world", true);
EXPECT_TRUE(findings.empty());
}
// ============================================================================
// Multi-chunk: boundary handling
// ============================================================================
TEST(PIIStreamScanner, DetectsTokenSplitAcrossChunks) {
auto engine = std::make_shared<StubEngine>();
PIIStreamScannerConfig cfg;
// Keep exactly the trailing "SECR" so the next chunk can complete "SECRET".
cfg.lookahead_bytes = 4;
PIIStreamScanner scanner(engine, cfg);
// "SECR" in first chunk, "ET" in second — lookahead must bridge them.
auto f1 = scanner.scan_chunk("Hello SECR");
auto f2 = scanner.scan_chunk("ET world", /*is_last=*/true);
size_t total = f1.size() + f2.size();
EXPECT_EQ(total, 1u) << "Expected exactly one SECRET finding across boundary";
}
TEST(PIIStreamScanner, DetectsMultipleTokensAcrossChunks) {
auto engine = std::make_shared<StubEngine>();
PIIStreamScanner scanner(engine);
auto f1 = scanner.scan_chunk("first SECRET chunk ");
auto f2 = scanner.scan_chunk("second SECRET chunk", true);
size_t total = f1.size() + f2.size();
EXPECT_EQ(total, 2u);
}
// ============================================================================
// Reset
// ============================================================================
TEST(PIIStreamScanner, ResetResetsState) {
auto engine = std::make_shared<StubEngine>();
PIIStreamScanner scanner(engine);
scanner.scan_chunk("some SECRET data", true);
EXPECT_GT(scanner.bytes_processed(), 0u);
scanner.reset();
EXPECT_EQ(scanner.bytes_processed(), 0u);
}
TEST(PIIStreamScanner, BytesProcessedAccumulates) {
auto engine = std::make_shared<StubEngine>();
PIIStreamScanner scanner(engine);
scanner.scan_chunk("abc");
scanner.scan_chunk("def", true);
// After two chunks the processed count must be at least 6
EXPECT_GE(scanner.bytes_processed(), 6u);
}