Ejemplo n.º 1
0
static void enumeration_test(void) {
    char *enumContext = NULL;
    static int i = 0;
    char *selectors = NULL;


    wsmc_reinit_conn(cl);
    options = wsmc_options_init();

    options->flags = tests[i].flags;

    if (tests[i].selectors) {
        selectors =
              u_strdup_printf(tests[i].selectors, host, host, host);
         wsmc_add_selectors_from_str(options, selectors);
    }

    options->max_elements = tests[i].max_elements;
    WsXmlDocH enum_response = wsmc_action_enumerate(cl,
                                (char *)tests[i].resource_uri, options, NULL);

    CU_ASSERT_TRUE(wsmc_get_response_code(cl) == tests[i].final_status);
    if (wsmc_get_response_code(cl) != tests[i].final_status) {
        if (verbose) {
            printf("\nExpected = %d\nReturned = %ld         ",
                   tests[i].final_status, wsmc_get_response_code(cl));
        }
        goto RETURN;
    }
    CU_ASSERT_PTR_NOT_NULL(enum_response);
    if (enum_response) {
        enumContext = wsmc_get_enum_context(enum_response);
    } else {
        goto RETURN;
    }
    check_response_header(enum_response, wsmc_get_response_code(cl),
       ENUM_ACTION_ENUMERATERESPONSE);

//if (i==11) ws_xml_dump_node_tree(stdout, ws_xml_get_doc_root(enum_response));

    handle_filters(enum_response, filters);
    handle_filters(enum_response, tests[i].common_filters);
    handle_filters(enum_response, tests[i].filters);

RETURN:
    u_free(selectors);
    if (enumContext) {
        wsmc_action_release(cl,
                       (char *)tests[i].resource_uri,
                       options,
                       enumContext);
    }
    if (enum_response) {
        ws_xml_destroy_doc(enum_response);
    }
    wsmc_options_destroy(options);
    i++; // decrease executed test number
}
Ejemplo n.º 2
0
/* This function guarantees that query is freed, even on failure */
int
hypervEnumAndPull(hypervPrivate *priv, virBufferPtr query, const char *root,
                  XmlSerializerInfo *serializerInfo, const char *resourceUri,
                  const char *className, hypervObject **list)
{
    int result = -1;
    WsSerializerContextH serializerContext;
    client_opt_t *options = NULL;
    char *query_string = NULL;
    filter_t *filter = NULL;
    WsXmlDocH response = NULL;
    char *enumContext = NULL;
    hypervObject *head = NULL;
    hypervObject *tail = NULL;
    WsXmlNodeH node = NULL;
    XML_TYPE_PTR data = NULL;
    hypervObject *object;

    if (virBufferCheckError(query) < 0) {
        virBufferFreeAndReset(query);
        return -1;
    }
    query_string = virBufferContentAndReset(query);

    if (list == NULL || *list != NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
        VIR_FREE(query_string);
        return -1;
    }

    serializerContext = wsmc_get_serialization_context(priv->client);

    options = wsmc_options_init();

    if (options == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not initialize options"));
        goto cleanup;
    }

    filter = filter_create_simple(WSM_WQL_FILTER_DIALECT, query_string);

    if (filter == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not create filter"));
        goto cleanup;
    }

    response = wsmc_action_enumerate(priv->client, root, options, filter);

    if (hyperyVerifyResponse(priv->client, response, "enumeration") < 0)
        goto cleanup;

    enumContext = wsmc_get_enum_context(response);

    ws_xml_destroy_doc(response);
    response = NULL;

    while (enumContext != NULL && *enumContext != '\0') {
        response = wsmc_action_pull(priv->client, resourceUri, options,
                                    filter, enumContext);

        if (hyperyVerifyResponse(priv->client, response, "pull") < 0)
            goto cleanup;

        node = ws_xml_get_soap_body(response);

        if (node == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not lookup SOAP body"));
            goto cleanup;
        }

        node = ws_xml_get_child(node, 0, XML_NS_ENUMERATION, WSENUM_PULL_RESP);

        if (node == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not lookup pull response"));
            goto cleanup;
        }

        node = ws_xml_get_child(node, 0, XML_NS_ENUMERATION, WSENUM_ITEMS);

        if (node == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not lookup pull response items"));
            goto cleanup;
        }

        if (ws_xml_get_child(node, 0, resourceUri, className) == NULL)
            break;

        data = ws_deserialize(serializerContext, node, serializerInfo,
                              className, resourceUri, NULL, 0, 0);

        if (data == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Could not deserialize pull response item"));
            goto cleanup;
        }

        if (VIR_ALLOC(object) < 0)
            goto cleanup;

        object->serializerInfo = serializerInfo;
        object->data = data;

        data = NULL;

        if (head == NULL) {
            head = object;
        } else {
            tail->next = object;
        }

        tail = object;

        VIR_FREE(enumContext);
        enumContext = wsmc_get_enum_context(response);

        ws_xml_destroy_doc(response);
        response = NULL;
    }

    *list = head;
    head = NULL;

    result = 0;

 cleanup:
    if (options != NULL)
        wsmc_options_destroy(options);

    if (filter != NULL)
        filter_destroy(filter);

    if (data != NULL) {
#if WS_SERIALIZER_FREE_MEM_WORKS
        /* FIXME: ws_serializer_free_mem is broken in openwsman <= 2.2.6,
         *        see hypervFreeObject for a detailed explanation. */
        if (ws_serializer_free_mem(serializerContext, data,
                                   serializerInfo) < 0) {
            VIR_ERROR(_("Could not free deserialized data"));
        }
#endif
    }

    VIR_FREE(query_string);
    ws_xml_destroy_doc(response);
    VIR_FREE(enumContext);
    hypervFreeObject(priv, head);

    return result;
}
Ejemplo n.º 3
0
int main(int argc, char** argv)
{
	int i;
	WsManClient *cl;
	WsXmlDocH docp;
	client_opt_t *options = NULL;
	char *enumContext = NULL;
	//unsigned int id = 0;

	//wsman_debug_set_level(DEBUG_LEVEL_DEBUG);
    initialize_logging();
	//wsmc_add_handler(wsmc_handler, NULL);

	for (i = 0; i < ntests; i++)
	{
		printf ("Test %d: %s:", i + 1, tests[i].explanation);
		//printf ("------------------------------------------------\n");

    	cl = wsmc_create(
    		sd[0].server,
    		sd[0].port,
    		sd[0].path,
    		sd[0].scheme,
    		sd[0].username,
    		sd[0].password);
	wsmc_transport_init(cl, NULL);

		options = wsmc_options_init();
		options->flags = tests[i].flags;
		options->max_elements = tests[i].max_elements;
		if (tests[i].selectors != NULL)
			wsmc_add_selectors_from_str (options, tests[i].selectors);

		WsXmlDocH enum_response = wsmc_action_enumerate(cl, (char *)tests[i].resource_uri ,
			 options, NULL);
		if (!enum_response) {
               printf("\t\t\033[22;31mUNRESOLVED\033[m\n");
               goto CONTINUE;
        }			//wsman_output(enum_response);
		if ((char *)tests[i].expected_value != NULL) {
			    char *xp = ws_xml_get_xpath_value(enum_response,
                                   (char *)tests[i].xpath_expression);
			    if (xp) {
                    if (strcmp(xp,(char *)tests[i].expected_value ) == 0)
                         printf("\t\t\033[22;32mPASSED\033[m\n");
                     else
                         printf("\t\t\033[22;31mFAILED\033[m\n");
                    u_free(xp);
			    }
		}
		wsmc_free_enum_context(enumContext);
		enumContext = wsmc_get_enum_context(enum_response);
		ws_xml_destroy_doc(enum_response);

		while (enumContext != NULL)
		{
			docp = wsmc_action_pull(cl, (char *)tests[i].resource_uri,
                             options, NULL, enumContext);
            if (!docp) {
                printf("\t\t\033[22;31mUNRESOLVED\033[m\n");
                goto CONTINUE;
            }
			wsman_output(docp);
			wsmc_free_enum_context(enumContext);
			enumContext = wsmc_get_enum_context(docp);
			ws_xml_destroy_doc(docp);
		}
CONTINUE:
		wsmc_options_destroy(options);
    	wsmc_release(cl);
	}

	return 0;
}
Ejemplo n.º 4
0
int Redirect_Enumerate_EP(WsContextH cntx, 
			WsEnumerateInfo* enumInfo,
			WsmanStatus *status, void *opaqueData)
{

    WsXmlNodeH r_header=NULL, r_node=NULL, r_body=NULL, r_opt=NULL;
    WsXmlDocH r_response=NULL;
    char *resource_uri, *remote_enumContext;
    int op; 
    WsManClient *cl=NULL;


    //The redirected Enumeration request must have RequestTotalItemsCountEstimate enabled

    r_header = ws_xml_get_soap_header(cntx->indoc);
    if ( (r_node = ws_xml_get_child(r_header,0,XML_NS_WS_MAN, WSM_REQUEST_TOTAL )) == NULL )
	    ws_xml_add_child(r_header, XML_NS_WS_MAN, WSM_REQUEST_TOTAL, NULL);     


    cl = setup_redirect_client(cntx,  enumInfo->auth_data.username, enumInfo->auth_data.password);


    //Set the enumInfo flags based on the indoc. This is required while handling the response in wsenum_eunmerate_stub
    r_body=ws_xml_get_soap_body(cntx->indoc);
    if ( ( r_node = ws_xml_get_child(r_body ,0, XML_NS_ENUMERATION, WSENUM_ENUMERATE )) != NULL )
    {
	    if ( (r_opt = ws_xml_get_child(r_node,0,XML_NS_WS_MAN,WSM_OPTIMIZE_ENUM )) != NULL )
		    enumInfo->flags |= WSMAN_ENUMINFO_OPT ;

    }


    wsman_send_request(cl,cntx->indoc);

    if (wsmc_get_last_error(cl) != WS_LASTERR_OK ){
	//CURL or HTTP errors
	enumInfo->pullResultPtr = NULL;
	status->fault_code = WSMAN_INTERNAL_ERROR;
	status->fault_detail_code = 0;
	status->fault_msg = redirect_fault_msg( wsman_transport_get_last_error_string(  wsmc_get_last_error(cl) )  );
            return 1;
    }




    r_response = ws_xml_duplicate_doc(wsmc_build_envelope_from_response(cl));

 
    if (  wsman_is_fault_envelope(r_response)){
        enumInfo->pullResultPtr = NULL;
        wsman_get_fault_status_from_doc(r_response, status);
	return 1;
    }
 


    //Get the Estimated Total No.of Items from the response.
    r_header=ws_xml_get_soap_header(r_response);
    r_node=ws_xml_get_child(r_header,0,XML_NS_WS_MAN, WSM_TOTAL_ESTIMATE );
    enumInfo->totalItems=(!r_node) ? 0: atoi(ws_xml_get_node_text(r_node));


    //Get the remote context
    remote_enumContext = wsmc_get_enum_context(r_response);



    //Set the pullResultPtr only if some Enum Items are returned, in optimized mode.
    r_body= ws_xml_get_soap_body(r_response);

    if (  (r_node = ws_xml_get_child(r_body,0,XML_NS_ENUMERATION, WSENUM_ENUMERATE_RESP )) != NULL && 
			( ws_xml_get_child(r_node,0,XML_NS_WS_MAN,WSENUM_ITEMS) != NULL)  )
    
    {

	enumInfo->pullResultPtr = r_response; 

	if( strlen(remote_enumContext) != 0 )
	    strncpy(enumInfo->enumId, remote_enumContext, strlen(remote_enumContext)+1);
    
	else  // If all the instances are returned, the context will be NULL
	    enumInfo->enumId[0]='\0';
	    
    }
    
    else{
	    //If not items are returned, set the context and return. 
	    strncpy(enumInfo->enumId, remote_enumContext, strlen(remote_enumContext)+1);
	    ws_xml_destroy_doc(r_response);
	
    }
    
    wsmc_release(cl);

    
    return 0;

}