wownero/tests/unit_tests/uri.cpp

218 lines
5.9 KiB
C++
Raw Normal View History

2018-01-07 16:55:52 -07:00
// Copyright (c) 2016-2018, The Monero Project
2016-11-28 10:53:20 -07:00
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include "wallet/wallet2.h"
#define TEST_ADDRESS "9tTLtauaEKSj7xoVXytVH32R1pLZBk4VV4mZFGEh4wkXhDWqw1soPyf3fGixf1kni31VznEZkWNEza9d5TvjWwq5PaohYHC"
#define TEST_INTEGRATED_ADDRESS "A4A1uPj4qaxj7xoVXytVH32R1pLZBk4VV4mZFGEh4wkXhDWqw1soPyf3fGixf1kni31VznEZkWNEza9d5TvjWwq5acaPMJfMbn3ReTsBpp"
// included payment id: <f612cac0b6cb1cda>
#define PARSE_URI(uri, expected) \
std::string address, payment_id, recipient_name, description, error; \
uint64_t amount; \
std::vector<std::string> unknown_parameters; \
2018-02-16 04:04:04 -07:00
tools::wallet2 w(cryptonote::TESTNET); \
2016-11-28 10:53:20 -07:00
bool ret = w.parse_uri(uri, address, payment_id, amount, description, recipient_name, unknown_parameters, error); \
ASSERT_EQ(ret, expected);
TEST(uri, empty_string)
{
PARSE_URI("", false);
}
TEST(uri, no_scheme)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, bad_scheme)
{
PARSE_URI("http://foo", false);
}
TEST(uri, scheme_not_first)
{
2018-06-04 20:33:16 -06:00
PARSE_URI(" wownero:", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, no_body)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, no_address)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:?", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, bad_address)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:44444", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, good_address)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS, true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(address, TEST_ADDRESS);
}
TEST(uri, good_integrated_address)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_INTEGRATED_ADDRESS, true);
2016-11-28 10:53:20 -07:00
}
TEST(uri, parameter_without_inter)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"&amount=1", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, parameter_without_equals)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?amount", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, parameter_without_value)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_amount=", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, negative_amount)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_amount=-1", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, bad_amount)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_amount=alphanumeric", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, duplicate_parameter)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_amount=1&tx_amount=1", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, unknown_parameter)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?unknown=1", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(unknown_parameters.size(), 1);
ASSERT_EQ(unknown_parameters[0], "unknown=1");
}
TEST(uri, unknown_parameters)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_amount=1&unknown=1&tx_description=desc&foo=bar", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(unknown_parameters.size(), 2);
ASSERT_EQ(unknown_parameters[0], "unknown=1");
ASSERT_EQ(unknown_parameters[1], "foo=bar");
}
TEST(uri, empty_payment_id)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_payment_id=", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, bad_payment_id)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_payment_id=1234567890", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, short_payment_id)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_payment_id=1234567890123456", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(address, TEST_ADDRESS);
ASSERT_EQ(payment_id, "1234567890123456");
}
TEST(uri, long_payment_id)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_payment_id=1234567890123456789012345678901234567890123456789012345678901234", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(address, TEST_ADDRESS);
ASSERT_EQ(payment_id, "1234567890123456789012345678901234567890123456789012345678901234");
}
TEST(uri, payment_id_with_integrated_address)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_INTEGRATED_ADDRESS"?tx_payment_id=1234567890123456", false);
2016-11-28 10:53:20 -07:00
}
TEST(uri, empty_description)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "");
}
TEST(uri, empty_recipient_name)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?recipient_name=", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(recipient_name, "");
}
TEST(uri, non_empty_description)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=foo", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "foo");
}
TEST(uri, non_empty_recipient_name)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?recipient_name=foo", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(recipient_name, "foo");
}
TEST(uri, url_encoding)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=foo%20bar", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "foo bar");
}
TEST(uri, non_alphanumeric_url_encoding)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=foo%2x", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "foo%2x");
}
TEST(uri, truncated_url_encoding)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=foo%2", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "foo%2");
}
TEST(uri, percent_without_url_encoding)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=foo%", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "foo%");
}
TEST(uri, url_encoded_once)
{
2018-06-04 20:33:16 -06:00
PARSE_URI("wownero:" TEST_ADDRESS"?tx_description=foo%2020", true);
2016-11-28 10:53:20 -07:00
ASSERT_EQ(description, "foo 20");
}