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