You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paymentservertests.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2009-2014 The Bitcoin developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "paymentservertests.h"
  5. #include "optionsmodel.h"
  6. #include "paymentrequestdata.h"
  7. #include "util.h"
  8. #include "utilstrencodings.h"
  9. #include <openssl/x509.h>
  10. #include <openssl/x509_vfy.h>
  11. #include <QFileOpenEvent>
  12. #include <QTemporaryFile>
  13. X509 *parse_b64der_cert(const char* cert_data)
  14. {
  15. std::vector<unsigned char> data = DecodeBase64(cert_data);
  16. assert(data.size() > 0);
  17. const unsigned char* dptr = &data[0];
  18. X509 *cert = d2i_X509(NULL, &dptr, data.size());
  19. assert(cert);
  20. return cert;
  21. }
  22. //
  23. // Test payment request handling
  24. //
  25. static SendCoinsRecipient handleRequest(PaymentServer* server, std::vector<unsigned char>& data)
  26. {
  27. RecipientCatcher sigCatcher;
  28. QObject::connect(server, SIGNAL(receivedPaymentRequest(SendCoinsRecipient)),
  29. &sigCatcher, SLOT(getRecipient(SendCoinsRecipient)));
  30. // Write data to a temp file:
  31. QTemporaryFile f;
  32. f.open();
  33. f.write((const char*)&data[0], data.size());
  34. f.close();
  35. // Create a QObject, install event filter from PaymentServer
  36. // and send a file open event to the object
  37. QObject object;
  38. object.installEventFilter(server);
  39. QFileOpenEvent event(f.fileName());
  40. // If sending the event fails, this will cause sigCatcher to be empty,
  41. // which will lead to a test failure anyway.
  42. QCoreApplication::sendEvent(&object, &event);
  43. QObject::disconnect(server, SIGNAL(receivedPaymentRequest(SendCoinsRecipient)),
  44. &sigCatcher, SLOT(getRecipient(SendCoinsRecipient)));
  45. // Return results from sigCatcher
  46. return sigCatcher.recipient;
  47. }
  48. void PaymentServerTests::paymentServerTests()
  49. {
  50. SelectParams(CBaseChainParams::MAIN);
  51. OptionsModel optionsModel;
  52. PaymentServer* server = new PaymentServer(NULL, false);
  53. X509_STORE* caStore = X509_STORE_new();
  54. X509_STORE_add_cert(caStore, parse_b64der_cert(caCert_BASE64));
  55. PaymentServer::LoadRootCAs(caStore);
  56. server->setOptionsModel(&optionsModel);
  57. server->uiReady();
  58. // Now feed PaymentRequests to server, and observe signals it produces:
  59. std::vector<unsigned char> data = DecodeBase64(paymentrequest1_BASE64);
  60. SendCoinsRecipient r = handleRequest(server, data);
  61. QString merchant;
  62. r.paymentRequest.getMerchant(caStore, merchant);
  63. QCOMPARE(merchant, QString("testmerchant.org"));
  64. // Version of the above, with an expired certificate:
  65. data = DecodeBase64(paymentrequest2_BASE64);
  66. r = handleRequest(server, data);
  67. r.paymentRequest.getMerchant(caStore, merchant);
  68. QCOMPARE(merchant, QString(""));
  69. // Long certificate chain:
  70. data = DecodeBase64(paymentrequest3_BASE64);
  71. r = handleRequest(server, data);
  72. r.paymentRequest.getMerchant(caStore, merchant);
  73. QCOMPARE(merchant, QString("testmerchant8.org"));
  74. // Long certificate chain, with an expired certificate in the middle:
  75. data = DecodeBase64(paymentrequest4_BASE64);
  76. r = handleRequest(server, data);
  77. r.paymentRequest.getMerchant(caStore, merchant);
  78. QCOMPARE(merchant, QString(""));
  79. // Validly signed, but by a CA not in our root CA list:
  80. data = DecodeBase64(paymentrequest5_BASE64);
  81. r = handleRequest(server, data);
  82. r.paymentRequest.getMerchant(caStore, merchant);
  83. QCOMPARE(merchant, QString(""));
  84. // Try again with no root CA's, verifiedMerchant should be empty:
  85. caStore = X509_STORE_new();
  86. PaymentServer::LoadRootCAs(caStore);
  87. data = DecodeBase64(paymentrequest1_BASE64);
  88. r = handleRequest(server, data);
  89. r.paymentRequest.getMerchant(caStore, merchant);
  90. QCOMPARE(merchant, QString(""));
  91. delete server;
  92. }
  93. void RecipientCatcher::getRecipient(SendCoinsRecipient r)
  94. {
  95. recipient = r;
  96. }