• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_log.h"
10 #include "osal_file.h"
11 #include "osal_test_case_def.h"
12 #include "osal_test_type.h"
13 #include "securec.h"
14 
15 #define HDF_LOG_TAG osal_test
16 
17 #define UT_TEST_CHECK_RET(val) do { \
18     if (val) { \
19         HDF_LOGE("[OSAL_UT_TEST] %s %d OSA_UT_TEST_FAIL ", __func__, __LINE__); \
20     } \
21 } while (0)
22 
23 #define HDF_BUFF_LEN 30
24 #define HDF_PATH_LEN 50
25 #define HDF_FILE_MODE 0777
26 
OsalFWTestBuff(void)27 char *OsalFWTestBuff(void)
28 {
29     static char *fwBuf = "123456789123456789123456789123456789123456789123456789123456789123456789";
30     return fwBuf;
31 }
32 #if defined(__KERNEL__)
OsalPutWriteAccess(OsalFile * file)33 static void OsalPutWriteAccess(OsalFile *file)
34 {
35     HDF_LOGE("%s %d %d", __func__, deny_write_access((struct file *)file->realFile), __LINE__);
36     HDF_LOGE("%s %d %d", __func__, atomic_read(&file_inode((struct file *)file->realFile)->i_writecount), __LINE__);
37     put_write_access(file_inode((struct file *)file->realFile));
38     HDF_LOGE("%s %d %d", __func__, atomic_read(&file_inode((struct file *)file->realFile)->i_writecount), __LINE__);
39     HDF_LOGE("%s %d %d", __func__, deny_write_access((struct file *)file->realFile), __LINE__);
40 }
41 #endif
42 
OsalTestFileInit(void)43 int OsalTestFileInit(void)
44 {
45     char *buf = OsalFWTestBuff();
46 #if defined(__KERNEL__)
47     OsalFile file;
48 #endif
49     int32_t ret = -1;
50 
51 #if defined(__KERNEL__)
52     ret = OsalFileOpen(&file, TEST_FILE_PATH_NAME, O_CREAT | OSAL_O_RDWR, OSAL_S_IWRITE);
53 #else
54     ret = open(TEST_FILE_PATH_NAME, O_CREAT | O_RDWR, HDF_FILE_MODE);
55 #endif
56     if (ret < 0) {
57         HDF_LOGE("%s %d err:%d %s", __func__, __LINE__, ret, TEST_FILE_PATH_NAME);
58         return ret;
59     }
60 
61 #if defined(__KERNEL__)
62     UT_TEST_CHECK_RET(OsalFileWrite(&file, buf, strlen(buf)) == -1);
63 #else
64     UT_TEST_CHECK_RET(write(ret, buf, strlen(buf)) == -1);
65 #endif
66 #if defined(__KERNEL__)
67     OsalFileClose(&file);
68 #else
69     close(ret);
70 #endif
71 
72 #if defined(__KERNEL__)
73     ret = OsalFileOpen(&file, TEST_FW_PATH_NAME, O_CREAT | OSAL_O_RDWR, OSAL_S_IWRITE);
74 #else
75     ret = open(TEST_FW_PATH_NAME, O_CREAT | O_RDWR, HDF_FILE_MODE);
76 #endif
77     if (ret < 0) {
78         HDF_LOGE("%s %d err:%d %s", __func__, __LINE__, ret, TEST_FILE_PATH_NAME);
79         return ret;
80     }
81 #if defined(__KERNEL__)
82     UT_TEST_CHECK_RET(OsalFileWrite(&file, buf, strlen(buf)) == -1);
83 #else
84     UT_TEST_CHECK_RET(write(ret, buf, strlen(buf)) == -1);
85 #endif
86 #if defined(__KERNEL__)
87     OsalPutWriteAccess(&file);
88 #endif
89 
90 #if defined(__KERNEL__)
91     OsalFileClose(&file);
92 #else
93     close(ret);
94 #endif
95     return 0;
96 }
97 
OsalTestFileDeInit(void)98 void OsalTestFileDeInit(void)
99 {
100     HDF_LOGE("%s end", __func__);
101 }
102 
103