xpcom/glue/tests/gtest/TestFileUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/tests/gtest/TestFileUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,279 @@
     1.4 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +/* Test ReadSysFile() */
    1.10 +
    1.11 +#include <sys/types.h>
    1.12 +#include <sys/stat.h>
    1.13 +
    1.14 +#include <errno.h>
    1.15 +#include <fcntl.h>
    1.16 +#include <stdio.h>
    1.17 +#include <string.h>
    1.18 +
    1.19 +#include "FileUtils.h"
    1.20 +
    1.21 +#include "gtest/gtest.h"
    1.22 +
    1.23 +namespace mozilla {
    1.24 +
    1.25 +#ifdef ReadSysFile_PRESENT
    1.26 +
    1.27 +/**
    1.28 + * Create a file with the specified contents.
    1.29 + */
    1.30 +static bool
    1.31 +WriteFile(
    1.32 +  const char* aFilename,
    1.33 +  const void* aContents,
    1.34 +  size_t aContentsLen)
    1.35 +{
    1.36 +  int fd;
    1.37 +  ssize_t ret;
    1.38 +  size_t offt;
    1.39 +
    1.40 +  fd = MOZ_TEMP_FAILURE_RETRY(
    1.41 +    open(aFilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR));
    1.42 +  if (fd == -1) {
    1.43 +    fprintf(stderr, "open(): %s: %s\n", aFilename, strerror(errno));
    1.44 +    return false;
    1.45 +  }
    1.46 +
    1.47 +  offt = 0;
    1.48 +  do {
    1.49 +    ret = MOZ_TEMP_FAILURE_RETRY(
    1.50 +      write(fd, (char*)aContents + offt, aContentsLen - offt));
    1.51 +    if (ret == -1) {
    1.52 +      fprintf(stderr, "write(): %s: %s\n", aFilename, strerror(errno));
    1.53 +      close(fd);
    1.54 +      return false;
    1.55 +    }
    1.56 +    offt += ret;
    1.57 +  } while (offt < aContentsLen);
    1.58 +
    1.59 +  ret = MOZ_TEMP_FAILURE_RETRY(close(fd));
    1.60 +  if (ret == -1) {
    1.61 +    fprintf(stderr, "close(): %s: %s\n", aFilename, strerror(errno));
    1.62 +    return false;
    1.63 +  }
    1.64 +  return true;
    1.65 +}
    1.66 +
    1.67 +TEST(ReadSysFile, Nonexistent) {
    1.68 +  bool ret;
    1.69 +  int errno_saved;
    1.70 +
    1.71 +  ret = ReadSysFile("/nonexistent", nullptr, 0);
    1.72 +  errno_saved = errno;
    1.73 +
    1.74 +  ASSERT_FALSE(ret);
    1.75 +  ASSERT_EQ(errno_saved, ENOENT);
    1.76 +}
    1.77 +
    1.78 +TEST(ReadSysFile, Main) {
    1.79 +  /* Use a different file name for each test since different tests could be
    1.80 +  executed concurrently. */
    1.81 +  static const char* fn = "TestReadSysFileMain";
    1.82 +  /* If we have a file which contains "abcd" and we read it with ReadSysFile(),
    1.83 +  providing a buffer of size 10 bytes, we would expect 5 bytes to be written
    1.84 +  to that buffer: "abcd\0". */
    1.85 +  struct {
    1.86 +    /* input (file contents), e.g. "abcd" */
    1.87 +    const char* input;
    1.88 +    /* pretended output buffer size, e.g. 10; the actual buffer is larger
    1.89 +    and we check if anything was written past the end of the allowed length */
    1.90 +    size_t output_size;
    1.91 +    /* expected number of bytes written to the output buffer, including the
    1.92 +    terminating '\0', e.g. 5 */
    1.93 +    size_t output_len;
    1.94 +    /* expected output buffer contents, e.g. "abcd\0", the first output_len
    1.95 +    bytes of the output buffer should match the first 'output_len' bytes from
    1.96 +    'output', the rest of the output buffer should be untouched. */
    1.97 +    const char* output;
    1.98 +  } tests[] = {
    1.99 +    /* No new lines */
   1.100 +    {"", 0, 0, ""},
   1.101 +    {"", 1, 1, "\0"}, /* \0 is redundant, but we write it for clarity */
   1.102 +    {"", 9, 1, "\0"},
   1.103 +
   1.104 +    {"a", 0, 0, ""},
   1.105 +    {"a", 1, 1, "\0"},
   1.106 +    {"a", 2, 2, "a\0"},
   1.107 +    {"a", 9, 2, "a\0"},
   1.108 +
   1.109 +    {"abcd", 0, 0, ""},
   1.110 +    {"abcd", 1, 1, "\0"},
   1.111 +    {"abcd", 2, 2, "a\0"},
   1.112 +    {"abcd", 3, 3, "ab\0"},
   1.113 +    {"abcd", 4, 4, "abc\0"},
   1.114 +    {"abcd", 5, 5, "abcd\0"},
   1.115 +    {"abcd", 9, 5, "abcd\0"},
   1.116 +
   1.117 +    /* A single trailing new line */
   1.118 +    {"\n", 0, 0, ""},
   1.119 +    {"\n", 1, 1, "\0"},
   1.120 +    {"\n", 2, 1, "\0"},
   1.121 +    {"\n", 9, 1, "\0"},
   1.122 +
   1.123 +    {"a\n", 0, 0, ""},
   1.124 +    {"a\n", 1, 1, "\0"},
   1.125 +    {"a\n", 2, 2, "a\0"},
   1.126 +    {"a\n", 3, 2, "a\0"},
   1.127 +    {"a\n", 9, 2, "a\0"},
   1.128 +
   1.129 +    {"abcd\n", 0, 0, ""},
   1.130 +    {"abcd\n", 1, 1, "\0"},
   1.131 +    {"abcd\n", 2, 2, "a\0"},
   1.132 +    {"abcd\n", 3, 3, "ab\0"},
   1.133 +    {"abcd\n", 4, 4, "abc\0"},
   1.134 +    {"abcd\n", 5, 5, "abcd\0"},
   1.135 +    {"abcd\n", 6, 5, "abcd\0"},
   1.136 +    {"abcd\n", 9, 5, "abcd\0"},
   1.137 +
   1.138 +    /* Multiple trailing new lines */
   1.139 +    {"\n\n", 0, 0, ""},
   1.140 +    {"\n\n", 1, 1, "\0"},
   1.141 +    {"\n\n", 2, 2, "\n\0"},
   1.142 +    {"\n\n", 3, 2, "\n\0"},
   1.143 +    {"\n\n", 9, 2, "\n\0"},
   1.144 +
   1.145 +    {"a\n\n", 0, 0, ""},
   1.146 +    {"a\n\n", 1, 1, "\0"},
   1.147 +    {"a\n\n", 2, 2, "a\0"},
   1.148 +    {"a\n\n", 3, 3, "a\n\0"},
   1.149 +    {"a\n\n", 4, 3, "a\n\0"},
   1.150 +    {"a\n\n", 9, 3, "a\n\0"},
   1.151 +
   1.152 +    {"abcd\n\n", 0, 0, ""},
   1.153 +    {"abcd\n\n", 1, 1, "\0"},
   1.154 +    {"abcd\n\n", 2, 2, "a\0"},
   1.155 +    {"abcd\n\n", 3, 3, "ab\0"},
   1.156 +    {"abcd\n\n", 4, 4, "abc\0"},
   1.157 +    {"abcd\n\n", 5, 5, "abcd\0"},
   1.158 +    {"abcd\n\n", 6, 6, "abcd\n\0"},
   1.159 +    {"abcd\n\n", 7, 6, "abcd\n\0"},
   1.160 +    {"abcd\n\n", 9, 6, "abcd\n\0"},
   1.161 +
   1.162 +    /* New line in the middle */
   1.163 +    {"ab\ncd", 9, 6, "ab\ncd\0"},
   1.164 +  };
   1.165 +
   1.166 +  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
   1.167 +    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
   1.168 +    /* Leave the file to exist if some of the assertions fail. */
   1.169 +
   1.170 +    char buf[128];
   1.171 +    static const char unmodified = 'X';
   1.172 +
   1.173 +    memset(buf, unmodified, sizeof(buf));
   1.174 +
   1.175 +    ASSERT_TRUE(ReadSysFile(fn, buf, tests[i].output_size));
   1.176 +
   1.177 +    if (tests[i].output_size == 0) {
   1.178 +      /* The buffer must be unmodified. We check only the first byte. */
   1.179 +      ASSERT_EQ(unmodified, buf[0]);
   1.180 +    } else {
   1.181 +      ASSERT_EQ(tests[i].output_len, strlen(buf) + 1);
   1.182 +      ASSERT_STREQ(tests[i].output, buf);
   1.183 +      /* Check that the first byte after the trailing '\0' has not been
   1.184 +      modified. */
   1.185 +      ASSERT_EQ(unmodified, buf[tests[i].output_len]);
   1.186 +    }
   1.187 +  }
   1.188 +
   1.189 +  unlink(fn);
   1.190 +}
   1.191 +
   1.192 +TEST(ReadSysFile, Int) {
   1.193 +  static const char* fn = "TestReadSysFileInt";
   1.194 +  struct {
   1.195 +    /* input (file contents), e.g. "5" */
   1.196 +    const char* input;
   1.197 +    /* expected return value, if false, then the output is not checked */
   1.198 +    bool ret;
   1.199 +    /* expected result */
   1.200 +    int output;
   1.201 +  } tests[] = {
   1.202 +    {"0", true, 0},
   1.203 +    {"00", true, 0},
   1.204 +    {"1", true, 1},
   1.205 +    {"5", true, 5},
   1.206 +    {"55", true, 55},
   1.207 +
   1.208 +    {" 123", true, 123},
   1.209 +    {"123 ", true, 123},
   1.210 +    {" 123 ", true, 123},
   1.211 +    {"123\n", true, 123},
   1.212 +
   1.213 +    {"", false, 0},
   1.214 +    {" ", false, 0},
   1.215 +    {"a", false, 0},
   1.216 +
   1.217 +    {"-1", true, -1},
   1.218 +    {" -456 ", true, -456},
   1.219 +    {" -78.9 ", true, -78},
   1.220 +  };
   1.221 +
   1.222 +  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
   1.223 +    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
   1.224 +    /* Leave the file to exist if some of the assertions fail. */
   1.225 +
   1.226 +    bool ret;
   1.227 +    int output = 424242;
   1.228 +
   1.229 +    ret = ReadSysFile(fn, &output);
   1.230 +
   1.231 +    ASSERT_EQ(tests[i].ret, ret);
   1.232 +
   1.233 +    if (ret) {
   1.234 +      ASSERT_EQ(tests[i].output, output);
   1.235 +    }
   1.236 +  }
   1.237 +
   1.238 +  unlink(fn);
   1.239 +}
   1.240 +
   1.241 +TEST(ReadSysFile, Bool) {
   1.242 +  static const char* fn = "TestReadSysFileBool";
   1.243 +  struct {
   1.244 +    /* input (file contents), e.g. "1" */
   1.245 +    const char* input;
   1.246 +    /* expected return value */
   1.247 +    bool ret;
   1.248 +    /* expected result */
   1.249 +    bool output;
   1.250 +  } tests[] = {
   1.251 +    {"0", true, false},
   1.252 +    {"00", true, false},
   1.253 +    {"1", true, true},
   1.254 +    {"5", true, true},
   1.255 +    {"23", true, true},
   1.256 +    {"-1", true, true},
   1.257 +
   1.258 +    {"", false, true /* unused */},
   1.259 +  };
   1.260 +
   1.261 +  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
   1.262 +    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
   1.263 +    /* Leave the file to exist if some of the assertions fail. */
   1.264 +
   1.265 +    bool ret;
   1.266 +    bool output;
   1.267 +
   1.268 +    ret = ReadSysFile(fn, &output);
   1.269 +
   1.270 +    ASSERT_EQ(tests[i].ret, ret);
   1.271 +
   1.272 +    if (ret) {
   1.273 +      ASSERT_EQ(tests[i].output, output);
   1.274 +    }
   1.275 +  }
   1.276 +
   1.277 +  unlink(fn);
   1.278 +}
   1.279 +
   1.280 +#endif /* ReadSysFile_PRESENT */
   1.281 +
   1.282 +}

mercurial