• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include <cerrno>
17 #include <cstring>
18 #include <fcntl.h>
19 #include <js_native_api.h>
20 #include <js_native_api_types.h>
21 #include <node_api.h>
22 #include <stdio_ext.h>
23 #include <unistd.h>
24 #include <hilog/log.h>
25 
26 #define PARAM_0 0
27 #define PARAM_5 5
28 #define INIT (-1)
29 #define FAIL (-1)
30 #define NO_ERR 0
31 #define PARAM_0777 0777
32 
33 #undef LOG_DOMAIN
34 #undef LOG_TAG
35 #define LOG_DOMAIN 0xFEFE
36 #define LOG_TAG "MUSL_LIBCTEST"
37 
38 static const char *TEMP_FILE = "/data/storage/el2/base/files/fzl.txt";
39 static const char *g_tempFileContent = "This is a test";
40 
FBufSize_One(napi_env env,napi_callback_info info)41 static napi_value FBufSize_One(napi_env env, napi_callback_info info)
42 {
43     napi_value result = nullptr;
44     int ret = INIT;
45     char buf[1024];
46     FILE *fp = nullptr;
47     do {
48         errno = 0;
49         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
50         if (fileDescribe == FAIL) {
51             OH_LOG_INFO(LOG_APP, "FBufSize_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
52             break;
53         }
54         fp = fdopen(fileDescribe, "r");
55         if (fp == nullptr) {
56             break;
57         }
58         errno = 0;
59         int res = setvbuf(fp, buf, _IOFBF, sizeof(buf));
60         if (res != NO_ERR) {
61             OH_LOG_INFO(LOG_APP, "FBufSize_One setvbuf failed: ret %{public}d errno : %{public}d", res, errno);
62             break;
63         }
64         size_t bufsize = __fbufsize(fp);
65         ret = bufsize <= sizeof(buf);
66     } while (PARAM_0);
67     if (fp != nullptr) {
68         fclose(fp);
69     }
70     remove(TEMP_FILE);
71     napi_create_int32(env, ret, &result);
72     return result;
73 }
74 
FBufSize_Two(napi_env env,napi_callback_info info)75 static napi_value FBufSize_Two(napi_env env, napi_callback_info info)
76 {
77     napi_value result = nullptr;
78     int ret = INIT;
79     FILE *fp = nullptr;
80     do {
81         errno = 0;
82         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
83         if (fileDescribe == FAIL) {
84             OH_LOG_INFO(LOG_APP, "FBufSize_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
85             break;
86         }
87         fp = fdopen(fileDescribe, "r");
88         if (fp == nullptr) {
89             break;
90         }
91         errno = 0;
92         int res = setvbuf(fp, nullptr, _IOFBF, PARAM_0);
93         if (res != NO_ERR) {
94             OH_LOG_INFO(LOG_APP, "FBufSize_Two setvbuf failed: ret %{public}d errno : %{public}d", res, errno);
95             break;
96         }
97         size_t bufsize = __fbufsize(fp);
98         ret = bufsize == PARAM_0;
99     } while (PARAM_0);
100     if (fp != nullptr) {
101         fclose(fp);
102     }
103     remove(TEMP_FILE);
104     napi_create_int32(env, ret, &result);
105     return result;
106 }
107 
Flbf_One(napi_env env,napi_callback_info info)108 static napi_value Flbf_One(napi_env env, napi_callback_info info)
109 {
110     napi_value result = nullptr;
111     int ret = INIT;
112     char buf[BUFSIZ];
113     FILE *fp = nullptr;
114     do {
115         errno = 0;
116         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
117         if (fileDescribe == FAIL) {
118             OH_LOG_INFO(LOG_APP, "Flbf_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
119             break;
120         }
121         fp = fdopen(fileDescribe, "r");
122         if (fp == nullptr) {
123             break;
124         }
125         errno = 0;
126         int res = setvbuf(fp, buf, _IOLBF, sizeof(buf));
127         if (res != NO_ERR) {
128             OH_LOG_INFO(LOG_APP, "Flbf_One setvbuf failed: ret %{public}d errno : %{public}d", res, errno);
129             break;
130         }
131         ret = __flbf(fp);
132     } while (PARAM_0);
133     if (fp != nullptr) {
134         fclose(fp);
135     }
136     remove(TEMP_FILE);
137     napi_create_int32(env, ret, &result);
138     return result;
139 }
140 
Flbf_Two(napi_env env,napi_callback_info info)141 static napi_value Flbf_Two(napi_env env, napi_callback_info info)
142 {
143     napi_value result = nullptr;
144     int ret = INIT;
145     char buf[BUFSIZ];
146     FILE *fp = nullptr;
147     do {
148         errno = 0;
149         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
150         if (fileDescribe == FAIL) {
151             OH_LOG_INFO(LOG_APP, "Flbf_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
152             break;
153         }
154         fp = fdopen(fileDescribe, "r");
155         if (fp == nullptr) {
156             break;
157         }
158         errno = 0;
159         int res = setvbuf(fp, buf, _IOFBF, sizeof(buf));
160         if (res != NO_ERR) {
161             OH_LOG_INFO(LOG_APP, "Flbf_Two setvbuf failed: ret %{public}d errno : %{public}d", res, errno);
162             break;
163         }
164         ret = __flbf(fp);
165     } while (PARAM_0);
166     if (fp != nullptr) {
167         fclose(fp);
168     }
169     remove(TEMP_FILE);
170     napi_create_int32(env, ret, &result);
171     return result;
172 }
173 
Flbf_Three(napi_env env,napi_callback_info info)174 static napi_value Flbf_Three(napi_env env, napi_callback_info info)
175 {
176     napi_value result = nullptr;
177     int ret = INIT;
178     char buf[BUFSIZ];
179     FILE *fp = nullptr;
180     do {
181         errno = 0;
182         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
183         if (fileDescribe == FAIL) {
184             OH_LOG_INFO(LOG_APP, "Flbf_Three open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
185         }
186         fp = fdopen(fileDescribe, "r");
187         if (fp == nullptr) {
188             break;
189         }
190         errno = 0;
191         int res = setvbuf(fp, buf, _IONBF, sizeof(buf));
192         if (res != NO_ERR) {
193             OH_LOG_INFO(LOG_APP, "Flbf_Three setvbuf failed: ret %{public}d errno : %{public}d", res, errno);
194             break;
195         }
196         ret = __flbf(fp);
197     } while (PARAM_0);
198     if (fp != nullptr) {
199         fclose(fp);
200     }
201     remove(TEMP_FILE);
202     napi_create_int32(env, ret, &result);
203     return result;
204 }
205 
Fpending_One(napi_env env,napi_callback_info info)206 static napi_value Fpending_One(napi_env env, napi_callback_info info)
207 {
208     napi_value result = nullptr;
209     int ret = INIT;
210     FILE *fp = nullptr;
211     do {
212         errno = 0;
213         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
214         if (fileDescribe == FAIL) {
215             OH_LOG_INFO(LOG_APP, "Fpending_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
216             break;
217         }
218         fp = fdopen(fileDescribe, "w");
219         if (fp == nullptr) {
220             break;
221         }
222         errno = 0;
223         int res = fputs(g_tempFileContent, fp);
224         if (res == EOF) {
225             OH_LOG_INFO(LOG_APP, "Fpending_One fputs failed: res %{public}d errno : %{public}d", res, errno);
226             break;
227         }
228         ret = __fpending(fp);
229     } while (PARAM_0);
230     if (fp != nullptr) {
231         fclose(fp);
232     }
233     remove(TEMP_FILE);
234     napi_create_int32(env, ret, &result);
235     return result;
236 }
237 
Fpending_Two(napi_env env,napi_callback_info info)238 static napi_value Fpending_Two(napi_env env, napi_callback_info info)
239 {
240     napi_value result = nullptr;
241     int ret = INIT;
242     FILE *fp = nullptr;
243     do {
244         errno = 0;
245         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
246         if (fileDescribe == FAIL) {
247             OH_LOG_INFO(LOG_APP, "Fpending_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
248             break;
249         }
250         fp = fdopen(fileDescribe, "w");
251         if (fp == nullptr) {
252             break;
253         }
254         int res = fputs(g_tempFileContent, fp);
255         if (res == EOF) {
256             OH_LOG_INFO(LOG_APP, "Fpending_Two fputs failed: res %{public}d errno : %{public}d", res, errno);
257         }
258         res = fflush(fp);
259         if (res != NO_ERR) {
260             OH_LOG_INFO(LOG_APP, "Fpending_Two fflush failed: res %{public}d errno : %{public}d", res, errno);
261             break;
262         }
263         ret = __fpending(fp);
264     } while (PARAM_0);
265     if (fp != nullptr) {
266         fclose(fp);
267     }
268     remove(TEMP_FILE);
269     napi_create_int32(env, ret, &result);
270     return result;
271 }
272 
Fpurge_One(napi_env env,napi_callback_info info)273 static napi_value Fpurge_One(napi_env env, napi_callback_info info)
274 {
275     napi_value result = nullptr;
276     int ret = INIT;
277     char buf[100] = {0};
278     FILE *fp = nullptr;
279     do {
280         errno = 0;
281         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
282         if (fileDescribe == FAIL) {
283             OH_LOG_INFO(LOG_APP, "Fpurge_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
284             break;
285         }
286         fp = fdopen(fileDescribe, "w");
287         if (fp == nullptr) {
288             break;
289         }
290         int res = fputs(g_tempFileContent, fp);
291         if (res == EOF) {
292             OH_LOG_INFO(LOG_APP, "Fpurge_One fputs failed: res %{public}d errno : %{public}d", res, errno);
293         }
294         ret = __fpurge(fp);
295         fgets(buf, sizeof(buf), fp);
296         ret = strlen(buf);
297     } while (PARAM_0);
298     if (fp != nullptr) {
299         fclose(fp);
300     }
301     remove(TEMP_FILE);
302     napi_create_int32(env, ret, &result);
303     return result;
304 }
305 
Freadable_One(napi_env env,napi_callback_info info)306 static napi_value Freadable_One(napi_env env, napi_callback_info info)
307 {
308     napi_value result = nullptr;
309     int ret = INIT;
310     FILE *fp = nullptr;
311     do {
312         errno = 0;
313         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
314         if (fileDescribe == FAIL) {
315             OH_LOG_INFO(LOG_APP, "Freadable_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
316             break;
317         }
318         fp = fdopen(fileDescribe, "r");
319         if (fp == nullptr) {
320             break;
321         }
322         ret = __freadable(fp);
323     } while (PARAM_0);
324     if (fp != nullptr) {
325         fclose(fp);
326     }
327     remove(TEMP_FILE);
328     napi_create_int32(env, !ret, &result);
329     return result;
330 }
331 
Freadable_Two(napi_env env,napi_callback_info info)332 static napi_value Freadable_Two(napi_env env, napi_callback_info info)
333 {
334     napi_value result = nullptr;
335     int ret = INIT;
336     FILE *fp = nullptr;
337     do {
338         errno = 0;
339         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
340         if (fileDescribe == FAIL) {
341             OH_LOG_INFO(LOG_APP, "Freadable_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
342             break;
343         }
344         fp = fdopen(fileDescribe, "w");
345         if (fp == nullptr) {
346             break;
347         }
348         ret = __freadable(fp);
349     } while (PARAM_0);
350     if (fp != nullptr) {
351         fclose(fp);
352     }
353     remove(TEMP_FILE);
354     napi_create_int32(env, ret, &result);
355     return result;
356 }
357 
Freading_One(napi_env env,napi_callback_info info)358 static napi_value Freading_One(napi_env env, napi_callback_info info)
359 {
360     napi_value result = nullptr;
361     int ret = INIT;
362     FILE *fp = nullptr;
363     do {
364         errno = 0;
365         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
366         if (fileDescribe == FAIL) {
367             OH_LOG_INFO(LOG_APP, "Freading_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
368             break;
369         }
370         fp = fdopen(fileDescribe, "r");
371         if (fp == nullptr) {
372             break;
373         }
374         ret = __freading(fp);
375     } while (PARAM_0);
376     if (fp != nullptr) {
377         fclose(fp);
378     }
379     remove(TEMP_FILE);
380     napi_create_int32(env, !ret, &result);
381     return result;
382 }
383 
Freading_Two(napi_env env,napi_callback_info info)384 static napi_value Freading_Two(napi_env env, napi_callback_info info)
385 {
386     napi_value result = nullptr;
387     int ret = INIT;
388     FILE *fp = nullptr;
389     do {
390         errno = 0;
391         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
392         if (fileDescribe == FAIL) {
393             OH_LOG_INFO(LOG_APP, "Freading_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
394             break;
395         }
396         fp = fdopen(fileDescribe, "w");
397         if (fp == nullptr) {
398             break;
399         }
400         ret = __freading(fp);
401     } while (PARAM_0);
402     if (fp != nullptr) {
403         fclose(fp);
404     }
405     remove(TEMP_FILE);
406     napi_create_int32(env, ret, &result);
407     return result;
408 }
409 
Fseterr(napi_env env,napi_callback_info info)410 static napi_value Fseterr(napi_env env, napi_callback_info info)
411 {
412     napi_value result = nullptr;
413     int ret = INIT;
414     FILE *fp = nullptr;
415     do {
416         errno = 0;
417         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
418         if (fileDescribe == FAIL) {
419             OH_LOG_INFO(LOG_APP, "Fseterr open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
420             break;
421         }
422         fp = fdopen(fileDescribe, "w");
423         if (fp == nullptr) {
424             break;
425         }
426         __fseterr(fp);
427         ret = ferror(fp);
428         clearerr(fp);
429     } while (PARAM_0);
430     if (fp != nullptr) {
431         fclose(fp);
432     }
433     remove(TEMP_FILE);
434     napi_create_int32(env, ret, &result);
435     return result;
436 }
437 
Fwritable_One(napi_env env,napi_callback_info info)438 static napi_value Fwritable_One(napi_env env, napi_callback_info info)
439 {
440     napi_value result = nullptr;
441     int ret = INIT;
442     FILE *fp = nullptr;
443     do {
444         errno = 0;
445         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
446         if (fileDescribe == FAIL) {
447             OH_LOG_INFO(LOG_APP, "Fwritable_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
448             break;
449         }
450         fp = fdopen(fileDescribe, "w");
451         if (fp == nullptr) {
452             break;
453         }
454         ret = __fwritable(fp);
455     } while (PARAM_0);
456     if (fp != nullptr) {
457         fclose(fp);
458     }
459     remove(TEMP_FILE);
460     napi_create_int32(env, !ret, &result);
461     return result;
462 }
463 
Fwritable_Two(napi_env env,napi_callback_info info)464 static napi_value Fwritable_Two(napi_env env, napi_callback_info info)
465 {
466     napi_value result = nullptr;
467     int ret = INIT;
468     FILE *fp = nullptr;
469     do {
470         errno = 0;
471         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
472         if (fileDescribe == FAIL) {
473             OH_LOG_INFO(LOG_APP, "Fwritable_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
474             break;
475         }
476         fp = fdopen(fileDescribe, "r");
477         if (fp == nullptr) {
478             break;
479         }
480         ret = __fwritable(fp);
481     } while (PARAM_0);
482     if (fp != nullptr) {
483         fclose(fp);
484     }
485     remove(TEMP_FILE);
486     napi_create_int32(env, ret, &result);
487     return result;
488 }
489 
Fwriting_One(napi_env env,napi_callback_info info)490 static napi_value Fwriting_One(napi_env env, napi_callback_info info)
491 {
492     napi_value result = nullptr;
493     int ret = INIT;
494     FILE *fp = nullptr;
495     do {
496         errno = 0;
497         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY);
498         if (fileDescribe == FAIL) {
499             OH_LOG_INFO(LOG_APP, "Fwriting_One open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
500             break;
501         }
502         fp = fdopen(fileDescribe, "w");
503         if (fp == nullptr) {
504             break;
505         }
506         ret = __fwriting(fp);
507     } while (PARAM_0);
508     if (fp != nullptr) {
509         fclose(fp);
510     }
511     remove(TEMP_FILE);
512     napi_create_int32(env, !ret, &result);
513     return result;
514 }
515 
Fwriting_Two(napi_env env,napi_callback_info info)516 static napi_value Fwriting_Two(napi_env env, napi_callback_info info)
517 {
518     napi_value result = nullptr;
519     int ret = INIT;
520     FILE *fp = nullptr;
521     do {
522         errno = 0;
523         int fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
524         if (fileDescribe == FAIL) {
525             OH_LOG_INFO(LOG_APP, "Fwriting_Two open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
526             break;
527         }
528         fp = fdopen(fileDescribe, "r");
529         if (fp == nullptr) {
530             break;
531         }
532         ret = __fwriting(fp);
533     } while (PARAM_0);
534     if (fp != nullptr) {
535         fclose(fp);
536     }
537     remove(TEMP_FILE);
538     napi_create_int32(env, ret, &result);
539     return result;
540 }
541 
Flushlbf(napi_env env,napi_callback_info info)542 static napi_value Flushlbf(napi_env env, napi_callback_info info)
543 {
544     napi_value result = nullptr;
545     int ret = INIT;
546     FILE *fp = nullptr;
547     char buf[BUFSIZ] = {0};
548     do {
549         errno = 0;
550         int fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY | O_TRUNC);
551         if (fileDescribe == FAIL) {
552             OH_LOG_INFO(LOG_APP, "Flushlbf open failed: ret %{public}d errno : %{public}d", fileDescribe, errno);
553             break;
554         }
555         fp = fdopen(fileDescribe, "w+");
556         if (fp == nullptr) {
557             break;
558         }
559         errno = 0;
560         int res = setvbuf(fp, buf, _IOLBF, sizeof(buf));
561         if (res != 0) {
562             OH_LOG_INFO(LOG_APP, "Flushlbf setvbuf failed: ret %{public}d errno : %{public}d", res, errno);
563             break;
564         }
565         res = fputs(g_tempFileContent, fp);
566         if (res == EOF) {
567             OH_LOG_INFO(LOG_APP, "Flushlbf fputs failed: res %{public}d errno : %{public}d", res, errno);
568             break;
569         }
570         ret = __fpending(fp);
571         if (ret) {
572             _flushlbf();
573             ret = __fpending(fp);
574         } else {
575             ret = !ret;
576         }
577     } while (PARAM_0);
578     if (fp != nullptr) {
579         fclose(fp);
580     }
581     remove(TEMP_FILE);
582     napi_create_int32(env, ret, &result);
583     return result;
584 }
585 
Init(napi_env env,napi_value exports)586 EXTERN_C_START static napi_value Init(napi_env env, napi_value exports)
587 {
588     napi_property_descriptor desc[] = {
589         {"fBufSize_One", nullptr, FBufSize_One, nullptr, nullptr, nullptr, napi_default, nullptr},
590         {"fBufSize_Two", nullptr, FBufSize_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
591         {"fLbf_One", nullptr, Flbf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
592         {"fLbf_Two", nullptr, Flbf_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
593         {"fLbf_Three", nullptr, Flbf_Three, nullptr, nullptr, nullptr, napi_default, nullptr},
594         {"fPending_One", nullptr, Fpending_One, nullptr, nullptr, nullptr, napi_default, nullptr},
595         {"fPending_Two", nullptr, Fpending_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
596         {"fPurge_One", nullptr, Fpurge_One, nullptr, nullptr, nullptr, napi_default, nullptr},
597         {"fReadable_One", nullptr, Freadable_One, nullptr, nullptr, nullptr, napi_default, nullptr},
598         {"fReadable_Two", nullptr, Freadable_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
599         {"fReading_One", nullptr, Freading_One, nullptr, nullptr, nullptr, napi_default, nullptr},
600         {"fReading_Two", nullptr, Freading_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
601         {"fSetErr", nullptr, Fseterr, nullptr, nullptr, nullptr, napi_default, nullptr},
602         {"fWritable_One", nullptr, Fwritable_One, nullptr, nullptr, nullptr, napi_default, nullptr},
603         {"fWritable_Two", nullptr, Fwritable_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
604         {"fWriting_One", nullptr, Fwriting_One, nullptr, nullptr, nullptr, napi_default, nullptr},
605         {"fWriting_Two", nullptr, Fwriting_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
606         {"flushLbf", nullptr, Flushlbf, nullptr, nullptr, nullptr, napi_default, nullptr},
607     };
608     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
609     return exports;
610 }
611 EXTERN_C_END
612 
613 static napi_module demoModule = {
614     .nm_version = 1,
615     .nm_flags = 0,
616     .nm_filename = nullptr,
617     .nm_register_func = Init,
618     .nm_modname = "libstdioextndk1",
619     .nm_priv = ((void *)0),
620     .reserved = {0},
621 };
622 
RegisterModule(void)623 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
624