diff options
Diffstat (limited to 'src/bank-lib')
| -rw-r--r-- | src/bank-lib/fakebank.c | 113 | ||||
| -rw-r--r-- | src/bank-lib/test_bank_interpreter.c | 50 | 
2 files changed, 105 insertions, 58 deletions
diff --git a/src/bank-lib/fakebank.c b/src/bank-lib/fakebank.c index ceda4d37..34a11f9b 100644 --- a/src/bank-lib/fakebank.c +++ b/src/bank-lib/fakebank.c @@ -65,7 +65,7 @@ struct Transaction    /**     * Subject of the transfer.     */ -  struct TALER_WireTransferIdentifierRawP wtid; +  char *subject;    /**     * Base URL of the exchange. @@ -119,7 +119,7 @@ struct TALER_FAKEBANK_Handle  /**   * Check that the @a want_amount was transferred from   * the @a want_debit to the @a want_credit account.  If - * so, set the @a wtid to the transfer identifier. + * so, set the @a subject to the transfer identifier.   * If not, return #GNUNET_SYSERR.   *   * @param h bank instance @@ -137,11 +137,9 @@ TALER_FAKEBANK_check (struct TALER_FAKEBANK_Handle *h,                        uint64_t want_debit,                        uint64_t want_credit,                        const char *exchange_base_url, -                      struct TALER_WireTransferIdentifierRawP *wtid) +                      char **subject)  { -  struct Transaction *t; - -  for (t = h->transactions_head; NULL != t; t = t->next) +  for (struct Transaction *t = h->transactions_head; NULL != t; t = t->next)    {      if ( (want_debit == t->debit_account) &&           (want_credit == t->credit_account) && @@ -153,7 +151,7 @@ TALER_FAKEBANK_check (struct TALER_FAKEBANK_Handle *h,        GNUNET_CONTAINER_DLL_remove (h->transactions_head,                                     h->transactions_tail,                                     t); -      *wtid = t->wtid; +      *subject = t->subject;        GNUNET_free (t->exchange_base_url);        GNUNET_free (t);        return GNUNET_OK; @@ -161,7 +159,7 @@ TALER_FAKEBANK_check (struct TALER_FAKEBANK_Handle *h,    }    fprintf (stderr,             "Did not find matching transaction!\nI have:\n"); -  for (t = h->transactions_head; NULL != t; t = t->next) +  for (struct Transaction *t = h->transactions_head; NULL != t; t = t->next)    {      char *s; @@ -179,6 +177,49 @@ TALER_FAKEBANK_check (struct TALER_FAKEBANK_Handle *h,  /** + * Tell the fakebank to create another wire transfer. + * + * @param h fake bank handle + * @param debit_account account to debit + * @param credit_account account to credit + * @param amount amount to transfer + * @param subject wire transfer subject to use + * @param exchange_base_url exchange URL + * @return serial_id of the transfer + */ +uint64_t +TALER_FAKEBANK_make_transfer (struct TALER_FAKEBANK_Handle *h, +                              uint64_t debit_account, +                              uint64_t credit_account, +                              const struct TALER_Amount *amount, +                              const char *subject, +                              const char *exchange_base_url) +{ +  struct Transaction *t; + +  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, +              "Making transfer from %llu to %llu over %s and subject %s\n", +              (unsigned long long) debit_account, +              (unsigned long long) credit_account, +              TALER_amount2s (amount), +              subject); +  t = GNUNET_new (struct Transaction); +  t->debit_account = debit_account; +  t->credit_account = credit_account; +  t->amount = *amount; +  t->exchange_base_url = GNUNET_strdup (exchange_base_url); +  t->serial_id = ++h->serial_counter; +  t->date = GNUNET_TIME_absolute_get (); +  t->subject = GNUNET_strdup (subject); +  GNUNET_TIME_round_abs (&t->date); +  GNUNET_CONTAINER_DLL_insert_tail (h->transactions_head, +                                    h->transactions_tail, +                                    t); +  return t->serial_id; +} + + +/**   * Check that no wire transfers were ordered (or at least none   * that have not been taken care of via #TALER_FAKEBANK_check()).   * If any transactions are onrecord, return #GNUNET_SYSERR. @@ -228,6 +269,7 @@ TALER_FAKEBANK_stop (struct TALER_FAKEBANK_Handle *h)      GNUNET_CONTAINER_DLL_remove (h->transactions_head,                                   h->transactions_tail,                                   t); +    GNUNET_free (t->subject);      GNUNET_free (t->exchange_base_url);      GNUNET_free (t);    } @@ -291,9 +333,9 @@ handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h,  {    enum GNUNET_JSON_PostResult pr;    json_t *json; -  struct Transaction *t;    struct MHD_Response *resp;    int ret; +  uint64_t serial_id;    pr = GNUNET_JSON_post_parser (REQUEST_BUFFER_MAX,                                  con_cls, @@ -316,14 +358,17 @@ handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h,    case GNUNET_JSON_PR_SUCCESS:      break;    } -  t = GNUNET_new (struct Transaction);    { +    const char *wtid; +    uint64_t debit_account; +    uint64_t credit_account;      const char *base_url; +    struct TALER_Amount amount;      struct GNUNET_JSON_Specification spec[] = { -      GNUNET_JSON_spec_fixed_auto ("wtid", &t->wtid), -      GNUNET_JSON_spec_uint64 ("debit_account", &t->debit_account), -      GNUNET_JSON_spec_uint64 ("credit_account", &t->credit_account), -      TALER_JSON_spec_amount ("amount", &t->amount), +      GNUNET_JSON_spec_string ("wtid", &wtid), +      GNUNET_JSON_spec_uint64 ("debit_account", &debit_account), +      GNUNET_JSON_spec_uint64 ("credit_account", &credit_account), +      TALER_JSON_spec_amount ("amount", &amount),        GNUNET_JSON_spec_string ("exchange_url", &base_url),        GNUNET_JSON_spec_end ()      }; @@ -336,19 +381,18 @@ handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h,        json_decref (json);        return MHD_NO;      } -    t->exchange_base_url = GNUNET_strdup (base_url); -    t->serial_id = ++h->serial_counter; -    t->date = GNUNET_TIME_absolute_get (); -    GNUNET_TIME_round_abs (&t->date); -    GNUNET_CONTAINER_DLL_insert_tail (h->transactions_head, -                                      h->transactions_tail, -                                      t); +    serial_id = TALER_FAKEBANK_make_transfer (h, +                                              debit_account, +                                              credit_account, +                                              &amount, +                                              wtid, +                                              base_url); +    GNUNET_log (GNUNET_ERROR_TYPE_INFO, +                "Receiving incoming wire transfer: %llu->%llu from %s\n", +                (unsigned long long) debit_account, +                (unsigned long long) credit_account, +                base_url);    } -  GNUNET_log (GNUNET_ERROR_TYPE_INFO, -              "Receiving incoming wire transfer: %llu->%llu from %s\n", -              (unsigned long long) t->debit_account, -              (unsigned long long) t->credit_account, -              t->exchange_base_url);    json_decref (json);    /* Finally build response object */ @@ -358,7 +402,7 @@ handle_admin_add_incoming (struct TALER_FAKEBANK_Handle *h,      json = json_pack ("{s:I}",                        "serial_id", -                      (json_int_t) t->serial_id); +                      (json_int_t) serial_id);      json_str = json_dumps (json,                             JSON_INDENT(2));      json_decref (json); @@ -514,17 +558,10 @@ handle_history (struct TALER_FAKEBANK_Handle *h,        continue;      } -    { -      char *ws; - -      ws = GNUNET_STRINGS_data_to_string_alloc (&pos->wtid, -                                                sizeof (pos->wtid)); -      GNUNET_asprintf (&subject, -                       "%s %s", -                       ws, -                       pos->exchange_base_url); -      GNUNET_free (ws); -    } +    GNUNET_asprintf (&subject, +                     "%s %s", +                     pos->subject, +                     pos->exchange_base_url);      trans = json_pack ("{s:I, s:o, s:o, s:s, s:I, s:s}",                         "row_id", (json_int_t) pos->serial_id,                         "date", GNUNET_JSON_from_time_abs (pos->date), diff --git a/src/bank-lib/test_bank_interpreter.c b/src/bank-lib/test_bank_interpreter.c index e966424e..f5aee8ee 100644 --- a/src/bank-lib/test_bank_interpreter.c +++ b/src/bank-lib/test_bank_interpreter.c @@ -572,7 +572,9 @@ history_cb (void *cls,                    "Expected history of length %llu, got %llu\n",                    (unsigned long long) total,                    (unsigned long long) cmd->details.history.results_obtained); -      print_expected (h, total, UINT_MAX); +      print_expected (h, +                      total, +                      UINT_MAX);        free_history (h,                      total);        fail (is); @@ -621,7 +623,6 @@ interpreter_run (void *cls)    struct InterpreterState *is = cls;    struct TBI_Command *cmd = &is->commands[is->ip];    const struct TBI_Command *ref; -  struct TALER_WireTransferIdentifierRawP wtid;    struct TALER_Amount amount;    const struct GNUNET_SCHEDULER_TaskContext *tc;    struct TALER_BANK_AuthenticationData auth; @@ -719,25 +720,34 @@ interpreter_run (void *cls)      GNUNET_assert (GNUNET_OK ==                     TALER_string_to_amount (ref->details.admin_add_incoming.amount,                                             &amount)); -    if (GNUNET_OK != -        TALER_FAKEBANK_check (is->fakebank, -                              &amount, -                              ref->details.admin_add_incoming.debit_account_no, -                              ref->details.admin_add_incoming.credit_account_no, -                              ref->details.admin_add_incoming.exchange_base_url, -                              &wtid))      { -      GNUNET_break (0); -      fail (is); -      return; -    } -    if (0 != memcmp (&wtid, -                     &ref->details.admin_add_incoming.wtid, -                     sizeof (wtid))) -    { -      GNUNET_break (0); -      fail (is); -      return; +      char *subject; +      char *expect; + +      if (GNUNET_OK != +          TALER_FAKEBANK_check (is->fakebank, +                                &amount, +                                ref->details.admin_add_incoming.debit_account_no, +                                ref->details.admin_add_incoming.credit_account_no, +                                ref->details.admin_add_incoming.exchange_base_url, +                                &subject)) +      { +        GNUNET_break (0); +        fail (is); +        return; +      } +      expect = GNUNET_STRINGS_data_to_string_alloc (&ref->details.admin_add_incoming.wtid, +                                                    sizeof (ref->details.admin_add_incoming.wtid)); +      if (0 != strcmp (subject, expect)) +      { +        GNUNET_free (expect); +        GNUNET_free (subject); +        GNUNET_break (0); +        fail (is); +        return; +      } +      GNUNET_free (subject); +      GNUNET_free (expect);      }      is->ip++;      is->task = GNUNET_SCHEDULER_add_now (&interpreter_run,  | 
