From 9009fb0aa496cd72ff6f1ea3d20ac4a547055e1b Mon Sep 17 00:00:00 2001 From: Phil Date: Wed, 25 Jul 2018 16:59:08 +0200 Subject: [PATCH] -fix identity file --- src/identity/plugin_rest_identity.c | 59 ++--- src/identity/test_plugin_identity_rest.sh | 252 ---------------------- 2 files changed, 30 insertions(+), 281 deletions(-) delete mode 100755 src/identity/test_plugin_identity_rest.sh diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 8d525b950..f1d33b0f3 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c @@ -2,20 +2,18 @@ This file is part of GNUnet. Copyright (C) 2012-2015 GNUnet e.V. - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ /** * @author Martin Schanzenbach @@ -202,8 +200,9 @@ struct RequestHandle * @param handle Handle to clean up */ static void -cleanup_handle (struct RequestHandle *handle) +cleanup_handle (void *cls) { + struct RequestHandle *handle = cls; struct EgoEntry *ego_entry; struct EgoEntry *ego_tmp; @@ -248,20 +247,22 @@ do_error (void *cls) { struct RequestHandle *handle = cls; struct MHD_Response *resp; - char *json_error; + json_t *json_error = json_object(); + char *response; if (NULL == handle->emsg) handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_UNKNOWN); - GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg); + json_object_set_new(json_error,"error", json_string(handle->emsg)); if (0 == handle->response_code) handle->response_code = MHD_HTTP_OK; - - resp = GNUNET_REST_create_response (json_error); + response = json_dumps (json_error, 0); + resp = GNUNET_REST_create_response (response); handle->proc (handle->proc_cls, resp, handle->response_code); - cleanup_handle (handle); - GNUNET_free(json_error); + json_decref(json_error); + GNUNET_free(response); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); } /** @@ -306,7 +307,7 @@ ego_get_for_subsystem (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); GNUNET_free(result_str); GNUNET_free(public_key_string); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); } /** @@ -420,7 +421,7 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, json_decref (json_root); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); GNUNET_free(result_str); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); } /** @@ -444,7 +445,7 @@ do_finished (void *cls, const char *emsg) } resp = GNUNET_REST_create_response (NULL); handle->proc (handle->proc_cls, resp, handle->response_code); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); } /** @@ -530,7 +531,7 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, const char* url, json_decref (data_js); resp = GNUNET_REST_create_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); return; } //This is a rename @@ -555,7 +556,7 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, const char* url, json_decref (data_js); resp = GNUNET_REST_create_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); return; } } @@ -677,7 +678,7 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, json_decref (data_js); resp = GNUNET_REST_create_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); return; } } @@ -732,7 +733,7 @@ ego_delete (struct GNUNET_REST_RequestHandle *con_handle, const char* url, { resp = GNUNET_REST_create_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); return; } handle->response_code = MHD_HTTP_NO_CONTENT; @@ -760,7 +761,7 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, resp = GNUNET_REST_create_response (NULL); MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods); handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); - cleanup_handle (handle); + GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); return; } diff --git a/src/identity/test_plugin_identity_rest.sh b/src/identity/test_plugin_identity_rest.sh deleted file mode 100755 index d43f4206b..000000000 --- a/src/identity/test_plugin_identity_rest.sh +++ /dev/null @@ -1,252 +0,0 @@ -#!/usr/bin/bash - -#First, start gnunet-arm and the rest-service. Make sure, no identity exists - -link_to_api="http://localhost:7776/identity" -wrong_link="http://localhost:7776/idenmmmy" - -#Test GET (multiple identities) for error when no identity exists - -echo "No test for subsystem available" -echo "The next test case can be ignored if you have already added identities" -cache="$(curl --silent "$link_to_api" | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for GET request when missing identity\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for GET request when missing identity\n" -fi - -#Test POST success code, error response code and error json -echo "The next test case can be ignored if you have already added an identity with the name Test" -cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 201")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 409")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for duplicate name POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for duplicate name POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no data POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no data POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "wrong" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong data POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong data POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "[{}]" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for json array input POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for json array input POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\",\"other\":\"Test\"}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for multi element json POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for multi element json POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "{\"nam\":\"Test\"}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":123}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json type POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json type POST request\n" -fi - -cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":""}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no name POST request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no name POST request\n" -fi - - -#Test GET (multiple identities) for success and error json -cache="$(curl --silent "$link_to_api" | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good GET request (multiple identities)\n" -else - echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good GET request (multiple identities)\n" -fi - - -id="$(gnunet-identity -d | grep "Test - " | sed "s/Test - //g")" -#Test GET (one identity) for success and error json -cache="$(curl --silent "${link_to_api}/$id" | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good GET request (one identity)\n" -else - echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good GET request (one identity)\n" -fi - - -#Test DELETE success code, error response code and error json -echo "Next tests for DELETE will probably fail when POST fails" -cache="$(curl -v -X "DELETE" "${link_to_api}/$id" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good DELETE request\n" -else - echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good DELETE request\n" -fi - -curl --silent -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" -id="$(gnunet-identity -d | grep "Test - " | sed "s/Test - //g")" - -cache="$(curl -v -X "DELETE" "${link_to_api}/df1" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong DELETE request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong DELETE request\n" -fi - -#Test PUT success code, error response codes and error json -cache="$(curl -v -X "PUT" "${link_to_api}/$id" --data "{\"newname\":\"NewTest\"}" 2>&1 | grep "HTTP/1.1 204")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good PUT request\n" -fi - -cache="$(curl -v -X "PUT" "${link_to_api}/${id}1" --data "{\"newname\":\"NewNewTest\"}" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong identity PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong identity PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":\"NewTest\"}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for duplicate name PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for duplicate name PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no data PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no data PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "wrong" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong data PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong data PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "[{}]" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for json array input PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for json array input PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":\"Test\",\"other\":\"Test\"}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for multi element json PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for multi element json PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newnam\":\"Test\"}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":123}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json type PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json type PUT request\n" -fi - -cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":""}" 2>&1 | grep "error")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no name PUT request\n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no name PUT request\n" -fi -#TODO Missing subsystem test - -#Missing OPTIONS success - nothing can really go wrong here - -#Test wrong url -cache="$(curl -v "$wrong_link" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url GET request \n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url GET request \n" -fi - -cache="$(curl -X "PUT" -v "$wrong_link/$id" --data "{\"newname\":\"Testing\"}" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url GET request \n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url GET request \n" -fi - -cache="$(curl -X "POST" -v "$wrong_link/$id" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url POST request \n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url POST request \n" -fi - -cache="$(curl -X "DELETE" -v "${wrong_link}/$id" 2>&1 | grep "HTTP/1.1 404")" -if [ "" == "$cache" ] -then - echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url DELETE request \n" -else - echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url DELETE request \n" -fi -- 2.25.1