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
24namespace android {
25
26int 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
45bool 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
71void callback(int event __unused, void* user __unused, void *info __unused)
72{
73}
74
75int main(int argc, char **argv, test_func_t testFunc)
76{
77    FILE *inputFile = nullptr;
78    int outputFileFd = STDOUT_FILENO;
79    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
80    int ret = 0;
81
82    if (argc > 5) {
83        fprintf(stderr, "Usage: %s [-i input_params.txt] [-o output_params.txt]\n", argv[0]);
84        return 1;
85    }
86
87    argv++;
88    while (*argv) {
89        if (strcmp(*argv, "-i") == 0) {
90            argv++;
91            if (*argv) {
92                inputFile = fopen(*argv, "r");
93                if (inputFile == nullptr) {
94                    ret = 1;
95                }
96            } else {
97                ret = 1;
98            }
99        }
100        if (strcmp(*argv, "-o") == 0) {
101            argv++;
102            if (*argv) {
103                outputFileFd = open(*argv, O_WRONLY|O_CREAT, mode);
104                if (outputFileFd < 0) {
105                    ret = 1;
106                }
107            } else {
108                ret = 1;
109            }
110            argv++;
111        }
112        if (*argv) {
113            argv++;
114        }
115    }
116
117    if (ret != 0) {
118        return ret;
119    }
120
121    ret = testFunc(inputFile, outputFileFd);
122
123    if (inputFile) {
124        fclose(inputFile);
125    }
126    if (outputFileFd >= 0 && outputFileFd != STDOUT_FILENO) {
127        close(outputFileFd);
128    }
129
130    return ret;
131}
132
133}; // namespace android
134
135