1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <stdio.h>
17570af302Sopenharmony_ci#include <string.h>
18570af302Sopenharmony_ci#include "test.h"
19570af302Sopenharmony_ci
20570af302Sopenharmony_ciconst char *wrstring = "This is a test sample!";
21570af302Sopenharmony_ciconst char *path = "/data/test.txt";
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci/**
24570af302Sopenharmony_ci * @tc.name      : fseeko_0100
25570af302Sopenharmony_ci * @tc.desc      : Verify that the file pointer is moved to the beginning of the file
26570af302Sopenharmony_ci * @tc.level     : Level 0
27570af302Sopenharmony_ci */
28570af302Sopenharmony_civoid fseeko_0100(void)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci    FILE *fp = fopen(path, "w+");
31570af302Sopenharmony_ci    if (!fp) {
32570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
33570af302Sopenharmony_ci    }
34570af302Sopenharmony_ci
35570af302Sopenharmony_ci    size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
36570af302Sopenharmony_ci    if (ret < 0) {
37570af302Sopenharmony_ci        t_error("%s fwrite failed\n", __func__);
38570af302Sopenharmony_ci    }
39570af302Sopenharmony_ci
40570af302Sopenharmony_ci    int result = fseeko(fp, 0L, SEEK_SET);
41570af302Sopenharmony_ci    char ch = fgetc(fp);
42570af302Sopenharmony_ci    if (result != 0) {
43570af302Sopenharmony_ci        t_error("%s fseeko failed, result is %d\n", __func__, result);
44570af302Sopenharmony_ci    }
45570af302Sopenharmony_ci    if (ch != 'T') {
46570af302Sopenharmony_ci        t_error("%s fseeko failed, ch is %c\n", __func__, ch);
47570af302Sopenharmony_ci    }
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci    fclose(fp);
50570af302Sopenharmony_ci    remove(path);
51570af302Sopenharmony_ci}
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci/**
54570af302Sopenharmony_ci * @tc.name      : fseeko_0200
55570af302Sopenharmony_ci * @tc.desc      : Verify that the file pointer is moved to any position in the file
56570af302Sopenharmony_ci * @tc.level     : Level 0
57570af302Sopenharmony_ci */
58570af302Sopenharmony_civoid fseeko_0200(void)
59570af302Sopenharmony_ci{
60570af302Sopenharmony_ci    FILE *fp = fopen(path, "w+");
61570af302Sopenharmony_ci    if (!fp) {
62570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
63570af302Sopenharmony_ci    }
64570af302Sopenharmony_ci
65570af302Sopenharmony_ci    size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
66570af302Sopenharmony_ci    if (ret < 0) {
67570af302Sopenharmony_ci        t_error("%s fwrite failed\n", __func__);
68570af302Sopenharmony_ci    }
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci    int result = fseeko(fp, 8L, SEEK_SET);
71570af302Sopenharmony_ci    char ch = fgetc(fp);
72570af302Sopenharmony_ci    if (result != 0) {
73570af302Sopenharmony_ci        t_error("%s fseeko failed, result is %d\n", __func__, result);
74570af302Sopenharmony_ci    }
75570af302Sopenharmony_ci    if (ch != 'a') {
76570af302Sopenharmony_ci        t_error("%s fseeko failed, ch is %c\n", __func__, ch);
77570af302Sopenharmony_ci    }
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci    fclose(fp);
80570af302Sopenharmony_ci    remove(path);
81570af302Sopenharmony_ci}
82570af302Sopenharmony_ci
83570af302Sopenharmony_ci/**
84570af302Sopenharmony_ci * @tc.name      : fseeko_0300
85570af302Sopenharmony_ci * @tc.desc      : Verify that the file pointer is moved to the current position of the file
86570af302Sopenharmony_ci * @tc.level     : Level 0
87570af302Sopenharmony_ci */
88570af302Sopenharmony_civoid fseeko_0300(void)
89570af302Sopenharmony_ci{
90570af302Sopenharmony_ci    FILE *fp = fopen(path, "w+");
91570af302Sopenharmony_ci    if (!fp) {
92570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
93570af302Sopenharmony_ci    }
94570af302Sopenharmony_ci
95570af302Sopenharmony_ci    size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
96570af302Sopenharmony_ci    if (ret < 0) {
97570af302Sopenharmony_ci        t_error("%s fwrite failed\n", __func__);
98570af302Sopenharmony_ci    }
99570af302Sopenharmony_ci
100570af302Sopenharmony_ci    int code = fseeko(fp, 10L, SEEK_SET);
101570af302Sopenharmony_ci    if (code != 0) {
102570af302Sopenharmony_ci        t_error("%s fseeko failed, code is %d\n", __func__, code);
103570af302Sopenharmony_ci    }
104570af302Sopenharmony_ci
105570af302Sopenharmony_ci    int data = fseeko(fp, 0L, SEEK_CUR);
106570af302Sopenharmony_ci    if (data != 0) {
107570af302Sopenharmony_ci        t_error("%s fseeko failed, data is %d\n", __func__, data);
108570af302Sopenharmony_ci    }
109570af302Sopenharmony_ci
110570af302Sopenharmony_ci    char ch = fgetc(fp);
111570af302Sopenharmony_ci    if (ch != 't') {
112570af302Sopenharmony_ci        t_error("%s fseeko failed, ch is %c\n", __func__, ch);
113570af302Sopenharmony_ci    }
114570af302Sopenharmony_ci
115570af302Sopenharmony_ci    fclose(fp);
116570af302Sopenharmony_ci    remove(path);
117570af302Sopenharmony_ci}
118570af302Sopenharmony_ci
119570af302Sopenharmony_ci/**
120570af302Sopenharmony_ci * @tc.name      : fseeko_0400
121570af302Sopenharmony_ci * @tc.desc      : Verify that the file pointer is moved to the end of the file
122570af302Sopenharmony_ci * @tc.level     : Level 0
123570af302Sopenharmony_ci */
124570af302Sopenharmony_civoid fseeko_0400(void)
125570af302Sopenharmony_ci{
126570af302Sopenharmony_ci    FILE *fp = fopen(path, "w+");
127570af302Sopenharmony_ci    if (!fp) {
128570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
129570af302Sopenharmony_ci    }
130570af302Sopenharmony_ci
131570af302Sopenharmony_ci    size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
132570af302Sopenharmony_ci    if (ret < 0) {
133570af302Sopenharmony_ci        t_error("%s fwrite failed\n", __func__);
134570af302Sopenharmony_ci    }
135570af302Sopenharmony_ci
136570af302Sopenharmony_ci    int result = fseeko(fp, -1L, SEEK_END);
137570af302Sopenharmony_ci    char ch = fgetc(fp);
138570af302Sopenharmony_ci    if (result != 0) {
139570af302Sopenharmony_ci        t_error("%s fseeko failed, result is %d\n", __func__, result);
140570af302Sopenharmony_ci    }
141570af302Sopenharmony_ci    if (ch != '!') {
142570af302Sopenharmony_ci        t_error("%s fseeko failed, ch is %c\n", __func__, ch);
143570af302Sopenharmony_ci    }
144570af302Sopenharmony_ci
145570af302Sopenharmony_ci    fclose(fp);
146570af302Sopenharmony_ci    remove(path);
147570af302Sopenharmony_ci}
148570af302Sopenharmony_ci
149570af302Sopenharmony_ci/**
150570af302Sopenharmony_ci * @tc.name      : fseeko_0500
151570af302Sopenharmony_ci * @tc.desc      : Verify that the moved file pointer position exceeds the starting position pointer movement failed
152570af302Sopenharmony_ci * @tc.level     : Level 2
153570af302Sopenharmony_ci */
154570af302Sopenharmony_civoid fseeko_0500(void)
155570af302Sopenharmony_ci{
156570af302Sopenharmony_ci    FILE *fp = fopen(path, "w+");
157570af302Sopenharmony_ci    if (!fp) {
158570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
159570af302Sopenharmony_ci    }
160570af302Sopenharmony_ci
161570af302Sopenharmony_ci    size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
162570af302Sopenharmony_ci    if (ret < 0) {
163570af302Sopenharmony_ci        t_error("%s fwrite failed\n", __func__);
164570af302Sopenharmony_ci    }
165570af302Sopenharmony_ci
166570af302Sopenharmony_ci    int result = fseeko(fp, -10L, SEEK_SET);
167570af302Sopenharmony_ci    if (result != -1) {
168570af302Sopenharmony_ci        t_error("%s fseeko should be failed, result is %d\n", __func__, result);
169570af302Sopenharmony_ci    }
170570af302Sopenharmony_ci
171570af302Sopenharmony_ci    fclose(fp);
172570af302Sopenharmony_ci    remove(path);
173570af302Sopenharmony_ci}
174570af302Sopenharmony_ci
175570af302Sopenharmony_ci/**
176570af302Sopenharmony_ci * @tc.name      : fseeko_0600
177570af302Sopenharmony_ci * @tc.desc      : Verify that the moved file pointer position exceeds the end position pointer movement failed
178570af302Sopenharmony_ci * @tc.level     : Level 1
179570af302Sopenharmony_ci */
180570af302Sopenharmony_civoid fseeko_0600(void)
181570af302Sopenharmony_ci{
182570af302Sopenharmony_ci    FILE *fp = fopen(path, "w+");
183570af302Sopenharmony_ci    if (!fp) {
184570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
185570af302Sopenharmony_ci    }
186570af302Sopenharmony_ci
187570af302Sopenharmony_ci    size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
188570af302Sopenharmony_ci    if (ret < 0) {
189570af302Sopenharmony_ci        t_error("%s fwrite failed\n", __func__);
190570af302Sopenharmony_ci    }
191570af302Sopenharmony_ci
192570af302Sopenharmony_ci    int result = fseeko(fp, 10L, SEEK_END);
193570af302Sopenharmony_ci    if (result != 0) {
194570af302Sopenharmony_ci        t_error("%s fseeko failed, result is %d\n", __func__, result);
195570af302Sopenharmony_ci    }
196570af302Sopenharmony_ci
197570af302Sopenharmony_ci    fclose(fp);
198570af302Sopenharmony_ci    remove(path);
199570af302Sopenharmony_ci}
200570af302Sopenharmony_ci
201570af302Sopenharmony_ciint main(int argc, char *argv[])
202570af302Sopenharmony_ci{
203570af302Sopenharmony_ci    fseeko_0100();
204570af302Sopenharmony_ci    fseeko_0200();
205570af302Sopenharmony_ci    fseeko_0300();
206570af302Sopenharmony_ci    fseeko_0400();
207570af302Sopenharmony_ci    fseeko_0500();
208570af302Sopenharmony_ci    fseeko_0600();
209570af302Sopenharmony_ci
210570af302Sopenharmony_ci    return t_status;
211570af302Sopenharmony_ci}