From ed93e07b1b7a9e0ba99609a9223e43247ff4224e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 18 Apr 2017 10:40:18 +0200 Subject: Implement curl process --- tests/buildfile | 2 +- tests/curl/buildfile | 7 +++ tests/curl/driver.cxx | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/curl/testscript | 57 ++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/curl/buildfile create mode 100644 tests/curl/driver.cxx create mode 100644 tests/curl/testscript (limited to 'tests') diff --git a/tests/buildfile b/tests/buildfile index efc54b8..6761a42 100644 --- a/tests/buildfile +++ b/tests/buildfile @@ -2,5 +2,5 @@ # copyright : Copyright (c) 2014-2017 Code Synthesis Ltd # license : MIT; see accompanying LICENSE file -./: {*/ -sendmail/} +./: {*/ -curl/ -sendmail/} diff --git a/tests/curl/buildfile b/tests/curl/buildfile new file mode 100644 index 0000000..6b19bea --- /dev/null +++ b/tests/curl/buildfile @@ -0,0 +1,7 @@ +# file : tests/curl/buildfile +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +exe{driver}: cxx{driver} ../../butl/lib{butl} test{testscript} + +include ../../butl/ diff --git a/tests/curl/driver.cxx b/tests/curl/driver.cxx new file mode 100644 index 0000000..3711e71 --- /dev/null +++ b/tests/curl/driver.cxx @@ -0,0 +1,121 @@ +// file : tests/curl/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include +#include + +#include +#include // operator<<(ostream, exception) +#include + +using namespace std; +using namespace butl; + +// Usage: argv[0] tftp|http +// + +static void +print_cmd (const char* c[], std::size_t n) +{ + cerr << endl; + process::print (cerr, c, n); + cerr << endl; +} + +static void +tftp () +{ + string u ("tftp://localhost:55123/test-driver/tftp"); + + auto p = print_cmd; + + // GET non-existent. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u + "/foo"); + assert (!c.wait ()); + } + + // PUT from file. + // + { + curl c (p, path ("foo-src"), nullfd, 2, curl::put, u + "/foo"); + assert (c.wait ()); + } + + // PUT from stream. + // + { + curl c (p, path ("-"), nullfd, 2, curl::put, u + "/bar"); + c.out << "bar" << endl; + c.out.close (); + assert (c.wait ()); + } + + // GET to stream. + // + { + curl c (p, nullfd, path ("-"), 2, curl::get, u + "/foo"); + string s; + getline (c.in, s); + c.in.close (); + assert (c.wait () && s == "foo"); + } + + // GET to /dev/null. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u + "/foo"); + assert (c.wait ()); + } +} + +static void +http () +{ + string u ("https://build2.org"); + + auto p = print_cmd; + + // GET non-existent. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u + "/bogus"); + assert (!c.wait ()); + } + + // GET to /dev/null. + // + { + curl c (p, nullfd, fdnull (), 2, curl::get, u); + assert (c.wait ()); + } + + // POST from stream. + // + { + curl c (p, path ("-"), 1, 2, curl::post, u + "/bogus"); + c.out << "bogus" << endl; + c.out.close (); + assert (!c.wait ()); + } +} + +int +main (int argc, const char* argv[]) +try +{ + assert (argc == 2); + + string a (argv[1]); + + if (a == "tftp") tftp (); + else if (a == "http") http (); + else assert (false); +} +catch (const system_error& e) +{ + cerr << argv[0] << ':' << argv[1] << ": " << e << endl; + return 1; +} diff --git a/tests/curl/testscript b/tests/curl/testscript new file mode 100644 index 0000000..e41aeb6 --- /dev/null +++ b/tests/curl/testscript @@ -0,0 +1,57 @@ +# file : tests/curl/testscript +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +#\ + +TFTP server (tftp-hpa) setup: from the test out_base, run (sudo is required +for --secure/chroot): + +sudo /usr/sbin/in.tftpd \ + --foreground \ + --address 127.0.0.1:55123 \ + --user "$(whoami)" \ + --permissive \ + --create \ + --secure \ + "$(pwd)" + +#\ + +: tftp +: +{ + echo 'foo' >=foo-src; + + $* 'tftp' &foo &bar 2>>EOE; + + curl -s -S tftp://localhost:55123/test-driver/tftp/foo + curl: (68) TFTP: File Not Found + + curl -s -S --upload-file foo-src tftp://localhost:55123/test-driver/tftp/foo + + curl -s -S --upload-file - tftp://localhost:55123/test-driver/tftp/bar + + curl -s -S tftp://localhost:55123/test-driver/tftp/foo + + curl -s -S tftp://localhost:55123/test-driver/tftp/foo + EOE + + diff -u foo-src foo; + diff -u - bar <'bar' +} + +: http +: +{ + $* 'http' 2>>EOE + + curl -s -S --fail --location https://build2.org/bogus + curl: (22) The requested URL returned error: 404 Not Found + + curl -s -S --fail --location https://build2.org + + curl -s -S --fail --location --data-binary @- https://build2.org/bogus + curl: (22) The requested URL returned error: 404 Not Found + EOE +} -- cgit v1.1