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