test_system.cc
Go to the documentation of this file.
00001 #include <boost/test/auto_unit_test.hpp>
00002 
00003 #include "testsuite.hh"
00004 #include <utilmm/system/system.hh>
00005 #include <utilmm/system/socket.hh>
00006 #include <utilmm/system/endian.hh>
00007 #include <boost/filesystem/operations.hpp>
00008 #include <boost/lexical_cast.hpp>
00009 #include <iostream>
00010 #include <errno.h>
00011 using namespace utilmm;
00012 using std::string;
00013 
00014 BOOST_AUTO_TEST_CASE( test_swap_endian )
00015 {
00016     BOOST_REQUIRE_EQUAL(0x10, endian::swap<int8_t>(0x10));
00017     BOOST_REQUIRE_EQUAL(0x1032, endian::swap<int16_t>(0x3210));
00018     BOOST_REQUIRE_EQUAL(0x10325476, endian::swap<int32_t>(0x76543210));
00019     BOOST_REQUIRE_EQUAL(0x1032547698badcfeLL,endian::swap<int64_t>(0xfedcba9876543210LL));
00020 }
00021 
00022 BOOST_AUTO_TEST_CASE( test_filetools )
00023 {
00024     // Open a file and test auto_close
00025     int files[2];
00026     pipe(files);
00027     { auto_close close_read(files[0]); }
00028     BOOST_REQUIRE( close(files[0]) == -1 && errno == EBADF );
00029 
00030     {
00031         auto_close close_write(files[1]);
00032         BOOST_REQUIRE(close_write.handle<int>() == files[1]);
00033         BOOST_REQUIRE_THROW(close_write.handle<FILE*>(), std::bad_cast);
00034         close_write.detach();
00035     }
00036     BOOST_REQUIRE( close(files[1]) == 0 );
00037 }
00038 
00039 BOOST_AUTO_TEST_CASE( test_tempfile )
00040 {
00041     boost::filesystem::path tmppath;
00042     {
00043         tempfile file("bla");
00044         tmppath = file.path();
00045         BOOST_REQUIRE(boost::filesystem::exists(tmppath));
00046     }
00047     BOOST_REQUIRE(!boost::filesystem::exists(tmppath));
00048 
00049     {
00050         tempfile file("blo");
00051         tmppath = file.path();
00052         std::cout << path_to_string(tmppath) << std::endl;
00053         BOOST_REQUIRE(boost::filesystem::exists(tmppath));
00054         file.detach();
00055         BOOST_REQUIRE(boost::filesystem::exists(tmppath));
00056         BOOST_REQUIRE_EQUAL(file.handle(), (FILE*) 0);
00057     }
00058     BOOST_REQUIRE(boost::filesystem::exists(tmppath));
00059     boost::filesystem::remove(tmppath);
00060 }
00061 
00062 BOOST_AUTO_TEST_CASE( test_socket )
00063 {
00064     // Allocate a random port in between 30000 and 60000. This is not perfect,
00065     // but helps mitigate some issues with TCP WAIT_STATE, and the possibility
00066     // of having a server on the chosen port
00067     srand(time(0));
00068     std::string port = boost::lexical_cast<std::string>(static_cast<long long>(rand()) * 30000 / RAND_MAX + 30000);
00069     std::string other_port = boost::lexical_cast<std::string>(static_cast<long long>(rand()) * 30000 / RAND_MAX + 30000);
00070 
00071     // First create a IP server socket
00072     server_socket server(server_socket::Inet, server_socket::Stream, string("0.0.0.0:") + port);
00073     // This should throw since the port is already used
00074     BOOST_REQUIRE_THROW(server_socket another_server(server_socket::Inet, server_socket::Stream, string("0.0.0.0:") + port), unix_error);
00075 
00076     // Create a socket and connect it to the server
00077     socket client(socket::Inet, socket::Stream, "localhost:" + port);
00078     // This should throw since there is nothing on the port
00079     BOOST_REQUIRE_THROW(socket another_client(socket::Inet, socket::Stream, string("127.0.0.1:") + other_port), unix_error);
00080 
00081     server.wait();
00082     BOOST_REQUIRE(server.try_wait());
00083     std::auto_ptr<socket> accepted(server.accept());
00084 
00085     // Should not have an incoming connection
00086     BOOST_REQUIRE(!server.try_wait());
00087 
00088     // write on the connection and check that 
00089     // we get data on the other end
00090     accepted->write("blabla", 7);
00091     char buffer[7];
00092 
00093     //accepted->wait(socket::WaitWrite);
00094     client.wait(socket::WaitRead);
00095     //BOOST_REQUIRE(client.try_wait(socket::WaitRead));
00096     client.read(buffer, 7);
00097     BOOST_REQUIRE(!client.try_wait(socket::WaitRead));
00098     BOOST_REQUIRE_EQUAL(std::string(buffer), std::string("blabla"));
00099 }


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Wed Sep 16 2015 07:05:43