// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/351564777): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif // The tests in this file attempt to verify the following through simulation: // a) That a server experiencing overload will actually benefit from the // anti-DDoS throttling logic, i.e. that its traffic spike will subside // and be distributed over a longer period of time; // b) That "well-behaved" clients of a server under DDoS attack actually // benefit from the anti-DDoS throttling logic; and // c) That the approximate increase in "perceived downtime" introduced by // anti-DDoS throttling for various different actual downtimes is what // we expect it to be. #include <stdarg.h> #include <stddef.h> #include <cmath> #include <limits> #include <memory> #include <vector> #include "base/environment.h" #include "base/memory/ptr_util.h" #include "base/memory/raw_ptr.h" #include "base/rand_util.h" #include "base/test/task_environment.h" #include "base/time/time.h" #include "extensions/renderer/extension_throttle_entry.h" #include "extensions/renderer/extension_throttle_test_support.h" #include "testing/gtest/include/gtest/gtest.h" TimeTicks; BackoffEntry; namespace extensions { namespace { // Set this variable in your environment if you want to see verbose results // of the simulation tests. const char kShowSimulationVariableName[] = …; // Prints output only if a given environment variable is set. We use this // to not print any output for human evaluation when the test is run without // supervision. void VerboseOut(const char* format, ...) { … } // A simple two-phase discrete time simulation. Actors are added in the order // they should take action at every tick of the clock. Ticks of the clock // are two-phase: // - Phase 1 advances every actor's time to a new absolute time. // - Phase 2 asks each actor to perform their action. class DiscreteTimeSimulation { … }; // Represents a web server in a simulation of a server under attack by // a lot of clients. Must be added to the simulation's list of actors // after all |Requester| objects. class Server : public DiscreteTimeSimulation::Actor { … }; // Mock throttler entry used by Requester class. class MockExtensionThrottleEntry : public ExtensionThrottleEntry { … }; // Registry of results for a class of |Requester| objects (e.g. attackers vs. // regular clients). class RequesterResults { … }; // Represents an Requester in a simulated DDoS situation, that periodically // requests a specific resource. class Requester : public DiscreteTimeSimulation::Actor { … }; void SimulateAttack(Server* server, RequesterResults* attacker_results, RequesterResults* client_results, bool enable_throttling) { … } TEST(URLRequestThrottlerSimulation, HelpsInAttack) { … } // Returns the downtime perceived by the client, as a ratio of the // actual downtime. double SimulateDowntime(const base::TimeDelta& duration, const base::TimeDelta& average_client_interval, bool enable_throttling) { … } TEST(URLRequestThrottlerSimulation, PerceivedDowntimeRatio) { … } } // namespace } // namespace extensions