示例#1
0
static int extract_network_info_from_json_file(netloc_network_t *network, char *dat_file)
{
    int ret;
    json_t *json = NULL;

    const char * key = NULL;
    json_t * value = NULL;
    const char * str_val = NULL;
    char * tmp_dat_file = NULL;

    tmp_dat_file = strdup(dat_file);

    /*
     * Open the file
     */
    ret = load_json_from_file(tmp_dat_file, &json);
    if( NETLOC_SUCCESS != ret ) {
        fprintf(stderr, "Error: Failed to mmap the file.\n");
        return ret;
    }

    if( !json_is_object(json) ) {
        fprintf(stderr, "Error: json handle is not a valid object\n");
        return NETLOC_ERROR;
    }

    /*
     * Process all of the node information in the file
     */
    json_object_foreach(json, key, value) {
        str_val = json_string_value( json_object_get( value, PERL_JSON_NODE_NETWORK_TYPE ));
        network->network_type = netloc_encode_network_type(str_val);

        str_val = json_string_value( json_object_get( value, PERL_JSON_NODE_SUBNET_ID ));
        if( NULL != str_val ) {
            network->subnet_id = strdup(str_val);
        } else {
            network->subnet_id = NULL;
        }

        asprintf(&network->data_uri, "file://%s", dirname(tmp_dat_file));

        str_val = json_string_value( json_object_get( value, PERL_JSON_EDGE_DESCRIPTION ));
        if( NULL != str_val ) {
            network->description = strdup(str_val);
        } else {
            network->description = NULL;
        }

        break;
    }
示例#2
0
/**
* Do all tests according to the testing sequence
* @param test Test details
* @testpath Base path of tests
* @return TRUE when all tests were verified ok
*/
gboolean tests_conduct_tests(testcase* test, gchar* testpath) {

	if(!test || !testpath) return FALSE;

	gboolean rval = TRUE;

	// Go through the test sequence
	for(gint testidx = 0; testidx < g_slist_length(test_sequence); testidx++) {

		// Get the item in test sequence
		gchar* searchparam = g_slist_nth_data(test_sequence,testidx);
		
#ifdef G_MESSAGES_DEBUG
		g_print("Press enter to continue with test \"%s\" file id=\"%s\"",test->name,searchparam);
		gchar c = '0';
		while(c != '\n') c = getc(stdin);
#endif
		// Get the data with the searchparameter from hash table
		testfile* tfile = (testfile*)g_hash_table_find(test->files,
			(GHRFunc)find_from_hash_table, 
			searchparam);
		
		// Read any other file except Empty.json	
		if(g_strcmp0(tfile->file,"Empty.json") != 0) {
			// Create path for the file to be read
			gchar* filepath = g_strjoin("/",testpath,tfile->file,NULL);
		
			JsonParser *parser = json_parser_new();
		
			// Read json detailed by this data (stucture)
			if(!load_json_from_file(parser, filepath)) return FALSE;
			
			// Do this only for files that are sent
			if(tests_file_sending_method(tfile->method))
				tests_check_fields_from_loaded_testfile(parser, tfile, testpath);
		
			// Establish a generator to get the character representation
			JsonGenerator *generator = json_generator_new();
			json_generator_set_root(generator, json_parser_get_root(parser));
	
			// Create new jsonreply and set it to contain json as string data
			tfile->send = jsonreply_initialize();
			tfile->send->data = json_generator_to_data(generator,&(tfile->send->length));
		
			g_object_unref(generator);
			g_object_unref(parser);
		}
		
		// If path contains {id} it needs to be replaced with case id
		if(g_strrstr(tfile->path,"{id}")) {
		
			// Get case file
			testfile* temp = (testfile*)g_hash_table_find(test->files,
				(GHRFunc)find_from_hash_table, 
				"0");
			
			// Get case id
			gchar* caseid = get_value_of_member(temp->recv,"guid",NULL);
			
			if(caseid) {
				
				// Tokenize path
				gchar** split_path = g_strsplit(tfile->path,"/",5);
				
				// Go through the tokens and replace {id} with case id
				for(gint splitidx = 0; split_path[splitidx] ; splitidx++) {
					if(g_strcmp0(split_path[splitidx],"{id}") == 0) {
						g_free(split_path[splitidx]);
						split_path[splitidx] = caseid;
					}
				}
				
				// Replace the path with new
				g_free(tfile->path);
				tfile->path = g_strjoinv("/",split_path);
				g_strfreev(split_path);
			}
		}
		
		// Create url
		gchar* url = g_strjoin("/",test->URL,tfile->path,NULL);
		
#ifdef G_MESSAGES_DEBUG
		g_print("Conducting test id \"%s\"\n",test->name);
#endif
				
		// First is login, it is always first in the list
		if(testidx == 0) {
			tfile->recv = http_post(url,tfile->send,tfile->method);
			if(tfile->recv) {
				gchar* token = get_value_of_member(tfile->recv,"token",NULL);
				set_token(token);
				g_free(token);
			}
			else rval = FALSE;
		}
		
		// Case creation is second
		else if(testidx == 1) {
			tfile->recv = http_post(url,tfile->send,tfile->method);
			if(tfile->recv && verify_server_response(tfile->send,tfile->recv)) {
				g_print ("Case added correctly\n\n\n");
			}
			else rval = FALSE;
		}
		
		// From third start the tests, here the required fields are checked and replaced
		else {
			gint index = 0;
			// Go through all fields having {parent} as value
			
			// Do this only for files that are sent
			if(tests_file_sending_method(tfile->method)) {
				for(index = 0; index < g_slist_length(tfile->required); index++) {
					//replace_required_member(test->files,tfile,index);
				
					// Use new function just to add member-new value pairs to hash table
					add_required_member_value_to_list(test->files,tfile,index);
				}
			
				// Go through the list of items requiring more info
				for(gint index = 0; index < g_slist_length(tfile->moreinfo); index++) {
					//replace_getinfo_member(tfile,index,test->URL);
				
					// Use new function just to add member-new value pairs to hash table
					add_getinfo_member_value_to_list(tfile,index,test->URL);
				}
				
				// Replace all values in the jsonreply_t data using the member-value
				// pairs in the replace hash table
				set_values_of_all_members(tfile->send, tfile->replace);		
			}

			tfile->recv = http_post(url,tfile->send,tfile->method);
			
			// If there is something to verify
			if(tfile->send) {
				g_print("Verifying test id \"%s\" (file: %s):\n",tfile->id,tfile->file);
				if(verify_server_response(tfile->send,tfile->recv)) {
					g_print ("Test id \"%s\" was added correctly\n",tfile->id);
				}
				else {
					g_print("Test id \"%s\" was not added correctly\n", tfile->id);
					rval = FALSE;
				}
				g_print("\n\n");
			}
		}

		g_free(url);	
	}
	return rval;
}
示例#3
0
/**
* Checks the fields from the file loaded in parser for presence of {parent} and
* {getinfo} values. These member names containing such values are added to the
* lists of the test files to be replaced later. 
* 
* @param parser - Parser where file is loaded
* @param tfile - testfile containing details
* @param testpath - Base path to tests
*/
void tests_check_fields_from_loaded_testfile(JsonParser *parser,testfile *tfile, gchar* testpath) {
		
	gchar* filepath = g_strjoin("/",testpath,tfile->file,NULL);
	JsonReader *reader = json_reader_new (json_parser_get_root (parser));

	gchar** members = json_reader_list_members(reader);
	
	// Go through all members of this json
	for(gint membidx = 0; members[membidx] != NULL; membidx++) {

		// Get the value of current member
		gchar* membstring = get_json_member_string(reader,members[membidx]);
		
		// Requires information from other file
		if(g_strcmp0(membstring,"{parent}") == 0) {
		
			// Add member name to list
			tfile->required = g_slist_append(tfile->required,g_strdup(members[membidx]));
			
			JsonParser *par_parser = json_parser_new();
			
			// Create file offering more information
			gchar* par_infopath = g_strjoin(".",filepath,"info",members[membidx],"json",NULL);
			
			if(load_json_from_file(par_parser,par_infopath)) {
				JsonGenerator *par_generator = json_generator_new();
				json_generator_set_root(par_generator, json_parser_get_root(par_parser));
				
				// Initialize struct for the new json and store json
				jsonreply* info = jsonreply_initialize();
				info->data = json_generator_to_data(par_generator,&(info->length));
				
				// To verify that this item is in correct position in the list 
				// and corresponds to the member string location
				gint add_position = g_slist_length(tfile->required) - 1;
				tfile->reqinfo = g_slist_insert(tfile->reqinfo,info,add_position);

				g_object_unref(par_generator);
			}
			g_free(par_infopath);
			g_object_unref(par_parser);
		}
		
		// Requires more information from the server
		if(g_strcmp0(membstring,"{getinfo}") == 0) {
		
			// Add member name to list
			tfile->moreinfo = g_slist_append(tfile->moreinfo,g_strdup(members[membidx]));
			
			JsonParser *info_parser = json_parser_new();
			
			// Create path to the file offering more information
			gchar* infopath = g_strjoin(".",filepath,"getinfo",members[membidx],"json",NULL);
			
			// Load the json file
			if(load_json_from_file(info_parser,infopath)) {
				JsonGenerator *info_generator = json_generator_new();
				json_generator_set_root(info_generator, json_parser_get_root(info_parser));
				
				// Initialize struct for the new json and store json
				jsonreply* info = jsonreply_initialize();
				info->data = json_generator_to_data(info_generator,&(info->length));
				
				// To verify that this item is in correct position in the list 
				// and corresponds to the member string location
				gint add_position = g_slist_length(tfile->moreinfo) - 1;
				tfile->infosend = g_slist_insert(tfile->infosend,info,add_position);

				g_object_unref(info_generator);
			}
			g_free(infopath);
			g_object_unref(info_parser);
		}
		g_free(membstring);
	}
		
	g_strfreev(members);
	g_free(filepath);
	g_object_unref(reader);
}