diff options
| author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2022-09-09 14:09:25 -0400 |
|---|---|---|
| committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-09-12 15:15:50 +0000 |
| commit | ec4c0fe845891168ab5c09b2ffc9c03eaa230418 (patch) | |
| tree | e9ac64469d48094ae013f43e88bc1cfdaaddfd0c /src/mongo/crypto | |
| parent | 9b5dcd425a6f6bb023fd577380ae458aa708e438 (diff) | |
| download | mongo-ec4c0fe845891168ab5c09b2ffc9c03eaa230418.tar.gz | |
SERVER-69123 Separate out the test vectors from fle_crypto_test.cpp
Diffstat (limited to 'src/mongo/crypto')
| -rw-r--r-- | src/mongo/crypto/SConscript | 1 | ||||
| -rw-r--r-- | src/mongo/crypto/fle_crypto_test.cpp | 156 | ||||
| -rw-r--r-- | src/mongo/crypto/fle_crypto_test_vectors.cpp | 219 |
3 files changed, 220 insertions, 156 deletions
diff --git a/src/mongo/crypto/SConscript b/src/mongo/crypto/SConscript index 4ce7ede944a..2e87a027dce 100644 --- a/src/mongo/crypto/SConscript +++ b/src/mongo/crypto/SConscript @@ -128,6 +128,7 @@ env.CppUnitTest( 'aead_encryption_test.cpp', 'encryption_fields_util_test.cpp', 'fle_crypto_test.cpp', + 'fle_crypto_test_vectors.cpp', 'mechanism_scram_test.cpp', 'sha1_block_test.cpp', 'sha256_block_test.cpp', diff --git a/src/mongo/crypto/fle_crypto_test.cpp b/src/mongo/crypto/fle_crypto_test.cpp index 705aee8da6c..32ba9474d5f 100644 --- a/src/mongo/crypto/fle_crypto_test.cpp +++ b/src/mongo/crypto/fle_crypto_test.cpp @@ -2405,94 +2405,6 @@ TEST(RangeTest, Double_Errors) { 6775008); } -// Edge calculator - -template <typename T> -struct EdgeCalcTestVector { - std::function<std::unique_ptr<Edges>(T, boost::optional<T>, boost::optional<T>, int)> getEdgesT; - T value; - boost::optional<T> min, max; - int sparsity; - stdx::unordered_set<std::string> expectedEdges; - - bool validate() const { - auto edgeCalc = getEdgesT(value, min, max, sparsity); - auto edges = edgeCalc->get(); - for (const std::string& ee : expectedEdges) { - if (!std::any_of(edges.begin(), edges.end(), [ee](auto edge) { return edge == ee; })) { - LOGV2_ERROR(6775120, - "Expected edge not found", - "expected-edge"_attr = ee, - "value"_attr = value, - "min"_attr = min, - "max"_attr = max, - "sparsity"_attr = sparsity, - "edges"_attr = edges, - "expected-edges"_attr = expectedEdges); - return false; - } - } - - for (const StringData& edgeSd : edges) { - std::string edge = edgeSd.toString(); - if (std::all_of(expectedEdges.begin(), expectedEdges.end(), [edge](auto ee) { - return edge != ee; - })) { - LOGV2_ERROR(6775121, - "An edge was found that is not expected", - "edge"_attr = edge, - "value"_attr = value, - "min"_attr = min, - "max"_attr = max, - "sparsity"_attr = sparsity, - "edges"_attr = edges, - "expected-edges"_attr = expectedEdges); - return false; - } - } - - if (edges.size() != expectedEdges.size()) { - LOGV2_ERROR(6775122, - "Unexpected number of elements in edges. Check for duplicates", - "value"_attr = value, - "min"_attr = min, - "max"_attr = max, - "sparsity"_attr = sparsity, - "edges"_attr = edges, - "expected-edges"_attr = expectedEdges); - return false; - } - - return true; - } -}; - -TEST(EdgeCalcTest, Int32_TestVectors) { - std::vector<EdgeCalcTestVector<int32_t>> testVectors = { -#include "test_vectors/edges_int32.cstruct" - }; - for (const auto& testVector : testVectors) { - ASSERT_TRUE(testVector.validate()); - } -} - -TEST(EdgeCalcTest, Int64_TestVectors) { - std::vector<EdgeCalcTestVector<int64_t>> testVectors = { -#include "test_vectors/edges_int64.cstruct" - }; - for (const auto& testVector : testVectors) { - ASSERT_TRUE(testVector.validate()); - } -} - -TEST(EdgeCalcTest, Double_TestVectors) { - std::vector<EdgeCalcTestVector<double>> testVectors = { -#include "test_vectors/edges_double.cstruct" - }; - for (const auto& testVector : testVectors) { - ASSERT_TRUE(testVector.validate()); - } -} TEST(EdgeCalcTest, SparsityConstraints) { ASSERT_THROWS_CODE(getEdgesInt32(1, 0, 8, 0), AssertionException, 6775101); @@ -2503,74 +2415,6 @@ TEST(EdgeCalcTest, SparsityConstraints) { ASSERT_THROWS_CODE(getEdgesDouble(1.0, 0.0, 8.0, -1), AssertionException, 6775101); } -template <typename T> -struct MinCoverTestVector { - T rangeMin, rangeMax; - T min, max; - int sparsity; - const char* expect; - - bool validate( - std::function<std::vector<std::string>(T, T, boost::optional<T>, boost::optional<T>, int)> - algo) const { - auto result = algo(rangeMin, rangeMax, min, max, sparsity); - - std::stringstream ss(expect); - std::vector<std::string> vexpect; - std::string item; - while (std::getline(ss, item, '\n') && !item.empty()) { - vexpect.push_back(std::move(item)); - } - - if (std::equal(result.begin(), result.end(), vexpect.begin())) { - return true; - } - - LOGV2_ERROR(6860020, - "MinCover algorithm produced unexpected result", - "rangeMin"_attr = rangeMin, - "rangeMax"_attr = rangeMax, - "min"_attr = min, - "max"_attr = max, - "sparsity"_attr = sparsity, - "expect"_attr = expect, - "got"_attr = result); - return false; - } -}; - - -#pragma clang optimize off - -TEST(MinCoverCalcTest, Int32_TestVectors) { - const MinCoverTestVector<int32_t> testVectors[] = { -#include "test_vectors/mincover_int32.cstruct" - }; - for (const auto& testVector : testVectors) { - ASSERT_TRUE(testVector.validate(minCoverInt32)); - } -} - -TEST(MinCoverCalcTest, Int64_TestVectors) { - const MinCoverTestVector<int64_t> testVectors[] = { -#include "test_vectors/mincover_int64.cstruct" - }; - for (const auto& testVector : testVectors) { - ASSERT_TRUE(testVector.validate(minCoverInt64)); - } -} - -TEST(MinCoverCalcTest, Double_TestVectors) { - MinCoverTestVector<double> testVectors[] = { -#include "test_vectors/mincover_double.cstruct" - }; - for (const auto& testVector : testVectors) { - ASSERT_TRUE(testVector.validate(minCoverDouble)); - } -} - -#pragma clang optimize on - TEST(MinCoverCalcTest, MinCoverConstraints) { ASSERT_THROWS_CODE(minCoverInt32(2, 1, 0, 7, 1), AssertionException, 6860001); ASSERT_THROWS_CODE(minCoverInt64(2, 1, 0, 7, 1), AssertionException, 6860001); diff --git a/src/mongo/crypto/fle_crypto_test_vectors.cpp b/src/mongo/crypto/fle_crypto_test_vectors.cpp new file mode 100644 index 00000000000..2bfaa94597f --- /dev/null +++ b/src/mongo/crypto/fle_crypto_test_vectors.cpp @@ -0,0 +1,219 @@ +/** + * Copyright (C) 2022-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include <algorithm> +#include <cstdint> +#include <iostream> +#include <limits> +#include <sstream> +#include <string> +#include <tuple> +#include <vector> + +#include "mongo/base/data_range.h" +#include "mongo/bson/bsonmisc.h" +#include "mongo/bson/bsonobj.h" +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/bson/bsontypes.h" +#include "mongo/bson/json.h" +#include "mongo/config.h" +#include "mongo/crypto/fle_crypto.h" +#include "mongo/db/operation_context.h" +#include "mongo/logv2/log.h" +#include "mongo/rpc/object_check.h" +#include "mongo/unittest/bson_test_util.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/assert_util.h" +#include "mongo/util/hex.h" +#include "mongo/util/time_support.h" + +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest + +namespace mongo { + +// Edge calculator + +template <typename T> +struct EdgeCalcTestVector { + std::function<std::unique_ptr<Edges>(T, boost::optional<T>, boost::optional<T>, int)> getEdgesT; + T value; + boost::optional<T> min, max; + int sparsity; + stdx::unordered_set<std::string> expectedEdges; + + bool validate() const { + auto edgeCalc = getEdgesT(value, min, max, sparsity); + auto edges = edgeCalc->get(); + for (const std::string& ee : expectedEdges) { + if (!std::any_of(edges.begin(), edges.end(), [ee](auto edge) { return edge == ee; })) { + LOGV2_ERROR(6775120, + "Expected edge not found", + "expected-edge"_attr = ee, + "value"_attr = value, + "min"_attr = min, + "max"_attr = max, + "sparsity"_attr = sparsity, + "edges"_attr = edges, + "expected-edges"_attr = expectedEdges); + return false; + } + } + + for (const StringData& edgeSd : edges) { + std::string edge = edgeSd.toString(); + if (std::all_of(expectedEdges.begin(), expectedEdges.end(), [edge](auto ee) { + return edge != ee; + })) { + LOGV2_ERROR(6775121, + "An edge was found that is not expected", + "edge"_attr = edge, + "value"_attr = value, + "min"_attr = min, + "max"_attr = max, + "sparsity"_attr = sparsity, + "edges"_attr = edges, + "expected-edges"_attr = expectedEdges); + return false; + } + } + + if (edges.size() != expectedEdges.size()) { + LOGV2_ERROR(6775122, + "Unexpected number of elements in edges. Check for duplicates", + "value"_attr = value, + "min"_attr = min, + "max"_attr = max, + "sparsity"_attr = sparsity, + "edges"_attr = edges, + "expected-edges"_attr = expectedEdges); + return false; + } + + return true; + } +}; + +TEST(EdgeCalcTest, Int32_TestVectors) { + std::vector<EdgeCalcTestVector<int32_t>> testVectors = { +#include "test_vectors/edges_int32.cstruct" + }; + for (const auto& testVector : testVectors) { + ASSERT_TRUE(testVector.validate()); + } +} + +TEST(EdgeCalcTest, Int64_TestVectors) { + std::vector<EdgeCalcTestVector<int64_t>> testVectors = { +#include "test_vectors/edges_int64.cstruct" + }; + for (const auto& testVector : testVectors) { + ASSERT_TRUE(testVector.validate()); + } +} + +TEST(EdgeCalcTest, Double_TestVectors) { + std::vector<EdgeCalcTestVector<double>> testVectors = { +#include "test_vectors/edges_double.cstruct" + }; + for (const auto& testVector : testVectors) { + ASSERT_TRUE(testVector.validate()); + } +} + +template <typename T> +struct MinCoverTestVector { + T rangeMin, rangeMax; + T min, max; + int sparsity; + const char* expect; + + bool validate( + std::function<std::vector<std::string>(T, T, boost::optional<T>, boost::optional<T>, int)> + algo) const { + auto result = algo(rangeMin, rangeMax, min, max, sparsity); + + std::stringstream ss(expect); + std::vector<std::string> vexpect; + std::string item; + while (std::getline(ss, item, '\n') && !item.empty()) { + vexpect.push_back(std::move(item)); + } + + if (std::equal(result.begin(), result.end(), vexpect.begin())) { + return true; + } + + LOGV2_ERROR(6860020, + "MinCover algorithm produced unexpected result", + "rangeMin"_attr = rangeMin, + "rangeMax"_attr = rangeMax, + "min"_attr = min, + "max"_attr = max, + "sparsity"_attr = sparsity, + "expect"_attr = expect, + "got"_attr = result); + return false; + } +}; + + +#pragma clang optimize off + +TEST(MinCoverCalcTest, Int32_TestVectors) { + const MinCoverTestVector<int32_t> testVectors[] = { +#include "test_vectors/mincover_int32.cstruct" + }; + for (const auto& testVector : testVectors) { + ASSERT_TRUE(testVector.validate(minCoverInt32)); + } +} + +TEST(MinCoverCalcTest, Int64_TestVectors) { + const MinCoverTestVector<int64_t> testVectors[] = { +#include "test_vectors/mincover_int64.cstruct" + }; + for (const auto& testVector : testVectors) { + ASSERT_TRUE(testVector.validate(minCoverInt64)); + } +} + +TEST(MinCoverCalcTest, Double_TestVectors) { + MinCoverTestVector<double> testVectors[] = { +#include "test_vectors/mincover_double.cstruct" + }; + for (const auto& testVector : testVectors) { + ASSERT_TRUE(testVector.validate(minCoverDouble)); + } +} + +#pragma clang optimize on + +} // namespace mongo |
