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