summaryrefslogtreecommitdiff
path: root/src/test-check.cc
blob: f62d175bc2e329f8193f42b015bf1e0b69cb6a60 (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
// Copyright © 2014 Richard Kettlewell.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#include <config.h>
#include "Utils.h"
#include "Errors.h"
#include "Command.h"
#include <cassert>
#include <pthread.h>
#include <unistd.h>
#include <cstdio>

static FILE *input;
static int output;
static bool result;

static void *background(void *) {
  result = check("%s", "spong");
  return NULL;
}

/* We can't use stdio here because reading from an unbuffered file causes a
 * flush of other files, which (at least in Glibc) implies taking a lock on
 * other files.  Since the background thread is blocked in a read, holding a
 * lock while it does so, a deadlock results.
 *
 * References:
 * - C99 7.19.3#3
 * - http://austingroupbugs.net/view.php?id=689
 * - _IO_flush_all_linebuffered in Glibc
 */
static int fd_getc(int fd) {
  unsigned char buffer[1];
  if(read(fd, buffer, 1) < 0)
    return EOF;
  else
    return buffer[0];
}

static char *fd_fgets(char buffer[], size_t bufsize, int fd) {
  size_t index = 0;
  int ch;
  assert(bufsize > 0);
  while(index + 1 < bufsize && (ch = fd_getc(fd)) >= 0) {
    buffer[index++] = ch;
    if(ch == '\n')
      break;
  }
  buffer[index] = 0;
  return ch < 0 ? NULL : buffer;
}

static void test(const char *typed, const char *typed2, bool expect) {
  pthread_t tid;
  char buffer[1024];

  assert(pthread_create(&tid, NULL, background, NULL) == 0);
  assert(fd_fgets(buffer, sizeof buffer, output));
  assert(std::string(buffer) == "spong\n");
  assert(fd_getc(output) == 'y');
  assert(fd_getc(output) == 'e');
  assert(fd_getc(output) == 's');
  assert(fd_getc(output) == '/');
  assert(fd_getc(output) == 'n');
  assert(fd_getc(output) == 'o');
  assert(fd_getc(output) == '>');
  assert(fd_getc(output) == ' ');
  assert(fprintf(input, "%s\n", typed) >= 0);
  assert(fflush(input) >= 0);

  if(typed2) {
    assert(fd_fgets(buffer, sizeof buffer, output));
    assert(std::string(buffer) == "Please answer 'yes' or 'no'.\n");
    assert(fd_fgets(buffer, sizeof buffer, output));
    assert(std::string(buffer) == "spong\n");
    assert(fd_getc(output) == 'y');
    assert(fd_getc(output) == 'e');
    assert(fd_getc(output) == 's');
    assert(fd_getc(output) == '/');
    assert(fd_getc(output) == 'n');
    assert(fd_getc(output) == 'o');
    assert(fd_getc(output) == '>');
    assert(fd_getc(output) == ' ');
    assert(fprintf(input, "%s\n", typed2) >= 0);
    assert(fflush(input) >= 0);
  }

  assert(pthread_join(tid, NULL) == 0);
  assert(result == expect);
}

static void test_force(void) {
  pthread_t tid;

  command.force = true;
  assert(pthread_create(&tid, NULL, background, NULL) == 0);
  assert(pthread_join(tid, NULL) == 0);
  assert(result == true);
  command.force = false;
}

int main() {
  int i[2], o[2];

  assert(pipe(i) >= 0);
  assert(pipe(o) >= 0);
  assert(dup2(i[0], 0) >= 0);
  assert(close(i[0]) >= 0);
  assert(dup2(o[1], 1) >= 0);
  assert(close(o[1]) >= 0);
  assert((input = fdopen(i[1], "w")));
  output = o[0];

  test("yes", NULL, true);
  test("no", NULL, false);
  test("", "yes", true);
  test("whatever", "yes", true);
  test_force();

  assert(fclose(input) >= 0);
  try {
    check("%s", "spong");
    assert(!"unexpectedly succeeded");
  } catch(IOError &e) {
  }

  return 0;
}