• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef KERNEL_LITE_IO_TEST_H
17 #define KERNEL_LITE_IO_TEST_H
18 
19 #include <stdio.h>
20 #include <gtest/gtest.h>
21 #include "log.h"
22 #include "utils.h"
23 
24 #define IOTEST_TEMPFILE "/storage/io_posix_test.txt"
25 
26 class IoTest : public::testing::Test {
27 protected:
TearDownTestCase()28     static void TearDownTestCase()
29     {
30         if (remove(IOTEST_TEMPFILE) == -1) {
31             LOG("TearDownTestCase: remove test file(/storage/io_posix_test.txt) failed, errno=%d", errno);
32         }
33     }
TearDown()34     void TearDown()
35     {
36         Msleep(50); // don't test too fast
37     }
38 };
39 
40 #define INIT_TEST_FILE(fp) do {                                                 \
41     FOPEN_WRITE(fp);                                                            \
42     ASSERT_NE(fputs("hello world", fp), -1) << "fputs fail, errno = " << errno; \
43     ASSERT_NE(fclose(fp), -1) << "fclose fail, errno = " << errno;              \
44 } while (0)
45 
46 #define FOPEN_WRITE(fp) do {                                                    \
47     fp = fopen(IOTEST_TEMPFILE, "w");                                           \
48     ASSERT_NE(fp, nullptr) << "fopen fail, errno = " << errno;                  \
49 } while (0)
50 
51 #define FOPEN_READ(fp) do {                                                     \
52     fp = fopen(IOTEST_TEMPFILE, "r");                                           \
53     ASSERT_NE(fp, nullptr) << "fopen fail, errno = " << errno;                  \
54 } while (0)
55 
56 #define FILENO(fp) do {                                                         \
57     fd = fileno(fp);                                                            \
58     ASSERT_NE(fd, -1) << "> fileno fail, errno = " << errno;                    \
59 } while (0)
60 
61 #endif
62