Debian Codegraph @

tests/unit_tests/address_from_url.cpp

raw
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // FIXME: move this into a full wallet2 unit test suite, if possible
30
31 #include "gtest/gtest.h"
32
33 #include "wallet/wallet2.h"
34 #include "common/dns_utils.h"
35 #include "simplewallet/simplewallet.h"
36 #include <string>
37
38 TEST(AddressFromTXT, Success)
39 {
40 std::string addr = "46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em";
41
42 std::string txtr = "oa1:xmr";
43 txtr += " recipient_address=";
44 txtr += addr;
45 txtr += ";";
46
47 std::string res = tools::dns_utils::address_from_txt_record(txtr);
48
49 EXPECT_STREQ(addr.c_str(), res.c_str());
50
51 std::string txtr2 = "foobar";
52
53 txtr2 += txtr;
54
55 txtr2 += "more foobar";
56
57 res = tools::dns_utils::address_from_txt_record(txtr2);
58
59 EXPECT_STREQ(addr.c_str(), res.c_str());
60
61 std::string txtr3 = "foobar oa1:xmr tx_description=\"Donation for Monero Development Fund\"; ";
62 txtr3 += "recipient_address=";
63 txtr3 += addr;
64 txtr3 += "; foobar";
65
66 res = tools::dns_utils::address_from_txt_record(txtr3);
67
68 EXPECT_STREQ(addr.c_str(), res.c_str());
69 }
70
71 TEST(AddressFromTXT, Failure)
72 {
73 std::string txtr = "oa1:xmr recipient_address=not a real address";
74
75 std::string res = tools::dns_utils::address_from_txt_record(txtr);
76
77 ASSERT_STREQ("", res.c_str());
78
79 txtr += ";";
80
81 res = tools::dns_utils::address_from_txt_record(txtr);
82 ASSERT_STREQ("", res.c_str());
83 }
84
85 TEST(AddressFromURL, Success)
86 {
87 const std::string addr = MONERO_DONATION_ADDR;
88
89 bool dnssec_result = false;
90
91 std::vector<std::string> addresses = tools::dns_utils::addresses_from_url("donate.getmonero.org", dnssec_result);
92
93 EXPECT_EQ(1, addresses.size());
94 if (addresses.size() == 1)
95 {
96 EXPECT_STREQ(addr.c_str(), addresses[0].c_str());
97 }
98
99 // OpenAlias address with an @ instead of first .
100 addresses = tools::dns_utils::addresses_from_url("[email protected]", dnssec_result);
101 EXPECT_EQ(1, addresses.size());
102 if (addresses.size() == 1)
103 {
104 EXPECT_STREQ(addr.c_str(), addresses[0].c_str());
105 }
106 }
107
108 TEST(AddressFromURL, Failure)
109 {
110 bool dnssec_result = false;
111
112 std::vector<std::string> addresses = tools::dns_utils::addresses_from_url("example.veryinvalid", dnssec_result);
113
114 // for a non-existing domain such as "example.invalid", the non-existence is proved with NSEC records
115 ASSERT_TRUE(dnssec_result);
116
117 ASSERT_EQ(0, addresses.size());
118 }