File indexing completed on 2024-05-12 17:15:20

0001 /*
0002    Copyright (C) 2018 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "connectaddress.h"
0025 #include "connection.h"
0026 #include "eventdispatcher.h"
0027 
0028 #include "../testutil.h"
0029 
0030 #include <iostream>
0031 #include <string>
0032 
0033 static void testFromString()
0034 {
0035 #ifdef __unix__
0036     {
0037         ConnectAddress addr;
0038         addr.setAddressFromString("unix:path=/dev/null");
0039         TEST(addr.type() == ConnectAddress::Type::UnixPath);
0040         TEST(addr.path() == "/dev/null");
0041     }
0042     {
0043         ConnectAddress addr;
0044         addr.setAddressFromString("unix:abstract=less/traveled");
0045         TEST(addr.type() == ConnectAddress::Type::AbstractUnixPath);
0046         TEST(addr.path() == "less/traveled");
0047     }
0048     {
0049         ConnectAddress addr;
0050         addr.setAddressFromString("unix:guid=00000000000000000000000000000000,abstract=/tmp/dbus-XXXXXXXXXX");
0051         TEST(addr.type() == ConnectAddress::Type::AbstractUnixPath);
0052         TEST(addr.path() == "/tmp/dbus-XXXXXXXXXX");
0053         TEST(addr.guid() == "00000000000000000000000000000000");
0054     }
0055 #endif
0056     {
0057         ConnectAddress addr;
0058         addr.setAddressFromString("tcp:port=2233,host=localhost,guid=10000000000000000000000000000000");
0059         TEST(addr.type() == ConnectAddress::Type::Tcp);
0060         TEST(addr.port() == 2233);
0061         TEST(addr.guid() == "10000000000000000000000000000000");
0062     }
0063     {
0064         ConnectAddress addr;
0065         addr.setAddressFromString("tcp:family=ipv4,host=127.0.0.1,port=65535");
0066         TEST(addr.type() == ConnectAddress::Type::Tcp4);
0067         TEST(addr.port() == 65535);
0068         TEST(addr.guid() == "");
0069     }
0070     {
0071         ConnectAddress addr;
0072         addr.setAddressFromString("tcp:host=localhost,port=1,family=ipv6");
0073         TEST(addr.type() == ConnectAddress::Type::Tcp6);
0074         TEST(addr.port() == 1);
0075         TEST(addr.guid() == "");
0076     }
0077     // TODO lots of ugly error cases
0078 }
0079 
0080 static void testToString()
0081 {
0082 #ifdef __unix__
0083     {
0084         ConnectAddress addr;
0085         addr.setType(ConnectAddress::Type::UnixPath);
0086         addr.setPath("/dev/null");
0087         TEST(addr.toString() == "unix:path=/dev/null");
0088     }
0089     {
0090         ConnectAddress addr;
0091         addr.setType(ConnectAddress::Type::AbstractUnixPath);
0092         addr.setPath("less/traveled");
0093         TEST(addr.toString() == "unix:abstract=less/traveled");
0094     }
0095     {
0096         ConnectAddress addr;
0097         addr.setType(ConnectAddress::Type::AbstractUnixPath);
0098         addr.setPath("/tmp/dbus-XXXXXXXXXX");
0099         addr.setGuid("00000000000000000000000000000000");
0100         TEST(addr.toString() =="unix:abstract=/tmp/dbus-XXXXXXXXXX,guid=00000000000000000000000000000000");
0101     }
0102 #endif
0103     {
0104         ConnectAddress addr;
0105         addr.setType(ConnectAddress::Type::Tcp);
0106         addr.setPort(2233);
0107         addr.setGuid("10000000000000000000000000000000");
0108         TEST(addr.toString() == "tcp:host=localhost,port=2233,guid=10000000000000000000000000000000");
0109     }
0110     {
0111         ConnectAddress addr;
0112         addr.setType(ConnectAddress::Type::Tcp4);
0113         addr.setPort(65535);
0114         TEST(addr.toString() == "tcp:host=localhost,family=ipv4,port=65535");
0115     }
0116     {
0117         ConnectAddress addr;
0118         addr.setType(ConnectAddress::Type::Tcp6);
0119         addr.setPort(1);
0120         TEST(addr.toString() == "tcp:host=localhost,family=ipv6,port=1");
0121     }
0122 }
0123 
0124 static void testFindBuses()
0125 {
0126     ConnectAddress systemAddr(ConnectAddress::StandardBus::System);
0127     // We'd have to duplicate the ConnectAddress code to check the result in a clean way, so just
0128     // try to connect instead...
0129     std::cout << "The system bus address seems to be: " << systemAddr.toString() << '\n';
0130 
0131     EventDispatcher eventDispatcher;
0132 #ifndef _WIN32
0133     {
0134         Connection conn(&eventDispatcher, systemAddr);
0135         conn.waitForConnectionEstablished();
0136         TEST(conn.isConnected());
0137     }
0138 #endif
0139 
0140     ConnectAddress sessionAddr(ConnectAddress::StandardBus::Session);
0141     std::cout << "The session bus address seems to be: " << sessionAddr.toString() << '\n';
0142     TEST(systemAddr != sessionAddr);
0143 
0144     {
0145         Connection conn(&eventDispatcher, sessionAddr);
0146         conn.waitForConnectionEstablished();
0147         TEST(conn.isConnected());
0148     }
0149 
0150     // also a few trivial tests of operator==...
0151     TEST(systemAddr == systemAddr);
0152     TEST(sessionAddr == sessionAddr);
0153 }
0154 
0155 int main(int, char *[])
0156 {
0157     testFromString();
0158     testToString();
0159     testFindBuses();
0160     std::cout << "Passed!\n";
0161 }