1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include "test_create_utils.h"
23 
24 namespace android {
25 
readLine(FILE * inputFile,char * line,int size)26 int readLine(FILE* inputFile, char* line, int size) {
27     int ret = 0;
28     while (true) {
29         char* str = fgets(line, size, inputFile);
30         if (str == nullptr) {
31             ret = -1;
32             break;
33         }
34         if (feof(inputFile) != 0 || ferror(inputFile) != 0) {
35             ret = -1;
36             break;
37         }
38         if (strlen(str) != 0 && str[0] != COMMENT_CHAR) {
39             break;
40         }
41     }
42     return ret;
43 }
44 
checkVersion(FILE * inputFile,const char * version)45 bool checkVersion(FILE* inputFile, const char* version) {
46     char line[MAX_INPUT_FILE_LINE_LENGTH];
47     char versionKey[MAX_INPUT_FILE_LINE_LENGTH];
48     char versionValue[MAX_INPUT_FILE_LINE_LENGTH];
49 
50     if (readLine(inputFile, line, MAX_INPUT_FILE_LINE_LENGTH) != 0) {
51         fprintf(stderr, "Missing version in input file\n");
52         return false;
53     }
54 
55     if (sscanf(line, " %s %s", versionKey, versionValue) != 2) {
56         fprintf(stderr, "Malformed version in input file\n");
57         return false;
58     }
59     if (strcmp(versionKey, VERSION_KEY) != 0) {
60         fprintf(stderr, "Malformed version in input file\n");
61         return false;
62     }
63     if (strcmp(versionValue, version) != 0) {
64         fprintf(stderr, "Wrong input file version %s expecting %s\n", versionValue, version);
65         return false;
66     }
67     return true;
68 }
69 
main(int argc,char ** argv,test_func_t testFunc)70 int main(int argc, char** argv, test_func_t testFunc) {
71     FILE* inputFile = nullptr;
72     int outputFileFd = STDOUT_FILENO;
73     mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
74     int ret = 0;
75 
76     if (argc > 5) {
77         fprintf(stderr, "Usage: %s [-i input_params.txt] [-o output_params.txt]\n", argv[0]);
78         return 1;
79     }
80 
81     argv++;
82     while (*argv) {
83         if (strcmp(*argv, "-i") == 0) {
84             argv++;
85             if (*argv) {
86                 inputFile = fopen(*argv, "r");
87                 if (inputFile == nullptr) {
88                     ret = 1;
89                 }
90             } else {
91                 ret = 1;
92             }
93         }
94         if (strcmp(*argv, "-o") == 0) {
95             argv++;
96             if (*argv) {
97                 outputFileFd = open(*argv, O_WRONLY | O_CREAT, mode);
98                 if (outputFileFd < 0) {
99                     ret = 1;
100                 }
101             } else {
102                 ret = 1;
103             }
104             argv++;
105         }
106         if (*argv) {
107             argv++;
108         }
109     }
110 
111     if (ret != 0) {
112         return ret;
113     }
114 
115     ret = testFunc(inputFile, outputFileFd);
116 
117     if (inputFile) {
118         fclose(inputFile);
119     }
120     if (outputFileFd >= 0 && outputFileFd != STDOUT_FILENO) {
121         close(outputFileFd);
122     }
123 
124     return ret;
125 }
126 
127 };  // namespace android
128