| 1 |
// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors |
| 2 |
// Licensed under the MIT License: |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
// THE SOFTWARE. |
| 21 |
|
| 22 |
#include "membrane.h" |
| 23 |
#include <kj/test.h> |
| 24 |
#include "test-util.h" |
| 25 |
#include <kj/function.h> |
| 26 |
#include <kj/async-io.h> |
| 27 |
#include "rpc-twoparty.h" |
| 28 |
|
| 29 |
namespace capnp { |
| 30 |
namespace _ { |
| 31 |
namespace { |
| 32 |
|
| 33 |
using Thing = test::TestMembrane::Thing; |
| 34 |
|
| 35 |
class ThingImpl final: public Thing::Server { |
| 36 |
public: |
| 37 |
ThingImpl(kj::StringPtr text): text(text) {} |
| 38 |
|
| 39 |
protected: |
| 40 |
kj::Promise<void> passThrough(PassThroughContext context) override { |
| 41 |
context.getResults().setText(text); |
| 42 |
return kj::READY_NOW; |
| 43 |
} |
| 44 |
|
| 45 |
kj::Promise<void> intercept(InterceptContext context) override { |
| 46 |
context.getResults().setText(text); |
| 47 |
return kj::READY_NOW; |
| 48 |
} |
| 49 |
|
| 50 |
private: |
| 51 |
kj::StringPtr text; |
| 52 |
}; |
| 53 |
|
| 54 |
class TestMembraneImpl final: public test::TestMembrane::Server { |
| 55 |
protected: |
| 56 |
kj::Promise<void> makeThing(MakeThingContext context) override { |
| 57 |
context.getResults().setThing(kj::heap<ThingImpl>("inside")); |
| 58 |
return kj::READY_NOW; |
| 59 |
} |
| 60 |
|
| 61 |
kj::Promise<void> callPassThrough(CallPassThroughContext context) override { |
| 62 |
auto params = context.getParams(); |
| 63 |
auto req = params.getThing().passThroughRequest(); |
| 64 |
if (params.getTailCall()) { |
| 65 |
return context.tailCall(kj::mv(req)); |
| 66 |
} else { |
| 67 |
return req.send().then( |
| 68 |
[KJ_CPCAP(context)](Response<test::TestMembrane::Result>&& result) mutable { |
| 69 |
context.setResults(result); |
| 70 |
}); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
kj::Promise<void> callIntercept(CallInterceptContext context) override { |
| 75 |
auto params = context.getParams(); |
| 76 |
auto req = params.getThing().interceptRequest(); |
| 77 |
if (params.getTailCall()) { |
| 78 |
return context.tailCall(kj::mv(req)); |
| 79 |
} else { |
| 80 |
return req.send().then( |
| 81 |
[KJ_CPCAP(context)](Response<test::TestMembrane::Result>&& result) mutable { |
| 82 |
context.setResults(result); |
| 83 |
}); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
kj::Promise<void> loopback(LoopbackContext context) override { |
| 88 |
context.getResults().setThing(context.getParams().getThing()); |
| 89 |
return kj::READY_NOW; |
| 90 |
} |
| 91 |
|
| 92 |
kj::Promise<void> waitForever(WaitForeverContext context) override { |
| 93 |
return kj::NEVER_DONE; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
class MembranePolicyImpl: public MembranePolicy, public kj::Refcounted { |
| 98 |
public: |
| 99 |
MembranePolicyImpl() = default; |
| 100 |
MembranePolicyImpl(kj::Maybe<kj::Promise<void>> revokePromise) |
| 101 |
: revokePromise(revokePromise.map([](kj::Promise<void>& p) { return p.fork(); })) {} |
| 102 |
|
| 103 |
kj::Maybe<Capability::Client> inboundCall(uint64_t interfaceId, uint16_t methodId, |
| 104 |
Capability::Client target) override { |
| 105 |
if (interfaceId == capnp::typeId<Thing>() && methodId == 1) { |
| 106 |
return Capability::Client(kj::heap<ThingImpl>("inbound")); |
| 107 |
} else { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
kj::Maybe<Capability::Client> outboundCall(uint64_t interfaceId, uint16_t methodId, |
| 113 |
Capability::Client target) override { |
| 114 |
if (interfaceId == capnp::typeId<Thing>() && methodId == 1) { |
| 115 |
return Capability::Client(kj::heap<ThingImpl>("outbound")); |
| 116 |
} else { |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
kj::Own<MembranePolicy> addRef() override { |
| 122 |
return kj::addRef(*this); |
| 123 |
} |
| 124 |
|
| 125 |
kj::Maybe<kj::Promise<void>> onRevoked() override { |
| 126 |
return revokePromise.map([](kj::ForkedPromise<void>& fork) { |
| 127 |
return fork.addBranch(); |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
bool shouldResolveBeforeRedirecting() override { return true; } |
| 132 |
|
| 133 |
private: |
| 134 |
kj::Maybe<kj::ForkedPromise<void>> revokePromise; |
| 135 |
}; |
| 136 |
|
| 137 |
void testThingImpl(kj::WaitScope& waitScope, test::TestMembrane::Client membraned, |
| 138 |
kj::Function<Thing::Client()> makeThing, |
| 139 |
kj::StringPtr localPassThrough, kj::StringPtr localIntercept, |
| 140 |
kj::StringPtr remotePassThrough, kj::StringPtr remoteIntercept) { |
| 141 |
KJ_EXPECT(makeThing().passThroughRequest().send().wait(waitScope).getText() == localPassThrough); |
| 142 |
KJ_EXPECT(makeThing().interceptRequest().send().wait(waitScope).getText() == localIntercept); |
| 143 |
|
| 144 |
{ |
| 145 |
auto req = membraned.callPassThroughRequest(); |
| 146 |
req.setThing(makeThing()); |
| 147 |
req.setTailCall(false); |
| 148 |
KJ_EXPECT(req.send().wait(waitScope).getText() == remotePassThrough); |
| 149 |
} |
| 150 |
{ |
| 151 |
auto req = membraned.callInterceptRequest(); |
| 152 |
req.setThing(makeThing()); |
| 153 |
req.setTailCall(false); |
| 154 |
KJ_EXPECT(req.send().wait(waitScope).getText() == remoteIntercept); |
| 155 |
} |
| 156 |
{ |
| 157 |
auto req = membraned.callPassThroughRequest(); |
| 158 |
req.setThing(makeThing()); |
| 159 |
req.setTailCall(true); |
| 160 |
KJ_EXPECT(req.send().wait(waitScope).getText() == remotePassThrough); |
| 161 |
} |
| 162 |
{ |
| 163 |
auto req = membraned.callInterceptRequest(); |
| 164 |
req.setThing(makeThing()); |
| 165 |
req.setTailCall(true); |
| 166 |
KJ_EXPECT(req.send().wait(waitScope).getText() == remoteIntercept); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
struct TestEnv { |
| 171 |
kj::EventLoop loop; |
| 172 |
kj::WaitScope waitScope; |
| 173 |
kj::Own<MembranePolicyImpl> policy; |
| 174 |
test::TestMembrane::Client membraned; |
| 175 |
|
| 176 |
TestEnv() |
| 177 |
: waitScope(loop), |
| 178 |
policy(kj::refcounted<MembranePolicyImpl>()), |
| 179 |
membraned(membrane(kj::heap<TestMembraneImpl>(), policy->addRef())) {} |
| 180 |
|
| 181 |
void testThing(kj::Function<Thing::Client()> makeThing, |
| 182 |
kj::StringPtr localPassThrough, kj::StringPtr localIntercept, |
| 183 |
kj::StringPtr remotePassThrough, kj::StringPtr remoteIntercept) { |
| 184 |
testThingImpl(waitScope, membraned, kj::mv(makeThing), |
| 185 |
localPassThrough, localIntercept, remotePassThrough, remoteIntercept); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
KJ_TEST("call local object inside membrane") { |
| 190 |
TestEnv env; |
| 191 |
env.testThing([&]() { |
| 192 |
return env.membraned.makeThingRequest().send().wait(env.waitScope).getThing(); |
| 193 |
}, "inside", "inbound", "inside", "inside"); |
| 194 |
} |
| 195 |
|
| 196 |
KJ_TEST("call local promise inside membrane") { |
| 197 |
TestEnv env; |
| 198 |
env.testThing([&]() { |
| 199 |
return env.membraned.makeThingRequest().send().getThing(); |
| 200 |
}, "inside", "inbound", "inside", "inside"); |
| 201 |
} |
| 202 |
|
| 203 |
KJ_TEST("call local resolved promise inside membrane") { |
| 204 |
TestEnv env; |
| 205 |
env.testThing([&]() { |
| 206 |
auto thing = env.membraned.makeThingRequest().send().getThing(); |
| 207 |
thing.whenResolved().wait(env.waitScope); |
| 208 |
return thing; |
| 209 |
}, "inside", "inbound", "inside", "inside"); |
| 210 |
} |
| 211 |
|
| 212 |
KJ_TEST("call local object outside membrane") { |
| 213 |
TestEnv env; |
| 214 |
env.testThing([&]() { |
| 215 |
return kj::heap<ThingImpl>("outside"); |
| 216 |
}, "outside", "outside", "outside", "outbound"); |
| 217 |
} |
| 218 |
|
| 219 |
KJ_TEST("call local capability that has passed into and back out of membrane") { |
| 220 |
TestEnv env; |
| 221 |
env.testThing([&]() { |
| 222 |
auto req = env.membraned.loopbackRequest(); |
| 223 |
req.setThing(kj::heap<ThingImpl>("outside")); |
| 224 |
return req.send().wait(env.waitScope).getThing(); |
| 225 |
}, "outside", "outside", "outside", "outbound"); |
| 226 |
} |
| 227 |
|
| 228 |
KJ_TEST("call local promise pointing into membrane that eventually resolves to outside") { |
| 229 |
TestEnv env; |
| 230 |
env.testThing([&]() { |
| 231 |
auto req = env.membraned.loopbackRequest(); |
| 232 |
req.setThing(kj::heap<ThingImpl>("outside")); |
| 233 |
return req.send().getThing(); |
| 234 |
}, "outside", "outside", "outside", "outbound"); |
| 235 |
} |
| 236 |
|
| 237 |
KJ_TEST("apply membrane using copyOutOfMembrane() on struct") { |
| 238 |
TestEnv env; |
| 239 |
|
| 240 |
env.testThing([&]() { |
| 241 |
MallocMessageBuilder outsideBuilder; |
| 242 |
auto root = outsideBuilder.initRoot<test::TestContainMembrane>(); |
| 243 |
root.setCap(kj::heap<ThingImpl>("inside")); |
| 244 |
MallocMessageBuilder insideBuilder; |
| 245 |
insideBuilder.adoptRoot(copyOutOfMembrane( |
| 246 |
root.asReader(), insideBuilder.getOrphanage(), env.policy->addRef())); |
| 247 |
return insideBuilder.getRoot<test::TestContainMembrane>().getCap(); |
| 248 |
}, "inside", "inbound", "inside", "inside"); |
| 249 |
} |
| 250 |
|
| 251 |
KJ_TEST("apply membrane using copyOutOfMembrane() on list") { |
| 252 |
TestEnv env; |
| 253 |
|
| 254 |
env.testThing([&]() { |
| 255 |
MallocMessageBuilder outsideBuilder; |
| 256 |
auto list = outsideBuilder.initRoot<test::TestContainMembrane>().initList(1); |
| 257 |
list.set(0, kj::heap<ThingImpl>("inside")); |
| 258 |
MallocMessageBuilder insideBuilder; |
| 259 |
insideBuilder.initRoot<test::TestContainMembrane>().adoptList(copyOutOfMembrane( |
| 260 |
list.asReader(), insideBuilder.getOrphanage(), env.policy->addRef())); |
| 261 |
return insideBuilder.getRoot<test::TestContainMembrane>().getList()[0]; |
| 262 |
}, "inside", "inbound", "inside", "inside"); |
| 263 |
} |
| 264 |
|
| 265 |
KJ_TEST("apply membrane using copyOutOfMembrane() on AnyPointer") { |
| 266 |
TestEnv env; |
| 267 |
|
| 268 |
env.testThing([&]() { |
| 269 |
MallocMessageBuilder outsideBuilder; |
| 270 |
auto ptr = outsideBuilder.initRoot<test::TestAnyPointer>().getAnyPointerField(); |
| 271 |
ptr.setAs<test::TestMembrane::Thing>(kj::heap<ThingImpl>("inside")); |
| 272 |
MallocMessageBuilder insideBuilder; |
| 273 |
insideBuilder.initRoot<test::TestAnyPointer>().getAnyPointerField().adopt(copyOutOfMembrane( |
| 274 |
ptr.asReader(), insideBuilder.getOrphanage(), env.policy->addRef())); |
| 275 |
return insideBuilder.getRoot<test::TestAnyPointer>().getAnyPointerField() |
| 276 |
.getAs<test::TestMembrane::Thing>(); |
| 277 |
}, "inside", "inbound", "inside", "inside"); |
| 278 |
} |
| 279 |
|
| 280 |
KJ_TEST("MembraneHook::whenMoreResolved returns same value even when called concurrently.") { |
| 281 |
TestEnv env; |
| 282 |
|
| 283 |
auto paf = kj::newPromiseAndFulfiller<test::TestMembrane::Client>(); |
| 284 |
test::TestMembrane::Client promCap(kj::mv(paf.promise)); |
| 285 |
|
| 286 |
auto prom = promCap.whenResolved(); |
| 287 |
prom = prom.then([promCap = kj::mv(promCap), &env]() mutable { |
| 288 |
auto membraned = membrane(kj::mv(promCap), env.policy->addRef()); |
| 289 |
auto hook = ClientHook::from(membraned); |
| 290 |
|
| 291 |
auto arr = kj::heapArrayBuilder<kj::Promise<kj::Own<ClientHook>>>(2); |
| 292 |
arr.add(KJ_ASSERT_NONNULL(hook->whenMoreResolved())); |
| 293 |
arr.add(KJ_ASSERT_NONNULL(hook->whenMoreResolved())); |
| 294 |
|
| 295 |
return kj::joinPromises(arr.finish()).attach(kj::mv(hook)); |
| 296 |
}).then([](kj::Vector<kj::Own<ClientHook>> hooks) { |
| 297 |
auto first = hooks[0].get(); |
| 298 |
auto second = hooks[1].get(); |
| 299 |
KJ_ASSERT(first == second); |
| 300 |
}).eagerlyEvaluate(nullptr); |
| 301 |
|
| 302 |
auto newClient = kj::heap<TestMembraneImpl>(); |
| 303 |
paf.fulfiller->fulfill(kj::mv(newClient)); |
| 304 |
prom.wait(env.waitScope); |
| 305 |
} |
| 306 |
|
| 307 |
struct TestRpcEnv { |
| 308 |
kj::EventLoop loop; |
| 309 |
kj::WaitScope waitScope; |
| 310 |
kj::TwoWayPipe pipe; |
| 311 |
TwoPartyClient client; |
| 312 |
TwoPartyClient server; |
| 313 |
test::TestMembrane::Client membraned; |
| 314 |
|
| 315 |
TestRpcEnv(kj::Maybe<kj::Promise<void>> revokePromise = nullptr) |
| 316 |
: waitScope(loop), |
| 317 |
pipe(kj::newTwoWayPipe()), |
| 318 |
client(*pipe.ends[0]), |
| 319 |
server(*pipe.ends[1], |
| 320 |
membrane(kj::heap<TestMembraneImpl>(), |
| 321 |
kj::refcounted<MembranePolicyImpl>(kj::mv(revokePromise))), |
| 322 |
rpc::twoparty::Side::SERVER), |
| 323 |
membraned(client.bootstrap().castAs<test::TestMembrane>()) {} |
| 324 |
|
| 325 |
void testThing(kj::Function<Thing::Client()> makeThing, |
| 326 |
kj::StringPtr localPassThrough, kj::StringPtr localIntercept, |
| 327 |
kj::StringPtr remotePassThrough, kj::StringPtr remoteIntercept) { |
| 328 |
testThingImpl(waitScope, membraned, kj::mv(makeThing), |
| 329 |
localPassThrough, localIntercept, remotePassThrough, remoteIntercept); |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
KJ_TEST("call remote object inside membrane") { |
| 334 |
TestRpcEnv env; |
| 335 |
env.testThing([&]() { |
| 336 |
return env.membraned.makeThingRequest().send().wait(env.waitScope).getThing(); |
| 337 |
}, "inside", "inbound", "inside", "inside"); |
| 338 |
} |
| 339 |
|
| 340 |
KJ_TEST("call remote promise inside membrane") { |
| 341 |
TestRpcEnv env; |
| 342 |
env.testThing([&]() { |
| 343 |
return env.membraned.makeThingRequest().send().getThing(); |
| 344 |
}, "inside", "inbound", "inside", "inside"); |
| 345 |
} |
| 346 |
|
| 347 |
KJ_TEST("call remote resolved promise inside membrane") { |
| 348 |
TestEnv env; |
| 349 |
env.testThing([&]() { |
| 350 |
auto thing = env.membraned.makeThingRequest().send().getThing(); |
| 351 |
thing.whenResolved().wait(env.waitScope); |
| 352 |
return thing; |
| 353 |
}, "inside", "inbound", "inside", "inside"); |
| 354 |
} |
| 355 |
|
| 356 |
KJ_TEST("call remote object outside membrane") { |
| 357 |
TestRpcEnv env; |
| 358 |
env.testThing([&]() { |
| 359 |
return kj::heap<ThingImpl>("outside"); |
| 360 |
}, "outside", "outside", "outside", "outbound"); |
| 361 |
} |
| 362 |
|
| 363 |
KJ_TEST("call remote capability that has passed into and back out of membrane") { |
| 364 |
TestRpcEnv env; |
| 365 |
env.testThing([&]() { |
| 366 |
auto req = env.membraned.loopbackRequest(); |
| 367 |
req.setThing(kj::heap<ThingImpl>("outside")); |
| 368 |
return req.send().wait(env.waitScope).getThing(); |
| 369 |
}, "outside", "outside", "outside", "outbound"); |
| 370 |
} |
| 371 |
|
| 372 |
KJ_TEST("call remote promise pointing into membrane that eventually resolves to outside") { |
| 373 |
TestRpcEnv env; |
| 374 |
env.testThing([&]() { |
| 375 |
auto req = env.membraned.loopbackRequest(); |
| 376 |
req.setThing(kj::heap<ThingImpl>("outside")); |
| 377 |
return req.send().getThing(); |
| 378 |
}, "outside", "outside", "outside", "outbound"); |
| 379 |
} |
| 380 |
|
| 381 |
KJ_TEST("revoke membrane") { |
| 382 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 383 |
|
| 384 |
TestRpcEnv env(kj::mv(paf.promise)); |
| 385 |
|
| 386 |
auto thing = env.membraned.makeThingRequest().send().wait(env.waitScope).getThing(); |
| 387 |
|
| 388 |
auto callPromise = env.membraned.waitForeverRequest().send(); |
| 389 |
|
| 390 |
KJ_EXPECT(!callPromise.poll(env.waitScope)); |
| 391 |
|
| 392 |
paf.fulfiller->reject(KJ_EXCEPTION(DISCONNECTED, "foobar")); |
| 393 |
|
| 394 |
// TRICKY: We need to use .ignoreResult().wait() below because when compiling with |
| 395 |
// -fno-exceptions, void waits throw recoverable exceptions while non-void waits necessarily |
| 396 |
// throw fatal exceptions... but testing for fatal exceptions when exceptions are disabled |
| 397 |
// involves fork()ing the process to run the code so if it has side effects on file descriptors |
| 398 |
// then we'll get in a bad state... |
| 399 |
|
| 400 |
KJ_ASSERT(callPromise.poll(env.waitScope)); |
| 401 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", callPromise.ignoreResult().wait(env.waitScope)); |
| 402 |
|
| 403 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", |
| 404 |
env.membraned.makeThingRequest().send().ignoreResult().wait(env.waitScope)); |
| 405 |
|
| 406 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", |
| 407 |
thing.passThroughRequest().send().ignoreResult().wait(env.waitScope)); |
| 408 |
} |
| 409 |
|
| 410 |
} // namespace |
| 411 |
} // namespace _ |
| 412 |
} // namespace capnp |