CR2RE Pipeline Reference Manual 1.7.0
hdrl_download-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2022 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE - 0) < 500
26#define _XOPEN_SOURCE 500 /* posix 2001, srandom */
27#endif
28
29/*----------------------------------------------------------------------------*/
33/*----------------------------------------------------------------------------*/
34
35#include <stdio.h> // for fseek
36#include <string.h>
37#include <stdlib.h>
38#include "hdrl_utils.h"
39#include "hdrl_download.h"
40
41#define INVALID_HOSTNAME "notthere.invalid"
42
43#define ESO_FTP_IP_ADDR "https://ftp.eso.org"
44
45
46/*----------------------------------------------------------------------------*/
50/*----------------------------------------------------------------------------*/
51/*----------------------------------------------------------------------------*/
55/*----------------------------------------------------------------------------*/
56
57
58
59static cpl_error_code hdrl_download_test(void)
60{
61
62 //Length of the raw retrieved data
63 size_t data_length;
64 char * raw_text = NULL;
65
66 //Get how many tests have failed so far
67 int test_failed_before = cpl_test_get_failed();
68
70 ESO_FTP_IP_ADDR"/pub/dfs/pipelines/gravity/finals2000A.data", &data_length);
71
72 //Check no cpl error is set
73 cpl_test_error(CPL_ERROR_NONE);
74
75 //Check there if the buffer is not empty
76 cpl_test(data_length > 0);
77
78 //Check pointer is not null
79 cpl_test_nonnull(raw_text);
80
81 /* Download file directly on disc and compare results */
82
83 hdrl_download_url_to_file(ESO_FTP_IP_ADDR"/pub/dfs/pipelines/gravity/finals2000A.data",
84 "finals2000A.data");
85
86 //Check no cpl error is set
87 cpl_test_error(CPL_ERROR_NONE);
88
89 // Open the file in read mode.
90 FILE *file = fopen("finals2000A.data", "r");
91 // Check if there was an error.
92
93 cpl_test_nonnull(file);
94
95 //If those tests passed (no more failed tests than before),
96 //then continue with further tests on the retrieved data
97 if(cpl_test_get_failed() == test_failed_before)
98 {
99
100
101 // Get the file length
102 fseek(file, 0, SEEK_END);
103 long length = ftell(file);
104 fseek(file, 0, SEEK_SET);
105
106 /*Compare the lenght of the buffer/file*/
107 cpl_test_eq(length, data_length);
108
109 // Create the string for the file contents.
110 char *buffer = malloc(sizeof(char) * (length + 1));
111 buffer[length] = '\0';
112 // Set the contents of the string.
113 size_t read = fread(buffer, sizeof(char), length, file);
114
115 /* Compare the length of the two buffer to make sure that there was no
116 * error when reading*/
117 cpl_test_eq(read, data_length);
118
119 int comp = memcmp(buffer, raw_text, data_length);
120
121 cpl_test_eq(comp, 0);
122
123 free(buffer);
124
125 //the first characters of raw_text is a number
126 char * endptr;
127 int num = strtol(raw_text, &endptr, 10);
128 cpl_test(num > 0);
129
130 //Test conversion to a table
131 cpl_table * eop_table =
132 hdrl_eop_data_totable (raw_text, data_length);
133
134 //Test that the table has more than one row
135 cpl_test(cpl_table_get_nrow(eop_table) > 0);
136
137 //Test that PMX, PMY, DUT don't have nonsense values (corrections are small)
138 cpl_test(cpl_table_get_column_max(eop_table, "PMX") < 10);
139 cpl_test(cpl_table_get_column_min(eop_table, "PMX") > -10);
140 cpl_test(cpl_table_get_column_max(eop_table, "PMY") < 10);
141 cpl_test(cpl_table_get_column_min(eop_table, "PMY") > -10);
142 cpl_test(cpl_table_get_column_max(eop_table, "DUT") < 10);
143 cpl_test(cpl_table_get_column_min(eop_table, "DUT") > -10);
144
145 //Test that MJD increases monotonically
146 for(cpl_size i_row = 1; i_row < cpl_table_get_nrow(eop_table); i_row++)
147 {
148 int null;
149 cpl_test(cpl_table_get_double(eop_table, "MJD", i_row, &null) >
150 cpl_table_get_double(eop_table, "MJD", i_row - 1, &null));
151 }
152 cpl_table_delete(eop_table);
153
154 //Test conversion with NULL pointer
155 eop_table = hdrl_eop_data_totable (NULL, data_length);
156 cpl_test_error(CPL_ERROR_NULL_INPUT);
157 cpl_test_null(eop_table);
158
159 //Test with wrong data length
160 eop_table = hdrl_eop_data_totable (raw_text, data_length - 1);
161 cpl_test_error(CPL_ERROR_NULL_INPUT);
162 cpl_test_null(eop_table);
163
164 //Test with wrong data length and NULL pointer
165 eop_table = hdrl_eop_data_totable (NULL, data_length - 1);
166 cpl_test_error(CPL_ERROR_NULL_INPUT);
167 cpl_test_null(eop_table);
168
169 free(raw_text);
170
171 }
172 // Close the file.
173 fclose(file);
174
175 //Test with wrong HOST name
177 INVALID_HOSTNAME"/products/eop/rapid/standard/finals2000A.data", &data_length);
178 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
179
180 //Test with wrong HOST name and PATH
181 hdrl_download_url_to_buffer(INVALID_HOSTNAME"/invalid/path",
182 &data_length);
183 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
184
185 //Test with wrong URL
186 hdrl_download_url_to_buffer(ESO_FTP_IP_ADDR"/invalid/path", &data_length);
187 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
188
189 //Test with NULL pointers
191 ESO_FTP_IP_ADDR"/products/eop/rapid/standard/finals2000A.data", NULL);
192 cpl_test_error(CPL_ERROR_NULL_INPUT);
193 hdrl_download_url_to_buffer(NULL, &data_length);
194 cpl_test_error(CPL_ERROR_NULL_INPUT);
195
196
197 //Test with wrong URL
198 hdrl_download_url_to_file(ESO_FTP_IP_ADDR"/invalid/path", "file.fits");
199 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
200
201 //Test with NULL pointers
203 ESO_FTP_IP_ADDR"/products/eop/rapid/standard/finals2000A.data", NULL);
204 cpl_test_error(CPL_ERROR_NULL_INPUT);
205 hdrl_download_url_to_file(NULL, "file.fits");
206 cpl_test_error(CPL_ERROR_NULL_INPUT);
207
208 //Test with wrong file
209 hdrl_download_url_to_file(ESO_FTP_IP_ADDR"/pub/dfs/pipelines/gravity/finals2000A.data",
210 "/dev/null/finals2000A.data");
211 cpl_test_error(CPL_ERROR_FILE_NOT_CREATED);
212
213
214 /* Checking the code inside the CPL_MSG_DEBUG if - statement */
215 cpl_msg_set_level(CPL_MSG_DEBUG);
217 ESO_FTP_IP_ADDR"/pub/dfs/pipelines/gravity/finals2000A.data",
218 &data_length);
219
220 hdrl_download_url_to_file(ESO_FTP_IP_ADDR"/pub/dfs/pipelines/gravity/finals2000A.data",
221 "finals2000A.data");
222
223 //Check no cpl error is set
224 cpl_test_error(CPL_ERROR_NONE);
225 free(raw_text);
226
227
228 return cpl_error_get_code();
229}
230
231
232/*----------------------------------------------------------------------------*/
239/*----------------------------------------------------------------------------*/
240
241int main(void)
242{
243 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
244
245 hdrl_download_test();
246
247 return cpl_test_end(0);
248}
249
250
251
252
253
254
255
256
cpl_error_code hdrl_download_url_to_file(const char *url, const char *filename)
Downloads a url into a file on disc.
char * hdrl_download_url_to_buffer(const char *url, size_t *data_length)
Downloads a url into a c data buffer.
cpl_table * hdrl_eop_data_totable(const char *eop_data, cpl_size data_length)
Export a raw string buffer containing EOP data to a CPL table.
Definition hdrl_utils.c:112