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
|
/**
* (c) 2024 by Mega Limited, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK 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.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include <gtest/gtest.h>
#include <mega/canceller.h>
#include <mega/hashcash.h>
#include <future>
#include <thread>
using namespace mega;
namespace
{
using Clock = std::chrono::steady_clock;
using namespace std::chrono_literals;
const std::string kTokenHard{"K4QHo4I6XmnLNNsFqutTwObWZMClxf7ov--5OHLdGXSMHRwN8bLvrUTlpnhXVdtO"};
constexpr uint8_t kHighEasiness{200};
constexpr uint8_t kLowEasiness{5};
constexpr auto kCappedWorkers{1u};
constexpr auto kLowTtl{30ms};
constexpr auto kLargeTtl{15min};
struct RunResult
{
std::string gencashRes;
std::chrono::milliseconds elapsed;
};
auto run_gencash(const std::string& token,
const uint8_t easiness,
const std::chrono::milliseconds ttl,
const cancel_epoch_t epoch,
const unsigned workers) -> RunResult
{
const auto start = Clock::now();
auto gencashRes = gencash(token, easiness, ttl, epoch, workers);
return {std::move(gencashRes),
std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start)};
}
auto run_gencash_async = [](auto&&... args)
{
auto ready = std::make_shared<std::promise<void>>();
std::future<void> go = ready->get_future();
auto fut =
std::async(std::launch::async,
[ready, tup = std::make_tuple(std::forward<decltype(args)>(args)...)]() mutable
{
ready->set_value();
return std::apply(run_gencash, std::move(tup));
});
go.wait();
return fut;
};
auto expect_cancelled = [](const RunResult& r,
const cancel_epoch_t epoch,
const std::chrono::milliseconds upper,
const bool cancelTriggered = false)
{
EXPECT_TRUE(r.gencashRes.empty());
EXPECT_LT(r.elapsed, upper);
EXPECT_EQ(ScopedCanceller(epoch).triggered(), cancelTriggered);
};
auto expect_completed = [](const RunResult& r, const cancel_epoch_t epoch)
{
EXPECT_FALSE(r.gencashRes.empty());
EXPECT_FALSE(ScopedCanceller(epoch).triggered());
};
} // namespace
TEST(Hashcash, Gencash)
{
const std::vector<std::uint8_t> easinessV{180, 192};
const std::vector<unsigned> numWorkersV{8u, 2u};
const std::vector<std::string> hashcash{
"wFqIT_wY3tYKcrm5zqwaUoWym3ZCz32cCsrJOgYBgihtpaWUhGyWJ--EY-zfwI-i",
"3NIjq_fgu6bTyepwHuKiaB8a1YRjISBhktWK1fjhRx86RhOqKZNAcOZht0wJvmhQ",
"HGztcvhT0sngIveS6C4CY1nx64YFtXnbcqX_Dvj7NxmX0SCNRlCZ51_pMWQgpHdv",
};
for (const auto& easiness: easinessV)
{
for (const auto& numWorkers: numWorkersV)
{
for (const auto& hc: hashcash)
{
const auto reqEpochSnapshot{cancel_epoch_snapshot()};
const auto res = run_gencash(hc, easiness, kLargeTtl, reqEpochSnapshot, numWorkers);
ASSERT_TRUE(validateHashcash(hc, easiness, res.gencashRes))
<< "Failed hash: " << hc << ": genCashResult [easiness = " << +easiness
<< ", numWorkers = " << numWorkers << "]";
ASSERT_EQ(retryGencashData(), std::nullopt);
}
}
}
}
TEST(Hashcash, CancelsDuringComputeReturnsQuickly)
{
const auto reqEpochSnapshot{cancel_epoch_snapshot()};
auto fut =
run_gencash_async(kTokenHard, kLowEasiness, kLargeTtl, reqEpochSnapshot, kCappedWorkers);
std::this_thread::sleep_for(200ms); // A bit more wait once we know that we are running
cancel_epoch_bump();
const auto res = fut.get();
expect_cancelled(res, reqEpochSnapshot, 1s, true);
}
TEST(Hashcash, CancelBeforeStartSkipsDoesNotAffect)
{
cancel_epoch_bump();
const auto reqEpochSnapshot{cancel_epoch_snapshot()};
const auto res = gencash(kTokenHard, kHighEasiness, reqEpochSnapshot);
EXPECT_FALSE(res.empty());
}
// With very hard difficulty and test-shortened budget, we should early-exit.
TEST(Hashcash, BudgetEarlyExitWithoutCancel)
{
const auto reqEpochSnapshot{cancel_epoch_snapshot()};
const auto res = run_gencash(kTokenHard,
kLowEasiness,
kLowTtl, // intentionally too small
reqEpochSnapshot,
kCappedWorkers);
expect_cancelled(res, reqEpochSnapshot, 2s);
}
TEST(Hashcash, BudgetEarlyExitWithoutCancelWRetries)
{
const auto reqEpochSnapshot{cancel_epoch_snapshot()};
// Warm up: gencash must succeed and reset previous retry data (if any)
{
const auto res = run_gencash(kTokenHard,
kHighEasiness,
kLargeTtl,
reqEpochSnapshot,
MAX_WORKERS_FOR_GENCASH);
expect_completed(res, reqEpochSnapshot);
ASSERT_EQ(retryGencashData(), std::nullopt);
}
// Force retry up to RetryGencash::kMaxRetries times
for (unsigned i = 0; i < RetryGencash::kMaxRetries; ++i)
{
const auto res =
run_gencash(kTokenHard, kLowEasiness, kLowTtl, reqEpochSnapshot, kCappedWorkers);
expect_cancelled(res, reqEpochSnapshot, 2s);
const auto retryData = retryGencashData();
ASSERT_NE(retryData, std::nullopt);
EXPECT_EQ(retryData->mForceRetryCount, i + 1);
EXPECT_GE(res.elapsed, retryData->mBudget);
EXPECT_GE(retryData->mGencashTime, retryData->mBudget);
}
// Attempt n RetryGencash::kMaxRetries won't trigger any more retries
{
const auto retryDataPre = retryGencashData();
ASSERT_NE(retryDataPre, std::nullopt);
EXPECT_EQ(retryDataPre->mForceRetryCount, RetryGencash::kMaxRetries);
const auto res = run_gencash(kTokenHard,
kHighEasiness,
kLowTtl,
reqEpochSnapshot,
MAX_WORKERS_FOR_GENCASH);
expect_completed(res, reqEpochSnapshot);
const auto retryData = retryGencashData();
ASSERT_NE(retryData, std::nullopt);
EXPECT_EQ(retryData->mForceRetryCount, 0u);
}
// Attempt n RetryGencash::kMaxRetries+1 should reset any previous retryGencashData
{
const auto res = run_gencash(kTokenHard,
kHighEasiness,
kLargeTtl,
reqEpochSnapshot,
MAX_WORKERS_FOR_GENCASH);
expect_completed(res, reqEpochSnapshot);
EXPECT_EQ(retryGencashData(), std::nullopt);
}
}
|