test: Checks for string2isds_credit_event_type() added
[libisds.git] / test / online / common.c
blobf179d0447941631fbf484799829f48ff48297813
1 #define _XOPEN_SOURCE 700
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 char credentials_file[] = "../../test_credentials";
8 static int read_config(char **line, int order) {
9 FILE *file;
10 size_t length = 0;
11 char *eol;
13 if (!line) return -1;
14 free(*line);
15 *line = NULL;
17 file = fopen(credentials_file, "r");
18 if (!file) {
19 fprintf(stderr, "Could open %s\n", credentials_file);
20 return -1;
23 for (int i = 0; i < order; i++) {
24 if (-1 == getline(line, &length, file)) {
25 fprintf(stderr, "Could not read line #%d from %s: ",
26 i + 1, credentials_file);
27 if (ferror(file))
28 fprintf(stderr, "error occured\n");
29 else if (feof(file))
30 fprintf(stderr, "end of file reached\n");
31 else
32 fprintf(stderr, "I don't know why\n");
33 fclose(file);
34 free(*line);
35 *line = NULL;
36 return -1;
40 fclose(file);
42 eol = strpbrk(*line, "\r\n");
43 if (eol) *eol = '\0';
45 return 0;
48 const char *username(void) {
49 static char *username;
51 if (!username) {
52 username = getenv("ISDS_USERNAME");
53 if (!username)
54 read_config(&username, 1);
56 return username;
59 const char *password(void) {
60 static char *password;
62 if (!password) {
63 password = getenv("ISDS_PASSWORD");
64 if (!password)
65 read_config(&password, 2);
67 return password;