media/mtransport/test/runnable_utils_unittest.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 // Original author: ekr@rtfm.com
     8 #include <iostream>
    10 #include "prio.h"
    12 #include "nsCOMPtr.h"
    13 #include "nsNetCID.h"
    14 #include "nsXPCOM.h"
    15 #include "nsXPCOMGlue.h"
    17 #include "mozilla/RefPtr.h"
    18 #include "nsIComponentManager.h"
    19 #include "nsIComponentRegistrar.h"
    20 #include "nsIIOService.h"
    21 #include "nsIServiceManager.h"
    22 #include "nsISocketTransportService.h"
    24 #include "nsASocketHandler.h"
    25 #include "nsServiceManagerUtils.h"
    26 #include "nsThreadUtils.h"
    28 #include "runnable_utils.h"
    29 #include "mtransport_test_utils.h"
    31 #define GTEST_HAS_RTTI 0
    32 #include "gtest/gtest.h"
    33 #include "gtest_utils.h"
    35 using namespace mozilla;
    36 MtransportTestUtils *test_utils;
    38 namespace {
    40 class Destructor {
    41  public:
    42   Destructor(bool* destroyed) : destroyed_(destroyed) {}
    43   ~Destructor() {
    44     std::cerr << "Destructor called" << std::endl;
    45     *destroyed_ = true;
    46   }
    48   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Destructor)
    50  private:
    51   bool *destroyed_;
    52 };
    54 class TargetClass {
    55  public:
    56   TargetClass(int *ran) : ran_(ran) {}
    58   void m1(int x) {
    59     std::cerr << __FUNCTION__ << " " << x << std::endl;
    60     *ran_ = 1;
    61   }
    63   void m2(int x, int y) {
    64     std::cerr << __FUNCTION__ << " " << x << " " << y << std::endl;
    65     *ran_ = 2;
    66   }
    68   void m1set(bool *z) {
    69     std::cerr << __FUNCTION__ << std::endl;
    70     *z = true;
    71   }
    72   int return_int(int x) {
    73     std::cerr << __FUNCTION__ << std::endl;
    74     return x;
    75   }
    76   void destructor_target(Destructor*) {
    77   }
    79   void destructor_target_ref(RefPtr<Destructor> destructor) {
    80   }
    82   int *ran_;
    83 };
    86 class RunnableArgsTest : public ::testing::Test {
    87  public:
    88   RunnableArgsTest() : ran_(0), cl_(&ran_){}
    90   void Test1Arg() {
    91     nsRunnable * r = WrapRunnable(&cl_, &TargetClass::m1, 1);
    92     r->Run();
    93     ASSERT_EQ(1, ran_);
    94   }
    96   void Test2Args() {
    97     nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
    98     r->Run();
    99     ASSERT_EQ(2, ran_);
   100   }
   102  private:
   103   int ran_;
   104   TargetClass cl_;
   105 };
   107 class DispatchTest : public ::testing::Test {
   108  public:
   109   DispatchTest() : ran_(0), cl_(&ran_) {}
   111   void SetUp() {
   112     nsresult rv;
   113     target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
   114     ASSERT_TRUE(NS_SUCCEEDED(rv));
   115   }
   117   void Test1Arg() {
   118     nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m1, 1);
   119     target_->Dispatch(r, NS_DISPATCH_SYNC);
   120     ASSERT_EQ(1, ran_);
   121   }
   123   void Test2Args() {
   124     nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
   125     target_->Dispatch(r, NS_DISPATCH_SYNC);
   126     ASSERT_EQ(2, ran_);
   127   }
   129   void Test1Set() {
   130     bool x = false;
   131     target_->Dispatch(WrapRunnable(&cl_, &TargetClass::m1set, &x),
   132                       NS_DISPATCH_SYNC);
   133     ASSERT_TRUE(x);
   134   }
   136   void TestRet() {
   137     int z;
   138     int x = 10;
   140     target_->Dispatch(WrapRunnableRet(&cl_, &TargetClass::return_int, x, &z),
   141                       NS_DISPATCH_SYNC);
   142     ASSERT_EQ(10, z);
   143   }
   145  protected:
   146   int ran_;
   147   TargetClass cl_;
   148   nsCOMPtr<nsIEventTarget> target_;
   149 };
   152 TEST_F(RunnableArgsTest, OneArgument) {
   153   Test1Arg();
   154 }
   156 TEST_F(RunnableArgsTest, TwoArguments) {
   157   Test2Args();
   158 }
   160 TEST_F(DispatchTest, OneArgument) {
   161   Test1Arg();
   162 }
   164 TEST_F(DispatchTest, TwoArguments) {
   165   Test2Args();
   166 }
   168 TEST_F(DispatchTest, Test1Set) {
   169   Test1Set();
   170 }
   172 TEST_F(DispatchTest, TestRet) {
   173   TestRet();
   174 }
   176 void SetNonMethod(TargetClass *cl, int x) {
   177   cl->m1(x);
   178 }
   180 int SetNonMethodRet(TargetClass *cl, int x) {
   181   cl->m1(x);
   183   return x;
   184 }
   186 TEST_F(DispatchTest, TestNonMethod) {
   187   test_utils->sts_target()->Dispatch(
   188       WrapRunnableNM(SetNonMethod, &cl_, 10), NS_DISPATCH_SYNC);
   190   ASSERT_EQ(1, ran_);
   191 }
   193 TEST_F(DispatchTest, TestNonMethodRet) {
   194   int z;
   196   test_utils->sts_target()->Dispatch(
   197       WrapRunnableNMRet(SetNonMethodRet, &cl_, 10, &z), NS_DISPATCH_SYNC);
   199   ASSERT_EQ(1, ran_);
   200   ASSERT_EQ(10, z);
   201 }
   203 TEST_F(DispatchTest, TestDestructor) {
   204   bool destroyed = false;
   205   RefPtr<Destructor> destructor = new Destructor(&destroyed);
   206   target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target,
   207                                  destructor),
   208                     NS_DISPATCH_SYNC);
   209   ASSERT_FALSE(destroyed);
   210   destructor = nullptr;
   211   ASSERT_TRUE(destroyed);
   212 }
   214 TEST_F(DispatchTest, TestDestructorRef) {
   215   bool destroyed = false;
   216   RefPtr<Destructor> destructor = new Destructor(&destroyed);
   217   target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target_ref,
   218                                  destructor),
   219                     NS_DISPATCH_SYNC);
   220   ASSERT_FALSE(destroyed);
   221   destructor = nullptr;
   222   ASSERT_TRUE(destroyed);
   223 }
   226 } // end of namespace
   229 int main(int argc, char **argv) {
   230   test_utils = new MtransportTestUtils();
   232   // Start the tests
   233   ::testing::InitGoogleTest(&argc, argv);
   235   int rv = RUN_ALL_TESTS();
   236   delete test_utils;
   237   return rv;
   238 }

mercurial