/* This file is part of TALER Copyright (C) 2014-2018 Taler Systems SA TALER 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. TALER 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 TALER; see the file COPYING. If not, see */ /** * @file testing_api_cmd_post_orders.c * @brief command to run POST /orders * @author Marcello Stanisci */ #include "platform.h" #include #include #include "taler_merchant_service.h" #include "taler_merchant_testing_lib.h" /** * State for a "POST /orders" CMD. */ struct OrdersState { /** * The order. */ char *order; /** * Expected status code. */ unsigned int http_status; /** * Order id. */ const char *order_id; /** * The order id we expect the merchant to assign (if not NULL). */ const char *expected_order_id; /** * Contract terms obtained from the backend. */ json_t *contract_terms; /** * Contract terms hash code. */ struct GNUNET_HashCode h_contract_terms; /** * The /orders operation handle. */ struct TALER_MERCHANT_PostOrdersOperation *po; /** * The (initial) POST /orders/$ID/claim operation handle. * The logic is such that after an order creation, * we immediately claim the order. */ struct TALER_MERCHANT_OrderClaimHandle *och; /** * The nonce. */ struct GNUNET_CRYPTO_EddsaPublicKey nonce; /** * Whether to generate a claim token. */ bool make_claim_token; /** * The claim token */ struct TALER_ClaimTokenP claim_token; /** * URL of the merchant backend. */ const char *merchant_url; /** * The interpreter state. */ struct TALER_TESTING_Interpreter *is; /** * Merchant signature over the orders. */ struct TALER_MerchantSignatureP merchant_sig; /** * Merchant public key. */ struct TALER_MerchantPublicKeyP merchant_pub; /** * The payment target for the order */ const char *payment_target; /** * The products the order is purchasing. */ const char *products; /** * The locks that the order should release. */ const char *locks; /** * Should the command also CLAIM the order? */ bool with_claim; /** * If not NULL, the command should duplicate the request and verify the * response is the same as in this command. */ const char *duplicate_of; }; /** * Offer internal data to other commands. * * @param cls closure * @param ret[out] result (could be anything) * @param trait name of the trait * @param index index number of the object to extract. * @return #GNUNET_OK on success */ static int orders_traits (void *cls, const void **ret, const char *trait, unsigned int index) { struct OrdersState *ps = cls; struct TALER_TESTING_Trait traits[] = { TALER_TESTING_make_trait_order_id (0, ps->order_id), TALER_TESTING_make_trait_contract_terms (0, ps->contract_terms), TALER_TESTING_make_trait_h_contract_terms (0, &ps->h_contract_terms), TALER_TESTING_make_trait_merchant_sig (0, &ps->merchant_sig), TALER_TESTING_make_trait_merchant_pub (0, &ps->merchant_pub), TALER_TESTING_make_trait_claim_nonce (0, &ps->nonce), TALER_TESTING_make_trait_claim_token (0, &ps->claim_token), TALER_TESTING_make_trait_string (0, ps->order), TALER_TESTING_trait_end () }; return TALER_TESTING_get_trait (traits, ret, trait, index); } /** * Used to fill the "orders" CMD state with backend-provided * values. Also double-checks that the order was correctly * created. * * @param cls closure * @param hr HTTP response we got * @param sig merchant's signature * @param hash hash over the contract */ static void orders_claim_cb (void *cls, const struct TALER_MERCHANT_HttpResponse *hr, const json_t *contract_terms, const struct TALER_MerchantSignatureP *sig, const struct GNUNET_HashCode *hash) { struct OrdersState *ps = cls; struct TALER_MerchantPublicKeyP merchant_pub; const char *error_name; unsigned int error_line; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed_auto ("merchant_pub", &merchant_pub), GNUNET_JSON_spec_end () }; ps->och = NULL; if (ps->http_status != hr->http_status) TALER_TESTING_FAIL (ps->is); ps->contract_terms = json_deep_copy (contract_terms); ps->h_contract_terms = *hash; ps->merchant_sig = *sig; if (GNUNET_OK != GNUNET_JSON_parse (contract_terms, spec, &error_name, &error_line)) { char *log; GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Parser failed on %s:%u\n", error_name, error_line); log = json_dumps (ps->contract_terms, JSON_INDENT (1)); fprintf (stderr, "%s\n", log); free (log); TALER_TESTING_FAIL (ps->is); } ps->merchant_pub = merchant_pub; TALER_TESTING_interpreter_next (ps->is); } /** * Callback that processes the response following a * POST /orders. NOTE: no contract terms are included * here; they need to be taken via the "orders lookup" * method. * * @param cls closure. * @param hr HTTP response * @param order_id order id of the orders. */ static void order_cb (void *cls, const struct TALER_MERCHANT_HttpResponse *hr, const char *order_id, const struct TALER_ClaimTokenP *claim_token) { struct OrdersState *ps = cls; ps->po = NULL; if (NULL != claim_token) ps->claim_token = *claim_token; if (ps->http_status != hr->http_status) { TALER_LOG_ERROR ("Given vs expected: %u(%d) vs %u\n", hr->http_status, (int) hr->ec, ps->http_status); TALER_TESTING_FAIL (ps->is); } if (0 == ps->http_status) { TALER_LOG_DEBUG ("/orders, expected 0 status code\n"); TALER_TESTING_interpreter_next (ps->is); return; } switch (hr->http_status) { case MHD_HTTP_OK: ps->order_id = GNUNET_strdup (order_id); if ((NULL != ps->expected_order_id) && (0 != strcmp (order_id, ps->expected_order_id))) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Order id assigned does not match\n"); TALER_TESTING_interpreter_fail (ps->is); return; } if (NULL != ps->duplicate_of) { const struct TALER_TESTING_Command *order_cmd; const struct TALER_ClaimTokenP *prev_token; struct TALER_ClaimTokenP zero_token = {0}; order_cmd = TALER_TESTING_interpreter_lookup_command ( ps->is, ps->duplicate_of); if (GNUNET_OK != TALER_TESTING_get_trait_claim_token (order_cmd, 0, &prev_token)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not fetch previous order claim token\n"); TALER_TESTING_interpreter_fail (ps->is); return; } if (NULL == claim_token) prev_token = &zero_token; if (0 != GNUNET_memcmp (prev_token, claim_token)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Claim tokens for identical requests do not match\n"); TALER_TESTING_interpreter_fail (ps->is); return; } } break; default: { char *s = json_dumps (hr->reply, JSON_COMPACT); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected status code from /orders: %u (%d) at %s; JSON: %s\n", hr->http_status, hr->ec, TALER_TESTING_interpreter_get_current_label (ps->is), s); GNUNET_free (s); /** * Not failing, as test cases are _supposed_ * to create non 200 OK situations. */ TALER_TESTING_interpreter_next (ps->is); } return; } if (false == ps->with_claim) { TALER_TESTING_interpreter_next (ps->is); return; } if (NULL == (ps->och = TALER_MERCHANT_order_claim (ps->is->ctx, ps->merchant_url, ps->order_id, &ps->nonce, &ps->claim_token, &orders_claim_cb, ps))) TALER_TESTING_FAIL (ps->is); } /** * Run a "orders" CMD. * * @param cls closure. * @param cmd command currently being run. * @param is interpreter state. */ static void orders_run (void *cls, const struct TALER_TESTING_Command *cmd, struct TALER_TESTING_Interpreter *is) { struct OrdersState *ps = cls; json_t *order; json_error_t error; ps->is = is; order = json_loads (ps->order, JSON_REJECT_DUPLICATES, &error); if (NULL == order) { // human error here. GNUNET_break (0); fprintf (stderr, "%s\n", error.text); TALER_TESTING_interpreter_fail (is); return; } if (NULL == json_object_get (order, "order_id")) { struct GNUNET_TIME_Absolute now; char *order_id; // FIXME: should probably use get_monotone() to ensure uniqueness! now = GNUNET_TIME_absolute_get (); order_id = GNUNET_STRINGS_data_to_string_alloc (&now.abs_value_us, sizeof (now.abs_value_us)); json_object_set_new (order, "order_id", json_string (order_id)); GNUNET_free (order_id); } GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &ps->nonce, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); ps->po = TALER_MERCHANT_orders_post (is->ctx, ps->merchant_url, order, GNUNET_TIME_UNIT_ZERO, &order_cb, ps); json_decref (order); GNUNET_assert (NULL != ps->po); } /** * Run a "orders" CMD. * * @param cls closure. * @param cmd command currently being run. * @param is interpreter state. */ static void orders_run2 (void *cls, const struct TALER_TESTING_Command *cmd, struct TALER_TESTING_Interpreter *is) { struct OrdersState *ps = cls; const char *order_str = ps->order; json_t *order; json_error_t error; char *products_string = GNUNET_strdup (ps->products); char *locks_string = GNUNET_strdup (ps->locks); char *token; struct TALER_MERCHANT_InventoryProduct *products = NULL; unsigned int products_length = 0; struct GNUNET_Uuid *locks = NULL; unsigned int locks_length = 0; ps->is = is; if (NULL != ps->duplicate_of) { const struct TALER_TESTING_Command *order_cmd; order_cmd = TALER_TESTING_interpreter_lookup_command ( is, ps->duplicate_of); if (GNUNET_OK != TALER_TESTING_get_trait_string (order_cmd, 0, &order_str)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not fetch previous order string\n"); TALER_TESTING_interpreter_fail (is); return; } } order = json_loads (order_str, JSON_REJECT_DUPLICATES, &error); if (NULL == order) { // human error here. GNUNET_break (0); fprintf (stderr, "%s\n", error.text); TALER_TESTING_interpreter_fail (is); return; } if (NULL == json_object_get (order, "order_id")) { struct GNUNET_TIME_Absolute now; char *order_id; // FIXME: should probably use get_monotone() to ensure uniqueness! now = GNUNET_TIME_absolute_get (); order_id = GNUNET_STRINGS_data_to_string_alloc (&now.abs_value_us, sizeof (now.abs_value_us)); json_object_set_new (order, "order_id", json_string (order_id)); GNUNET_free (order_id); } GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, &ps->nonce, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); for (token = strtok (products_string, ";"); NULL != token; token = strtok (NULL, ";")) { char *ctok; struct TALER_MERCHANT_InventoryProduct pd; /* Token syntax is "[product_id]/[quantity]" */ ctok = strchr (token, '/'); if (NULL != ctok) { *ctok = '\0'; ctok++; if (1 != sscanf (ctok, "%u", &pd.quantity)) { GNUNET_break (0); break; } } else { pd.quantity = 1; } pd.product_id = token; GNUNET_array_append (products, products_length, pd); } for (token = strtok (locks_string, ";"); NULL != token; token = strtok (NULL, ";")) { const struct TALER_TESTING_Command *lock_cmd; struct GNUNET_Uuid *uuid; lock_cmd = TALER_TESTING_interpreter_lookup_command ( is, token); if (GNUNET_OK != TALER_TESTING_get_trait_uuid (lock_cmd, 0, &uuid)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not fetch lock uuid\n"); TALER_TESTING_interpreter_fail (is); return; } GNUNET_array_append (locks, locks_length, *uuid); } ps->po = TALER_MERCHANT_orders_post2 (is->ctx, ps->merchant_url, order, GNUNET_TIME_UNIT_ZERO, ps->payment_target, products_length, products, locks_length, locks, ps->make_claim_token, &order_cb, ps); json_decref (order); GNUNET_free (products_string); GNUNET_free (locks_string); GNUNET_array_grow (products, products_length, 0); GNUNET_array_grow (locks, locks_length, 0); GNUNET_assert (NULL != ps->po); } /** * Free the state of a "orders" CMD, and possibly * cancel it if it did not complete. * * @param cls closure. * @param cmd command being freed. */ static void orders_cleanup (void *cls, const struct TALER_TESTING_Command *cmd) { struct OrdersState *ps = cls; if (NULL != ps->po) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Command '%s' did not complete (orders put)\n", cmd->label); TALER_MERCHANT_orders_post_cancel (ps->po); ps->po = NULL; } if (NULL != ps->och) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Command '%s' did not complete" " (orders lookup)\n", cmd->label); TALER_MERCHANT_order_claim_cancel (ps->och); ps->och = NULL; } json_decref (ps->contract_terms); GNUNET_free (ps->order); GNUNET_free_nz ((void *) ps->order_id); GNUNET_free (ps); } /** * Mark part of the contract terms as possible to forget. * * @param cls pointer to the result of the forget operation. * @param object_id name of the object to forget. * @param parent parent of the object at @e object_id. */ static void mark_forgettable (void *cls, const char *object_id, json_t *parent) { GNUNET_assert (GNUNET_OK == TALER_JSON_contract_mark_forgettable (parent, object_id)); } /** * Constructs the json for a POST order request. * * @param order_id the name of the order to add. * @param refund_deadline the deadline for refunds on this order. * @param pay_deadline the deadline for payment on this order. * @param amount the amount this order is for. * @param order[out] where to write the json string. */ static void make_order_json (const char *order_id, struct GNUNET_TIME_Absolute refund_deadline, struct GNUNET_TIME_Absolute pay_deadline, const char *amount, char **order) { struct GNUNET_TIME_Absolute refund = refund_deadline; struct GNUNET_TIME_Absolute pay = pay_deadline; json_t *contract_terms; GNUNET_TIME_round_abs (&refund); GNUNET_TIME_round_abs (&pay); /* Include required fields and some dummy objects to test forgetting. */ contract_terms = json_pack ( "{s:s, s:s?, s:s, s:s, s:o, s:o, s:s, s:[{s:s}, {s:s}, {s:s}]}", "summary", "merchant-lib testcase", "order_id", order_id, "amount", amount, "fulfillment_url", "https://example.com", "refund_deadline", GNUNET_JSON_from_time_abs (refund), "pay_deadline", GNUNET_JSON_from_time_abs (pay), "dummy_obj", "EUR:1.0", "dummy_array", /* For testing forgetting parts of arrays */ "item", "speakers", "item", "headphones", "item", "earbuds" ); GNUNET_assert (GNUNET_OK == TALER_JSON_expand_path (contract_terms, "$.dummy_obj", &mark_forgettable, NULL)); GNUNET_assert (GNUNET_OK == TALER_JSON_expand_path (contract_terms, "$.dummy_array[*].item", &mark_forgettable, NULL)); *order = json_dumps (contract_terms, 0); json_decref (contract_terms); } /** * Make the "proposal" command AVOIDING claiming the order. * * @param label command label * @param merchant_url base URL of the merchant serving * the proposal request. * @param http_status expected HTTP status. * @param order_id the name of the order to add. * @param refund_deadline the deadline for refunds on this order. * @param pay_deadline the deadline for payment on this order. * @param amount the amount this order is for. * @return the command */ struct TALER_TESTING_Command TALER_TESTING_cmd_merchant_post_orders_no_claim (const char *label, const char *merchant_url, unsigned int http_status, const char *order_id, struct GNUNET_TIME_Absolute refund_deadline, struct GNUNET_TIME_Absolute pay_deadline, const char *amount) { struct OrdersState *ps; ps = GNUNET_new (struct OrdersState); make_order_json (order_id, refund_deadline, pay_deadline, amount, &ps->order); ps->http_status = http_status; ps->expected_order_id = order_id; ps->merchant_url = merchant_url; ps->with_claim = false; { struct TALER_TESTING_Command cmd = { .cls = ps, .label = label, .run = &orders_run, .cleanup = &orders_cleanup, .traits = &orders_traits }; return cmd; } } /** * Make the "proposal" command. * * @param label command label * @param merchant_url base URL of the merchant serving * the proposal request. * @param http_status expected HTTP status. * @param order_id the name of the order to add. * @param refund_deadline the deadline for refunds on this order. * @param pay_deadline the deadline for payment on this order. * @param amount the amount this order is for. * @return the command */ struct TALER_TESTING_Command TALER_TESTING_cmd_merchant_post_orders (const char *label, const char *merchant_url, unsigned int http_status, const char *order_id, struct GNUNET_TIME_Absolute refund_deadline, struct GNUNET_TIME_Absolute pay_deadline, const char *amount) { struct OrdersState *ps; ps = GNUNET_new (struct OrdersState); make_order_json (order_id, refund_deadline, pay_deadline, amount, &ps->order); ps->http_status = http_status; ps->expected_order_id = order_id; ps->merchant_url = merchant_url; ps->with_claim = true; { struct TALER_TESTING_Command cmd = { .cls = ps, .label = label, .run = &orders_run, .cleanup = &orders_cleanup, .traits = &orders_traits }; return cmd; } } /** * Make the "proposal" command. * * @param label command label * @param merchant_url base URL of the merchant serving * the proposal request. * @param http_status expected HTTP status. * @param order_id the name of the order to add. * @param refund_deadline the deadline for refunds on this order. * @param pay_deadline the deadline for payment on this order. * @param claim_token whether to generate a claim token. * @param amount the amount this order is for. * @param payment_target payment target for the order. * @param products a string indicating the products this order will be * purchasing. Should be formatted as * "[product_id]/[quantity];...". * @param locks a string of references to lock product commands that should * be formatted as "[lock_1];[lock_2];...". * @param duplicate_of if not NULL, a reference to a previous order command * that should be duplicated and checked for an identical response. * @return the command */ struct TALER_TESTING_Command TALER_TESTING_cmd_merchant_post_orders2 (const char *label, const char *merchant_url, unsigned int http_status, const char *order_id, struct GNUNET_TIME_Absolute refund_deadline, struct GNUNET_TIME_Absolute pay_deadline, bool claim_token, const char *amount, const char *payment_target, const char *products, const char *locks, const char *duplicate_of) { struct OrdersState *ps; ps = GNUNET_new (struct OrdersState); make_order_json (order_id, refund_deadline, pay_deadline, amount, &ps->order); ps->http_status = http_status; ps->expected_order_id = order_id; ps->merchant_url = merchant_url; ps->payment_target = payment_target; ps->products = products; ps->locks = locks; ps->with_claim = (NULL == duplicate_of); ps->make_claim_token = claim_token; ps->duplicate_of = duplicate_of; { struct TALER_TESTING_Command cmd = { .cls = ps, .label = label, .run = &orders_run2, .cleanup = &orders_cleanup, .traits = &orders_traits }; return cmd; } }