Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #include "TestUrgency.h" |
michael@0 | 2 | |
michael@0 | 3 | #include "IPDLUnitTests.h" // fail etc. |
michael@0 | 4 | #if defined(OS_POSIX) |
michael@0 | 5 | #include <unistd.h> |
michael@0 | 6 | #else |
michael@0 | 7 | #include <windows.h> |
michael@0 | 8 | #endif |
michael@0 | 9 | |
michael@0 | 10 | template<> |
michael@0 | 11 | struct RunnableMethodTraits<mozilla::_ipdltest::TestUrgencyParent> |
michael@0 | 12 | { |
michael@0 | 13 | static void RetainCallee(mozilla::_ipdltest::TestUrgencyParent* obj) { } |
michael@0 | 14 | static void ReleaseCallee(mozilla::_ipdltest::TestUrgencyParent* obj) { } |
michael@0 | 15 | }; |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace _ipdltest { |
michael@0 | 19 | |
michael@0 | 20 | #if defined(OS_POSIX) |
michael@0 | 21 | static void Sleep(int ms) |
michael@0 | 22 | { |
michael@0 | 23 | sleep(ms / 1000); |
michael@0 | 24 | } |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | //----------------------------------------------------------------------------- |
michael@0 | 28 | // parent |
michael@0 | 29 | |
michael@0 | 30 | TestUrgencyParent::TestUrgencyParent() |
michael@0 | 31 | : inreply_(false) |
michael@0 | 32 | { |
michael@0 | 33 | MOZ_COUNT_CTOR(TestUrgencyParent); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | TestUrgencyParent::~TestUrgencyParent() |
michael@0 | 37 | { |
michael@0 | 38 | MOZ_COUNT_DTOR(TestUrgencyParent); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | void |
michael@0 | 42 | TestUrgencyParent::Main() |
michael@0 | 43 | { |
michael@0 | 44 | if (!SendStart()) |
michael@0 | 45 | fail("sending Start"); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | bool |
michael@0 | 49 | TestUrgencyParent::RecvTest1(uint32_t *value) |
michael@0 | 50 | { |
michael@0 | 51 | if (!CallReply1(value)) |
michael@0 | 52 | fail("sending Reply1"); |
michael@0 | 53 | if (*value != 99) |
michael@0 | 54 | fail("bad value"); |
michael@0 | 55 | return true; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | bool |
michael@0 | 59 | TestUrgencyParent::RecvTest2() |
michael@0 | 60 | { |
michael@0 | 61 | uint32_t value; |
michael@0 | 62 | inreply_ = true; |
michael@0 | 63 | if (!CallReply2(&value)) |
michael@0 | 64 | fail("sending Reply2"); |
michael@0 | 65 | inreply_ = false; |
michael@0 | 66 | if (value != 500) |
michael@0 | 67 | fail("bad value"); |
michael@0 | 68 | return true; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | bool |
michael@0 | 72 | TestUrgencyParent::RecvTest3(uint32_t *value) |
michael@0 | 73 | { |
michael@0 | 74 | if (inreply_) |
michael@0 | 75 | fail("nested non-urgent on top of urgent rpc"); |
michael@0 | 76 | *value = 1000; |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool |
michael@0 | 81 | TestUrgencyParent::RecvFinalTest_Begin() |
michael@0 | 82 | { |
michael@0 | 83 | SetReplyTimeoutMs(2000); |
michael@0 | 84 | if (CallFinalTest_Hang()) |
michael@0 | 85 | fail("should have failed due to timeout"); |
michael@0 | 86 | if (!GetIPCChannel()->Unsound_IsClosed()) |
michael@0 | 87 | fail("channel should have closed"); |
michael@0 | 88 | |
michael@0 | 89 | MessageLoop::current()->PostTask( |
michael@0 | 90 | FROM_HERE, |
michael@0 | 91 | NewRunnableMethod(this, &TestUrgencyParent::Close)); |
michael@0 | 92 | return false; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | //----------------------------------------------------------------------------- |
michael@0 | 96 | // child |
michael@0 | 97 | |
michael@0 | 98 | enum { |
michael@0 | 99 | kFirstTestBegin = 1, |
michael@0 | 100 | kFirstTestGotReply, |
michael@0 | 101 | kSecondTestBegin, |
michael@0 | 102 | kSecondTestGotReply, |
michael@0 | 103 | }; |
michael@0 | 104 | |
michael@0 | 105 | bool |
michael@0 | 106 | TestUrgencyChild::RecvStart() |
michael@0 | 107 | { |
michael@0 | 108 | uint32_t result; |
michael@0 | 109 | |
michael@0 | 110 | // Send a synchronous message, expect to get an urgent message while |
michael@0 | 111 | // blocked. |
michael@0 | 112 | test_ = kFirstTestBegin; |
michael@0 | 113 | if (!SendTest1(&result)) |
michael@0 | 114 | fail("calling SendTest1"); |
michael@0 | 115 | if (result != 99) |
michael@0 | 116 | fail("bad result in RecvStart"); |
michael@0 | 117 | if (test_ != kFirstTestGotReply) |
michael@0 | 118 | fail("never received urgent message"); |
michael@0 | 119 | |
michael@0 | 120 | // Initiate the next test by sending an asynchronous message, then becoming |
michael@0 | 121 | // blocked. This tests that the urgent message is still delivered properly, |
michael@0 | 122 | // and that the parent does not try to service the sync |
michael@0 | 123 | test_ = kSecondTestBegin; |
michael@0 | 124 | if (!SendTest2()) |
michael@0 | 125 | fail("calling SendTest2"); |
michael@0 | 126 | if (!SendTest3(&result)) |
michael@0 | 127 | fail("calling SendTest3"); |
michael@0 | 128 | if (test_ != kSecondTestGotReply) |
michael@0 | 129 | fail("never received urgent message #2"); |
michael@0 | 130 | if (result != 1000) |
michael@0 | 131 | fail("wrong value from test3"); |
michael@0 | 132 | |
michael@0 | 133 | // This must be the last test, since the child process may die. |
michael@0 | 134 | if (SendFinalTest_Begin()) |
michael@0 | 135 | fail("Final test should not have succeeded"); |
michael@0 | 136 | |
michael@0 | 137 | Close(); |
michael@0 | 138 | |
michael@0 | 139 | return true; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | bool |
michael@0 | 143 | TestUrgencyChild::AnswerReply1(uint32_t *reply) |
michael@0 | 144 | { |
michael@0 | 145 | if (test_ != kFirstTestBegin) |
michael@0 | 146 | fail("wrong test # in AnswerReply1"); |
michael@0 | 147 | |
michael@0 | 148 | *reply = 99; |
michael@0 | 149 | test_ = kFirstTestGotReply; |
michael@0 | 150 | return true; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | bool |
michael@0 | 154 | TestUrgencyChild::AnswerReply2(uint32_t *reply) |
michael@0 | 155 | { |
michael@0 | 156 | if (test_ != kSecondTestBegin) |
michael@0 | 157 | fail("wrong test # in AnswerReply2"); |
michael@0 | 158 | |
michael@0 | 159 | // sleep for 5 seconds so the parent process tries to deliver more messages. |
michael@0 | 160 | Sleep(5000); |
michael@0 | 161 | |
michael@0 | 162 | *reply = 500; |
michael@0 | 163 | test_ = kSecondTestGotReply; |
michael@0 | 164 | return true; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | bool |
michael@0 | 168 | TestUrgencyChild::AnswerFinalTest_Hang() |
michael@0 | 169 | { |
michael@0 | 170 | Sleep(10); |
michael@0 | 171 | return true; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | TestUrgencyChild::TestUrgencyChild() |
michael@0 | 175 | : test_(0) |
michael@0 | 176 | { |
michael@0 | 177 | MOZ_COUNT_CTOR(TestUrgencyChild); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | TestUrgencyChild::~TestUrgencyChild() |
michael@0 | 181 | { |
michael@0 | 182 | MOZ_COUNT_DTOR(TestUrgencyChild); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | } // namespace _ipdltest |
michael@0 | 186 | } // namespace mozilla |