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 | |
michael@0 | 2 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 3 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 6 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | // Original author: ekr@rtfm.com |
michael@0 | 9 | #include <iostream> |
michael@0 | 10 | |
michael@0 | 11 | #include "nsThreadUtils.h" |
michael@0 | 12 | #include "nsXPCOM.h" |
michael@0 | 13 | |
michael@0 | 14 | // nrappkit includes |
michael@0 | 15 | extern "C" { |
michael@0 | 16 | #include "nr_api.h" |
michael@0 | 17 | #include "async_timer.h" |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | #include "mtransport_test_utils.h" |
michael@0 | 21 | #include "runnable_utils.h" |
michael@0 | 22 | |
michael@0 | 23 | #define GTEST_HAS_RTTI 0 |
michael@0 | 24 | #include "gtest/gtest.h" |
michael@0 | 25 | #include "gtest_utils.h" |
michael@0 | 26 | |
michael@0 | 27 | using namespace mozilla; |
michael@0 | 28 | |
michael@0 | 29 | MtransportTestUtils *test_utils; |
michael@0 | 30 | |
michael@0 | 31 | namespace { |
michael@0 | 32 | |
michael@0 | 33 | class TimerTest : public ::testing::Test { |
michael@0 | 34 | public: |
michael@0 | 35 | TimerTest() : handle_(nullptr), fired_(false) {} |
michael@0 | 36 | |
michael@0 | 37 | int ArmTimer(int timeout) { |
michael@0 | 38 | int ret; |
michael@0 | 39 | |
michael@0 | 40 | test_utils->sts_target()->Dispatch( |
michael@0 | 41 | WrapRunnableRet(this, &TimerTest::ArmTimer_w, timeout, &ret), |
michael@0 | 42 | NS_DISPATCH_SYNC); |
michael@0 | 43 | |
michael@0 | 44 | return ret; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | int ArmTimer_w(int timeout) { |
michael@0 | 48 | return NR_ASYNC_TIMER_SET(timeout, cb, this, &handle_); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | int CancelTimer() { |
michael@0 | 52 | int ret; |
michael@0 | 53 | |
michael@0 | 54 | test_utils->sts_target()->Dispatch( |
michael@0 | 55 | WrapRunnableRet(this, &TimerTest::CancelTimer_w, &ret), |
michael@0 | 56 | NS_DISPATCH_SYNC); |
michael@0 | 57 | |
michael@0 | 58 | return ret; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | int CancelTimer_w() { |
michael@0 | 62 | return NR_async_timer_cancel(handle_); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | int Schedule() { |
michael@0 | 66 | int ret; |
michael@0 | 67 | |
michael@0 | 68 | test_utils->sts_target()->Dispatch( |
michael@0 | 69 | WrapRunnableRet(this, &TimerTest::Schedule_w, &ret), |
michael@0 | 70 | NS_DISPATCH_SYNC); |
michael@0 | 71 | |
michael@0 | 72 | return ret; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | int Schedule_w() { |
michael@0 | 76 | NR_ASYNC_SCHEDULE(cb, this); |
michael@0 | 77 | |
michael@0 | 78 | return 0; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | static void cb(NR_SOCKET r, int how, void *arg) { |
michael@0 | 83 | std::cerr << "Timer fired " << std::endl; |
michael@0 | 84 | |
michael@0 | 85 | TimerTest *t = static_cast<TimerTest *>(arg); |
michael@0 | 86 | |
michael@0 | 87 | t->fired_ = true; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | protected: |
michael@0 | 91 | void *handle_; |
michael@0 | 92 | bool fired_; |
michael@0 | 93 | }; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | TEST_F(TimerTest, SimpleTimer) { |
michael@0 | 97 | ArmTimer(100); |
michael@0 | 98 | ASSERT_TRUE_WAIT(fired_, 1000); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | TEST_F(TimerTest, CancelTimer) { |
michael@0 | 102 | ArmTimer(1000); |
michael@0 | 103 | CancelTimer(); |
michael@0 | 104 | PR_Sleep(2000); |
michael@0 | 105 | ASSERT_FALSE(fired_); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | TEST_F(TimerTest, ScheduleTest) { |
michael@0 | 109 | Schedule(); |
michael@0 | 110 | ASSERT_TRUE_WAIT(fired_, 1000); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | int main(int argc, char **argv) { |
michael@0 | 114 | test_utils = new MtransportTestUtils(); |
michael@0 | 115 | |
michael@0 | 116 | // Start the tests |
michael@0 | 117 | ::testing::InitGoogleTest(&argc, argv); |
michael@0 | 118 | |
michael@0 | 119 | int rv = RUN_ALL_TESTS(); |
michael@0 | 120 | delete test_utils; |
michael@0 | 121 | return rv; |
michael@0 | 122 | } |