]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // |
2 | // Copyright (c) 2020 Alexander Grund | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See | |
5 | // accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #include <boost/nowide/stat.hpp> | |
9 | ||
10 | #include <boost/nowide/cstdio.hpp> | |
1e59de90 | 11 | #include "test.hpp" |
20effc67 TL |
12 | #ifdef BOOST_WINDOWS |
13 | #include <errno.h> | |
14 | #endif | |
15 | ||
1e59de90 | 16 | // coverity [root_function] |
20effc67 TL |
17 | void test_main(int, char** argv, char**) |
18 | { | |
19 | const std::string prefix = argv[0]; | |
20 | const std::string filename = prefix + "\xd7\xa9-\xd0\xbc-\xce\xbd.txt"; | |
21 | ||
22 | // Make sure file does not exist | |
23 | boost::nowide::remove(filename.c_str()); | |
24 | ||
25 | std::cout << " -- stat - non-existing file" << std::endl; | |
26 | { | |
27 | #ifdef BOOST_WINDOWS | |
28 | struct _stat stdStat; | |
29 | #else | |
30 | struct stat stdStat; | |
31 | #endif | |
32 | TEST(boost::nowide::stat(filename.c_str(), &stdStat) != 0); | |
33 | boost::nowide::stat_t boostStat; | |
34 | TEST(boost::nowide::stat(filename.c_str(), &boostStat) != 0); | |
35 | } | |
36 | ||
37 | std::cout << " -- stat - existing file" << std::endl; | |
38 | FILE* f = boost::nowide::fopen(filename.c_str(), "wb"); | |
39 | TEST(f); | |
40 | const char testData[] = "Hello World"; | |
41 | constexpr size_t testDataSize = sizeof(testData); | |
1e59de90 | 42 | TEST_EQ(std::fwrite(testData, sizeof(char), testDataSize, f), testDataSize); |
20effc67 TL |
43 | std::fclose(f); |
44 | { | |
45 | #ifdef BOOST_WINDOWS | |
46 | struct _stat stdStat; | |
47 | #else | |
48 | struct stat stdStat; | |
49 | #endif /* */ | |
1e59de90 TL |
50 | TEST_EQ(boost::nowide::stat(filename.c_str(), &stdStat), 0); |
51 | TEST_EQ(static_cast<size_t>(stdStat.st_size), testDataSize); | |
20effc67 | 52 | boost::nowide::stat_t boostStat; |
1e59de90 TL |
53 | TEST_EQ(boost::nowide::stat(filename.c_str(), &boostStat), 0); |
54 | TEST_EQ(static_cast<size_t>(boostStat.st_size), testDataSize); | |
20effc67 TL |
55 | } |
56 | ||
57 | #ifdef BOOST_WINDOWS | |
58 | std::cout << " -- stat - invalid struct size" << std::endl; | |
59 | { | |
60 | struct _stat stdStat; | |
61 | // Simulate passing a struct that is 4 bytes smaller, e.g. if it uses 32 bit time field instead of 64 bit | |
62 | // Need to use the detail function directly | |
1e59de90 TL |
63 | TEST_EQ(boost::nowide::detail::stat(filename.c_str(), &stdStat, sizeof(stdStat) - 4u), EINVAL); |
64 | TEST_EQ(errno, EINVAL); | |
65 | // Same for our stat_t | |
66 | boost::nowide::stat_t boostStat; | |
67 | TEST_EQ(boost::nowide::detail::stat(filename.c_str(), &boostStat, sizeof(boostStat) - 4u), EINVAL); | |
68 | TEST_EQ(errno, EINVAL); | |
20effc67 TL |
69 | } |
70 | #endif | |
71 | ||
72 | boost::nowide::remove(filename.c_str()); | |
73 | } |