Anoncoin  0.9.4
P2P Digital Currency
paymentservertests.cpp
Go to the documentation of this file.
1 #include "paymentservertests.h"
2 
3 #include "optionsmodel.h"
4 #include "paymentrequestdata.h"
5 
6 #include "util.h"
7 
8 #include <openssl/x509.h>
9 #include <openssl/x509_vfy.h>
10 
11 #include <QFileOpenEvent>
12 #include <QTemporaryFile>
13 
14 X509 *parse_b64der_cert(const char* cert_data)
15 {
16  std::vector<unsigned char> data = DecodeBase64(cert_data);
17  assert(data.size() > 0);
18  const unsigned char* dptr = &data[0];
19  X509 *cert = d2i_X509(NULL, &dptr, data.size());
20  assert(cert);
21  return cert;
22 }
23 
24 
25 //
26 // Test payment request handling
27 //
28 
29 static SendCoinsRecipient handleRequest(PaymentServer* server, std::vector<unsigned char>& data)
30 {
31  RecipientCatcher sigCatcher;
32  QObject::connect(server, SIGNAL(receivedPaymentRequest(SendCoinsRecipient)),
33  &sigCatcher, SLOT(getRecipient(SendCoinsRecipient)));
34 
35  // Write data to a temp file:
36  QTemporaryFile f;
37  f.open();
38  f.write((const char*)&data[0], data.size());
39  f.close();
40 
41  // Create a QObject, install event filter from PaymentServer
42  // and send a file open event to the object
43  QObject object;
44  object.installEventFilter(server);
45  QFileOpenEvent event(f.fileName());
46  // If sending the event fails, this will cause sigCatcher to be empty,
47  // which will lead to a test failure anyway.
48  QCoreApplication::sendEvent(&object, &event);
49 
50  QObject::disconnect(server, SIGNAL(receivedPaymentRequest(SendCoinsRecipient)),
51  &sigCatcher, SLOT(getRecipient(SendCoinsRecipient)));
52 
53  // Return results from sigCatcher
54  return sigCatcher.recipient;
55 }
56 
58 {
59  OptionsModel optionsModel;
60  PaymentServer* server = new PaymentServer(NULL, false);
61  X509_STORE* caStore = X509_STORE_new();
62  X509_STORE_add_cert(caStore, parse_b64der_cert(caCert_BASE64));
64  server->setOptionsModel(&optionsModel);
65  server->uiReady();
66 
67  // Now feed PaymentRequests to server, and observe signals it produces:
68  std::vector<unsigned char> data = DecodeBase64(paymentrequest1_BASE64);
69  SendCoinsRecipient r = handleRequest(server, data);
70  QString merchant;
71  r.paymentRequest.getMerchant(caStore, merchant);
72  QCOMPARE(merchant, QString("testmerchant.org"));
73 
74  // Version of the above, with an expired certificate:
76  r = handleRequest(server, data);
77  r.paymentRequest.getMerchant(caStore, merchant);
78  QCOMPARE(merchant, QString(""));
79 
80  // Long certificate chain:
82  r = handleRequest(server, data);
83  r.paymentRequest.getMerchant(caStore, merchant);
84  QCOMPARE(merchant, QString("testmerchant8.org"));
85 
86  // Long certificate chain, with an expired certificate in the middle:
88  r = handleRequest(server, data);
89  r.paymentRequest.getMerchant(caStore, merchant);
90  QCOMPARE(merchant, QString(""));
91 
92  // Validly signed, but by a CA not in our root CA list:
94  r = handleRequest(server, data);
95  r.paymentRequest.getMerchant(caStore, merchant);
96  QCOMPARE(merchant, QString(""));
97 
98  // Try again with no root CA's, verifiedMerchant should be empty:
99  caStore = X509_STORE_new();
102  r = handleRequest(server, data);
103  r.paymentRequest.getMerchant(caStore, merchant);
104  QCOMPARE(merchant, QString(""));
105 
106  delete server;
107 }
108 
110 {
111  recipient = r;
112 }
PaymentRequestPlus paymentRequest
Definition: walletmodel.h:56
const char * paymentrequest3_BASE64
void setOptionsModel(OptionsModel *optionsModel)
SendCoinsRecipient recipient
bool getMerchant(X509_STORE *certStore, QString &merchant) const
void getRecipient(SendCoinsRecipient r)
X509 * parse_b64der_cert(const char *cert_data)
const char * paymentrequest5_BASE64
const char * paymentrequest1_BASE64
const char * paymentrequest4_BASE64
Interface from Qt to configuration data structure for Anoncoin client.
Definition: optionsmodel.h:27
const char * caCert_BASE64
vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
Definition: util.cpp:599
static void LoadRootCAs(X509_STORE *store=NULL)
const char * paymentrequest2_BASE64