File: test_stat.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (72 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (9)
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
//
// Copyright (c) 2020 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/nowide/stat.hpp>

#include <boost/nowide/cstdio.hpp>
#include "test.hpp"
#ifdef BOOST_WINDOWS
#include <errno.h>
#endif

// coverity[root_function]
void test_main(int, char** argv, char**)
{
    const std::string prefix = argv[0];
    const std::string filename = prefix + "\xd7\xa9-\xd0\xbc-\xce\xbd.txt";

    // Make sure file does not exist
    boost::nowide::remove(filename.c_str());

    std::cout << " -- stat - non-existing file" << std::endl;
    {
#ifdef BOOST_WINDOWS
        struct _stat stdStat;
#else
        struct stat stdStat;
#endif
        TEST(boost::nowide::stat(filename.c_str(), &stdStat) != 0);
        boost::nowide::stat_t boostStat;
        TEST(boost::nowide::stat(filename.c_str(), &boostStat) != 0);
    }

    std::cout << " -- stat - existing file" << std::endl;
    FILE* f = boost::nowide::fopen(filename.c_str(), "wb");
    TEST(f);
    const char testData[] = "Hello World";
    constexpr size_t testDataSize = sizeof(testData);
    TEST_EQ(std::fwrite(testData, sizeof(char), testDataSize, f), testDataSize);
    std::fclose(f);
    {
#ifdef BOOST_WINDOWS
        struct _stat stdStat;
#else
        struct stat stdStat;
#endif /*  */
        TEST_EQ(boost::nowide::stat(filename.c_str(), &stdStat), 0);
        TEST_EQ(static_cast<size_t>(stdStat.st_size), testDataSize);
        boost::nowide::stat_t boostStat;
        TEST_EQ(boost::nowide::stat(filename.c_str(), &boostStat), 0);
        TEST_EQ(static_cast<size_t>(boostStat.st_size), testDataSize);
    }

#ifdef BOOST_WINDOWS
    std::cout << " -- stat - invalid struct size" << std::endl;
    {
        struct _stat stdStat;
        // Simulate passing a struct that is 4 bytes smaller, e.g. if it uses 32 bit time field instead of 64 bit
        // Need to use the detail function directly
        TEST_EQ(boost::nowide::detail::stat(filename.c_str(), &stdStat, sizeof(stdStat) - 4u), EINVAL);
        TEST_EQ(errno, EINVAL);
        // Same for our stat_t
        boost::nowide::stat_t boostStat;
        TEST_EQ(boost::nowide::detail::stat(filename.c_str(), &boostStat, sizeof(boostStat) - 4u), EINVAL);
        TEST_EQ(errno, EINVAL);
    }
#endif

    boost::nowide::remove(filename.c_str());
}