diff options
author | Phil <phil.buschmann@tum.de> | 2018-08-05 22:43:35 +0200 |
---|---|---|
committer | Phil <phil.buschmann@tum.de> | 2018-08-05 22:43:35 +0200 |
commit | dc1c69e3109ccd7311a6d1f07ecc9dce0ee4c186 (patch) | |
tree | 1814039209cf3f8df15932d196f74267d5bb4d87 /src/identity | |
parent | 36a899058d6863eee242a026b282e6150b4855fa (diff) | |
download | gnunet-dc1c69e3109ccd7311a6d1f07ecc9dce0ee4c186.tar.gz gnunet-dc1c69e3109ccd7311a6d1f07ecc9dce0ee4c186.zip |
Identity REST API finished
Diffstat (limited to 'src/identity')
-rw-r--r-- | src/identity/plugin_rest_identity.c | 286 | ||||
-rwxr-xr-x | src/identity/test_plugin_rest_identity.sh | 371 |
2 files changed, 329 insertions, 328 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 83bf4075d..a518a74cc 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -265,6 +265,47 @@ do_error (void *cls) | |||
265 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 265 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
266 | } | 266 | } |
267 | 267 | ||
268 | |||
269 | |||
270 | /** | ||
271 | * Get EgoEntry from list with either a public key or a name | ||
272 | * If public key and name are not NULL, it returns the public key result first | ||
273 | * | ||
274 | * @param handle the RequestHandle | ||
275 | * @param pubkey the public key of an identity (only one can be NULL) | ||
276 | * @param name the name of an identity (only one can be NULL) | ||
277 | * @return EgoEntry or NULL if not found | ||
278 | */ | ||
279 | struct EgoEntry* | ||
280 | get_egoentry(struct RequestHandle *handle, char* pubkey, char *name) | ||
281 | { | ||
282 | struct EgoEntry *ego_entry; | ||
283 | if (NULL != pubkey) | ||
284 | { | ||
285 | for (ego_entry = handle->ego_head; | ||
286 | NULL != ego_entry; | ||
287 | ego_entry = ego_entry->next) | ||
288 | { | ||
289 | if (0 != strcasecmp (pubkey, ego_entry->keystring)) | ||
290 | continue; | ||
291 | return ego_entry; | ||
292 | } | ||
293 | } | ||
294 | if (NULL != name) | ||
295 | { | ||
296 | for (ego_entry = handle->ego_head; | ||
297 | NULL != ego_entry; | ||
298 | ego_entry = ego_entry->next) | ||
299 | { | ||
300 | if (0 != strcasecmp (name, ego_entry->identifier)) | ||
301 | continue; | ||
302 | return ego_entry; | ||
303 | } | ||
304 | } | ||
305 | return NULL; | ||
306 | } | ||
307 | |||
308 | |||
268 | /** | 309 | /** |
269 | * Callback for GET Request with subsystem | 310 | * Callback for GET Request with subsystem |
270 | * | 311 | * |
@@ -330,7 +371,6 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
330 | json_t *json_root; | 371 | json_t *json_root; |
331 | json_t *json_ego; | 372 | json_t *json_ego; |
332 | char *result_str; | 373 | char *result_str; |
333 | size_t index; | ||
334 | 374 | ||
335 | //requested default identity of subsystem | 375 | //requested default identity of subsystem |
336 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_SUBSYSTEM, | 376 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_SUBSYSTEM, |
@@ -371,14 +411,14 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
371 | keystring = GNUNET_CONTAINER_multihashmap_get ( | 411 | keystring = GNUNET_CONTAINER_multihashmap_get ( |
372 | handle->rest_handle->url_param_map, &key); | 412 | handle->rest_handle->url_param_map, &key); |
373 | 413 | ||
374 | for (ego_entry = handle->ego_head; | 414 | ego_entry = get_egoentry(handle, keystring, NULL); |
375 | NULL != ego_entry; ego_entry = ego_entry->next) | 415 | if (NULL == ego_entry) |
376 | { | 416 | { |
377 | if ((NULL != keystring) | 417 | handle->emsg = GNUNET_strdup("No identity found for public key"); |
378 | && (0 != strcmp (keystring, ego_entry->keystring))) | 418 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
379 | continue; | 419 | return; |
380 | egoname = ego_entry->identifier; | ||
381 | } | 420 | } |
421 | egoname = ego_entry->identifier; | ||
382 | } | 422 | } |
383 | 423 | ||
384 | //one identity requested with name | 424 | //one identity requested with name |
@@ -393,6 +433,12 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
393 | { | 433 | { |
394 | egoname = GNUNET_CONTAINER_multihashmap_get ( | 434 | egoname = GNUNET_CONTAINER_multihashmap_get ( |
395 | handle->rest_handle->url_param_map, &key); | 435 | handle->rest_handle->url_param_map, &key); |
436 | if (0 >= strlen(egoname)) | ||
437 | { | ||
438 | handle->emsg = GNUNET_strdup("No identity found for name"); | ||
439 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
440 | return; | ||
441 | } | ||
396 | //LOWERCASE ego names? | 442 | //LOWERCASE ego names? |
397 | GNUNET_STRINGS_utf8_tolower(egoname, egoname); | 443 | GNUNET_STRINGS_utf8_tolower(egoname, egoname); |
398 | } | 444 | } |
@@ -418,6 +464,7 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
418 | GNUNET_REST_PARAM_NAME, | 464 | GNUNET_REST_PARAM_NAME, |
419 | json_string (ego_entry->identifier)); | 465 | json_string (ego_entry->identifier)); |
420 | json_array_append (json_root, json_ego); | 466 | json_array_append (json_root, json_ego); |
467 | json_decref (json_ego); | ||
421 | } | 468 | } |
422 | 469 | ||
423 | if ((size_t) 0 == json_array_size (json_root)) | 470 | if ((size_t) 0 == json_array_size (json_root)) |
@@ -432,17 +479,13 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
432 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 479 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
433 | resp = GNUNET_REST_create_response (result_str); | 480 | resp = GNUNET_REST_create_response (result_str); |
434 | 481 | ||
435 | //delete json_objects in json_array with macro | ||
436 | json_array_foreach(json_root, index, json_ego ) | ||
437 | { | ||
438 | json_decref (json_ego); | ||
439 | } | ||
440 | json_decref (json_root); | 482 | json_decref (json_root); |
441 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 483 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
442 | GNUNET_free(result_str); | 484 | GNUNET_free(result_str); |
443 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 485 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
444 | } | 486 | } |
445 | 487 | ||
488 | |||
446 | /** | 489 | /** |
447 | * Processing finished | 490 | * Processing finished |
448 | * | 491 | * |
@@ -467,6 +510,7 @@ do_finished (void *cls, const char *emsg) | |||
467 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 510 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
468 | } | 511 | } |
469 | 512 | ||
513 | |||
470 | /** | 514 | /** |
471 | * Handle identity PUT request | 515 | * Handle identity PUT request |
472 | * | 516 | * |
@@ -475,23 +519,22 @@ do_finished (void *cls, const char *emsg) | |||
475 | * @param cls the RequestHandle | 519 | * @param cls the RequestHandle |
476 | */ | 520 | */ |
477 | void | 521 | void |
478 | ego_edit (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | 522 | ego_edit (struct GNUNET_REST_RequestHandle *con_handle, |
479 | void *cls) | 523 | const char* url, |
524 | void *cls) | ||
480 | { | 525 | { |
481 | struct RequestHandle *handle = cls; | 526 | struct RequestHandle *handle = cls; |
482 | struct EgoEntry *ego_entry; | 527 | struct EgoEntry *ego_entry; |
483 | struct EgoEntry *ego_entry_tmp; | 528 | struct EgoEntry *ego_entry_tmp; |
484 | struct MHD_Response *resp; | 529 | struct MHD_Response *resp; |
485 | json_t *subsys_json; | 530 | int json_state; |
486 | json_t *name_json; | ||
487 | json_t *key_json; | ||
488 | json_t *data_js; | 531 | json_t *data_js; |
489 | json_error_t err; | 532 | json_error_t err; |
490 | const char *keystring; | 533 | char *pubkey; |
491 | const char *subsys; | 534 | char *name; |
492 | const char *newname; | 535 | char *newsubsys; |
536 | char *newname; | ||
493 | char term_data[handle->data_size + 1]; | 537 | char term_data[handle->data_size + 1]; |
494 | int ego_exists = GNUNET_NO; | ||
495 | 538 | ||
496 | //if no data | 539 | //if no data |
497 | if (0 >= handle->data_size) | 540 | if (0 >= handle->data_size) |
@@ -504,111 +547,135 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
504 | term_data[handle->data_size] = '\0'; | 547 | term_data[handle->data_size] = '\0'; |
505 | GNUNET_memcpy(term_data, handle->data, handle->data_size); | 548 | GNUNET_memcpy(term_data, handle->data, handle->data_size); |
506 | data_js = json_loads (term_data,JSON_DECODE_ANY,&err); | 549 | data_js = json_loads (term_data,JSON_DECODE_ANY,&err); |
550 | |||
507 | if (NULL == data_js) | 551 | if (NULL == data_js) |
508 | { | 552 | { |
509 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | 553 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); |
510 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 554 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
511 | return; | 555 | return; |
512 | } | 556 | } |
513 | if (!json_is_object(data_js)) | ||
514 | { | ||
515 | json_decref (data_js); | ||
516 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
517 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
518 | return; | ||
519 | } | ||
520 | //json must contain pubkey and (subsystem or name) | ||
521 | if (2 != json_object_size (data_js)) | ||
522 | { | ||
523 | json_decref (data_js); | ||
524 | handle->emsg = GNUNET_strdup("Resource amount invalid"); | ||
525 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
526 | return; | ||
527 | } | ||
528 | 557 | ||
529 | key_json = json_object_get (data_js, GNUNET_REST_PARAM_PUBKEY); | 558 | ego_entry = NULL; |
530 | if ((NULL == key_json) || !json_is_string(key_json)) | 559 | pubkey = NULL; |
560 | name = NULL; | ||
561 | newname = NULL; | ||
562 | //NEW NAME | ||
563 | json_state = 0; | ||
564 | json_state = json_unpack(data_js, | ||
565 | "{s:s,s?:s,s?:s}", | ||
566 | GNUNET_REST_PARAM_NEWNAME, | ||
567 | &newname, | ||
568 | GNUNET_REST_PARAM_PUBKEY, | ||
569 | &pubkey, | ||
570 | GNUNET_REST_PARAM_NAME, | ||
571 | &name); | ||
572 | //Change name with pubkey or name identifier | ||
573 | if (0 == json_state) | ||
531 | { | 574 | { |
532 | json_decref (data_js); | 575 | if (NULL == newname) |
533 | handle->emsg = GNUNET_strdup("Missing element pubkey"); | 576 | { |
534 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 577 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); |
535 | return; | 578 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
536 | } | 579 | json_decref (data_js); |
537 | keystring = json_string_value (key_json); | 580 | return; |
581 | } | ||
538 | 582 | ||
539 | for (ego_entry = handle->ego_head; | 583 | if (0 >= strlen(newname)) |
540 | NULL != ego_entry; ego_entry = ego_entry->next) | 584 | { |
541 | { | 585 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); |
542 | if (0 != strcasecmp (keystring, ego_entry->keystring)) | 586 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
543 | continue; | 587 | json_decref (data_js); |
544 | ego_exists = GNUNET_YES; | 588 | return; |
545 | break; | 589 | } |
546 | } | 590 | //lower case name |
591 | GNUNET_STRINGS_utf8_tolower(newname,newname); | ||
547 | 592 | ||
548 | if (GNUNET_NO == ego_exists) | 593 | ego_entry = get_egoentry(handle,pubkey,name); |
549 | { | 594 | |
550 | json_decref (data_js); | 595 | if (NULL == ego_entry) |
551 | resp = GNUNET_REST_create_response (NULL); | ||
552 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); | ||
553 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
554 | return; | ||
555 | } | ||
556 | //This is a rename | ||
557 | name_json = json_object_get (data_js, GNUNET_REST_PARAM_NEWNAME); | ||
558 | if ((NULL != name_json) && json_is_string(name_json)) | ||
559 | { | ||
560 | newname = json_string_value (name_json); | ||
561 | if (0 >= strlen (newname)) | ||
562 | { | 596 | { |
597 | resp = GNUNET_REST_create_response (NULL); | ||
598 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); | ||
599 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
563 | json_decref (data_js); | 600 | json_decref (data_js); |
564 | handle->emsg = GNUNET_strdup("No name provided"); | ||
565 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
566 | return; | 601 | return; |
567 | } | 602 | } |
603 | |||
568 | for (ego_entry_tmp = handle->ego_head; | 604 | for (ego_entry_tmp = handle->ego_head; |
569 | NULL != ego_entry_tmp; ego_entry_tmp = ego_entry_tmp->next) | 605 | NULL != ego_entry_tmp; ego_entry_tmp = ego_entry_tmp->next) |
570 | { | 606 | { |
571 | if (0 == strcasecmp (newname, ego_entry_tmp->identifier) | 607 | if (0 == strcasecmp (newname, ego_entry_tmp->identifier)) |
572 | && 0 != strcasecmp (keystring, ego_entry_tmp->keystring)) | ||
573 | { | 608 | { |
574 | //Ego with same name not allowed | 609 | //Ego with same name not allowed |
575 | json_decref (data_js); | 610 | resp = GNUNET_REST_create_response (NULL); |
576 | resp = GNUNET_REST_create_response (NULL); | 611 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
577 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 612 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
578 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 613 | json_decref (data_js); |
579 | return; | 614 | return; |
580 | } | 615 | } |
581 | } | 616 | } |
582 | handle->response_code = MHD_HTTP_NO_CONTENT; | 617 | handle->response_code = MHD_HTTP_NO_CONTENT; |
583 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, | 618 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, |
584 | ego_entry->identifier, newname, | 619 | ego_entry->identifier, newname, |
585 | &do_finished, handle); | 620 | &do_finished, handle); |
586 | json_decref (data_js); | 621 | json_decref (data_js); |
587 | return; | 622 | return; |
588 | } | 623 | } |
589 | 624 | ||
590 | //Set subsystem | 625 | newsubsys = NULL; |
591 | subsys_json = json_object_get (data_js, GNUNET_REST_PARAM_SUBSYSTEM); | 626 | //SUBSYSTEM |
592 | if ((NULL != subsys_json) && json_is_string(subsys_json)) | 627 | json_state = 0; |
628 | json_state = json_unpack(data_js, | ||
629 | "{s:s,s?:s,s?:s}", | ||
630 | GNUNET_REST_PARAM_SUBSYSTEM, | ||
631 | &newsubsys, | ||
632 | GNUNET_REST_PARAM_PUBKEY, | ||
633 | &pubkey, | ||
634 | GNUNET_REST_PARAM_NAME, | ||
635 | &name); | ||
636 | //Change subsystem with pubkey or name identifier | ||
637 | if (0 == json_state) | ||
593 | { | 638 | { |
594 | subsys = json_string_value (subsys_json); | 639 | if (NULL == newsubsys || (NULL == pubkey && NULL == name)) |
595 | if (0 >= strlen (subsys)) | 640 | { |
641 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
642 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
643 | json_decref (data_js); | ||
644 | return; | ||
645 | } | ||
646 | |||
647 | if (0 >= strlen(newsubsys)) | ||
596 | { | 648 | { |
649 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
650 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
597 | json_decref (data_js); | 651 | json_decref (data_js); |
598 | handle->emsg = GNUNET_strdup("Invalid subsystem name"); | 652 | return; |
653 | } | ||
654 | |||
655 | ego_entry = get_egoentry(handle, pubkey, name); | ||
656 | |||
657 | if (NULL == ego_entry) | ||
658 | { | ||
659 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
599 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 660 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
661 | json_decref (data_js); | ||
600 | return; | 662 | return; |
601 | } | 663 | } |
602 | GNUNET_asprintf (&handle->subsystem, "%s", subsys); | 664 | |
603 | json_decref (data_js); | ||
604 | handle->response_code = MHD_HTTP_NO_CONTENT; | 665 | handle->response_code = MHD_HTTP_NO_CONTENT; |
605 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, handle->subsystem, | 666 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, |
606 | ego_entry->ego, &do_finished, handle); | 667 | newsubsys, |
668 | ego_entry->ego, | ||
669 | &do_finished, | ||
670 | handle); | ||
671 | json_decref (data_js); | ||
607 | return; | 672 | return; |
608 | } | 673 | } |
609 | json_decref (data_js); | 674 | |
610 | handle->emsg = GNUNET_strdup("Subsystem not provided"); | 675 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); |
611 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 676 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
677 | json_decref (data_js); | ||
678 | return; | ||
612 | } | 679 | } |
613 | 680 | ||
614 | /** | 681 | /** |
@@ -625,10 +692,10 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
625 | struct RequestHandle *handle = cls; | 692 | struct RequestHandle *handle = cls; |
626 | struct EgoEntry *ego_entry; | 693 | struct EgoEntry *ego_entry; |
627 | struct MHD_Response *resp; | 694 | struct MHD_Response *resp; |
628 | json_t *egoname_json; | ||
629 | json_t *data_js; | 695 | json_t *data_js; |
630 | json_error_t err; | 696 | json_error_t err; |
631 | const char* egoname; | 697 | char* egoname; |
698 | int json_unpack_state; | ||
632 | char term_data[handle->data_size + 1]; | 699 | char term_data[handle->data_size + 1]; |
633 | 700 | ||
634 | if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url)) | 701 | if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url)) |
@@ -649,39 +716,33 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
649 | data_js = json_loads (term_data, | 716 | data_js = json_loads (term_data, |
650 | JSON_DECODE_ANY, | 717 | JSON_DECODE_ANY, |
651 | &err); | 718 | &err); |
652 | |||
653 | if (NULL == data_js) | 719 | if (NULL == data_js) |
654 | { | 720 | { |
655 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | 721 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); |
656 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 722 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
723 | json_decref (data_js); | ||
657 | return; | 724 | return; |
658 | } | 725 | } |
659 | //instead of parse | 726 | json_unpack_state = 0; |
660 | if (!json_is_object(data_js)) | 727 | json_unpack_state = json_unpack(data_js, |
728 | "{s:s!}", | ||
729 | GNUNET_REST_PARAM_NAME, | ||
730 | &egoname); | ||
731 | if (0 != json_unpack_state) | ||
661 | { | 732 | { |
662 | json_decref (data_js); | ||
663 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | 733 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); |
664 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 734 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
665 | return; | ||
666 | } | ||
667 | |||
668 | if (1 != json_object_size (data_js)) | ||
669 | { | ||
670 | json_decref (data_js); | 735 | json_decref (data_js); |
671 | handle->emsg = GNUNET_strdup("Provided resource count invalid"); | ||
672 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
673 | return; | 736 | return; |
674 | } | 737 | } |
675 | 738 | ||
676 | egoname_json = json_object_get (data_js, GNUNET_REST_PARAM_NAME); | 739 | if (NULL == egoname) |
677 | if (!json_is_string(egoname_json)) | ||
678 | { | 740 | { |
679 | json_decref (data_js); | ||
680 | handle->emsg = GNUNET_strdup("No name provided"); | 741 | handle->emsg = GNUNET_strdup("No name provided"); |
681 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 742 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
743 | json_decref (data_js); | ||
682 | return; | 744 | return; |
683 | } | 745 | } |
684 | egoname = json_string_value (egoname_json); | ||
685 | if (0 >= strlen (egoname)) | 746 | if (0 >= strlen (egoname)) |
686 | { | 747 | { |
687 | json_decref (data_js); | 748 | json_decref (data_js); |
@@ -689,19 +750,20 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
689 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 750 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
690 | return; | 751 | return; |
691 | } | 752 | } |
753 | GNUNET_STRINGS_utf8_tolower(egoname, egoname); | ||
692 | for (ego_entry = handle->ego_head; | 754 | for (ego_entry = handle->ego_head; |
693 | NULL != ego_entry; ego_entry = ego_entry->next) | 755 | NULL != ego_entry; ego_entry = ego_entry->next) |
694 | { | 756 | { |
695 | if (0 == strcasecmp (egoname, ego_entry->identifier)) | 757 | if (0 == strcasecmp (egoname, ego_entry->identifier)) |
696 | { | 758 | { |
697 | json_decref (data_js); | ||
698 | resp = GNUNET_REST_create_response (NULL); | 759 | resp = GNUNET_REST_create_response (NULL); |
699 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 760 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
700 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 761 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
762 | json_decref (data_js); | ||
701 | return; | 763 | return; |
702 | } | 764 | } |
703 | } | 765 | } |
704 | GNUNET_asprintf (&handle->name, "%s", egoname); | 766 | handle->name = GNUNET_strdup(egoname); |
705 | json_decref (data_js); | 767 | json_decref (data_js); |
706 | handle->response_code = MHD_HTTP_CREATED; | 768 | handle->response_code = MHD_HTTP_CREATED; |
707 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, handle->name, | 769 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, handle->name, |
@@ -750,7 +812,7 @@ ego_delete (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
750 | egoname = GNUNET_CONTAINER_multihashmap_get ( | 812 | egoname = GNUNET_CONTAINER_multihashmap_get ( |
751 | handle->rest_handle->url_param_map, &key); | 813 | handle->rest_handle->url_param_map, &key); |
752 | //LOWERCASE ego names? | 814 | //LOWERCASE ego names? |
753 | GNUNET_STRINGS_utf8_tolower(egoname, egoname); | 815 | //GNUNET_STRINGS_utf8_tolower(egoname, egoname); |
754 | } | 816 | } |
755 | 817 | ||
756 | if (NULL != keystring) | 818 | if (NULL != keystring) |
@@ -769,7 +831,7 @@ ego_delete (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
769 | for (ego_entry = handle->ego_head; | 831 | for (ego_entry = handle->ego_head; |
770 | NULL != ego_entry; ego_entry = ego_entry->next) | 832 | NULL != ego_entry; ego_entry = ego_entry->next) |
771 | { | 833 | { |
772 | if (0 != strcasecmp (egoname, ego_entry->identifier)) | 834 | if (0 != strcmp (egoname, ego_entry->identifier)) |
773 | continue; | 835 | continue; |
774 | ego_exists = GNUNET_YES; | 836 | ego_exists = GNUNET_YES; |
775 | break; | 837 | break; |
diff --git a/src/identity/test_plugin_rest_identity.sh b/src/identity/test_plugin_rest_identity.sh index f391f827f..22a7a53c6 100755 --- a/src/identity/test_plugin_rest_identity.sh +++ b/src/identity/test_plugin_rest_identity.sh | |||
@@ -1,222 +1,161 @@ | |||
1 | #!/usr/bin/bash | 1 | #!/usr/bin/bash |
2 | 2 | ||
3 | #First, start gnunet-arm and the rest-service. Make sure, no identity exists | 3 | #First, start gnunet-arm and the rest-service. |
4 | #Exit 0 means success, exit 1 means failed test | 4 | #Exit 0 means success, exit 1 means failed test |
5 | 5 | ||
6 | #No test for subsystem available | 6 | #No test for subsystem available |
7 | 7 | ||
8 | link_to_api="http://localhost:7776/identity" | 8 | identity_link="http://localhost:7776/identity" |
9 | wrong_link="http://localhost:7776/idenmmmy" | 9 | wrong_link="http://localhost:7776/identityandmore" |
10 | wrong_link2="http://localhost:7776/identityandmore" | 10 | |
11 | 11 | ||
12 | #Test GET (multiple identities) for error when no identity exists | 12 | curl_get () { |
13 | #The next test case can be ignored if you have already added identities | 13 | #$1 is link |
14 | cache="$(curl --silent "$link_to_api" | grep "error")" | 14 | #$2 is grep |
15 | if [ "" == "$cache" ] | 15 | cache="$(curl -v "$1" 2>&1 | grep "$2")" |
16 | then | 16 | #echo $cache |
17 | exit 1 | 17 | if [ "" == "$cache" ] |
18 | fi | 18 | then |
19 | 19 | exit 1 | |
20 | #Test POST success code, error response code and error json | 20 | fi |
21 | #The next test case can be ignored if you have already added an identity with the name Test | 21 | } |
22 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 201")" | 22 | |
23 | if [ "" == "$cache" ] | 23 | curl_post () { |
24 | then | 24 | #$1 is link |
25 | exit 1 | 25 | #$2 is data |
26 | fi | 26 | #$3 is grep |
27 | 27 | cache="$(curl -v -X "POST" "$1" --data "$2" 2>&1 | grep "$3")" | |
28 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 409")" | 28 | #echo $cache |
29 | if [ "" == "$cache" ] | 29 | if [ "" == "$cache" ] |
30 | then | 30 | then |
31 | exit 1 | 31 | exit 1 |
32 | fi | 32 | fi |
33 | 33 | } | |
34 | cache="$(curl -v -X "POST" "$link_to_api" 2>&1 | grep "error")" | 34 | |
35 | if [ "" == "$cache" ] | 35 | curl_delete () { |
36 | then | 36 | #$1 is link |
37 | exit 1 | 37 | #$2 is grep |
38 | fi | 38 | cache="$(curl -v -X "DELETE" "$1" 2>&1 | grep "$2")" |
39 | 39 | #echo $cache | |
40 | cache="$(curl -v -X "POST" "$link_to_api" --data "wrong" 2>&1 | grep "error")" | 40 | if [ "" == "$cache" ] |
41 | if [ "" == "$cache" ] | 41 | then |
42 | then | 42 | exit 1 |
43 | exit 1 | 43 | fi |
44 | fi | 44 | } |
45 | 45 | ||
46 | cache="$(curl -v -X "POST" "$link_to_api" --data "[{}]" 2>&1 | grep "error")" | 46 | curl_put () { |
47 | if [ "" == "$cache" ] | 47 | #$1 is link |
48 | then | 48 | #$2 is data |
49 | exit 1 | 49 | #$3 is grep |
50 | fi | 50 | cache="$(curl -v -X "PUT" "$1" --data "$2" 2>&1 | grep "$3")" |
51 | 51 | #echo $cache | |
52 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\",\"other\":\"Test\"}" 2>&1 | grep "error")" | 52 | if [ "" == "$cache" ] |
53 | if [ "" == "$cache" ] | 53 | then |
54 | then | 54 | exit 1 |
55 | exit 1 | 55 | fi |
56 | fi | 56 | } |
57 | 57 | ||
58 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"nam\":\"Test\"}" 2>&1 | grep "error")" | 58 | #Test GET |
59 | if [ "" == "$cache" ] | 59 | test="$(gnunet-identity -d)" |
60 | then | 60 | #if no identity exists |
61 | exit 1 | 61 | if [ "" == "$test" ] |
62 | fi | 62 | then |
63 | 63 | curl_get "$identity_link" "error" | |
64 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":123}" 2>&1 | grep "error")" | 64 | gnunet-identity -C "test_plugin_rest_identity" |
65 | if [ "" == "$cache" ] | 65 | name="$(gnunet-identity -d | awk 'NR==1{print $1}')" |
66 | then | 66 | public="$(gnunet-identity -d | awk 'NR==1{print $3}')" |
67 | exit 1 | 67 | |
68 | fi | 68 | curl_get "${identity_link}?name=$name" "$public" |
69 | 69 | curl_get "${identity_link}?name=" "error" | |
70 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":""}" 2>&1 | grep "error")" | 70 | curl_get "${identity_link}?name=$public" "error" |
71 | if [ "" == "$cache" ] | 71 | |
72 | then | 72 | curl_get "${identity_link}?pubkey=$public" "$name" |
73 | exit 1 | 73 | curl_get "${identity_link}?pubkey=$name" "error" |
74 | fi | 74 | curl_get "${identity_link}?pubkey=" "error" |
75 | 75 | ||
76 | 76 | gnunet-identity -D "test_plugin_rest_identity" | |
77 | #Test GET (multiple identities) for success and error json | 77 | else |
78 | cache="$(curl --silent "$link_to_api" | grep "error")" | 78 | name="$(gnunet-identity -d | awk 'NR==1{print $1}')" |
79 | if [ "" != "$cache" ] | 79 | public="$(gnunet-identity -d | awk 'NR==1{print $3}')" |
80 | then | 80 | |
81 | exit 1 | 81 | curl_get "${identity_link}?name=$name" "$public" |
82 | fi | 82 | curl_get "${identity_link}?name=" "error" |
83 | 83 | curl_get "${identity_link}?name=$public" "error" | |
84 | 84 | ||
85 | id="$(gnunet-identity -d | grep "Test - " | sed "s/Test - //g")" | 85 | curl_get "${identity_link}?pubkey=$public" "$name" |
86 | #Test GET (one identity) for success and error json | 86 | curl_get "${identity_link}?pubkey=$name" "error" |
87 | #Only lowercase | 87 | curl_get "${identity_link}?pubkey=" "error" |
88 | cache="$(curl --silent "${link_to_api}?name=Test" | grep "error")" | 88 | fi |
89 | if [ "" == "$cache" ] | 89 | |
90 | then | 90 | #Test POST |
91 | exit 1 | 91 | gnunet-identity -D "test_plugin_rest_identity" > /dev/null 2>&1 |
92 | fi | 92 | gnunet-identity -D "test_plugin_rest_identity1" > /dev/null 2>&1 |
93 | #Test GET (one identity) for success and error json | 93 | |
94 | cache="$(curl --silent "${link_to_api}?pubkey=$id" | grep "error")" | 94 | curl_post "${identity_link}" '{"name":"test_plugin_rest_identity"}' "HTTP/1.1 201 Created" |
95 | if [ "" != "$cache" ] | 95 | curl_post "${identity_link}" '{"name":"test_plugin_rest_identity"}' "HTTP/1.1 409" |
96 | then | 96 | curl_post "${identity_link}" '{"name":"Test_plugin_rest_identity"}' "HTTP/1.1 409" |
97 | exit 1 | 97 | curl_post "${identity_link}" '{}' "error" |
98 | fi | 98 | curl_post "${identity_link}" '' "error" |
99 | 99 | curl_post "${identity_link}" '{"name":""}' "error" | |
100 | #Test DELETE success code, error response code and error json | 100 | curl_post "${identity_link}" '{"name":123}' "error" |
101 | #echo "Next tests for DELETE will probably fail when POST fails" | 101 | curl_post "${identity_link}" '{"name":[]}' "error" |
102 | cache="$(curl -v -X "DELETE" "${link_to_api}?pubkey=$id" 2>&1 | grep "HTTP/1.1 404")" | 102 | curl_post "${identity_link}" '{"name1":"test_plugin_rest_identity"}' "error" |
103 | if [ "" != "$cache" ] | 103 | curl_post "${identity_link}" '{"other":""}' "error" |
104 | then | 104 | curl_post "${identity_link}" '{"name":"test_plugin_rest_identity1", "other":"test_plugin_rest_identity2"}' "error" |
105 | exit 1 | 105 | |
106 | fi | 106 | #Test PUT |
107 | 107 | name="$(gnunet-identity -d | grep "test_plugin_rest_identity" | awk 'NR==1{print $1}')" | |
108 | curl --silent -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" | 108 | public="$(gnunet-identity -d | grep "test_plugin_rest_identity" | awk 'NR==1{print $3}')" |
109 | id="$(gnunet-identity -d | grep "Test - " | sed "s/Test - //g")" | 109 | |
110 | 110 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubkey":"'$public'"}' "HTTP/1.1 204" | |
111 | cache="$(curl -v -X "DELETE" "${link_to_api}?pubkey=df1" 2>&1 | grep "HTTP/1.1 404")" | 111 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubkey":"'$public'"}' "HTTP/1.1 409" |
112 | if [ "" == "$cache" ] | 112 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubkey":"'$public'xx"}' "HTTP/1.1 404" |
113 | then | 113 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubkey":""}' "HTTP/1.1 404" |
114 | exit 1 | 114 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubke":""}' "HTTP/1.1 404" |
115 | fi | 115 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubke":"","other":"sdfdsf"}' "HTTP/1.1 404" |
116 | 116 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","pubke":"","name":"sdfdsf"}' "HTTP/1.1 404" | |
117 | cache="$(curl -v -X "DELETE" "${link_to_api}?pubke=$id" 2>&1 | grep "error")" | 117 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity","pubke":"","name":"test_plugin_rest_identity1"}' "HTTP/1.1 204" |
118 | if [ "" == "$cache" ] | 118 | curl_put "${identity_link}" '{"newnam":"test_plugin_rest_identity","pubkey":"'$public'"}' "error" |
119 | then | 119 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","name":"test_plugin_rest_identity"}' "HTTP/1.1 204" |
120 | exit 1 | 120 | curl_put "${identity_link}" '{"newname":"TEST_plugin_rest_identity1","name":"test_plugin_rest_identity1"}' "HTTP/1.1 409" |
121 | fi | 121 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity1","name":"test_plugin_rest_identity1"}' "HTTP/1.1 409" |
122 | 122 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity","name":"test_plugin_rest_identityxxx"}' "HTTP/1.1 404" | |
123 | #Test PUT success code, error response codes and error json | 123 | curl_put "${identity_link}" '{"newname":"test_plugin_rest_identity","name":"test_plugin_rest_identity1"}' "HTTP/1.1 204" |
124 | cache="$(curl -v -X "PUT" "${link_to_api}" --data "{\"newname\":\"NewTest\",\"pubkey\":\"${id}\"}" 2>&1 | grep "HTTP/1.1 204")" | 124 | curl_put "${identity_link}" '{"newnam":"test_plugin_rest_identityfail","name":"test_plugin_rest_identity"}' "error" |
125 | if [ "" == "$cache" ] | 125 | |
126 | then | 126 | |
127 | exit 1 | 127 | #Test subsystem |
128 | fi | 128 | curl_put "${identity_link}" '{"subsystem":"namestore","name":"test_plugin_rest_identity"}' "HTTP/1.1 204" |
129 | 129 | curl_put "${identity_link}" '{"subsystem":"namestore","name":"test_plugin_rest_identity"}' "HTTP/1.1 204" | |
130 | cache="$(curl -v -X "PUT" "${link_to_api}" --data "{\"newname\":\"NewNewTest\",\"pubkey\":\"${id}1\"}" 2>&1 | grep "HTTP/1.1 404")" | 130 | curl_get "${identity_link}?subsystem=namestore" "test_plugin_rest_identity" |
131 | if [ "" == "$cache" ] | 131 | curl_post "${identity_link}" '{"name":"test_plugin_rest_identity1"}' "HTTP/1.1 201 Created" |
132 | then | 132 | public="$(gnunet-identity -d | grep "test_plugin_rest_identity" | awk 'NR==1{print $3}')" |
133 | exit 1 | 133 | curl_put "${identity_link}" '{"subsystem":"namestore","pubkey":"'"$public"'"}' "HTTP/1.1 204" |
134 | fi | 134 | curl_get "${identity_link}?subsystem=namestore" "test_plugin_rest_identity1" |
135 | 135 | curl_get "${identity_link}?subsystem=test_plugin_rest_identity_no_subsystem" "error" | |
136 | # feature: you can rename your identity with its own name. | 136 | curl_put "${identity_link}" '{"subsystem":"test_plugin_rest_identity_no_subsystem","name":"test_plugin_rest_identity1"}' "HTTP/1.1 204" |
137 | # cache="$(curl -v -X "PUT" "$link_to_api" --data "{\"newname\":\"NewTest\",\"pubkey\":\"${id}\"}" 2>&1 | grep "error")" | 137 | curl_get "${identity_link}?subsystem=test_plugin_rest_identity_no_subsystem" "test_plugin_rest_identity1" |
138 | # if [ "" == "$cache" ] | 138 | |
139 | # then | 139 | curl_put "${identity_link}" '{"subsyste":"test_plugin_rest_identity_no_subsystem","name":"test_plugin_rest_identity1"}' "error" |
140 | # exit 1 | 140 | curl_put "${identity_link}" '{"subsystem":"test_plugin_rest_identity_no_subsystem","name":"Test_plugin_rest_identity1"}' "HTTP/1.1 204" |
141 | # fi | 141 | |
142 | 142 | #Test DELETE | |
143 | 143 | curl_delete "${identity_link}?name=test_plugin_rest_identity" "HTTP/1.1 204" | |
144 | cache="$(curl -v -X "PUT" "$link_to_api" 2>&1 | grep "error")" | 144 | curl_get "${identity_link}?name=test_plugin_rest_identity" "error" |
145 | if [ "" == "$cache" ] | 145 | curl_delete "${identity_link}?name=TEST_plugin_rest_identity1" "HTTP/1.1 404" |
146 | then | 146 | curl_delete "${identity_link}?name=test_plugin_rest_identity1" "HTTP/1.1 204" |
147 | exit 1 | 147 | curl_get "${identity_link}?name=test_plugin_rest_identity1" "error" |
148 | fi | 148 | curl_delete "${identity_link}?name=test_plugin_rest_identity_not_found" "HTTP/1.1 404" |
149 | 149 | curl_post "${identity_link}" '{"name":"test_plugin_rest_identity1"}' "HTTP/1.1 201 Created" | |
150 | cache="$(curl -v -X "PUT" "$link_to_api" --data "wrong" 2>&1 | grep "error")" | 150 | public="$(gnunet-identity -d | grep "test_plugin_rest_identity1" | awk 'NR==1{print $3}')" |
151 | if [ "" == "$cache" ] | 151 | curl_delete "${identity_link}?pubkey=$public" "HTTP/1.1 204" |
152 | then | 152 | curl_delete "${identity_link}?pubke=$public" "error" |
153 | exit 1 | 153 | curl_delete "${identity_link}?pubkey=$public&other=232" "HTTP/1.1 404" |
154 | fi | 154 | |
155 | 155 | #Test wrong_link | |
156 | cache="$(curl -v -X "PUT" "$link_to_api" --data "[{}]" 2>&1 | grep "error")" | 156 | curl_get "$wrong_link" "HTTP/1.1 404" |
157 | if [ "" == "$cache" ] | 157 | curl_post "$wrong_link" '{"name":"test_plugin_rest_identity"}' "HTTP/1.1 404" |
158 | then | 158 | curl_put "$wrong_link" '{"newname":"test_plugin_rest_identity1","name":"test_plugin_rest_identity"}' "HTTP/1.1 404" |
159 | exit 1 | 159 | curl_delete "$wrong_link?name=test_plugin_rest_identity1" "HTTP/1.1 404" |
160 | fi | 160 | |
161 | 161 | exit 0; | |
162 | cache="$(curl -v -X "PUT" "$link_to_api" --data "{\"newname\":\"Test\",\"other\":\"Test\",\"pubkey\":\"${id}\"}" 2>&1 | grep "error")" | ||
163 | if [ "" == "$cache" ] | ||
164 | then | ||
165 | exit 1 | ||
166 | fi | ||
167 | |||
168 | cache="$(curl -v -X "PUT" "$link_to_api" --data "{\"newnam\":\"Test\",\"pubkey\":\"${id}\"}" 2>&1 | grep "error")" | ||
169 | if [ "" == "$cache" ] | ||
170 | then | ||
171 | exit 1 | ||
172 | fi | ||
173 | |||
174 | cache="$(curl -v -X "PUT" "$link_to_api" --data "{\"newname\":\"Test\",\"pubke\":\"${id}\"}" 2>&1 | grep "error")" | ||
175 | if [ "" == "$cache" ] | ||
176 | then | ||
177 | exit 1 | ||
178 | fi | ||
179 | |||
180 | cache="$(curl -v -X "PUT" "$link_to_api" --data "{\"newname\":123,\"pubkey\":\"${id}\"}" 2>&1 | grep "error")" | ||
181 | if [ "" == "$cache" ] | ||
182 | then | ||
183 | exit 1 | ||
184 | fi | ||
185 | |||
186 | cache="$(curl -v -X "PUT" "$link_to_api" --data "{\"newname\":"",\"pubkey\":\"${id}\"}" 2>&1 | grep "error")" | ||
187 | if [ "" == "$cache" ] | ||
188 | then | ||
189 | exit 1 | ||
190 | fi | ||
191 | #TODO Missing subsystem test | ||
192 | |||
193 | #Missing OPTIONS success - nothing can really go wrong here | ||
194 | |||
195 | #Test wrong url | ||
196 | cache="$(curl -v "$wrong_link" 2>&1 | grep "HTTP/1.1 404")" | ||
197 | if [ "" == "$cache" ] | ||
198 | then | ||
199 | exit 1 | ||
200 | fi | ||
201 | |||
202 | cache="$(curl -X "PUT" -v "$wrong_link" --data "{\"newname\":\"Testing\",\"pubkey\":\"${id}\"}" 2>&1 | grep "HTTP/1.1 404")" | ||
203 | if [ "" == "$cache" ] | ||
204 | then | ||
205 | exit 1 | ||
206 | fi | ||
207 | |||
208 | cache="$(curl -X "POST" -v "$wrong_link?pubkey=$id" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 404")" | ||
209 | if [ "" == "$cache" ] | ||
210 | then | ||
211 | exit 1 | ||
212 | fi | ||
213 | |||
214 | cache="$(curl -X "DELETE" -v "${wrong_link}?pubkey=$id" 2>&1 | grep "HTTP/1.1 404")" | ||
215 | if [ "" == "$cache" ] | ||
216 | then | ||
217 | exit 1 | ||
218 | fi | ||
219 | |||
220 | gnunet-identity -D NewTest | ||
221 | |||
222 | exit 0 | ||