1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "It_vfs_fat.h"
33
TestCase(VOID)34 static UINT32 TestCase(VOID)
35 {
36 INT32 i, j, k, ret, len;
37 INT32 fd = -1;
38 CHAR filebuf[260] = "01234567890123456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123"
39 "456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123456789abcedfgh"
40 "ij9876543210abcdeabcde0123456789abcedfghij9876543210lalalalalalalala";
41 CHAR *bufWrite = nullptr;
42 off_t off;
43 CHAR readbuf[2000] = "";
44 CHAR pathname[FAT_STANDARD_NAME_LENGTH] = FAT_PATH_NAME;
45 CHAR pathname1[FAT_STANDARD_NAME_LENGTH] = FAT_PATH_NAME;
46 struct stat buf1 = { 0 };
47
48 ret = chdir("/");
49 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT0);
50
51 ret = umount(FAT_MOUNT_DIR);
52 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT_MOUNT);
53
54 ret = format(FAT_DEV_PATH, 0, 0x02);
55 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT_MOUNT);
56
57 ret = mount(FAT_DEV_PATH1, FAT_MOUNT_DIR, FAT_FILESYS_TYPE, 0, NULL);
58 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT_MOUNT);
59
60 bufWrite = (CHAR *)malloc(BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB
61 ICUNIT_ASSERT_NOT_EQUAL_NULL(bufWrite, NULL, NULL);
62 (void)memset_s(bufWrite, BYTES_PER_MBYTES + 1, 0,
63 BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB
64
65 for (i = 0; i < BYTES_PER_KBYTES * 4; i++) { // 256 * BYTES_PER_KBYTES * 4 = 1MB
66 (void)strcat_s(bufWrite, BYTES_PER_MBYTES + 1, filebuf);
67 }
68
69 ret = mkdir(pathname, S_IRWXU | S_IRWXG | S_IRWXO);
70 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
71
72 (void)strcat_s(pathname, FAT_STANDARD_NAME_LENGTH, "testfile.txt");
73 fd = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO);
74 ICUNIT_GOTO_NOT_EQUAL(fd, FAT_IS_ERROR, fd, EXIT2);
75
76 for (j = 0; j < 100; j++) { // д100M
77 ret = write(fd, bufWrite, strlen(bufWrite));
78 printf("\n write times = : %d\n", j + 1); // j+1����10����ʱ�����SD��
79 ICUNIT_GOTO_EQUAL(ret, BYTES_PER_MBYTES, ret, EXIT2); // not equal to 1M
80
81 off = lseek(fd, 0, SEEK_CUR);
82 ICUNIT_GOTO_EQUAL(off, (BYTES_PER_MBYTES * (j + 1)), off, EXIT2); // from 1M to 4G
83 }
84
85 off = lseek(fd, 64, SEEK_SET); // 64 byte
86 ICUNIT_GOTO_EQUAL(off, 64, off, EXIT2); // 64 byte
87
88 for (k = 0; k < 500; k++) { // ��500��
89 len = read(fd, readbuf, BYTES_PER_KBYTES); // read BYTES_PER_KBYTES bytes
90 printf("\n read times = : %d\n", k + 1);
91 ICUNIT_GOTO_EQUAL(len, BYTES_PER_KBYTES, len, EXIT2); // make sure BYTES_PER_KBYTES bytes
92 }
93
94 ret = close(fd);
95 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT2);
96
97 ret = stat(pathname1, &buf1);
98 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
99 FatStatPrintf(buf1);
100
101 ret = unlink(pathname);
102 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
103
104 ret = rmdir(pathname1);
105 ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
106
107 free(bufWrite);
108
109 return FAT_NO_ERROR;
110
111 free(bufWrite);
112 EXIT2:
113 close(fd);
114 EXIT1:
115 unlink(pathname);
116 EXIT:
117 rmdir(pathname1);
118 EXIT_MOUNT:
119 mount(FAT_DEV_PATH1, FAT_MOUNT_DIR, FAT_FILESYS_TYPE, 0, NULL);
120 free(bufWrite);
121 EXIT0:
122 return FAT_NO_ERROR;
123 }
124
125 /* *
126 * - @test IT_FS_FAT_909
127 * - @tspec function test
128 * - @ttitle Insert and remove SD card during reading and writing
129 * - @tbrief
130 1. Create a new file and open it
131 2. Unplug the SD card when writing 10M
132 3. View serial port printing status and file status.
133 * - @ tprior 1
134 * - @ tauto TRUE
135 * - @ tremark
136 */
137
ItFsFat909(VOID)138 VOID ItFsFat909(VOID)
139 {
140 TEST_ADD_CASE("IT_FS_FAT_909", TestCase, TEST_VFS, TEST_VFAT, TEST_LEVEL2, TEST_FUNCTION);
141 }
142