okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3227bd218
main
1// The peer-push decision (src/hip/peer_policy.hpp): FNX_PEER=0 forces the host exchange, a failing probe leaves the host exchange for the whole run with one plain 2// line, a forced probe failure (FNX_PEER_PROBE_FAIL=1) does the same, and nothing runs unless the policy says so. CPU only. 3#include <cstdio> 4#include <cstdlib> 5#include <string> 6 7#include "../src/hip/peer_policy.hpp" 8 9static int bad = 0 ; 10#define CHECK (c ) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++bad; } } while (0) 11 12int main () { 13using namespace fnx ; 14int calls = 0 ; 15auto pass = [& ](std::string & why ) {++ calls ;why = "all pairs ok" ;return true; }; 16auto fail = [& ](std::string & why ) {++ calls ;why = "card 0 -> card 2: 3 of 1048576 words arrived wrong" ;return false; }; 17unsetenv ("FNX_PEER_PROBE_FAIL" ); 18 19// FNX_PEER=0: the host exchange, no probe, no line, whatever the default 20calls = 0 ; 21PeerDecision d = peer_decide ("0" , true,pass ); 22CHECK (!d .use && d .line .empty ()&& calls == 0 ); 23// unset and the default off: nothing, no probe 24d = peer_decide (nullptr , false,pass ); 25CHECK (!d .use && d .line .empty ()&& calls == 0 ); 26// unset and the default on: the probe runs and decides 27d = peer_decide (nullptr , true,pass ); 28CHECK (d .use && calls == 1 && d .line .find ("Peer push between the cards is on" )== 0 ); 29// FNX_PEER=1: the probe runs even if the default is off 30calls = 0 ; 31d = peer_decide ("1" , false,pass ); 32CHECK (d .use && calls == 1 ); 33// a failing probe: the host exchange for the whole run and one plain line naming the reason 34calls = 0 ; 35d = peer_decide ("1" , false,fail ); 36CHECK (!d .use && calls == 1 ); 37CHECK (d .line == "The cards cannot push into each other's memory here (card 0 -> card 2: 3 of 1048576 words arrived wrong): using the host exchange for this run." ); 38CHECK (d .line .find ('\n' )== std::string::npos ); 39// the same through the default 40d = peer_decide (nullptr , true,fail ); 41CHECK (!d .use ); 42// a forced probe failure (the tester hook): the probe is not even called, the line says why 43setenv ("FNX_PEER_PROBE_FAIL" ,"1" ,1 ); 44calls = 0 ; 45d = peer_decide ("1" , false,pass ); 46CHECK (!d .use && calls == 0 && d .line .find ("forced failure (FNX_PEER_PROBE_FAIL=1)" )!= std::string::npos && d .line .find ("using the host exchange for this run." )!= std::string::npos ); 47d = peer_decide (nullptr , true,pass ); 48CHECK (!d .use && calls == 0 ); 49// but FNX_PEER=0 stays silent and off 50d = peer_decide ("0" , true,pass ); 51CHECK (!d .use && d .line .empty ()); 52unsetenv ("FNX_PEER_PROBE_FAIL" ); 53 std::printf (bad ?"peer_policy_test: %d FAILED\n" :"peer_policy_test: ok\n" ,bad ); 54return bad ?1 :0 ; 55}