tests: Correct an error message when a file with the testing credentials cannot be...
[libisds.git] / test / online / common.c
blob3686205392da49feaa660bdff81a5a0a670af5f6
1 #define _XOPEN_SOURCE 700
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 char credentials_pwd_file[] = "../../test_credentials";
7 char credentials_mep_file[] = "../../test_credentials_mep";
9 static int read_config(const char *credentials_file, char **line, int order) {
10 FILE *file;
11 size_t length = 0;
12 char *eol;
14 if (NULL == credentials_file) {
15 return -1;
18 if (!line) return -1;
19 free(*line);
20 *line = NULL;
22 file = fopen(credentials_file, "r");
23 if (!file) {
24 fprintf(stderr, "Could not open %s\n", credentials_file);
25 return -1;
28 for (int i = 0; i < order; i++) {
29 if (-1 == getline(line, &length, file)) {
30 fprintf(stderr, "Could not read line #%d from %s: ",
31 i + 1, credentials_file);
32 if (ferror(file))
33 fprintf(stderr, "error occured\n");
34 else if (feof(file))
35 fprintf(stderr, "end of file reached\n");
36 else
37 fprintf(stderr, "I don't know why\n");
38 fclose(file);
39 free(*line);
40 *line = NULL;
41 return -1;
45 fclose(file);
47 eol = strpbrk(*line, "\r\n");
48 if (eol) *eol = '\0';
50 return 0;
53 const char *username(void) {
54 static char *username = NULL;
56 if (NULL == username) {
57 username = getenv("ISDS_USERNAME");
58 if (NULL == username)
59 read_config(credentials_pwd_file, &username, 1);
61 return username;
64 const char *password(void) {
65 static char *password = NULL;
67 if (NULL == password) {
68 password = getenv("ISDS_PASSWORD");
69 if (NULL == password)
70 read_config(credentials_pwd_file, &password, 2);
72 return password;
75 const char *username_mep(void) {
76 static char *username = NULL;
78 if (NULL == username) {
79 username = getenv("ISDS_USERNAME");
80 if (NULL == username) {
81 read_config(credentials_mep_file, &username, 1);
84 return username;
87 const char *code_mep(void) {
88 static char *code = NULL;
90 if (NULL == code) {
91 code = getenv("ISDS_CODE_MEP");
92 if (NULL == code) {
93 read_config(credentials_mep_file, &code, 2);
96 return code;