non_existent_script.cc 2.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
** Copyright 2012 Merethis
**
** This file is part of Centreon Connector Perl.
**
** Centreon Connector Perl is free software: you can redistribute it
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
** Centreon Connector Perl is distributed in the hope that it will be
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
** License along with Centreon Connector Perl. If not, see
** <http://www.gnu.org/licenses/>.
*/

Dorian Guillois's avatar
Dorian Guillois committed
21
22
#include <cstring>
#include <iostream>
23
24
#include <sstream>
#include <string>
25
#include "com/centreon/clib.hh"
Dorian Guillois's avatar
Dorian Guillois committed
26
#include "com/centreon/exceptions/basic.hh"
27
#include "com/centreon/process.hh"
28
#include "test/connector/paths.hh"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

using namespace com::centreon;

#define CMD "2\0" \
            "4242\0" \
            "3\0" \
            "123456789\0" \
            "/non/existent/script.pl\0\0\0\0"
#define RESULT "3\0" \
               "4242\0" \
               "0\0" \
               "-1\0" \
               " \0" \
               " \0\0\0\0"

/**
 *  Check that connector respond properly when running a non existent
 *  script.
 *
 *  @return 0 on success.
 */
int main() {
51
  clib::load();
52
53
  // Process.
  process p;
54
55
  p.enable_stream(process::in, true);
  p.enable_stream(process::out, true);
56
57
58
59
60
61
62
63
64
65
66
67
68
  p.exec(CONNECTOR_PERL_BINARY);

  // Write command.
  std::ostringstream oss;
  oss.write(CMD, sizeof(CMD) - 1);
  std::string cmd(oss.str());
  char const* ptr(cmd.c_str());
  unsigned int size(cmd.size());
  while (size > 0) {
    unsigned int rb(p.write(ptr, size));
    size -= rb;
    ptr += rb;
  }
69
  p.enable_stream(process::in, false);
70
71

  // Read reply.
72
73
74
75
76
77
78
  std::string output;
  while (true) {
    std::string buffer;
    p.read(buffer);
    if (buffer.empty())
      break;
    output.append(buffer);
79
80
81
  }

  // Wait for process termination.
82
83
  int retval(1);
  if (!p.wait(4000)) {
84
85
86
87
    p.terminate();
    p.wait();
  }
  else
88
    retval = (p.exit_code() != 0);
89

90
  clib::unload();
Dorian Guillois's avatar
Dorian Guillois committed
91
92
93
94

  try {
    if (retval)
      throw (basic_error() << "invalid return code: " << retval);
Dorian Guillois's avatar
Dorian Guillois committed
95
96
97
    if (output.find("could not run") == std::string::npos
        && (output.size() != (sizeof(RESULT) - 1)
            || memcmp(output.c_str(), RESULT, sizeof(RESULT) - 1)))
Dorian Guillois's avatar
Dorian Guillois committed
98
      throw (basic_error()
Dorian Guillois's avatar
Dorian Guillois committed
99
100
             << "invalid output: size=" << output.size()
             << ", output=" << output);
Dorian Guillois's avatar
Dorian Guillois committed
101
102
103
104
105
106
107
  }
  catch (std::exception const& e) {
    retval = 1;
    std::cerr << "error: " << e.what() << std::endl;
  }

  return (retval);
108
}