alarmCause($value); } check('plain cause is kept', cause('HA-test'), 'HA-test'); check('punctuation clients send is kept', cause('Doorbell (front): pressed!'), 'Doorbell (front): pressed!'); check('non-ASCII letters are kept', cause('Türklingel'), 'Türklingel'); check('missing cause', cause(null), ''); check('empty cause', cause(''), ''); check('array cause (cause[]=x)', cause(array('x')), ''); check('markup is dropped', cause(''), 'svg onload=alert(1)'); check('quotes, backslash and ampersand are dropped', cause("a'b\"c\\d&e`f"), 'abcdef'); check('$, | and backtick are dropped (escapeshellarg quotes the rest)', cause('x; rm -rf / $(id) | `y`'), 'x; rm -rf / (id) y'); check('NUL byte is dropped', cause("a\0b"), 'ab'); check('newline and tab are dropped', cause("line1\nline2\tx"), 'line1line2x'); check('spaces are collapsed and trimmed', cause(' a b '), 'a b'); check('only unsafe characters leaves nothing', cause('<>"\'&'), ''); check('invalid UTF-8 is refused', cause("ok\xC3("), ''); check('a leading dash stays a value', cause('--help'), '--help'); check('long cause is cut at 31 bytes', cause(str_repeat('a', 40)), str_repeat('a', 31)); $long = str_repeat('a', 30).'é'; check('cut does not split a character', cause($long), str_repeat('a', 30)); check('cut result is valid UTF-8', preg_match('//u', cause(str_repeat('é', 20))) === 1, true); check('cut result fits 31 bytes', strlen(cause(str_repeat('é', 20))) <= 31, true); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0);