test_anastasis_api.c (11839B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020, 2021 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file testing/test_anastasis_api.c 18 * @brief testcase to test anastasis' HTTP API interface 19 * @author Christian Grothoff 20 * @author Dennis Neufeld 21 * @author Dominik Meister 22 */ 23 #include "platform.h" 24 #include "anastasis_testing_lib.h" 25 #include <taler/taler_merchant_testing_lib.h> 26 27 28 /** 29 * Configuration file we use. One (big) configuration is used 30 * for the various components for this test. 31 */ 32 #define CONFIG_FILE "test_anastasis_api.conf" 33 34 /** 35 * Exchange base URL. Could also be taken from config. 36 */ 37 #define EXCHANGE_URL "http://localhost:8081/" 38 39 /** 40 * Account number of the exchange at the bank. 41 */ 42 #define EXCHANGE_ACCOUNT_NAME "2" 43 44 /** 45 * Account number of some user. 46 */ 47 #define USER_ACCOUNT_NAME "62" 48 49 /** 50 * Account number used by the merchant 51 */ 52 #define MERCHANT_ACCOUNT_NAME "3" 53 54 /** 55 * Test credentials. 56 */ 57 static struct TALER_TESTING_Credentials cred; 58 59 /** 60 * Payto URI of the customer (payer). 61 */ 62 static struct TALER_FullPayto payer_payto; 63 64 /** 65 * Payto URI of the exchange (escrow account). 66 */ 67 static struct TALER_FullPayto exchange_payto; 68 69 /** 70 * Payto URI of the merchant (receiver). 71 */ 72 static struct TALER_FullPayto merchant_payto; 73 74 /** 75 * Merchant base URL. 76 */ 77 static const char *merchant_url; 78 79 /** 80 * Anastasis base URL. 81 */ 82 static char *anastasis_url; 83 84 /** 85 * Anastasis process. 86 */ 87 static struct GNUNET_OS_Process *anastasisd; 88 89 /** 90 * Name of the file for exchanging the secret. 91 */ 92 static char *file_secret; 93 94 /** 95 * Execute the taler-exchange-wirewatch command with our configuration 96 * file. 97 * 98 * @param label label to use for the command. 99 */ 100 static struct TALER_TESTING_Command 101 cmd_exec_wirewatch (const char *label) 102 { 103 return TALER_TESTING_cmd_exec_wirewatch (label, 104 CONFIG_FILE); 105 } 106 107 108 /** 109 * Run wire transfer of funds from some user's account to the 110 * exchange. 111 * 112 * @param label label to use for the command. 113 * @param amount amount to transfer, i.e. "EUR:1" 114 * @param url exchange_url 115 */ 116 static struct TALER_TESTING_Command 117 cmd_transfer_to_exchange (const char *label, 118 const char *amount) 119 { 120 return TALER_TESTING_cmd_admin_add_incoming (label, 121 amount, 122 &cred.ba, 123 payer_payto); 124 } 125 126 127 /** 128 * Main function that will tell the interpreter what commands to run. 129 * 130 * @param cls closure 131 */ 132 static void 133 run (void *cls, 134 struct TALER_TESTING_Interpreter *is) 135 { 136 struct TALER_TESTING_Command withdraw[] = { 137 cmd_transfer_to_exchange ("create-reserve-1", 138 "EUR:10.02"), 139 cmd_exec_wirewatch ("wirewatch-1"), 140 TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-1", 141 "create-reserve-1", 142 "EUR:5", 143 0, /* age */ 144 MHD_HTTP_OK), 145 TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-2", 146 "create-reserve-1", 147 "EUR:5", 148 0, /* age */ 149 MHD_HTTP_OK), 150 TALER_TESTING_cmd_status ("withdraw-status-1", 151 "create-reserve-1", 152 "EUR:0", 153 MHD_HTTP_OK), 154 TALER_TESTING_cmd_end () 155 }; 156 157 struct TALER_TESTING_Command policy[] = { 158 ANASTASIS_TESTING_cmd_policy_store ("policy-store-1", 159 anastasis_url, 160 NULL /* prev upload */, 161 MHD_HTTP_PAYMENT_REQUIRED, 162 ANASTASIS_TESTING_PSO_NONE, 163 "Test-1", 164 strlen ("Test-1")), 165 /* what would we have to pay? */ 166 TALER_TESTING_cmd_merchant_claim_order ("fetch-proposal", 167 merchant_url, 168 MHD_HTTP_OK, 169 "policy-store-1", 170 NULL), 171 /* make the payment */ 172 TALER_TESTING_cmd_merchant_pay_order ("pay-account", 173 merchant_url, 174 MHD_HTTP_OK, 175 "fetch-proposal", 176 "withdraw-coin-1", 177 "EUR:5", 178 "EUR:4.99", /* must match ANNUAL_FEE in config! */ 179 NULL), 180 ANASTASIS_TESTING_cmd_policy_store ("policy-store-2", 181 anastasis_url, 182 "policy-store-1", 183 MHD_HTTP_NO_CONTENT, 184 ANASTASIS_TESTING_PSO_NONE, 185 "Test-1", 186 strlen ("Test-1")), 187 ANASTASIS_TESTING_cmd_policy_lookup ("policy-lookup-1", 188 anastasis_url, 189 MHD_HTTP_OK, 190 "policy-store-2"), 191 TALER_TESTING_cmd_end () 192 }; 193 194 struct TALER_TESTING_Command truth[] = { 195 ANASTASIS_TESTING_cmd_truth_question ( 196 "truth-store-1", 197 anastasis_url, 198 NULL, 199 "The-Answer", 200 ANASTASIS_TESTING_TSO_NONE, 201 MHD_HTTP_NO_CONTENT), 202 ANASTASIS_TESTING_cmd_truth_solve ( 203 "keyshare-lookup-1", 204 anastasis_url, 205 "The-Answer", 206 NULL, /* payment ref */ 207 "truth-store-1", 208 0, 209 MHD_HTTP_OK), 210 ANASTASIS_TESTING_cmd_truth_store ( 211 "truth-store-2", 212 anastasis_url, 213 NULL, 214 "file", 215 "text/plain", 216 strlen (file_secret), 217 file_secret, 218 ANASTASIS_TESTING_TSO_NONE, 219 MHD_HTTP_NO_CONTENT), 220 ANASTASIS_TESTING_cmd_truth_solve ( 221 "challenge-fail-1", 222 anastasis_url, 223 "Wrong-Answer", 224 NULL, /* payment ref */ 225 "truth-store-1", /* upload ref */ 226 0, /* security question mode */ 227 MHD_HTTP_FORBIDDEN), 228 ANASTASIS_TESTING_cmd_truth_challenge ( 229 "file-challenge-run-1", 230 anastasis_url, 231 NULL, /* payment ref */ 232 "truth-store-2", /* upload ref */ 233 MHD_HTTP_PAYMENT_REQUIRED), 234 /* what would we have to pay? */ 235 TALER_TESTING_cmd_merchant_claim_order ("fetch-proposal-2", 236 merchant_url, 237 MHD_HTTP_OK, 238 "file-challenge-run-1", 239 NULL), 240 /* make the payment */ 241 TALER_TESTING_cmd_merchant_pay_order ("pay-account-2", 242 merchant_url, 243 MHD_HTTP_OK, 244 "fetch-proposal-2", 245 "withdraw-coin-2", 246 "EUR:1.01", 247 "EUR:1", 248 NULL), 249 250 ANASTASIS_TESTING_cmd_truth_challenge ( 251 "file-challenge-run-2", 252 anastasis_url, 253 "file-challenge-run-1", /* payment ref */ 254 "truth-store-2", 255 MHD_HTTP_OK), 256 ANASTASIS_TESTING_cmd_truth_solve ( 257 "file-challenge-run-3", 258 anastasis_url, 259 "file-challenge-run-2", /* answer */ 260 "file-challenge-run-1", /* payment ref */ 261 "truth-store-2", 262 1, 263 MHD_HTTP_OK), 264 TALER_TESTING_cmd_end () 265 }; 266 267 struct TALER_TESTING_Command commands[] = { 268 /* general setup */ 269 TALER_TESTING_cmd_run_fakebank ("run-fakebank", 270 cred.cfg, 271 "exchange-account-exchange"), 272 TALER_TESTING_cmd_system_start ("start-taler", 273 CONFIG_FILE, 274 "-em", 275 "-u", "exchange-account-exchange", 276 NULL), 277 TALER_TESTING_cmd_get_exchange ("get-exchange", 278 cred.cfg, 279 NULL, 280 true, 281 true), 282 TALER_TESTING_cmd_merchant_post_instances ("instance-create-admin", 283 merchant_url, 284 "admin", 285 MHD_HTTP_NO_CONTENT), 286 TALER_TESTING_cmd_merchant_post_account ( 287 "instance-create-default-account", 288 merchant_url, 289 merchant_payto, 290 NULL, NULL, 291 MHD_HTTP_OK), 292 ANASTASIS_TESTING_cmd_config ("salt-request-1", 293 anastasis_url, 294 MHD_HTTP_OK), 295 TALER_TESTING_cmd_batch ("withdraw", 296 withdraw), 297 TALER_TESTING_cmd_batch ("policy", 298 policy), 299 TALER_TESTING_cmd_batch ("truth", 300 truth), 301 TALER_TESTING_cmd_end () 302 }; 303 304 TALER_TESTING_run (is, 305 commands); 306 } 307 308 309 int 310 main (int argc, 311 char *const *argv) 312 { 313 int ret; 314 315 { 316 char dir[] = "/tmp/test-anastasis-file-XXXXXX"; 317 318 if (NULL == mkdtemp (dir)) 319 { 320 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 321 "mkdtemp", 322 dir); 323 return 77; 324 } 325 GNUNET_asprintf (&file_secret, 326 "%s/.secret", 327 dir); 328 } 329 payer_payto.full_payto = 330 (char *) "payto://x-taler-bank/localhost/" USER_ACCOUNT_NAME 331 "?receiver-name=62"; 332 exchange_payto.full_payto = 333 (char *) "payto://x-taler-bank/localhost/" EXCHANGE_ACCOUNT_NAME 334 "?receiver-name=exchange"; 335 merchant_payto.full_payto = 336 (char *) "payto://x-taler-bank/localhost/" MERCHANT_ACCOUNT_NAME 337 "?receiver-name=merchant"; 338 merchant_url = "http://localhost:8080/"; 339 340 if (NULL == 341 (anastasis_url = ANASTASIS_TESTING_prepare_anastasis (CONFIG_FILE))) 342 return 77; 343 if (NULL == (anastasisd = 344 ANASTASIS_TESTING_run_anastasis (CONFIG_FILE, 345 anastasis_url))) 346 { 347 GNUNET_break (0); 348 return 1; 349 } 350 ret = TALER_TESTING_main (argv, 351 "INFO", 352 CONFIG_FILE, 353 "exchange-account-exchange", 354 TALER_TESTING_BS_FAKEBANK, 355 &cred, 356 &run, 357 NULL); 358 GNUNET_OS_process_kill (anastasisd, 359 SIGTERM); 360 GNUNET_OS_process_wait (anastasisd); 361 GNUNET_OS_process_destroy (anastasisd); 362 GNUNET_free (anastasis_url); 363 return ret; 364 } 365 366 367 /* end of test_anastasis_api.c */