Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* Original author: bcampen@mozilla.com */ |
michael@0 | 8 | |
michael@0 | 9 | #include "simpletokenbucket.h" |
michael@0 | 10 | |
michael@0 | 11 | #define GTEST_HAS_RTTI 0 |
michael@0 | 12 | #include "gtest/gtest.h" |
michael@0 | 13 | #include "gtest_utils.h" |
michael@0 | 14 | |
michael@0 | 15 | using mozilla::SimpleTokenBucket; |
michael@0 | 16 | |
michael@0 | 17 | class TestSimpleTokenBucket : public SimpleTokenBucket { |
michael@0 | 18 | public: |
michael@0 | 19 | TestSimpleTokenBucket(size_t bucketSize, size_t tokensPerSecond) : |
michael@0 | 20 | SimpleTokenBucket(bucketSize, tokensPerSecond) { |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | void fastForward(int32_t timeMilliSeconds) { |
michael@0 | 24 | if (timeMilliSeconds >= 0) { |
michael@0 | 25 | last_time_tokens_added_ -= PR_MillisecondsToInterval(timeMilliSeconds); |
michael@0 | 26 | } else { |
michael@0 | 27 | last_time_tokens_added_ += PR_MillisecondsToInterval(-timeMilliSeconds); |
michael@0 | 28 | } |
michael@0 | 29 | } |
michael@0 | 30 | }; |
michael@0 | 31 | |
michael@0 | 32 | TEST(SimpleTokenBucketTest, TestConstruct) { |
michael@0 | 33 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | TEST(SimpleTokenBucketTest, TestGet) { |
michael@0 | 37 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 38 | ASSERT_EQ(5U, b.getTokens(5)); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | TEST(SimpleTokenBucketTest, TestGetAll) { |
michael@0 | 42 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 43 | ASSERT_EQ(10U, b.getTokens(10)); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | TEST(SimpleTokenBucketTest, TestGetInsufficient) { |
michael@0 | 47 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 48 | ASSERT_EQ(5U, b.getTokens(5)); |
michael@0 | 49 | ASSERT_EQ(5U, b.getTokens(6)); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | TEST(SimpleTokenBucketTest, TestGetBucketCount) { |
michael@0 | 53 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 54 | ASSERT_EQ(10U, b.getTokens(UINT32_MAX)); |
michael@0 | 55 | ASSERT_EQ(5U, b.getTokens(5)); |
michael@0 | 56 | ASSERT_EQ(5U, b.getTokens(UINT32_MAX)); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | TEST(SimpleTokenBucketTest, TestTokenRefill) { |
michael@0 | 60 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 61 | ASSERT_EQ(5U, b.getTokens(5)); |
michael@0 | 62 | b.fastForward(1000); |
michael@0 | 63 | ASSERT_EQ(6U, b.getTokens(6)); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | TEST(SimpleTokenBucketTest, TestNoTimeWasted) { |
michael@0 | 67 | // Makes sure that when the time elapsed is insufficient to add any |
michael@0 | 68 | // tokens to the bucket, the internal timestamp that is used in this |
michael@0 | 69 | // calculation is not updated (ie; two subsequent 0.5 second elapsed times |
michael@0 | 70 | // counts as a full second) |
michael@0 | 71 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 72 | ASSERT_EQ(5U, b.getTokens(5)); |
michael@0 | 73 | b.fastForward(500); |
michael@0 | 74 | ASSERT_EQ(5U, b.getTokens(6)); |
michael@0 | 75 | b.fastForward(500); |
michael@0 | 76 | ASSERT_EQ(6U, b.getTokens(6)); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | TEST(SimpleTokenBucketTest, TestNegativeTime) { |
michael@0 | 80 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 81 | b.fastForward(-1000); |
michael@0 | 82 | // Make sure we don't end up with an invalid number of tokens, but otherwise |
michael@0 | 83 | // permit anything. |
michael@0 | 84 | ASSERT_GT(11U, b.getTokens(100)); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | TEST(SimpleTokenBucketTest, TestEmptyBucket) { |
michael@0 | 88 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 89 | ASSERT_EQ(10U, b.getTokens(10)); |
michael@0 | 90 | ASSERT_EQ(0U, b.getTokens(10)); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | TEST(SimpleTokenBucketTest, TestEmptyThenFillBucket) { |
michael@0 | 94 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 95 | ASSERT_EQ(10U, b.getTokens(10)); |
michael@0 | 96 | ASSERT_EQ(0U, b.getTokens(1)); |
michael@0 | 97 | b.fastForward(50000); |
michael@0 | 98 | ASSERT_EQ(10U, b.getTokens(10)); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | TEST(SimpleTokenBucketTest, TestNoOverflow) { |
michael@0 | 102 | TestSimpleTokenBucket b(10, 1); |
michael@0 | 103 | ASSERT_EQ(10U, b.getTokens(10)); |
michael@0 | 104 | ASSERT_EQ(0U, b.getTokens(1)); |
michael@0 | 105 | b.fastForward(50000); |
michael@0 | 106 | ASSERT_EQ(10U, b.getTokens(11)); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | int main(int argc, char** argv) { |
michael@0 | 110 | ::testing::InitGoogleTest(&argc, argv); |
michael@0 | 111 | |
michael@0 | 112 | int rv = RUN_ALL_TESTS(); |
michael@0 | 113 | return rv; |
michael@0 | 114 | } |
michael@0 | 115 |