Skip to content

Commit b765769

Browse files
authored
Add UT to test encryption keys obtaining (#1552)
1 parent 31adbe0 commit b765769

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

ydb/core/blobstorage/nodewarden/blobstorage_node_warden_ut.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,84 @@ Y_UNIT_TEST_SUITE(TBlobStorageWardenTest) {
822822
TestHttpMonForPath("");
823823
TestHttpMonForPath("/json/groups");
824824
}
825+
826+
void TestObtainPDiskKey(TString pin1, TString pin2) {
827+
std::unique_ptr<TTempDir> tmp(new TTempDir());
828+
TString keyfile = Sprintf("%s/key.txt", (*tmp)().data());
829+
{
830+
TFileOutput file(keyfile);
831+
file << "some data";
832+
}
833+
834+
NKikimrProto::TKeyConfig keyConfig;
835+
NKikimrProto::TKeyRecord* keyRecord = keyConfig.AddKeys();
836+
keyRecord->SetContainerPath(keyfile);
837+
keyRecord->SetPin(pin1);
838+
keyRecord->SetId("Key");
839+
keyRecord->SetVersion(1);
840+
841+
NPDisk::TMainKey mainKey1;
842+
UNIT_ASSERT(ObtainPDiskKey(&mainKey1, keyConfig));
843+
844+
keyRecord->SetPin(pin2);
845+
NPDisk::TMainKey mainKey2;
846+
UNIT_ASSERT(ObtainPDiskKey(&mainKey2, keyConfig));
847+
848+
UNIT_ASSERT_VALUES_EQUAL(mainKey1.Keys.size(), 1);
849+
UNIT_ASSERT_VALUES_EQUAL(mainKey2.Keys.size(), 1);
850+
851+
if (pin1 == pin2) {
852+
UNIT_ASSERT_VALUES_EQUAL(mainKey1.Keys[0], mainKey2.Keys[0]);
853+
} else {
854+
UNIT_ASSERT_VALUES_UNEQUAL(mainKey1.Keys[0], mainKey2.Keys[0]);
855+
}
856+
}
857+
858+
CUSTOM_UNIT_TEST(ObtainPDiskKeySamePin) {
859+
TestObtainPDiskKey("pin", "pin");
860+
}
861+
862+
// TODO (serg-belyakov): Fix conversion from TEncryption key to PDisk's TKey
863+
// CUSTOM_UNIT_TEST(ObtainPDiskKeyDifferentPin) {
864+
// TestObtainPDiskKey("pin1", "pin2");
865+
// }
866+
867+
void TestObtainTenantKey(TString pin1, TString pin2) {
868+
std::unique_ptr<TTempDir> tmp(new TTempDir());
869+
TString keyfile = Sprintf("%s/key.txt", (*tmp)().data());
870+
{
871+
TFileOutput file(keyfile);
872+
file << "some data";
873+
}
874+
875+
NKikimrProto::TKeyConfig keyConfig;
876+
NKikimrProto::TKeyRecord* keyRecord = keyConfig.AddKeys();
877+
keyRecord->SetContainerPath(keyfile);
878+
keyRecord->SetPin(pin1);
879+
keyRecord->SetId("Key");
880+
keyRecord->SetVersion(1);
881+
882+
TEncryptionKey key1;
883+
UNIT_ASSERT(ObtainTenantKey(&key1, keyConfig));
884+
885+
keyRecord->SetPin(pin2);
886+
TEncryptionKey key2;
887+
UNIT_ASSERT(ObtainTenantKey(&key2, keyConfig));
888+
889+
if (pin1 == pin2) {
890+
UNIT_ASSERT(key1.Key == key2.Key);
891+
} else {
892+
UNIT_ASSERT(!(key1.Key == key2.Key));
893+
}
894+
}
895+
896+
CUSTOM_UNIT_TEST(ObtainTenantKeySamePin) {
897+
TestObtainTenantKey("pin", "pin");
898+
}
899+
900+
CUSTOM_UNIT_TEST(ObtainTenantKeyDifferentPin) {
901+
TestObtainTenantKey("pin1", "pin2");
902+
}
825903
}
826904

827905
} // namespace NBlobStorageNodeWardenTest

0 commit comments

Comments
 (0)