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 <stdarg.h> 17570af302Sopenharmony_ci#include <stdio.h> 18570af302Sopenharmony_ci#include <string.h> 19570af302Sopenharmony_ci#include "filepath_util.h" 20570af302Sopenharmony_ci 21570af302Sopenharmony_ciint readFile(FILE *stream, char *fmt, ...) 22570af302Sopenharmony_ci{ 23570af302Sopenharmony_ci va_list ap; 24570af302Sopenharmony_ci va_start(ap, fmt); 25570af302Sopenharmony_ci int result = vfscanf(stream, fmt, ap); 26570af302Sopenharmony_ci va_end(ap); 27570af302Sopenharmony_ci return result; 28570af302Sopenharmony_ci} 29570af302Sopenharmony_ci 30570af302Sopenharmony_ci/** 31570af302Sopenharmony_ci * @tc.name : vfscanf_0100 32570af302Sopenharmony_ci * @tc.desc : Reads data from the stream and stores them according to the parameter format. 33570af302Sopenharmony_ci * @tc.level : Level 0 34570af302Sopenharmony_ci */ 35570af302Sopenharmony_civoid vfscanf_0100(void) 36570af302Sopenharmony_ci{ 37570af302Sopenharmony_ci FILE *fp = NULL; 38570af302Sopenharmony_ci int val = 0; 39570af302Sopenharmony_ci char buffer[BUFSIZ]; 40570af302Sopenharmony_ci char file[PATH_MAX] = {0}; 41570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_VFSCANF_TXT, file); 42570af302Sopenharmony_ci fp = fopen(file, "w+"); 43570af302Sopenharmony_ci if (fp == NULL) { 44570af302Sopenharmony_ci t_error("%s fopen failed\n", __func__); 45570af302Sopenharmony_ci return; 46570af302Sopenharmony_ci } 47570af302Sopenharmony_ci if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) { 48570af302Sopenharmony_ci t_error("%s fprintf failed\n", __func__); 49570af302Sopenharmony_ci return; 50570af302Sopenharmony_ci } 51570af302Sopenharmony_ci rewind(fp); 52570af302Sopenharmony_ci 53570af302Sopenharmony_ci int result = readFile(fp, "%s %d", buffer, &val); 54570af302Sopenharmony_ci if (result < 0) { 55570af302Sopenharmony_ci t_error("%s vfscanf get result is %d are less 0\n", __func__, result); 56570af302Sopenharmony_ci } 57570af302Sopenharmony_ci if (strcmp(buffer, "vfscanftest") != 0) { 58570af302Sopenharmony_ci t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer); 59570af302Sopenharmony_ci } 60570af302Sopenharmony_ci if (val != 123) { 61570af302Sopenharmony_ci t_error("%s vfscanf get is %d are not 123\n", __func__, val); 62570af302Sopenharmony_ci } 63570af302Sopenharmony_ci fclose(fp); 64570af302Sopenharmony_ci remove(file); 65570af302Sopenharmony_ci} 66570af302Sopenharmony_ci 67570af302Sopenharmony_ci/** 68570af302Sopenharmony_ci * @tc.name : vfscanf_0200 69570af302Sopenharmony_ci * @tc.desc : Test conversion of different formats 70570af302Sopenharmony_ci * @tc.level : Level 1 71570af302Sopenharmony_ci */ 72570af302Sopenharmony_civoid vfscanf_0200(void) 73570af302Sopenharmony_ci{ 74570af302Sopenharmony_ci FILE *fp = NULL; 75570af302Sopenharmony_ci char val[BUFSIZ]; 76570af302Sopenharmony_ci char buffer[BUFSIZ]; 77570af302Sopenharmony_ci char file[PATH_MAX] = {0}; 78570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_VFSCANF_TXT, file); 79570af302Sopenharmony_ci fp = fopen(file, "w+"); 80570af302Sopenharmony_ci if (fp == NULL) { 81570af302Sopenharmony_ci t_error("%s fopen failed\n", __func__); 82570af302Sopenharmony_ci return; 83570af302Sopenharmony_ci } 84570af302Sopenharmony_ci if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) { 85570af302Sopenharmony_ci t_error("%s fprintf failed\n", __func__); 86570af302Sopenharmony_ci return; 87570af302Sopenharmony_ci } 88570af302Sopenharmony_ci rewind(fp); 89570af302Sopenharmony_ci 90570af302Sopenharmony_ci int result = readFile(fp, "%s %s", buffer, val); 91570af302Sopenharmony_ci if (result < 0) { 92570af302Sopenharmony_ci t_error("%s vfscanf get result is %d are less 0\n", __func__, result); 93570af302Sopenharmony_ci } 94570af302Sopenharmony_ci if (strcmp(buffer, "vfscanftest") != 0) { 95570af302Sopenharmony_ci t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer); 96570af302Sopenharmony_ci } 97570af302Sopenharmony_ci if (strcmp(val, "123") != 0) { 98570af302Sopenharmony_ci t_error("%s vfscanf get is '%s' are not '123'\n", __func__, val); 99570af302Sopenharmony_ci } 100570af302Sopenharmony_ci fclose(fp); 101570af302Sopenharmony_ci remove(file); 102570af302Sopenharmony_ci} 103570af302Sopenharmony_ci 104570af302Sopenharmony_ci/** 105570af302Sopenharmony_ci * @tc.name : vfscanf_0300 106570af302Sopenharmony_ci * @tc.desc : The test corresponding position does not match the format 107570af302Sopenharmony_ci * @tc.level : Level 2 108570af302Sopenharmony_ci */ 109570af302Sopenharmony_civoid vfscanf_0300(void) 110570af302Sopenharmony_ci{ 111570af302Sopenharmony_ci FILE *fp = NULL; 112570af302Sopenharmony_ci int val1 = 0; 113570af302Sopenharmony_ci int val2 = 0; 114570af302Sopenharmony_ci char file[PATH_MAX] = {0}; 115570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_VFSCANF_TXT, file); 116570af302Sopenharmony_ci fp = fopen(file, "w+"); 117570af302Sopenharmony_ci if (fp == NULL) { 118570af302Sopenharmony_ci t_error("%s fopen failed", __func__); 119570af302Sopenharmony_ci return; 120570af302Sopenharmony_ci } 121570af302Sopenharmony_ci if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) { 122570af302Sopenharmony_ci t_error("%s fprintf failed", __func__); 123570af302Sopenharmony_ci return; 124570af302Sopenharmony_ci } 125570af302Sopenharmony_ci rewind(fp); 126570af302Sopenharmony_ci 127570af302Sopenharmony_ci int result = readFile(fp, "%d %d", val1, val2); 128570af302Sopenharmony_ci if (result > 0) { 129570af302Sopenharmony_ci t_error("%s vfscanf get result is %d are more than the 0\n", __func__, result); 130570af302Sopenharmony_ci } 131570af302Sopenharmony_ci if (val1 != 0) { 132570af302Sopenharmony_ci t_error("%s vfscanf get is %d are not 0\n", __func__, val1); 133570af302Sopenharmony_ci } 134570af302Sopenharmony_ci if (val2 != 0) { 135570af302Sopenharmony_ci t_error("%s vfscanf get is %d are not 0\n", __func__, val2); 136570af302Sopenharmony_ci } 137570af302Sopenharmony_ci fclose(fp); 138570af302Sopenharmony_ci remove(file); 139570af302Sopenharmony_ci} 140570af302Sopenharmony_ci 141570af302Sopenharmony_ciint main(int argc, char *argv[]) 142570af302Sopenharmony_ci{ 143570af302Sopenharmony_ci vfscanf_0100(); 144570af302Sopenharmony_ci vfscanf_0200(); 145570af302Sopenharmony_ci vfscanf_0300(); 146570af302Sopenharmony_ci return t_status; 147570af302Sopenharmony_ci}