mcl_core_wire_test (mcl_core v1.0.0)

View Source

The one way to test a capability: deliver the request the way a station does, then read the reply the way a client does (mcl-om#31).

WHY THIS EXISTS. mcl-chess 0.7.1 passed 206 eunit tests and refused every real call on the live mesh (mcl-chess#10). Its handlers read maps:get(<<"key">>, Payload, ...), and on this wire no payload looks like that: every text key arrives as {text, Bin}, and the verified caller arrives under the ATOM key as a raw 32-byte node id. Its tests built the request by hand in the shape the handler expected, so they agreed with the handler and disagreed with the wire. A reply has the mirror hazard: a plain binary is a CBOR BYTE string, so a game_id sent that way reaches a client as an opaque blob, and 32 raw bytes tagged as text are refused outright because CBOR text must be UTF-8.

WHAT IT DOES, in the order the mesh does it:

1. the request is encoded with macula_record_cbor:encode/1 and decoded with macula_frame:plain_payload/1, which is the provider's own path, so the handler sees exactly what a station hands it; 2. the verified caller is merged the way macula_station_link's own with_caller/2 merges it, including dropping a caller-sent text caller (a peer that names itself is not read); and, for a handler_authorizes capability, the call's ucan_token and proofs are merged the way macula 18's with_call_token/3 merges them (deliver/3, call/4) — a text-keyed pair of those names is dropped first, and the token is what the caller presented, UNVERIFIED; 3. the handler runs inside the request's caller context, the way macula_station_link runs it, so a handler reading macula_response:caller/0 is tested on the path a station gives it; 4. the reply is put through macula_frame:check_payload/1, the same check a provider runs before answering, so a reply macula would refuse fails the TEST rather than the first real caller; 5. the reply is encoded and decoded again and handed back as the client's own view: text unwraps to its Bin, and a byte string stays named as one, {bytes, Bin}, so an assertion that expects text fails on bytes rather than reading the two as the same value (mcl-om#32).

WHAT IT DOES NOT DO. It does not sign, dial, or check the D25 grant: this is the payload contract, not the mesh integration (mcl_core_SUITE is that). It does not know which caller a service considers authorised, it only makes sure the handler is handed one at all.

THE CALLER IS RAW. A node id is 32 bytes on the wire; a handler that stores or republishes it wants the 64-character hex form and gets it from mcl_core_wire:caller_hex/1. Passing hex here would hide the fault this helper exists to catch, so a 64-character caller is refused rather than accepted.

Summary

Functions

Call a capability handler as a station would, and answer what the client would see.

call/3 for a handler_authorizes capability: CallToken says what the request carried (#{ucan_token => Bin, proofs => List}, either key omitted for none), and the handler is handed it the way macula 18 hands it over — merged under the atom keys, after a text-keyed pair of those names is dropped. A capability under any other policy must use call/3: the link merges nothing there, and a helper that merged anyway would test a payload no station delivers.

The payload the handler is handed for this request: what a station delivers, caller merged in. Use it only to assert on the shape itself; a test that wants an answer calls call/3, or call/4 for a handler_authorizes capability, whose handler is handed the call's token as well (deliver/3).

deliver/2 for a handler_authorizes capability: what a station hands a handler that must check the call's own token, which is CallToken merged in under the atom keys of call_token() — or none for a capability under any other policy, in which case this is deliver/2.

The reply as a client receives it, after the check a provider runs before answering. Text arrives as its Bin; a CBOR byte string arrives as {bytes, Bin}, because a client would get bytes and a test that read them as text could not tell a regression from a correct answer (mcl-om#32).

Types

authored/0

-type authored() :: #{term() => term()}.

call_outcome/0

-type call_outcome() :: client_view() | {error, term()}.

call_token/0

-type call_token() :: #{ucan_token => binary(), proofs => [binary()]}.

client_view/0

-type client_view() :: map().

Functions

call(Handler, Request, Caller)

-spec call(module(), authored(), binary()) -> call_outcome().

Call a capability handler as a station would, and answer what the client would see.

Handler is a macula_response module (its handle_request/2 is called with the state init/1 returned). Caller is the caller's raw 32-byte node id: on a real call the station has verified it and macula injects it, so a test states it here instead of a handler accepting it.

TWO KINDS OF REFUSAL, and a caller sees them differently:

* an APPLICATION refusal is a handler answering a reply that says so, #{<<"error">> => {text, <<"invalid_color">>}}. It comes back as a reply like any other, because that is what the client reads; * a HANDLER refusal is the behaviour's own {error, Reason, State}. macula answers that with an ERROR frame carrying the reason's name, and the client's call answers {error, Detail}, so this returns {error, Detail} too: the reason's text capped at 256 bytes, or {call_error, <<"handler_error">>, undefined} when the reason has no name to cross with.

A reply macula would not put on the wire raises bad_reply.

call(Handler, Request, Caller, CallToken)

-spec call(module(), authored(), binary(), call_token() | none) -> call_outcome().

call/3 for a handler_authorizes capability: CallToken says what the request carried (#{ucan_token => Bin, proofs => List}, either key omitted for none), and the handler is handed it the way macula 18 hands it over — merged under the atom keys, after a text-keyed pair of those names is dropped. A capability under any other policy must use call/3: the link merges nothing there, and a helper that merged anyway would test a payload no station delivers.

deliver(Request, Caller)

-spec deliver(authored(), binary()) -> map().

The payload the handler is handed for this request: what a station delivers, caller merged in. Use it only to assert on the shape itself; a test that wants an answer calls call/3, or call/4 for a handler_authorizes capability, whose handler is handed the call's token as well (deliver/3).

deliver(Request, Caller, CallToken)

-spec deliver(authored(), binary(), call_token() | none) -> map().

deliver/2 for a handler_authorizes capability: what a station hands a handler that must check the call's own token, which is CallToken merged in under the atom keys of call_token() — or none for a capability under any other policy, in which case this is deliver/2.

reply(Reply)

-spec reply(term()) -> client_view().

The reply as a client receives it, after the check a provider runs before answering. Text arrives as its Bin; a CBOR byte string arrives as {bytes, Bin}, because a client would get bytes and a test that read them as text could not tell a regression from a correct answer (mcl-om#32).