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 {
47 char line[MAX_INPUT_FILE_LINE_LENGTH];
48 char versionKey[MAX_INPUT_FILE_LINE_LENGTH];
49 char versionValue[MAX_INPUT_FILE_LINE_LENGTH];
50
51 if (readLine(inputFile, line, MAX_INPUT_FILE_LINE_LENGTH) != 0) {
52 fprintf(stderr, "Missing version in input file\n");
53 return false;
54 }
55
56 if (sscanf(line, " %s %s", versionKey, versionValue) != 2) {
57 fprintf(stderr, "Malformed version in input file\n");
58 return false;
59 }
60 if (strcmp(versionKey, VERSION_KEY) != 0) {
61 fprintf(stderr, "Malformed version in input file\n");
62 return false;
63 }
64 if (strcmp(versionValue, version) != 0) {
65 fprintf(stderr, "Wrong input file version %s expecting %s\n", versionValue, version);
66 return false;
67 }
68 return true;
69 }
70
main(int argc,char ** argv,test_func_t testFunc)71 int main(int argc, char **argv, test_func_t testFunc)
72 {
73 FILE *inputFile = nullptr;
74 int outputFileFd = STDOUT_FILENO;
75 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
76 int ret = 0;
77
78 if (argc > 5) {
79 fprintf(stderr, "Usage: %s [-i input_params.txt] [-o output_params.txt]\n", argv[0]);
80 return 1;
81 }
82
83 argv++;
84 while (*argv) {
85 if (strcmp(*argv, "-i") == 0) {
86 argv++;
87 if (*argv) {
88 inputFile = fopen(*argv, "r");
89 if (inputFile == nullptr) {
90 ret = 1;
91 }
92 } else {
93 ret = 1;
94 }
95 }
96 if (strcmp(*argv, "-o") == 0) {
97 argv++;
98 if (*argv) {
99 outputFileFd = open(*argv, O_WRONLY|O_CREAT, mode);
100 if (outputFileFd < 0) {
101 ret = 1;
102 }
103 } else {
104 ret = 1;
105 }
106 argv++;
107 }
108 if (*argv) {
109 argv++;
110 }
111 }
112
113 if (ret != 0) {
114 return ret;
115 }
116
117 ret = testFunc(inputFile, outputFileFd);
118
119 if (inputFile) {
120 fclose(inputFile);
121 }
122 if (outputFileFd >= 0 && outputFileFd != STDOUT_FILENO) {
123 close(outputFileFd);
124 }
125
126 return ret;
127 }
128
129 }; // namespace android
130
131