• 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 <dirent.h>
17 #include <malloc.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <test.h>
21 
22 #define NAME_BUFFER_SIZE 512
23 
check_log(const char * file,const char * pattern)24 void check_log(const char *file, const char *pattern)
25 {
26     FILE *fp = fopen(file, "r");
27     if (!fp) {
28         return;
29     }
30     if (fseek(fp, 0, SEEK_END) == -1) {
31         fclose(fp);
32         return;
33     }
34     int size = ftell(fp);
35     if (size <= 0) {
36         fclose(fp);
37         t_error("FAIL %s size is <=0!\n", file);
38     }
39     if (fseek(fp, 0, SEEK_SET) == -1) {
40         fclose(fp);
41         return;
42     }
43     char *buffer = malloc(size);
44     if (!buffer) {
45         t_error("FAIL %s malloc %d failed!\n", size);
46         return;
47     }
48 
49     int rsize = fread(buffer, 1, size, fp);
50     if (rsize == 0) {
51         fclose(fp);
52         return;
53     }
54 
55     if (strstr(buffer, pattern) != NULL) {
56         printf("It's ok to found %s in %s.\n", pattern, file);
57     } else {
58         // libctest use "FAIL" to determine whether test case failed.
59         t_error("FAIL can't find %s in %s!\n", pattern, file);
60     }
61     int r = fclose(fp);
62     if (r) {
63         t_error("FAIL fclose failed!\n");
64     }
65     return;
66 }
67 
find_and_check_file(const char * log_dir,const char * file_tag,const char * pattern)68 void find_and_check_file(const char *log_dir, const char *file_tag, const char *pattern)
69 {
70     struct dirent *ptr;
71     int found = 0;
72     if (!log_dir) {
73         return;
74     }
75     DIR *dir = opendir(log_dir);
76     if (!dir) {
77         return;
78     }
79     while ((ptr = readdir(dir)) != NULL) {
80         if (strstr(ptr->d_name, file_tag) != NULL) {
81             found = 1;
82             char target_file[NAME_BUFFER_SIZE];
83             snprintf(target_file, NAME_BUFFER_SIZE, "%s%s", log_dir, ptr->d_name);
84             check_log(target_file, pattern);
85         }
86     }
87     if (!found) {
88         t_error("FAIL can't find matched file, log_dir:%s file_tag:%s.\n", log_dir, file_tag);
89     }
90     closedir(dir);
91 }
92 
clear_log(const char * log_dir,const char * file_tag)93 void clear_log(const char *log_dir, const char *file_tag)
94 {
95     struct dirent *ptr;
96     if (!log_dir) {
97         return;
98     }
99     DIR *dir = opendir(log_dir);
100     if (!dir) {
101         return;
102     }
103     while ((ptr = readdir(dir)) != NULL) {
104         if (strstr(ptr->d_name, file_tag) != NULL) {
105             char target_file[NAME_BUFFER_SIZE];
106             snprintf(target_file, NAME_BUFFER_SIZE, "%s%s", log_dir, ptr->d_name);
107             remove(target_file);
108         }
109     }
110     closedir(dir);
111 }