1/* 2 * Copyright (c) 2022 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 <stdio.h> 17#include <string.h> 18#include <stdarg.h> 19#include <stdlib.h> 20#include "test.h" 21#include "functionalext.h" 22 23void vsnprintf_test(char *str, size_t n, char *fmt, const char *func_name, ...) 24{ 25 char s[n]; 26 va_list ap; 27 va_start(ap, func_name); 28 int result = vsnprintf(s, n, fmt, ap); 29 va_end(ap); 30 if (result < 0) { 31 t_error("%s vsnprintf get result is %d are less 0\n", func_name, result); 32 } 33 if (strcmp(s, str) != 0) { 34 t_error("%s vsnprintf get is '%s' are not '%s'\n", func_name, s, str); 35 } 36} 37 38void vsnprintf_test2(char *fmt, const char *func_name, ...) 39{ 40 char s[2] = "a"; 41 va_list ap; 42 va_start(ap, func_name); 43 int result = vsnprintf(s, 1, fmt, ap); 44 va_end(ap); 45 if (result < 0) { 46 t_error("%s vsnprintf get result is %d are less 0\n", func_name, result); 47 } 48 if(s[0] != '\0'){ 49 t_error("%s vsnprintf n is 1 but s[0] is not a mull character\n", func_name);; 50 } 51} 52 53void vsnprintf_zeron(char *str, char *fmt, const char *func_name, ...) 54{ 55 char s[10]; 56 va_list ap; 57 va_start(ap, func_name); 58 int result = vsnprintf(s, 0, fmt, ap); 59 va_end(ap); 60 if (result < 0) { 61 t_error("%s vsnprintf get result is %d are less 0\n", func_name, result); 62 } 63 if (strcmp(s, str) == 0) { 64 t_error("%s vsnprintf get is '%s' but donnot want that\n", func_name, s); 65 } 66} 67 68void vsnprintf_zeron_all(char *str, char *fmt, const char *func_name, ...) 69{ 70 va_list ap; 71 va_start(ap, func_name); 72 int result = vsnprintf(0, 0, fmt, ap); 73 va_end(ap); 74 EXPECT_EQ(func_name, result, strlen(str)); 75} 76 77int main(int argc, char *argv[]) 78{ 79 /** 80 * @tc.name : vsnprintf_0100 81 * @tc.desc : Call vsnprinf to get formatted output 82 * @tc.level : Level 0 83 */ 84 vsnprintf_test("value is use", 13, "value is %s", "vsnprintf_0100", "use"); 85 /** 86 * @tc.name : vsnprintf_0200 87 * @tc.desc : The number of digits to be truncated is less than the length of the format string 88 * @tc.level : Level 1 89 */ 90 vsnprintf_test("value is ", 10, "value is %s", "vsnprintf_0200", "use"); 91 /** 92 * @tc.name : vsnprintf_0300 93 * @tc.desc : The number of bits to be truncated is greater than the format string length 94 * @tc.level : Level 1 95 */ 96 vsnprintf_test("value is use", 15, "value is %s", "vsnprintf_0300", "use"); 97 /** 98 * @tc.name : vsnprintf_0400 99 * @tc.desc : Truncate bits to 0 100 * @tc.level : Level 2 101 */ 102 vsnprintf_zeron("value is use", "value is %s", "vsnprintf_0400", "use"); 103 /** 104 * @tc.name : vsnprintf_0500 105 * @tc.desc : truncate buffer, and bits to 0 106 * @tc.level : Level 3 107 */ 108 vsnprintf_zeron_all("value is use", "value is %s", "vsnprintf_0500", "use"); 109 /** 110 * @tc.name : vsnprintf_0600 111 * @tc.desc : The number of bits to be 1 112 * @tc.level : Level 2 113 */ 114 vsnprintf_test("", 1, "value is %s", "vsnprintf_0600", "use"); 115 /** 116 * @tc.name : vsnprintf_0700 117 * @tc.desc : This test case aims to validate that when vsnprintf is called with a buffer size of 1, 118 * and the buffer initially contains data, it properly overwrites the buffer to contain only a null character. 119 * @tc.level : Level 2 120 */ 121 vsnprintf_test2("value is %s", "vsnprintf_0700", "use"); 122 123 return t_status; 124} 125