Commit 780b77a0 authored by Murray Cumming's avatar Murray Cumming

Run clang-format on tests.

parent 249bdd53
#include <giomm.h>
#include <iostream>
void on_read_async(const Glib::RefPtr<Gio::AsyncResult>& result)
void
on_read_async(const Glib::RefPtr<Gio::AsyncResult>& result)
{
if(!result)
if (!result)
{
std::cerr << G_STRFUNC << ": result is empty." << std::endl;
exit(EXIT_FAILURE);
}
if(!g_async_result_get_source_object(result->gobj()))
if (!g_async_result_get_source_object(result->gobj()))
{
std::cerr << G_STRFUNC << ": g_async_result_get_source_object() failed." << std::endl;
exit(EXIT_FAILURE);
}
if(!result->get_source_object_base())
if (!result->get_source_object_base())
{
std::cerr << G_STRFUNC << ": result->get_source_object_base() failed." << std::endl;
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
int main(int, char**)
int
main(int, char**)
{
Glib::init();
Gio::init();
......
......@@ -6,10 +6,10 @@
#include <iostream>
#include <string.h>
//Use this line if you want debug output:
//std::ostream& ostr = std::cout;
// Use this line if you want debug output:
// std::ostream& ostr = std::cout;
//This seems nicer and more useful than putting an ifdef around the use of ostr:
// This seems nicer and more useful than putting an ifdef around the use of ostr:
std::stringstream debug;
std::ostream& ostr = debug;
......@@ -27,7 +27,8 @@ std::ostream& ostr = debug;
#define TEST_FILE "/etc/fstab"
#endif
int main(int, char**)
int
main(int, char**)
{
Glib::init();
Gio::init();
......@@ -35,49 +36,48 @@ int main(int, char**)
try
{
auto file = Gio::File::create_for_path(TEST_FILE);
if(!file)
if (!file)
{
std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
auto stream = file->read();
if(!stream)
if (!stream)
{
std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
gchar buffer[1000]; //TODO: This is unpleasant.
gchar buffer[1000]; // TODO: This is unpleasant.
memset(buffer, 0, sizeof buffer);
const gsize bytes_read = stream->read(buffer, sizeof buffer - 1);
if(bytes_read)
if (bytes_read)
ostr << "File contents read: " << buffer << std::endl;
else
{
std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
}
catch(const Gio::Error& ex)
catch (const Gio::Error& ex)
{
//This is just here to check that HOST_WAS_NOT_FOUND is still in our API,
//because we hack it into our gio_enums.defs file and there is a risk of
//losing it when we regenerate that file. murrayc.
if(ex.code() == Gio::Error::HOST_WAS_NOT_FOUND)
// This is just here to check that HOST_WAS_NOT_FOUND is still in our API,
// because we hack it into our gio_enums.defs file and there is a risk of
// losing it when we regenerate that file. murrayc.
if (ex.code() == Gio::Error::HOST_WAS_NOT_FOUND)
{
std::cerr << "Host was not found." << std::endl;
}
else
std::cerr << "Gio::Error exception caught: " << ex.what() << std::endl;
std::cerr << "Gio::Error exception caught: " << ex.what() << std::endl;
}
catch(const Glib::Exception& ex)
catch (const Glib::Exception& ex)
{
std::cerr << "Exception caught: " << ex.what() << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#include <giomm.h>
#include <gio/gio.h> //For the C enum values.
#include <giomm.h>
#include <iostream>
#include <string.h>
// This tests that both Gio::Error and Gio::DBus::Error are registered in
// This tests that both Gio::Error and Gio::DBus::Error are registered in
// Gio::wrap_init(), and that they are properly registered.
// This was previously a problem, but is now fixed, and we want to make sure
// This was previously a problem, but is now fixed, and we want to make sure
// that we don't regress.
int main(int, char**)
int
main(int, char**)
{
Glib::init();
Gio::init();
//Check that Gio::Error is thrown:
// Check that Gio::Error is thrown:
bool gio_error_thrown = false;
try
{
GError* gerror = g_error_new_literal (G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT, "Arbitrary test error text.");
GError* gerror =
g_error_new_literal(G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Arbitrary test error text.");
::Glib::Error::throw_exception(gerror);
}
catch(const Gio::Error& /* ex */)
catch (const Gio::Error& /* ex */)
{
gio_error_thrown = true;
}
catch(const Gio::DBus::Error& ex)
catch (const Gio::DBus::Error& ex)
{
std::cerr << "Gio::DBus::Error caught when a Gio::Error was expected." << std::endl;
return EXIT_FAILURE;
}
catch(const Glib::Error& ex)
catch (const Glib::Error& ex)
{
std::cerr << "Glib::Error caught when a Gio::Error was expected." << std::endl;
return EXIT_FAILURE;
}
if(!gio_error_thrown)
if (!gio_error_thrown)
{
std::cerr << "Gio::Error was not thrown, but should have been thrown." << std::endl;
std::cerr << "Gio::Error was not thrown, but should have been thrown." << std::endl;
return EXIT_FAILURE;
}
//Check that Gio::DBus::Error is thrown:
// Check that Gio::DBus::Error is thrown:
bool gio_dbus_error_thrown = false;
try
{
GError* gerror = g_error_new_literal (G_DBUS_ERROR,
G_DBUS_ERROR_FAILED, "Arbitrary test error text.");
GError* gerror =
g_error_new_literal(G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Arbitrary test error text.");
::Glib::Error::throw_exception(gerror);
}
catch(const Gio::DBus::Error& /* ex */)
catch (const Gio::DBus::Error& /* ex */)
{
gio_dbus_error_thrown = true;
}
catch(const Gio::Error& ex)
catch (const Gio::Error& ex)
{
std::cerr << "Gio::Error caught when a Gio::Dbus::Error was expected." << std::endl;
return EXIT_FAILURE;
}
catch(const Glib::Error& ex)
catch (const Glib::Error& ex)
{
std::cerr << "Glib::Error caught when a Gio::DBus::Error was expected." << std::endl;
return EXIT_FAILURE;
}
if(!gio_dbus_error_thrown)
if (!gio_dbus_error_thrown)
{
std::cerr << "Gio::DBus::Error was not thrown, but should have been thrown." << std::endl;
std::cerr << "Gio::DBus::Error was not thrown, but should have been thrown." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <cstring>
#include <giomm.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
namespace
{
//Use this line if you want debug output:
//std::ostream& ostr = std::cout;
// Use this line if you want debug output:
// std::ostream& ostr = std::cout;
//This seems nicer and more useful than putting an ifdef around the use of ostr:
// This seems nicer and more useful than putting an ifdef around the use of ostr:
std::ostringstream debug;
std::ostream& ostr = debug;
std::string func1_output;
std::string func2_output;
void destroy_func1(void* data)
void
destroy_func1(void* data)
{
char* cdata = static_cast<char*>(data);
func1_output += "Deleting ";
......@@ -25,7 +26,8 @@ void destroy_func1(void* data)
delete[] cdata;
}
void destroy_func2(void* data, const Glib::ustring& intro)
void
destroy_func2(void* data, const Glib::ustring& intro)
{
char* cdata = static_cast<char*>(data);
func2_output += intro + cdata;
......@@ -34,7 +36,8 @@ void destroy_func2(void* data, const Glib::ustring& intro)
} // anonymous namespace
int main(int, char**)
int
main(int, char**)
{
Glib::init();
Gio::init();
......@@ -47,7 +50,7 @@ int main(int, char**)
if (!stream)
{
std::cerr << "Could not create a MemoryInputStream." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
// Add data that shall not be deleted by stream.
......@@ -71,25 +74,24 @@ int main(int, char**)
else
{
std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
}
catch (const Glib::Exception& ex)
{
std::cerr << "Exception caught: " << ex.what() << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
ostr << func1_output << std::endl;
ostr << func2_output << std::endl;
if (std::strcmp(buffer, "Data not owned by stream.\ndata2\ndata3\n") == 0 &&
func1_output == "Deleting data2\n" &&
func2_output == "Now deleting data3\n")
func1_output == "Deleting data2\n" && func2_output == "Now deleting data3\n")
return EXIT_SUCCESS;
std::cerr << "buffer: \"" << buffer << "\"" << std::endl;
std::cerr << "func1_output: \"" << func1_output << "\"" << std::endl;
std::cerr << "func2_output: \"" << func2_output << "\"" << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
......@@ -2,10 +2,10 @@
#include <iostream>
#include <string.h>
//Use this line if you want debug output:
//std::ostream& ostr = std::cout;
// Use this line if you want debug output:
// std::ostream& ostr = std::cout;
//This seems nicer and more useful than putting an ifdef around the use of ostr:
// This seems nicer and more useful than putting an ifdef around the use of ostr:
std::stringstream debug;
std::ostream& ostr = debug;
......@@ -15,7 +15,8 @@ std::ostream& ostr = debug;
#define TEST_FILE "/etc/fstab"
#endif
int main(int, char**)
int
main(int, char**)
{
Glib::init();
Gio::init();
......@@ -23,37 +24,36 @@ int main(int, char**)
try
{
auto file = Gio::File::create_for_path(TEST_FILE);
if(!file)
if (!file)
{
std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
auto stream = file->read();
if(!stream)
if (!stream)
{
std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
gchar buffer[1000]; //TODO: This is unpleasant.
gchar buffer[1000]; // TODO: This is unpleasant.
memset(buffer, 0, sizeof buffer);
const gsize bytes_read = stream->read(buffer, sizeof buffer - 1);
if(bytes_read)
if (bytes_read)
ostr << "File contents read: " << buffer << std::endl;
else
{
std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
}
catch(const Glib::Exception& ex)
catch (const Glib::Exception& ex)
{
std::cerr << "Exception caught: " << ex.what() << std::endl;
return EXIT_FAILURE;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -16,145 +16,130 @@
// https://developer.gnome.org/gio/stable/gio-Extension-Points.html
// https://developer.gnome.org/gio/stable/gio-querymodules.html
#include <cstdlib>
#include <giomm.h>
#include <iostream>
#include <cstdlib>
bool on_accept_certificate(const Glib::RefPtr<const Gio::TlsCertificate>& cert, Gio::TlsCertificateFlags)
bool
on_accept_certificate(const Glib::RefPtr<const Gio::TlsCertificate>& cert, Gio::TlsCertificateFlags)
{
std::cout << "Handshake is ocurring." << std::endl
<< "The server is requesting that its certificate be accepted." <<
std::endl;
<< "The server is requesting that its certificate be accepted." << std::endl;
std::cout << "Outputing certificate data:" << std::endl <<
cert->property_certificate_pem().get_value();
std::cout << "Outputing certificate data:" << std::endl
<< cert->property_certificate_pem().get_value();
auto issuer = cert->get_issuer();
std::cout << "Outputing the issuer's certificate data:" << std::endl <<
issuer->property_certificate_pem().get_value();
std::cout << "Outputing the issuer's certificate data:" << std::endl
<< issuer->property_certificate_pem().get_value();
std::cout << "Accepting the certificate (completing the handshake)." <<
std::endl;
std::cout << "Accepting the certificate (completing the handshake)." << std::endl;
return true;
}
int main(int, char**)
int
main(int, char**)
{
Gio::init();
const Glib::ustring test_host = "www.gnome.org";
std::vector< Glib::RefPtr<Gio::InetAddress> > inet_addresses;
std::vector<Glib::RefPtr<Gio::InetAddress>> inet_addresses;
try
{
inet_addresses =
Gio::Resolver::get_default()->lookup_by_name(test_host);
inet_addresses = Gio::Resolver::get_default()->lookup_by_name(test_host);
}
catch(const Gio::ResolverError& ex)
catch (const Gio::ResolverError& ex)
{
//This happens if it could not resolve the name,
//for instance if we are not connected to the internet.
//TODO: Change this test so it can do something useful and succeed even
//if the testing computer is not connected to the internet.
// This happens if it could not resolve the name,
// for instance if we are not connected to the internet.
// TODO: Change this test so it can do something useful and succeed even
// if the testing computer is not connected to the internet.
std::cerr << "Gio::Resolver::lookup_by_name() threw exception: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
//Actually, it would throw an exception instead of reaching here with 0 addresses resolved.
if(inet_addresses.size() == 0)
// Actually, it would throw an exception instead of reaching here with 0 addresses resolved.
if (inet_addresses.size() == 0)
{
std::cerr << "Could not resolve test host '" << test_host << "'." <<
std::endl;
std::cerr << "Could not resolve test host '" << test_host << "'." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Successfully resolved address of test host '" << test_host <<
"'." << std::endl;
std::cout << "Successfully resolved address of test host '" << test_host << "'." << std::endl;
auto first_inet_address = inet_addresses[0];
std::cout << "First address of test host is " <<
first_inet_address->to_string() << "." << std::endl;
std::cout << "First address of test host is " << first_inet_address->to_string() << "."
<< std::endl;
auto socket =
Gio::Socket::create(first_inet_address->get_family(),
Gio::SOCKET_TYPE_STREAM, Gio::SOCKET_PROTOCOL_TCP);
auto socket = Gio::Socket::create(
first_inet_address->get_family(), Gio::SOCKET_TYPE_STREAM, Gio::SOCKET_PROTOCOL_TCP);
auto address =
Gio::InetSocketAddress::create(first_inet_address, 443);
auto address = Gio::InetSocketAddress::create(first_inet_address, 443);
try
{
socket->connect(address);
}
catch(const Gio::Error& ex)
catch (const Gio::Error& ex)
{
std::cout << "Could not connect socket to " <<
address->get_address()->to_string() << ":" << address->get_port() <<
". Exception: " << ex.what() << std::endl;
std::cout << "Could not connect socket to " << address->get_address()->to_string() << ":"
<< address->get_port() << ". Exception: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
if(!socket->is_connected())
if (!socket->is_connected())
{
std::cout << "Could not connect socket to " <<
address->get_address()->to_string() << ":" << address->get_port() <<
"." << std::endl;
std::cout << "Could not connect socket to " << address->get_address()->to_string() << ":"
<< address->get_port() << "." << std::endl;
}
auto conn = Glib::RefPtr<Gio::TcpConnection>::cast_dynamic(Gio::SocketConnection::create(socket));
if(!conn || !conn->is_connected())
if (!conn || !conn->is_connected())
{
std::cout << "Could not establish connection to " <<
address->get_address()->to_string() << ":" << address->get_port() <<
"." << std::endl;
std::cout << "Could not establish connection to " << address->get_address()->to_string() << ":"
<< address->get_port() << "." << std::endl;
socket->close();
return EXIT_FAILURE;
}
std::cout << "Successfully established connection to " <<
address->get_address()->to_string() << ":" << address->get_port() <<
"." << std::endl;
std::cout << "Successfully established connection to " << address->get_address()->to_string()
<< ":" << address->get_port() << "." << std::endl;
try
{
auto tls_connection =
Gio::TlsClientConnection::create(conn, address);
auto tls_connection = Gio::TlsClientConnection::create(conn, address);
tls_connection->signal_accept_certificate().connect(
sigc::ptr_fun(&on_accept_certificate));
tls_connection->signal_accept_certificate().connect(sigc::ptr_fun(&on_accept_certificate));
tls_connection->handshake();
tls_connection->handshake();
std::cout << "Attempting to get the issuer's certificate from the "
"connection." << std::endl;
"connection."
<< std::endl;
auto issuer_certificate =
tls_connection->get_peer_certificate()->get_issuer();
auto issuer_certificate = tls_connection->get_peer_certificate()->get_issuer();
if(!issuer_certificate)
if (!issuer_certificate)
{
std::cout << "Could not get the issuer's certificate of the peer." <<
std::endl;
std::cout << "Could not get the issuer's certificate of the peer." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Successfully retrieved the issuer's certificate." <<
std::endl;
std::cout << "Successfully retrieved the issuer's certificate." << std::endl;
std::cout << "Attempting to use the connection's database." << std::endl;
auto database = tls_connection->get_database();
std::cout << "Looking up the certificate's issuer in the database." <<
std::endl;
std::cout << "Looking up the certificate's issuer in the database." << std::endl;
auto db_certificate =
database->lookup_certificate_issuer(issuer_certificate);
auto db_certificate = database->lookup_certificate_issuer(issuer_certificate);
if(!db_certificate)
if (!db_certificate)
{
std::cout << "The issuer's certificate was not found in the database." << std::endl;
}
......
......@@ -7,7 +7,8 @@ main()
{
std::string glib_base64 = "R2xpYg==";
std::string glibmm_base64 = "R2xpYm1t";
std::string stallman_quote_base64 = "VmFsdWUgeW91ciBmcmVlZG9tIG9yIHlvdSB3aWxsIGxvc2UgaXQsIHRlYWNoZXMgaGlzdG9yeS4K\n"
std::string stallman_quote_base64 =
"VmFsdWUgeW91ciBmcmVlZG9tIG9yIHlvdSB3aWxsIGxvc2UgaXQsIHRlYWNoZXMgaGlzdG9yeS4K\n"
"J0Rvbid0IGJvdGhlciB1cyB3aXRoIHBvbGl0aWNzJywgcmVzcG9uZCB0aG9zZSB3aG8gZG9uJ3Qg\n"
"d2FudCB0byBsZWFybi4KCi0tIFJpY2hhcmQgU3RhbGxtYW4=\n";
......@@ -15,12 +16,18 @@ main()
std::cerr << Glib::Base64::encode("Glib") << std::endl;
g_assert(Glib::Base64::encode("Glib") == glib_base64);
// test that encodes the quote by Richard Stallman into base64 with linebreaks (the output has line breaks)
// test that encodes the quote by Richard Stallman into base64 with linebreaks (the output has
// line breaks)
std::cerr << Glib::Base64::encode("Value your freedom or you will lose it, teaches history.\n"
"'Don't bother us with politics', respond those who don't want to learn.\n\n-- Richard Stallman", true) << std::endl;
"'Don't bother us with politics', respond those who don't want "
"to learn.\n\n-- Richard Stallman",
true)
<< std::endl;
std::cerr << stallman_quote_base64 << std::endl;
g_assert(Glib::Base64::encode("Value your freedom or you will lose it, teaches history.\n"
"'Don't bother us with politics', respond those who don't want to learn.\n\n-- Richard Stallman", true) == stallman_quote_base64);
"'Don't bother us with politics', respond those who don't want to "
"learn.\n\n-- Richard Stallman",
true) == stallman_quote_base64);
// test that decodes the string "Glibmm" from base64
std::cerr << Glib::Base64::decode(glibmm_base64) << std::endl;
......
......@@ -21,35 +21,34 @@
#include <glibmm.h>
// Use this line if you want debug output:
// std::ostream& ostr = std::cout;
//Use this line if you want debug output:
//std::ostream& ostr = std::cout;
//This seems nicer and more useful than putting an ifdef around the use of std::cout:
// This seems nicer and more useful than putting an ifdef around the use of std::cout:
std::stringstream debug;
std::ostream& ostr = debug;
const unsigned int magic_limit (5);
const unsigned int magic_limit(5);
void
setup_rand ()
setup_rand()
{
static bool setup (false);
static bool setup(false);
if (!setup)
{
std::srand (std::time (nullptr));
std::srand(std::time(nullptr));
setup = true;
}
}
gboolean*
c_get_bool_array ()
c_get_bool_array()
{
gboolean* array (static_cast<gboolean*> (g_malloc ((magic_limit + 1) * sizeof (gboolean))));
gboolean* array(static_cast<gboolean*>(g_malloc((magic_limit + 1) * sizeof(gboolean))));
setup_rand ();
for (unsigned int iter (0); iter < magic_limit; ++iter)
setup_rand();
for (unsigned int iter(0); iter < magic_limit; ++iter)
{
array[iter] = std::rand() % 2 ? TRUE : FALSE;
}
......@@ -58,40 +57,41 @@ c_get_bool_array ()
}
void
c_print_bool_array (gboolean* array)
c_print_bool_array(gboolean* array)
{
for (unsigned int iter (0); iter < magic_limit; ++iter)
for (unsigned int iter(0); iter < magic_limit; ++iter)
{
ostr << iter << ": " << (array[iter] ? "TRUE" : "FALSE") << "\n";
}
}
Glib::ArrayHandle<bool>
cxx_get_bool_array ()
cxx_get_bool_array()
{
return Glib::ArrayHandle<bool> (c_get_bool_array (), magic_limit, Glib::OWNERSHIP_SHALLOW);
return Glib::ArrayHandle<bool>(c_get_bool_array(), magic_limit, Glib::OWNERSHIP_SHALLOW);
}
void
cxx_print_bool_array (const Glib::ArrayHandle<bool>& array)
cxx_print_bool_array(const Glib::ArrayHandle<bool>& array)
{
c_print_bool_array (const_cast<gboolean*> (array.data ()));
c_print_bool_array(const_cast<gboolean*>(array.data()));
}
int main()
int
main()
{
Glib::init();
std::vector<bool> v (cxx_get_bool_array ());
std::list<bool> l (cxx_get_bool_array ());
std::deque<bool> d (cxx_get_bool_array ());
std::vector<bool> v(cxx_get_bool_array());
std::list<bool> l(cxx_get_bool_array());
std::deque<bool> d(cxx_get_bool_array());
ostr << "vector:\n";
cxx_print_bool_array (v);
cxx_print_bool_array(v);
ostr << "list:\n";
cxx_print_bool_array (l);
cxx_print_bool_array(l);
ostr << "deque:\n";
cxx_print_bool_array (d);
cxx_print_bool_array(d);
return EXIT_SUCCESS;