bool TestExtFile::test_vfprintf() { Variant f = f_fopen("test/test_ext_file.tmp", "w"); f_vfprintf(f, "%s %s", CREATE_VECTOR2("testing", "vfprintf")); f_fclose(f); f = f_fopen("test/test_ext_file.tmp", "r"); VF(f, "testing vfprintf"); return Count(true); }
bool TestExtFile::test_fscanf() { Variant f = f_fopen("test/test_ext_file.tmp", "w"); f_fputs(f, "testing fscanf"); f_fclose(f); f = f_fopen("test/test_ext_file.tmp", "r"); VS(f_fscanf(2, f, "%s %s"), CREATE_VECTOR2("testing", "fscanf")); return Count(true); }
bool TestExtFile::test_file() { Variant f = f_fopen("test/test_ext_file.tmp", "w"); f_fputs(f, "testing\nfile\n"); f_fclose(f); Variant items = f_file("test/test_ext_file.tmp"); VS(items, CREATE_VECTOR2("testing\n", "file\n")); return Count(true); }
bool TestExtPreg::test_preg_grep() { Array array = CREATE_VECTOR4("foo 123.1", "fg 24bar", "123.1", "24"); Array fl_array = f_preg_grep("/^(\\d+)?\\.\\d+$/", array); VS(fl_array.size(), 1); VS(fl_array[2], "123.1"); VS(f_preg_grep("/a/", CREATE_VECTOR2("c", "b")), Array::Create()); return Count(true); }
bool TestExtOutput::test_ob_list_handlers() { f_ob_start(); f_ob_start("test"); Array handlers = f_ob_list_handlers(); f_ob_end_clean(); f_ob_end_clean(); VS(handlers, CREATE_VECTOR2(null, "test")); return Count(true); }
void AsioSession::onSetResultToRefCreate(c_SetResultToRefWaitHandle* wait_handle, CObjRef child) { assert(m_onSetResultToRefCreateCallback.get()); try { vm_call_user_func( m_onSetResultToRefCreateCallback, CREATE_VECTOR2(wait_handle, child)); } catch (const Object& callback_exception) { raise_warning("[asio] Ignoring exception thrown by SetResultToRefWaitHandle::onCreate callback"); } }
void AsioSession::onGenArrayCreate(c_GenArrayWaitHandle* wait_handle, CVarRef dependencies) { assert(m_onGenArrayCreateCallback.get()); try { vm_call_user_func( m_onGenArrayCreateCallback, CREATE_VECTOR2(wait_handle, dependencies)); } catch (const Object& callback_exception) { raise_warning("[asio] Ignoring exception thrown by GenArrayWaitHandle::onCreate callback"); } }
void AsioSession::onContinuationFail(c_ContinuationWaitHandle* cont, CObjRef exception) { assert(m_onContinuationFailCallback.get()); try { vm_call_user_func( m_onContinuationFailCallback, CREATE_VECTOR2(cont, exception)); } catch (const Object& callback_exception) { raise_warning("[asio] Ignoring exception thrown by ContinuationWaitHandle::onFail callback"); } }
void AsioSession::onContinuationSuccess(c_ContinuationWaitHandle* cont, CVarRef result) { assert(m_onContinuationSuccessCallback.get()); try { vm_call_user_func( m_onContinuationSuccessCallback, CREATE_VECTOR2(cont, result)); } catch (const Object& callback_exception) { raise_warning("[asio] Ignoring exception thrown by ContinuationWaitHandle::onSuccess callback"); } }
bool TestExtFb::test_fb_compact_serialize() { fb_cs_test(null); fb_cs_test(true); fb_cs_test(false); fb_cs_test(1234.5678); fb_cs_test(""); fb_cs_test("a"); fb_cs_test("\0"); fb_cs_test("\0 a"); fb_cs_test("0123012301230123"); fb_cs_test("0123012301230123a"); fb_cs_test("012301230123012"); fb_cs_test(Array()); fb_cs_test(CREATE_VECTOR1(12345)); fb_cs_test(CREATE_VECTOR3(12345,"abc",0.1234)); fb_cs_test(CREATE_MAP1(1, 12345)); fb_cs_test(CREATE_MAP3(1, 12345, "a", 123124, "sdf", 0.1234)); fb_cs_test(CREATE_VECTOR1(CREATE_VECTOR1("a"))); fb_cs_test(CREATE_VECTOR2(1, CREATE_VECTOR1("a"))); fb_cs_test(CREATE_VECTOR2(CREATE_VECTOR1("a"), 1)); fb_cs_test(CREATE_VECTOR2(CREATE_VECTOR1("a"), CREATE_VECTOR1(1))); // Test skips fb_cs_test(CREATE_MAP3(0, "a", 1, "b", 3, "c")); fb_cs_test(CREATE_MAP3(1, "a", 2, "b", 3, "c")); fb_cs_test(CREATE_MAP3(0, "a", 2, "b", 3, "c")); fb_cs_test(CREATE_MAP1(3, "a")); // Test for overflow fb_cs_test(CREATE_MAP1((int64)((1ULL << 63) - 1), "a")); // Test each power of two, +/- 1 and the negatives of them // Test a single number and packed inside an array for (int i = 0; i < 64; ++i) { int64 n = (1ULL << i); fb_cs_test(n); fb_cs_test(CREATE_VECTOR1(n)); fb_cs_test(n-1); fb_cs_test(CREATE_VECTOR1(n-1)); fb_cs_test(n+1); fb_cs_test(CREATE_VECTOR1(n+1)); fb_cs_test(-n); fb_cs_test(CREATE_VECTOR1(-n)); fb_cs_test(-n-1); fb_cs_test(CREATE_VECTOR1(-n-1)); fb_cs_test(-n+1); fb_cs_test(CREATE_VECTOR1(-n+1)); } return Count(true); }
Variant EvalObjectData::t___set(Variant v_name, Variant v_value) { const MethodStatement *ms = getMethodStatement("__set"); if (ms) { return ms->invokeInstance(Object(root), CREATE_VECTOR2(v_name, withRefBind(v_value)), false); } else { return DynamicObjectData::t___set(v_name, withRefBind(v_value)); } }
bool TestExtFile::test_fputcsv() { Array fields = CREATE_VECTOR2("apple", "\"banana\""); Variant f = f_fopen("test/test_ext_file.tmp", "w"); f_fputcsv(f, fields); f_fclose(f); f = f_fopen("test/test_ext_file.tmp", "r"); VF(f, "apple,\"\"\"banana\"\"\"\n"); return Count(true); }
bool TestExtIconv::test_iconv_mime_decode_headers() { VS(f_iconv_mime_decode_headers ("Subject: =?UTF-8?B?UHLDnGZ1bmcgUHLDnGZ1bmc=?=\n" "Subject: =?UTF-8?B?UHLDnGZ1bmcgUHLDnGZ1bmc=?=\n", 0, "ISO-8859-1"), CREATE_MAP1("Subject", CREATE_VECTOR2("Pr\xDC""fung Pr\xDC""fung", "Pr\xDC""fung Pr\xDC""fung"))); return Count(true); }
bool BaseExecutionContext::obFlush() { ASSERT(m_protectedLevel >= 0); if ((int)m_buffers.size() > m_protectedLevel) { std::list<OutputBuffer*>::const_iterator iter = m_buffers.end(); OutputBuffer *last = *(--iter); const int flag = PHP_OUTPUT_HANDLER_START | PHP_OUTPUT_HANDLER_END; if (iter != m_buffers.begin()) { OutputBuffer *prev = *(--iter); if (last->handler.isNull()) { prev->oss.absorb(last->oss); } else { try { Variant tout = f_call_user_func_array(last->handler, CREATE_VECTOR2(last->oss.detach(), flag)); prev->oss.append(tout.toString()); last->oss.reset(); } catch (...) { prev->oss.absorb(last->oss); } } return true; } if (!last->handler.isNull()) { try { Variant tout = f_call_user_func_array(last->handler, CREATE_VECTOR2(last->oss.detach(), flag)); String sout = tout.toString(); writeStdout(sout.data(), sout.size()); last->oss.reset(); return true; } catch (...) {} } writeStdout(last->oss.data(), last->oss.size()); last->oss.reset(); return true; } return false; }
bool TestExtFile::test_fgetcsv() { Array fields = CREATE_VECTOR2("a", "b"); Variant f = f_fopen("test/test_ext_file.tmp", "w"); f_fputcsv(f, fields); f_fclose(f); f = f_fopen("test/test_ext_file.tmp", "r"); Array read = f_fgetcsv(f); VS(read, fields); return Count(true); }
bool TestExtMb::test_mb_ereg_search_pos() { String str = "Pr\xC3\x9C\xC3\x9D""fung abc p\xC3\x9C"; String reg = "\\w+"; f_mb_regex_encoding("UTF-8"); f_mb_ereg_search_init(str, reg); Variant r = f_mb_ereg_search(); r = f_mb_ereg_search_getregs(); // get first result VS(r, CREATE_VECTOR1("Pr\xC3\x9C\xC3\x9D""fung")); VS(f_mb_ereg_search_pos(), CREATE_VECTOR2(11, 3)); return Count(true); }
bool TestExtString::test_str_replace() { { VS(f_str_replace("%body%", "black", "<body text='%body%'>"), "<body text='black'>"); } { Array vowels; vowels.append("a"); vowels.append("e"); vowels.append("i"); vowels.append("o"); vowels.append("u"); vowels.append("A"); vowels.append("E"); vowels.append("I"); vowels.append("O"); vowels.append("U"); VS(f_str_replace(vowels, "", "Hello World of PHP"), "Hll Wrld f PHP"); } { String phrase = "You should eat fruits, vegetables, and fiber every day."; Array healthy = CREATE_VECTOR3("fruits", "vegetables", "fiber"); Array yummy = CREATE_VECTOR3("pizza", "beer", "ice cream"); VS(f_str_replace(healthy, yummy, phrase), "You should eat pizza, beer, and ice cream every day."); } { Variant count; Variant str = f_str_replace("ll", "", "good golly miss molly!", ref(count)); VS(count, 2); } { Array letters = CREATE_VECTOR2("a", "p"); Array fruit = CREATE_VECTOR2("apple", "pear"); String text = "a p"; VS(f_str_replace(letters, fruit, text), "apearpearle pear"); } return Count(true); }
void objOffsetSet(TypedValue* base, CVarRef offset, TypedValue* val, bool validate /* = true */) { if (validate) { objArrayAccess(base); } static StringData* sd__offsetSet = StringData::GetStaticString("offsetSet"); ObjectData* obj = base->m_data.pobj; if (LIKELY(obj->isInstance())) { Instance* instance = static_cast<Instance*>(obj); const Func* method = instance->methodNamed(sd__offsetSet); ASSERT(method != NULL); TypedValue tvResult; tvWriteUninit(&tvResult); instance->invokeUserMethod(&tvResult, method, CREATE_VECTOR2(offset, tvAsCVarRef(val))); tvRefcountedDecRef(&tvResult); } else { tvAsVariant(base).getArrayAccess() ->o_invoke(sd__offsetSet, CREATE_VECTOR2(offset, tvAsCVarRef(val))); } }
Variant EvalObjectData::t___set(Variant v_name, Variant v_value) { if (v_value.isReferenced()) { v_value.setContagious(); } const MethodStatement *ms = getMethodStatement("__set"); if (ms) { return ms->invokeInstance(Object(root), CREATE_VECTOR2(v_name, v_value), false); } else { return DynamicObjectData::t___set(v_name, v_value); } }
bool TestExtMb::test_mb_convert_variables() { Variant str = "Pr\xC3\x9C""fung"; Variant str1 = "Pr\xC3\x9C""fung"; Variant str2 = "Pr\xC3\x9C""fung"; Variant inputenc = f_mb_convert_variables(5, "ISO-8859-1", "UTF-8", ref(str), CREATE_VECTOR2(ref(str1), ref(str2))); VS(str, "Pr\xDC""fung"); VS(str1, "Pr\xDC""fung"); VS(str2, "Pr\xDC""fung"); return Count(true); }
bool TestExtProcess::test_exec() { Variant output, ret; String last_line = f_exec("echo hello; echo world;", ref(output), ref(ret)); VS(output, CREATE_VECTOR2("hello", "world")); VS(last_line, "world"); VS(ret, 0); string cur_cwd = Process::GetCurrentDirectory(); f_chdir("/tmp/"); VS(f_exec("/bin/pwd"), "/tmp"); f_chdir(String(cur_cwd)); return Count(true); }
Variant EvalObjectData::doCall(Variant v_name, Variant v_arguments, bool fatal) { const MethodStatement *ms = getMethodStatement("__call"); if (ms) { if (v_arguments.isNull()) { v_arguments = Array::Create(); } return ms->invokeInstance(Object(root), CREATE_VECTOR2(v_name, v_arguments), false); } else { return DynamicObjectData::doCall(v_name, v_arguments, fatal); } }
bool TestExtProcess::test_proc_get_status() { static const StaticString s_command("command"), s_pid("pid"), s_running("running"), s_signaled("signaled"), s_exitcode("exitcode"), s_termsig("termsig"), s_stopsig("stopsig"); Array descriptorspec = CREATE_MAP3(0, CREATE_VECTOR2("pipe", "r"), 1, CREATE_VECTOR2("pipe", "w"), 2, CREATE_VECTOR3("file", "/tmp/error-output.txt", "a")); Variant pipes; Variant process = f_proc_open(php_path, descriptorspec, ref(pipes)); VERIFY(!same(process, false)); Array ret = f_proc_get_status(process.toObject()); VS(ret[s_command], php_path); VERIFY(ret[s_pid].toInt32() > 0); VERIFY(ret[s_running]); VERIFY(!ret[s_signaled]); VS(ret[s_exitcode], -1); VS(ret[s_termsig], 0); VS(ret[s_stopsig], 0); { File *f = pipes[0].toObject().getTyped<File>(); VERIFY(f->valid()); f->close(); } { File *f = pipes[1].toObject().getTyped<File>(); VERIFY(f->valid()); f->close(); } VS(f_proc_close(process.toObject()), 0); return Count(true); }
bool TestExtJson::test_json_encode() { VS(f_json_encode(CREATE_MAP3("a", 1, "b", 2.3, 3, "test")), "{\"a\":1,\"b\":2.3,\"3\":\"test\"}"); VS(f_json_encode(CREATE_VECTOR5("a", 1, true, false, uninit_null())), "[\"a\",1,true,false,null]"); VS(f_json_encode("a\xE0"), "null"); VS(f_json_encode("a\xE0", k_JSON_FB_LOOSE), "\"a?\""); VS(f_json_encode(CREATE_MAP2("0", "apple", "1", "banana")), "[\"apple\",\"banana\"]"); VS(f_json_encode(CREATE_VECTOR1(CREATE_MAP1("a", "apple"))), "[{\"a\":\"apple\"}]"); VS(f_json_encode(CREATE_VECTOR1(CREATE_MAP1("a", "apple")), k_JSON_PRETTY_PRINT), "[\n {\n \"a\": \"apple\"\n }\n]"); VS(f_json_encode(CREATE_VECTOR4(1, 2, 3, CREATE_VECTOR3(1, 2, 3)), k_JSON_PRETTY_PRINT), "[\n" " 1,\n" " 2,\n" " 3,\n" " [\n" " 1,\n" " 2,\n" " 3\n" " ]\n" "]"); Array arr = CREATE_MAP3( "a", 1, "b", CREATE_VECTOR2(1, 2), "c", CREATE_MAP1("d", 42) ); VS(f_json_encode(arr, k_JSON_PRETTY_PRINT), "{\n" " \"a\": 1,\n" " \"b\": [\n" " 1,\n" " 2\n" " ],\n" " \"c\": {\n" " \"d\": 42\n" " }\n" "}"); return Count(true); }
bool TestExtMath::test_max() { VS(f_max(4, 2, CREATE_VECTOR4(3, 1, 6, 7)), 7); VS(f_max(0, CREATE_VECTOR3(2, 4, 5)), 5); VS(f_max(1, 0, CREATE_VECTOR1("hello")), 0); VS(f_max(1, "hello", CREATE_VECTOR1(0)), "hello"); VS(f_max(1, "hello", CREATE_VECTOR1(-1)), "hello"); VS(f_max(1, CREATE_VECTOR3(2, 4, 8), CREATE_VECTOR1(CREATE_VECTOR3(2, 5, 1))), CREATE_VECTOR3(2, 5, 1)); VS(f_max(1, "string", CREATE_VECTOR2(CREATE_VECTOR3(2, 5, 7), 42)), CREATE_VECTOR3(2, 5, 7)); VS(f_max(1, CREATE_MAP1(1, "1236150163")), "1236150163"); return Count(true); }
bool TestExtMailparse::test_mailparse_rfc822_parse_addresses() { static const StaticString s_display("display"), s_address("address"), s_is_group("is_group"); Array addresses = CREATE_VECTOR2("\":sysmail\"@ Some-Group. Some-Org, Muhammed." "(I am the greatest) Ali @(the)Vegas.WBA", "\"strange\":\":sysmail\"@ Some-Group. Some-Org, Muhammed." "(I am the greatest) Ali @(the)Vegas.WBA;"); f_ob_start(); for (ArrayIter iter(addresses); iter; ++iter) { Variant parsed = f_mailparse_rfc822_parse_addresses(iter.second()); for (ArrayIter iter2(parsed); iter2; ++iter2) { Variant pair = iter2.second(); echo(pair[s_display]); echo("\n"); echo(pair[s_address]); echo("\n"); if (pair[s_is_group].toBoolean()) { Variant sub = f_mailparse_rfc822_parse_addresses (f_substr(pair[s_address], 1, f_strlen(pair[s_address]) - 2)); for (ArrayIter iter3(sub); iter3; ++iter3) { echo(" "); echo(iter3.second()[s_address]); echo("\n"); } } } echo("...\n"); } String output = f_ob_get_contents(); f_ob_end_clean(); VS(output, ":[email protected]\n" "\":sysmail\"@Some-Group.Some-Org\n" "I am the greatest the\n" "[email protected]\n" "...\n" "strange\n" ":\":sysmail\"@Some-Group.Some-Org,[email protected];\n" " \":sysmail\"@Some-Group.Some-Org\n" " [email protected]\n" "...\n"); return Count(true); }
bool TestExtSoap::test_SoapServerFunctionParam() { Array funcs = CREATE_VECTOR2("Sub", "Add"); m_server->t_addfunction(funcs); VS(m_server->t_getfunctions(), funcs); VSOAP("<ns1:Add xmlns:ns1=\"http://testuri.org\">" "<x xsi:type=\"xsd:int\">22</x>" "<y xsi:type=\"xsd:int\">33</y>" "</ns1:Add>", "<ns1:AddResponse><return xsi:type=\"xsd:int\">55" "</return></ns1:AddResponse>"); return Count(true); }
bool TestExtProcess::test_proc_open_env_inh() { Array descriptorspec = CREATE_MAP3(0, CREATE_VECTOR2("pipe", "r"), 1, CREATE_VECTOR2("pipe", "w"), 2, CREATE_VECTOR3("file", "/tmp/error-output.txt", "a")); Variant pipes; g_context->setenv("inherit_me", "please"); Variant process = f_proc_open("echo $inherit_me", descriptorspec, ref(pipes)); VERIFY(!same(process, false)); { File *f = pipes[1].toObject().getTyped<File>(); VERIFY(f->valid()); StringBuffer sbuf; sbuf.read(f); f->close(); VS(sbuf.detach(), "please\n"); } VS(f_proc_close(process.toObject()), 0); return Count(true); }
void objOffsetSet(ObjectData* base, CVarRef offset, TypedValue* val, bool validate /* = true */) { if (validate) { objArrayAccess(base); } static StringData* sd__offsetSet = StringData::GetStaticString("offsetSet"); assert(!base->isCollection()); const Func* method = base->methodNamed(sd__offsetSet); assert(method != nullptr); TypedValue tvResult; tvWriteUninit(&tvResult); base->invokeUserMethod(&tvResult, method, CREATE_VECTOR2(offset, tvAsCVarRef(val))); tvRefcountedDecRef(&tvResult); }
// Create a PHP object given a typename and call the ctor, optionally passing up to 2 arguments Object createObject(CStrRef obj_typename, int nargs = 0, CVarRef arg1 = null_variant, CVarRef arg2 = null_variant) { if (!f_class_exists(obj_typename)) { raise_warning("runtime/ext_thrift: Class %s does not exist", obj_typename.data()); return Object(); } Array args; if (nargs == 1) { args = CREATE_VECTOR1(arg1); } else if (nargs == 2 ) { args = CREATE_VECTOR2(arg1, arg2); } return create_object(obj_typename, args); }