summaryrefslogtreecommitdiffstats
path: root/xpcom/glue/tests/gtest/TestFileUtils.cpp
blob: 55106c6c5fbadc2094b73ba72af9fb29a16a693b (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/* Test ReadSysFile() */

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>

#include "FileUtils.h"

#include "gtest/gtest.h"

namespace mozilla {

#ifdef ReadSysFile_PRESENT

/**
 * Create a file with the specified contents.
 */
static bool
WriteFile(
  const char* aFilename,
  const void* aContents,
  size_t aContentsLen)
{
  int fd;
  ssize_t ret;
  size_t offt;

  fd = MOZ_TEMP_FAILURE_RETRY(
    open(aFilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR));
  if (fd == -1) {
    fprintf(stderr, "open(): %s: %s\n", aFilename, strerror(errno));
    return false;
  }

  offt = 0;
  do {
    ret = MOZ_TEMP_FAILURE_RETRY(
      write(fd, (char*)aContents + offt, aContentsLen - offt));
    if (ret == -1) {
      fprintf(stderr, "write(): %s: %s\n", aFilename, strerror(errno));
      close(fd);
      return false;
    }
    offt += ret;
  } while (offt < aContentsLen);

  ret = MOZ_TEMP_FAILURE_RETRY(close(fd));
  if (ret == -1) {
    fprintf(stderr, "close(): %s: %s\n", aFilename, strerror(errno));
    return false;
  }
  return true;
}

TEST(ReadSysFile, Nonexistent) {
  bool ret;
  int errno_saved;

  ret = ReadSysFile("/nonexistent", nullptr, 0);
  errno_saved = errno;

  ASSERT_FALSE(ret);
  ASSERT_EQ(errno_saved, ENOENT);
}

TEST(ReadSysFile, Main) {
  /* Use a different file name for each test since different tests could be
  executed concurrently. */
  static const char* fn = "TestReadSysFileMain";
  /* If we have a file which contains "abcd" and we read it with ReadSysFile(),
  providing a buffer of size 10 bytes, we would expect 5 bytes to be written
  to that buffer: "abcd\0". */
  struct
  {
    /* input (file contents), e.g. "abcd" */
    const char* input;
    /* pretended output buffer size, e.g. 10; the actual buffer is larger
    and we check if anything was written past the end of the allowed length */
    size_t output_size;
    /* expected number of bytes written to the output buffer, including the
    terminating '\0', e.g. 5 */
    size_t output_len;
    /* expected output buffer contents, e.g. "abcd\0", the first output_len
    bytes of the output buffer should match the first 'output_len' bytes from
    'output', the rest of the output buffer should be untouched. */
    const char* output;
  } tests[] = {
    /* No new lines */
    {"", 0, 0, ""},
    {"", 1, 1, "\0"}, /* \0 is redundant, but we write it for clarity */
    {"", 9, 1, "\0"},

    {"a", 0, 0, ""},
    {"a", 1, 1, "\0"},
    {"a", 2, 2, "a\0"},
    {"a", 9, 2, "a\0"},

    {"abcd", 0, 0, ""},
    {"abcd", 1, 1, "\0"},
    {"abcd", 2, 2, "a\0"},
    {"abcd", 3, 3, "ab\0"},
    {"abcd", 4, 4, "abc\0"},
    {"abcd", 5, 5, "abcd\0"},
    {"abcd", 9, 5, "abcd\0"},

    /* A single trailing new line */
    {"\n", 0, 0, ""},
    {"\n", 1, 1, "\0"},
    {"\n", 2, 1, "\0"},
    {"\n", 9, 1, "\0"},

    {"a\n", 0, 0, ""},
    {"a\n", 1, 1, "\0"},
    {"a\n", 2, 2, "a\0"},
    {"a\n", 3, 2, "a\0"},
    {"a\n", 9, 2, "a\0"},

    {"abcd\n", 0, 0, ""},
    {"abcd\n", 1, 1, "\0"},
    {"abcd\n", 2, 2, "a\0"},
    {"abcd\n", 3, 3, "ab\0"},
    {"abcd\n", 4, 4, "abc\0"},
    {"abcd\n", 5, 5, "abcd\0"},
    {"abcd\n", 6, 5, "abcd\0"},
    {"abcd\n", 9, 5, "abcd\0"},

    /* Multiple trailing new lines */
    {"\n\n", 0, 0, ""},
    {"\n\n", 1, 1, "\0"},
    {"\n\n", 2, 2, "\n\0"},
    {"\n\n", 3, 2, "\n\0"},
    {"\n\n", 9, 2, "\n\0"},

    {"a\n\n", 0, 0, ""},
    {"a\n\n", 1, 1, "\0"},
    {"a\n\n", 2, 2, "a\0"},
    {"a\n\n", 3, 3, "a\n\0"},
    {"a\n\n", 4, 3, "a\n\0"},
    {"a\n\n", 9, 3, "a\n\0"},

    {"abcd\n\n", 0, 0, ""},
    {"abcd\n\n", 1, 1, "\0"},
    {"abcd\n\n", 2, 2, "a\0"},
    {"abcd\n\n", 3, 3, "ab\0"},
    {"abcd\n\n", 4, 4, "abc\0"},
    {"abcd\n\n", 5, 5, "abcd\0"},
    {"abcd\n\n", 6, 6, "abcd\n\0"},
    {"abcd\n\n", 7, 6, "abcd\n\0"},
    {"abcd\n\n", 9, 6, "abcd\n\0"},

    /* New line in the middle */
    {"ab\ncd", 9, 6, "ab\ncd\0"},
  };

  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
    /* Leave the file to exist if some of the assertions fail. */

    char buf[128];
    static const char unmodified = 'X';

    memset(buf, unmodified, sizeof(buf));

    ASSERT_TRUE(ReadSysFile(fn, buf, tests[i].output_size));

    if (tests[i].output_size == 0) {
      /* The buffer must be unmodified. We check only the first byte. */
      ASSERT_EQ(unmodified, buf[0]);
    } else {
      ASSERT_EQ(tests[i].output_len, strlen(buf) + 1);
      ASSERT_STREQ(tests[i].output, buf);
      /* Check that the first byte after the trailing '\0' has not been
      modified. */
      ASSERT_EQ(unmodified, buf[tests[i].output_len]);
    }
  }

  unlink(fn);
}

TEST(ReadSysFile, Int) {
  static const char* fn = "TestReadSysFileInt";
  struct
  {
    /* input (file contents), e.g. "5" */
    const char* input;
    /* expected return value, if false, then the output is not checked */
    bool ret;
    /* expected result */
    int output;
  } tests[] = {
    {"0", true, 0},
    {"00", true, 0},
    {"1", true, 1},
    {"5", true, 5},
    {"55", true, 55},

    {" 123", true, 123},
    {"123 ", true, 123},
    {" 123 ", true, 123},
    {"123\n", true, 123},

    {"", false, 0},
    {" ", false, 0},
    {"a", false, 0},

    {"-1", true, -1},
    {" -456 ", true, -456},
    {" -78.9 ", true, -78},
  };

  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
    /* Leave the file to exist if some of the assertions fail. */

    bool ret;
    int output = 424242;

    ret = ReadSysFile(fn, &output);

    ASSERT_EQ(tests[i].ret, ret);

    if (ret) {
      ASSERT_EQ(tests[i].output, output);
    }
  }

  unlink(fn);
}

TEST(ReadSysFile, Bool) {
  static const char* fn = "TestReadSysFileBool";
  struct
  {
    /* input (file contents), e.g. "1" */
    const char* input;
    /* expected return value */
    bool ret;
    /* expected result */
    bool output;
  } tests[] = {
    {"0", true, false},
    {"00", true, false},
    {"1", true, true},
    {"5", true, true},
    {"23", true, true},
    {"-1", true, true},

    {"", false, true /* unused */},
  };

  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
    /* Leave the file to exist if some of the assertions fail. */

    bool ret;
    bool output;

    ret = ReadSysFile(fn, &output);

    ASSERT_EQ(tests[i].ret, ret);

    if (ret) {
      ASSERT_EQ(tests[i].output, output);
    }
  }

  unlink(fn);
}

#endif /* ReadSysFile_PRESENT */

} // namespace mozilla