示例#1
0
string SFlattenIterator::SFrame::MakePath() const
{
    if (m_Index == (size_t) -1) {
        if (m_Path.empty())
            return m_Iterator.GetKey();

        string path(m_Path + '.');
        path += m_Iterator.GetKey();
        return path;
    } else {
        string index_str(NStr::NumericToString(m_Index));

        if (m_Path.empty())
            return index_str;

        string path(m_Path + '.');
        path += index_str;
        return path;
    }
}
示例#2
0
int main(int argc, char* argv[])
{
	int randnum = 0;
	char* nvram = NULL, *value = NULL;
	char* delimiter = NULL, *tmp, *del;
	int position=0, c, i = 0, func = 0, val;
	char help_msg[2048] = "Usage: xobjconf [-s | -x <nvram variable>] {[-a | -m <values> -p <position>] | [ -n <to get # of digits>] | [-i <position>] | [-v <value>] | [-e <value>] | [ -r <position>] | [ -o <new delimiter>]} [-d delimiter]\n\n";

	init_gen( );
	strcat(help_msg, "xobjconf command summary\n");
	strcat(help_msg, "\txobjconf is a function to insert/delete/modify/view nvram variable by delimiter(s).\n");
	strcat(help_msg, "\t<nvram variable>:input a nvram variable.\n");
	strcat(help_msg, "\tdelimiter:a delimiter which is a seperator.\n");
	strcat(help_msg, "\t-s : to specify a nvram variable.\n");
	strcat(help_msg, "\t-x : to get total counts of token by specified delimiter.\n");
	strcat(help_msg, "\t-d : to specify a delimiter.\n");
	strcat(help_msg, "\t-a:insert a value to the nvram variable by delimiter.\n");
	strcat(help_msg, "\t-r : remove a value from a nvram variable by specfied position.\n");
	strcat(help_msg, "\t-v : remove a value from a nvram variable by specfied value.\n");
	strcat(help_msg, "\t-e : to check a value in a nvram variable is exist or not.\n");
	strcat(help_msg, "\t-o : to replace delimiters in a token list by new delimiters.\n");
	strcat(help_msg, "\t-n : to get digits in a nvram variable by speficied counts.\n");
	strcat(help_msg, "\t-m : to change a value by spcified position from a nvram variable.\n");
	strcat(help_msg, "\t-i : to return the value which is at the specified position from a nvram variable by delimiter.\n\n");

	if(argc <= 1 || ((isgraph(*argv[1]) || ispunct(*argv[1])) && *argv[1]!='-')){
		fprintf(stderr, "%s", help_msg);
		exit(0);
	}
	while ((c = getopt(argc, argv, "a:d:e:i:m:n:o:p:r:v:s:x:h")) != -1){
		switch (c) {
			case 'a':
				func = 1;
				value = optarg;
				break;
			case 'r':
				func = 2;
				position = atoi(optarg);
				break;
			case 'm':
				func = 3;
				value = optarg;
				break;
			case 's':
				nvram = optarg;
				break;
			case 'i':
				func = 4;
				position = atoi(optarg);
				break;
			case 'v':
				func = 5;
				value = optarg;
				position = 1;
				break;
			case 'x':
				func = 6;
				nvram = optarg;
				break;
			case 'e':
				func = 7;
				value = optarg;
				position = 1;
				break;
			case 'n':
				func = 8;
				val = atoi(optarg);
				position = 1;
				break;
			case 'o':
				func = 9;
				value = optarg;
				break;
			case 'p':
				position = atoi(optarg);
				break;
			case 'd':
				delimiter = optarg;
				break;
			case 'h':
				fprintf(stderr, "%s", help_msg);
				exit(0);
				break;
			default:
				fprintf(stderr, "%s", help_msg);
				exit(0);
				break;
		}
	}

	if(nvram_get(nvram) == NULL && delimiter != NULL && nvram != NULL){
		if(func == 1 && value != NULL){
			nvram_set(nvram, "");
			//printf("Inserting '%s' at position 1 ...\n", value);
			//tmp = StrDup(nvram_get(nvram));
			//value = insert_str(tmp, value, delimiter, 1);
			//tmp = StrDup(insert_str(nvram_get(nvram), value, delimiter, 1));
			//nvram_set(nvram, value);
			nvram_set(nvram, StrDup(insert_str(nvram_get(nvram), value, delimiter, 1)));
			printf("%s\n", nvram_get(nvram));
		}
		else{
			fprintf(stderr, "%s", help_msg);
			exit(0);
		}
	}
	else if(delimiter != NULL && position > 0 && nvram != NULL){
		if(func == 1 && value != NULL){
			//printf("Inserting %s at position %d ...\n", value, position);
			//tmp = StrDup(nvram_get(nvram));
			//value = insert_str(tmp, value, delimiter, 1);
			//nvram_set(nvram, value);
			nvram_set(nvram, insert_str(nvram_get(nvram), value, delimiter, position));
			printf("%s\n", nvram_get(nvram));
		}
		else if(func == 2){
			//printf("Deleting position %d of %s ... \n", position, nvram);
			//tmp = StrDup(nvram_get(nvram));
			//value = delete_str(nvram_get(nvram), delimiter, position);
			//nvram_set(nvram, value);
			nvram_set(nvram, delete_str(nvram_get(nvram), delimiter, position));
			printf("%s\n", nvram_get(nvram));
		}
		else if(func == 3 && value != NULL){
			//printf("Modifying position %d by %s ... \n", position, value);
			//tmp = StrDup(nvram_get(nvram));
			//value = modify_str(nvram_get(nvram), value, delimiter, position);
			//nvram_set(nvram, value);
			nvram_set(nvram, StrDup(modify_str(nvram_get(nvram), value, delimiter, position)));
			printf("%s\n", nvram_get(nvram));
		}
		else if(func == 4){
			//printf("\nReading the values from nvram variable %s ... \n", nvram);
			tmp = StrDup(nvram_get(nvram));
			printf("%s\n", index_str(tmp, delimiter, position));
		}
		else if(func == 5){
			/*c = matchStrPosAt(delimiter, StrDup(nvram_get(nvram)), -1) + 1;
			for (i = 1; i <= c ; i++) {
				tmp = StrDup(nvram_get(nvram));
				printf("tmp = %s\n", tmp);
				del = StrDup(index_str(tmp, delimiter, i));
				printf("%s[%d] = %s\n", nvram, i, del);
				if(!strcmp(del, value)){
					printf("value %s is matched in %s\n", value, nvram_get(nvram));
					nvram_set(nvram, delete_str(nvram_get(nvram), delimiter, i));
					printf("delete '%s' which is spcified at position %d\n", value, i);
					break;
				}
			}*/
			nvram_set(nvram, delete_val(nvram_get(nvram), value, delimiter));
			printf("%s\n", nvram_get(nvram));
		}
		else if(func == 7){
			tmp = StrDup(nvram_get(nvram));
			if(val_exist(tmp, value, delimiter))
				printf("%s is exist\n", value);
			else
				printf("%s is not exist\n", value);
		}
		else if(func == 8){
			tmp = StrDup(nvram_get(nvram));
			printf("%s\n", str2digits(tmp, delimiter, val));
		}
		else{
			fprintf(stderr, "%s", help_msg);
			exit(0);
		}
	}
	else if(func == 6 && delimiter != NULL && nvram != NULL){
		position = matchStrPosAt(delimiter, nvram_get(nvram), -1) + 1;
		printf("%d\n", position);
		/*printf("Number of token counts is %d\n", position);
		for(i = 1; i <= position; i++){
			tmp = StrDup(nvram_get(nvram));
			printf("\t%s\n", index_str(tmp, delimiter, i));
		}*/
	}
	else if(func == 9 && delimiter != NULL && nvram != NULL && value != NULL){
		/* this code section is fix the old delimiter(s) is a subset of new delimiter(s) issue */
		randnum = number_range(6, 16);
		tmp = StrDup(nvram_get(nvram));
		if(strstr(value, delimiter) && strlen(value) > strlen(delimiter)){
			char temp_deli[]="olddeliisasubsetofnewdeli";
			random_string(temp_deli, randnum);
			nvram_set(nvram, replaceall(tmp, delimiter, temp_deli, -2, matchStrPosAt(delimiter, tmp, -1)));
			strcpy(tmp, nvram_get(nvram));
			nvram_set(nvram, replaceall(tmp, temp_deli, value, -2, matchStrPosAt(temp_deli, tmp, -1)));
		}
		else
			nvram_set(nvram, replaceall(tmp, delimiter, value, -2, matchStrPosAt(delimiter, tmp, -1)));
		printf("%s\n", nvram_get(nvram));
	}
	else{
		fprintf(stderr, "%s", help_msg);
		exit(0);
	}

	//StrFree(tmp);
	return 0;
}
示例#3
0
/** send out response according to different http request. 
a typical workflow of auto case would be
/check_server_status						(by com-module)
/init_test									(by com-module)
/set_testcase								(by com-module)
/check_server 								(by widget)
/init_session_id?session_id=2033			(by widget)
/auto_test_task?session_id=2033				(by widget)
/check_execution_progress?session_id=2033	(by widget)
/ask_next_step?session_id=2033				(by widget)
/commit_result 								(by widget)
/auto_test_task?session_id=2033				(by widget)
/manual_cases 								(by widget)
/generate_xml 								(by widget)
/check_server_status 						(by com-module)
/get_test_result 							(by com-module)

a typical workflow of manual case would be
/check_server_status						(by com-module)
/init_test									(by com-module)
/set_testcase								(by com-module)
/check_server 								(by widget)
/init_session_id?session_id=2033			(by widget)
/auto_test_task?session_id=2033				(by widget)
/manual_cases 								(by widget)
/check_server_status 						(by com-module)
/commit_manual_result 						(by widget)
...
/check_server_status 						(by com-module)
/commit_manual_result 						(by widget)
/generate_xml 								(by widget)
/get_test_result 							(by com-module)

**/
void HttpServer::processpost(int s, struct HttpRequest *prequest)
{
	prequest->prefix = "application/json";
	string json_str = "{\"OK\":1}";

#if defined(__WIN32__) || defined(__WIN64__)
	cout << "prequest->path is:" << prequest->path << endl;
	cout << "prequest->content is:" << prequest->content << endl;
#else
	if (g_show_log) {
		DBG_ONLY("prequest->path is:" << prequest->path);
		DBG_ONLY("prequest->content is:" << prequest->content);
	}
#endif

	if (prequest->path.find("/init_test") != string::npos) {	// invoke by com-module to init some para for test
		set_cl_timeout(0);
		Json::Reader reader;
		Json::Value value;
		if (g_show_log)
			DBG_ONLY("[ init the test suite ]");

		bool parsed = reader.parse(prequest->content, value);
		if (parsed) {
			g_launcher = value["launcher"].asString();
            g_platform = value["platform"].asString();

			m_suite_name = value["suite_name"].asString();
			if (g_launcher == "wrt-launcher") {
				g_run_wiget = true;
				m_suite_id = value["suite_id"].asString();

				g_launch_cmd = g_launcher + " -s " + m_suite_id;
				g_kill_cmd = g_launcher + " -k " + m_suite_id;
				killAllWidget();
			} else if (g_platform == "androidmobile") {	// xwalk on android
				g_run_wiget = false;
				//am start -n org.xwalk.tct-2dtransforms-css3-tests/.tct-2dtransforms-css3-testsActivity
				//g_launch_cmd = "am start -n org.xwalk." + m_suite_name + "/." + m_suite_name + "Activity";
				//g_kill_cmd = "am force-stop org.xwalk." + m_suite_name;
				g_launch_cmd = "";
				g_kill_cmd = "";
			} else if (g_platform == "windowshttp"){ // xwalk on windows
				g_run_wiget = true;
				m_suite_id = value["suite_id"].asString();
				//g_launch_cmd = "C:\\\"Program Files\"\\"+m_suite_id + "\\" + g_launcher;
				g_launch_cmd = "notepad C:\\\"Program Files\"\\" + m_suite_id + "\\" + m_suite_id + "\\manifest.json";
				g_kill_cmd = "";
				sleep(1);
		    }else {
#if defined(__WIN32__) || defined(__WIN64__)
				g_launch_cmd = "start /b " + g_launcher;
				g_kill_cmd = "";
#else
				g_launch_cmd = g_launcher + " &";
				if (g_launcher.find("XWalkLauncher") != string::npos)	//kill xwalk
					g_kill_cmd = "pkill XWalkLauncher";
				//g_kill_cmd = "ps ax | grep xwalk | grep -v testkit-lite | grep -v grep | awk '{print $1F}' | xargs kill -9;";
				else
					g_kill_cmd = "";	// not kill browser
#endif
				m_suite_id = value["suite_id"].asString();
				g_run_wiget = true;
				//wait for the index window.close, otherwise will occur bind aleady error
				sleep(1);
			}
		} else {
			if (g_show_log) {
				DBG_ONLY
				    ("error while parse para from com-module, can't start test");
				DBG_ONLY(prequest->content);
			}
			json_str = "{\"Error\":\"parse error\"}";
		}
	} else if (prequest->path.find("/execute_cmd") != string::npos){  //Only work on Windows! invoke by com-module for executing command on windows platform
		if (g_show_log){
			DBG_ONLY("[ execute command: ]" << endl);
			DBG_ONLY(prequest->content);
		}
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_cmd = "";
		if (parsed){
			json_cmd = value["cmd"].asString();
		}

		std::vector < string > outputs;
		string cmd = json_cmd;
		cout << "[ command: ]" << cmd << endl;
		run_cmd(cmd, "", &outputs);
		for (unsigned int i = 0; i < outputs.size(); i++){
			cout << "[ output: ]" << outputs[i] << endl;
		}
#if defined(__WIN32__) || defined(__WIN64__)
	} else if (prequest->path.find("/powershell_install") != string::npos){
		//Only work with Windows PowerShell. JSON {suite: suitename, host: hostip, file:suitefile_file_path}
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_suite = "";
		string json_file = "";
		string json_host = "";
		if (parsed) {
			json_suite = value["suite"].asString();
			json_file = value["file"].asString();
			json_host = value["host"].asString();
		}

		string ps_cmd = "powershell -file c:\\stub\\powershell\\download.ps1 ";
		ps_cmd = ps_cmd + json_suite + " " + json_file + " " + json_host;
		cout << "[ command: ]" << ps_cmd << endl;
		int ret = run_cmd_return_code(ps_cmd);
		if(ret != 1){
			if (ret == -1)
				json_str = "{\"Error\":\"Fail to install "+ json_suite +"\"}";
			else if (ret == -2)
				json_str = "{\"Error\":\"Fail to download "+ json_file +"\"}";
			else if (ret == -5)
				json_str = "{\"Error\":\"Fail to execute "+ json_file +". Popen return null\"}";
			else
				json_str = "{\"Error\":\"Unknown script errors.\"}";
		}
	} else if (prequest->path.find("/powershell_uninstall") != string::npos){
		//Only work with Windows PowerShell. JSON {suite: suitename}
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_suite = "";
		if (parsed) {
			json_suite = value["suite"].asString();
		}

		string ps_cmd = "powershell -file c:\\stub\\powershell\\uninstall.ps1 ";
		ps_cmd = ps_cmd + json_suite;
		cout << "[ command: ]" << ps_cmd << endl;
		int ret = run_cmd_return_code(ps_cmd);
		if(ret != 1){
			json_str = "{\"Error\":\"Fail to uninstall.\"}";
		}
	} else if (prequest->path.find("/powershell_exitcode") != string::npos){
		string ps_cmd = "powershell -file c:\\stub\\powershell\\exitcode.ps1 0";
		cout << "[ command: ]" << ps_cmd << endl;
		int ret = run_cmd_return_code(ps_cmd);
		cout << "[ return: ]" << ret << endl;
#endif
	} else if (prequest->path.find("/execute_async_cmd") != string::npos) {
		Json::Reader reader;
		Json::Value value;

		bool parsed = reader.parse(prequest->content, value);
		string json_cmd = "";
		if (parsed) {
			json_cmd = value["cmd"].asString();
		}
		string cmd = json_cmd;
		run_cmd_async(cmd);
    }else if (prequest->path.find("/set_testcase") != string::npos) {	// invoke by com-module to send testcase data
		m_block_finished = false;
		m_set_finished = false;
		m_timeout_count = 0;
		m_server_checked = false;
		if (g_show_log)
			DBG_ONLY("[ set test cases ]");
		parse_json_str(prequest->content);

		set_timer(20);	// set timer here incase widget hang.
		m_check_times = 0;

		m_block_case_index = 0;
		if (m_current_block_index == 1)
			m_total_case_index = 0;
	} else if (prequest->path == "/check_server") {	// invoke by index.html to find server running or not
		set_cl_timeout(0);
		m_server_checked = true;
		m_check_times = 0;
		cancel_time_check();
		if (g_show_log)
			DBG_ONLY
			    ("[ checking server, and found the server is running ]");
	} else if (prequest->path == "/check_server_status") {	// invoke by com-module to get server status
		Json::Value status;
		status["block_finished"] = m_block_finished ? 1 : 0;
		status["finished"] = m_set_finished ? 1 : 0;
		if (m_exeType == "auto" && check_cl_timeout()) {
			status["error_code"] = 2;
			status["finished"] = 1;	//finish current set if widget is timeout
		} else if (m_failto_launch > m_max_fail_launch) {
			status["error_code"] = 2;
			status["finished"] = 1;	// finish current set if can't launch widget
		} else if (g_error_code > 0) {
			status["error_code"] = g_error_code;
			g_error_code = 0;
		}

		pthread_mutex_lock(&result_mutex);
		char count[8];
		memset(count, 0, 8);
		sprintf(count, "%d", m_result.size());
		status["count"] = count;
		status["cases"] = m_result;
		m_result.clear();
		pthread_mutex_unlock(&result_mutex);

		json_str = status.toStyledString();

		if (!m_server_checked) {
			if (g_show_log)
				DBG_ONLY
				    ("wait for widget check_server, please check on device."
				     << endl);
		}
		if (m_totalBlocks > 0) {
			if (g_show_log)
				DBG_ONLY("group: " << m_current_block_index <<
					 "/" << m_totalBlocks <<
					 ", total case: " << m_total_case_index
					 << "/" << m_totalcaseCount <<
					 ", group case: " << m_block_case_index
					 << "/" << m_block_case_count <<
					 ", m_timeout_count:" <<
					 m_timeout_count);
			if (m_exeType != "auto") {
				if (g_show_log)
					DBG_ONLY
					    ("manual cases. please check on device."
					     << endl);
			}
		}
	} else if (prequest->path == "/shut_down_server") {
		if (g_run_wiget == true)
			killAllWidget();	// kill all widget when shutdown server
		gIsRun = 0;
	} else if (prequest->path.find("/init_session_id") != string::npos) {	// invoke by index.html to record a session id
		set_cl_timeout(0);
		int index = prequest->path.find('=');
		if (index != -1) {
			m_running_session = prequest->path.substr(index + 1);
			if (g_show_log)
				DBG_ONLY("[ sessionID: " << m_running_session <<
					 " is gotten from the client ]");
		} else {
			if (g_show_log)
				DBG_ONLY("[ invalid session id ]");
		}
	} else if (prequest->path.find("/ask_next_step") != string::npos) {	// invoke by index.html to check whether there are more cases
		set_cl_timeout(0);
		if (m_block_finished || m_set_finished)
			json_str = "{\"step\":\"stop\"}";
		else
			json_str = "{\"step\":\"continue\"}";

		m_timeout_count = 0;	// reset the timeout count
	} else if (prequest->path.find("/auto_test_task") != string::npos) {	// invoke by index.html to get current auto case
		set_cl_timeout(0);
		if (m_test_cases == NULL) {
			json_str = "{\"Error\":\"no case\"}";
		} else if (m_exeType != "auto") {
			json_str = "{\"none\":0}";
		} else {
			string error_type = "";
			bool find_tc =
			    get_auto_case(prequest->content, &error_type);
			if (find_tc == false) {
				json_str = "{\"" + error_type + "\":0}";
			} else {
				json_str =
				    m_test_cases[m_block_case_index].to_json().
				    toStyledString();
			}
		}
	} else if (prequest->path.find("/manual_cases") != string::npos) {	// invoke by index.html to get all manual cases
		cancel_time_check();	// should not timeout in manual mode
		if (!m_test_cases) {
			json_str = "{\"Error\":\"no case\"}";
		} else if (m_exeType == "auto") {
			json_str = "{\"none\":0}";
		} else {
			Json::Value arrayObj;
			for (int i = 0; i < m_block_case_count; i++)
				arrayObj.append(m_test_cases[i].to_json());

			json_str = arrayObj.toStyledString();
			set_timer(60);	// check every 60 seconds to make sure widget alive when run manual cases.
		}
	} else if (prequest->path.find("/commit_manual_result") != string::npos) {	// invoke by index.html to provide result of a manual case.
		set_cl_timeout(0);
		if ((prequest->content.length() == 0) || (!m_test_cases)) {
			json_str = "{\"Error\":\"no manual result\"}";
		} else {
			find_id(splitContent(prequest->content), false);	// will set index in find_id
		}
	} else if (prequest->path.find("/check_execution_progress") != string::npos) {	//invoke by index.html to get test result of last auto case
		set_cl_timeout(0);
		char *total_count = new char[16];
		sprintf(total_count, "%d", m_totalcaseCount);
		char *current_index = new char[16];
		sprintf(current_index, "%d", m_total_case_index + 1);

		string count_str(total_count);
		string index_str(current_index);
		json_str =
		    "{\"total\":" + count_str + ",\"current\":" + index_str +
		    ",\"last_test_result\":\"" + m_last_auto_result + "\"}";

		delete[]total_count;
		delete[]current_index;
	}
	//generate_xml:from index_html, a maually block finished when click done in widget, or no manual cases anymore(when run auto block)
	else if (prequest->path == "/generate_xml") {
		set_cl_timeout(0);
		if (m_exeType != "auto") {
			cancel_time_check();
			m_block_finished = true;
			if (m_current_block_index == m_totalBlocks)
				m_set_finished = true;
		}
	}
	//from com module,when m_set_finished is true
	else if (prequest->path == "/get_test_result") {
		cancel_time_check();
		if (!m_test_cases) {
			json_str = "{\"Error\":\"no case\"}";
		} else {
			Json::Value root;
			root["count"] = "0";
			root["cases"] = m_result;	// m_result will always be empty here
			json_str = root.toStyledString();
		}
	}
	// index.html invoke this with id and result of an auto case, auto case commit result. 
	// we need find correct test case by id, and record test result to it.
	else if (prequest->path == "/commit_result") {
		set_cl_timeout(0);
		if ((prequest->content.length() == 0) || (!m_test_cases)) {
			json_str = "{\"Error\":\"no result\"}";
			m_last_auto_result = "BLOCK";
		} else {
			Json::Value paras = splitContent(prequest->content);
			if (m_running_session == paras["session_id"].asString()) {
				find_id(paras, true);
			}
		}
	} else if (prequest->path == "/set_capability") {	// by com-module to send capability data
		Json::Reader reader;

		reader.parse(prequest->content, m_capability);
	} else if (prequest->path.find("/capability") != string::npos) {	// by test suite. only one query parameter each time
		json_str = "{\"support\":0}";

		Json::Value paras = splitContent(prequest->content);
		string value = paras["value"].asString();
		string name = paras["name"].asString();
		for (unsigned int i = 0; i < name.size(); i++)
			name[i] = tolower(name[i]);

		if (m_capability[name].isBool()) {	// for bool value, omit the value part
			json_str = "{\"support\":1}";
		} else if (m_capability[name].isInt()) {
			if (m_capability[name].asInt() == atoi(value.c_str()))
				json_str = "{\"support\":1}";
		} else if (m_capability[name].isString()) {
			if (m_capability[name].asString() == value)
				json_str = "{\"support\":1}";
		}
	} else {
		json_str = "{\"Error\":\"unknown request\"}";
	}

	if (json_str != "")
		sendresponse(s, 200, prequest, json_str);

#if defined(__WIN32__) || defined(__WIN64__)
	cout << "server response is:" << json_str << endl;
#else
	if (g_show_log)
		DBG_ONLY("server response is:" << json_str);
#endif
}