add payto URL test, fix payto parser implementation

This commit is contained in:
Christian Grothoff 2020-01-14 23:54:43 +01:00
parent 2edee5ac4a
commit 1d9049ab99
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
4 changed files with 115 additions and 21 deletions

@ -1 +1 @@
Subproject commit ca53235ccfa0458ebf11c204888ca370e20ec3f5
Subproject commit 934a6a18301e81c4fd1b3a8cda2dc13dca4741cc

1
src/util/.gitignore vendored
View File

@ -1 +1,2 @@
taler-config
test_payto

View File

@ -21,13 +21,55 @@
#include "platform.h"
#include "taler_util.h"
#define CHECK(a,b) do { \
if (0 != strcmp (a,b)) { \
GNUNET_break (0); \
fprintf (stderr, "Got %s, wanted %s\n", b, a); \
GNUNET_free (b); \
return 1; \
} else { \
GNUNET_free (b); \
} \
} while (0)
int
main (int argc,
const char *const argv[])
{
char *r;
GNUNET_log_setup ("test-payto",
"WARNING",
NULL);
r = TALER_payto_xtalerbank_make ("https://localhost/",
"account");
CHECK ("payto://x-taler-bank/localhost/account",
r);
r = TALER_payto_xtalerbank_make ("http://localhost:80/",
"account");
CHECK ("payto://x-taler-bank/localhost:80/account",
r);
r = TALER_payto_xtalerbank_make ("http://localhost/",
"account");
CHECK ("payto://x-taler-bank/localhost:80/account",
r);
r = TALER_xtalerbank_base_url_from_payto (
"payto://x-taler-bank/localhost/bob");
CHECK ("https://localhost/",
r);
r = TALER_xtalerbank_base_url_from_payto (
"payto://x-taler-bank/localhost:1080/bob");
CHECK ("http://localhost:1080/",
r);
r = TALER_xtalerbank_account_url_from_payto (
"payto://x-taler-bank/localhost/bob");
CHECK ("https://localhost/bob",
r);
r = TALER_xtalerbank_account_url_from_payto (
"payto://x-taler-bank/localhost:1080/alice");
CHECK ("http://localhost:1080/alice",
r);
return 0;
}

View File

@ -789,16 +789,38 @@ TALER_payto_xtalerbank_make (const char *bank_url,
const char *account_name)
{
char *payto;
int ends_slash;
int plaintext;
const char *port;
size_t slen;
if (0 < strlen (bank_url))
ends_slash = '/' == bank_url[strlen (bank_url) - 1];
if (0 == strncasecmp ("https://",
bank_url,
strlen ("https://")))
{
bank_url += strlen ("https://");
plaintext = GNUNET_NO;
}
else if (0 == strncasecmp ("http://",
bank_url,
strlen ("http://")))
{
bank_url += strlen ("http://");
plaintext = GNUNET_YES;
}
else
ends_slash = 0;
return NULL;
slen = strlen (bank_url);
port = memchr (bank_url,
':',
slen);
if ( (0 < slen) &&
('/' == bank_url[slen - 1]) )
slen--;
GNUNET_asprintf (&payto,
(ends_slash)
? "payto://x-taler-bank/%s%s"
: "payto://x-taler-bank/%s/%s",
( (NULL == port) && (GNUNET_YES == plaintext) )
? "payto://x-taler-bank/%.*s:80/%s"
: "payto://x-taler-bank/%.*s/%s",
(int) slen,
bank_url,
account_name);
return payto;
@ -817,20 +839,35 @@ TALER_xtalerbank_base_url_from_payto (const char *payto)
{
const char *start;
const char *end;
char *ret;
int https;
const char *colon;
unsigned int port;
if (0 != strncasecmp (payto,
"payto://x-taler-bank/",
strlen ("payto://x-taler-bank/")))
{
return NULL;
}
start = &payto[strlen ("payto://x-taler-bank/")];
end = strchr (start,
(unsigned char) '/');
'/');
if (NULL == end)
end = &start[strlen (start)];
return GNUNET_strndup (start,
end - start);
colon = strrchr (start,
':');
https = GNUNET_YES;
if ( (NULL != colon) &&
(1 == sscanf (colon + 1,
"%u",
&port)) &&
(443 != port) )
https = GNUNET_NO;
GNUNET_asprintf (&ret,
"%s://%.*s/",
(https ? "https" : "http"),
(int) (end - start),
start);
return ret;
}
@ -846,20 +883,34 @@ TALER_xtalerbank_account_url_from_payto (const char *payto)
{
const char *start;
const char *end;
char *ret;
int https;
const char *colon;
unsigned int port;
if (0 != strncasecmp (payto,
"payto://x-taler-bank/",
strlen ("payto://x-taler-bank/")))
{
return NULL;
}
start = &payto[strlen ("payto://x-taler-bank/")];
end = strchr (start,
(unsigned char) '?');
'/');
if (NULL == end)
end = &start[strlen (start)];
return GNUNET_strndup (start,
end - start);
colon = strrchr (start,
':');
https = GNUNET_YES;
if ( (NULL != colon) &&
(1 == sscanf (colon + 1,
"%u",
&port)) &&
(443 != port) )
https = GNUNET_NO;
GNUNET_asprintf (&ret,
"%s://%s",
(https ? "https" : "http"),
start);
return ret;
}