aboutsummaryrefslogtreecommitdiff
path: root/tests/curl/driver.cxx
blob: 18ed1e630162b7968f22af4e5428b8b12236e974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// file      : tests/curl/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <iostream>
#include <system_error>
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.curl;
import butl.path;
import butl.process;
import butl.utility; // operator<<(ostream, exception)
import butl.fdstream;

import butl.optional;     // @@ MOD Clang should not be necessary.
import butl.small_vector; // @@ MOD Clang should not be necessary.
#else
#include <libbutl/curl.mxx>
#include <libbutl/path.mxx>
#include <libbutl/process.mxx>
#include <libbutl/utility.mxx>
#include <libbutl/fdstream.mxx>
#endif

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, fdopen_null (), 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, fdopen_null (), 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, fdopen_null (), 2, curl::get, u + "/bogus");
    assert (!c.wait ());
  }

  // GET to /dev/null.
  //
  {
    curl c (p, nullfd, fdopen_null (), 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;
}