1/*
2 * Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include <fcntl.h>
8#include <stdio.h>
9#include <string.h>
10#include <sys/stat.h>
11#include <sys/types.h>
12#include <unistd.h>
13
14int
15main (int argc, const char *argv[])
16{
17  int f;
18  const char *stuff = "stuff";
19  const int stuff_len = strlen(stuff) + 1;
20  char read_back[10];
21  int retval = 0;
22
23  if (argc != 3) {
24    fprintf (stderr, "Usage: %s <file_name> <redirected_file>\n", argv[0]);
25    return 1;
26  }
27
28  f = open (argv[1], O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU | S_IROTH);
29  if (f == -1) {
30    fprintf (stderr, "Inconclusive: Could not open file to write.\n");
31    return 1;
32  }
33  if (write (f, stuff, stuff_len) < stuff_len) {
34    fprintf (stderr, "Inconclusive: Could not write to the file.\n");
35    return 1;
36  }
37
38  if (close (f) != 0) {
39    fprintf (stderr, "Inconclusive: Error closing write file.\n");
40    return 1;
41  }
42
43  f = open (argv[2], O_RDONLY);
44  if (f == -1) {
45    retval = 1;
46    fprintf (stderr, "Failed. Couldn't open file to read.\n");
47  } else if (read (f, read_back, stuff_len) != stuff_len) {
48    retval = 1;
49    fprintf (stderr, "Failed. Couldn't read back data.\n");
50  } else if (strncmp (stuff, read_back, stuff_len) != 0) {
51    retval = 1;
52    fprintf (stderr, "Failed. The read back string does not match the orignial."
53                     " Original: |%s|, Read back: |%s|\n",
54                     stuff, read_back);
55  } else {
56    fprintf (stdout, "Success. Woohoo!\n");
57  }
58
59  if (f != -1)
60    close (f);
61
62  return retval;
63}
64