File: check_map_config.c

package info (click to toggle)
spatialite 5.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,240 kB
  • sloc: ansic: 587,051; makefile: 8,583; sh: 4,276; yacc: 1,973; xml: 717
file content (501 lines) | stat: -rw-r--r-- 13,007 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

/*

 check_map_config.c -- SpatiaLite Test Case

 Author: Sandro Furieri <a.furieri@lqt.it>

 ------------------------------------------------------------------------------
 
 Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.

The Original Code is the SpatiaLite library

The Initial Developer of the Original Code is Alessandro Furieri
 
Portions created by the Initial Developer are Copyright (C) 2020
the Initial Developer. All Rights Reserved.

Contributor(s):

Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
 
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <spatialite/gaiaconfig.h>

#include "sqlite3.h"
#include "spatialite.h"

static int
execute_check (sqlite3 * sqlite, const char *sql, char **error)
{
/* executing an SQL statement returning True/False */
    sqlite3_stmt *stmt;
    int ret;
    int retcode = 0;

    if (error != NULL)
	*error = NULL;
    ret = sqlite3_prepare_v2 (sqlite, sql, strlen (sql), &stmt, NULL);
    if (ret != SQLITE_OK)
      {
	  if (error != NULL)
	      *error = sqlite3_mprintf ("%s", sqlite3_errmsg (sqlite));
	  return SQLITE_ERROR;
      }
    ret = sqlite3_step (stmt);
    if (ret == SQLITE_DONE || ret == SQLITE_ROW)
      {
	  if (sqlite3_column_int (stmt, 0) == 1)
	      retcode = 1;
      }
    sqlite3_finalize (stmt);
    if (retcode == 1)
	return SQLITE_OK;
    return SQLITE_ERROR;
}

static int
execute_check_int (sqlite3 * sqlite, const char *sql)
{
/* executing an SQL statement returning an Integer Value */
    sqlite3_stmt *stmt;
    int ret;
    int value = -1;

    ret = sqlite3_prepare_v2 (sqlite, sql, strlen (sql), &stmt, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "%s", sqlite3_errmsg (sqlite));
	  return value;
      }
    ret = sqlite3_step (stmt);
    if (ret == SQLITE_DONE || ret == SQLITE_ROW)
      {
	  if (sqlite3_column_type (stmt, 0) == SQLITE_INTEGER)
	      value = sqlite3_column_int (stmt, 0);
      }
    sqlite3_finalize (stmt);
    return value;
}

static char *
execute_check_text (sqlite3 * sqlite, const char *sql)
{
/* executing an SQL statement returning a TEXT Value */
    sqlite3_stmt *stmt;
    int ret;
    char *value = NULL;

    ret = sqlite3_prepare_v2 (sqlite, sql, strlen (sql), &stmt, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "%s", sqlite3_errmsg (sqlite));
	  return value;
      }
    ret = sqlite3_step (stmt);
    if (ret == SQLITE_DONE || ret == SQLITE_ROW)
      {
	  if (sqlite3_column_type (stmt, 0) == SQLITE_TEXT)
	    {
		int len;
		const char *val = (const char *) sqlite3_column_text (stmt, 0);
		len = strlen (val);
		value = malloc (len + 1);
		strcpy (value, val);
	    }
      }
    sqlite3_finalize (stmt);
    return value;
}

#ifdef ENABLE_LIBXML2		/* only if LIBXML2 is supported */
#ifndef OMIT_ICONV		/* only if ICONV is supported */

static unsigned char *
load_xml (const char *path, int *len)
{
/* loading an external XML */
    unsigned char *xml;
    int sz = 0;
    int rd;
    FILE *fl = fopen (path, "rb");
    if (!fl)
      {
	  fprintf (stderr, "cannot open \"%s\"\n", path);
	  return NULL;
      }
    if (fseek (fl, 0, SEEK_END) == 0)
	sz = ftell (fl);
    xml = malloc (sz + 1);
    *len = sz;
    rewind (fl);
    rd = fread (xml, 1, sz, fl);
    if (rd != sz)
      {
	  fprintf (stderr, "read error \"%s\"\n", path);
	  return NULL;
      }
    fclose (fl);
    xml[rd] = '\0';
    return xml;
}

static char *
build_hex_blob (const unsigned char *blob, int blob_len)
{
/* building an HEX blob */
    int i;
    const unsigned char *p_in = blob;
    char *hex = malloc ((blob_len * 2) + 1);
    char *p_out = hex;
    for (i = 0; i < blob_len; i++)
      {
	  sprintf (p_out, "%02x", *p_in);
	  p_in++;
	  p_out += 2;
      }
    return hex;
}

static int
check_map_config (sqlite3 * handle, void *cache)
{
/* testing RL2 Map Configuration */
    int ret;
    char *err_msg = NULL;
    char *sql;
    unsigned char *blob;
    int blob_len;
    char *hexBlob;
    unsigned char *xml;
    int len;
    int intval;
    char *txtval;

/* testing Map Configuration */
    xml = load_xml ("mapconfig.xml", &len);
    if (xml == NULL)
	return -4;
    gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL);
    free (xml);
    if (blob == NULL)
      {
	  fprintf (stderr, "this is not a well-formed XML !!!\n");
	  return -5;
      }
    hexBlob = build_hex_blob (blob, blob_len);
    free (blob);
    if (hexBlob == NULL)
	return -6;

/* Register Map Configuration Layer */
    sql = sqlite3_mprintf ("SELECT RL2_RegisterMapConfiguration(x%Q)", hexBlob);
    ret = execute_check (handle, sql, &err_msg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error RegisterMapConfiguration #1: %s\n\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -7;
      }

    xml = load_xml ("mapconfig2.xml", &len);
    if (xml == NULL)
	return -10;
    gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL);
    free (xml);
    if (blob == NULL)
      {
	  fprintf (stderr, "this is not a well-formed XML !!!\n");
	  return -11;
      }
    hexBlob = build_hex_blob (blob, blob_len);
    free (blob);
    if (hexBlob == NULL)
	return -12;

    sql = sqlite3_mprintf ("SELECT RL2_RegisterMapConfiguration(x%Q)", hexBlob);
    ret = execute_check (handle, sql, &err_msg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error RegisterMapConfiguration #2: %s\n\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -13;
      }

/* Reload Map Configuration */
    sql =
	sqlite3_mprintf ("SELECT RL2_ReloadMapConfiguration(12, x%Q)", hexBlob);
    ret = execute_check (handle, sql, NULL);
    sqlite3_free (sql);
    if (ret == SQLITE_OK)
      {
	  fprintf (stderr, "Error MapConfiguration #1: %s\n\n",
		   "expected failure");
	  return -17;
      }

    sql =
	sqlite3_mprintf ("SELECT RL2_ReloadMapConfiguration(1, x%Q)", hexBlob);
    ret = execute_check (handle, sql, NULL);
    sqlite3_free (sql);
    if (ret == SQLITE_OK)
      {
	  fprintf (stderr, "Error MapConfiguration #2: %s\n\n",
		   "expected failure");
	  return -18;
      }

    sql =
	sqlite3_mprintf ("SELECT RL2_ReloadMapConfiguration('bad map', x%Q)",
			 hexBlob);
    ret = execute_check (handle, sql, NULL);
    sqlite3_free (sql);
    if (ret == SQLITE_OK)
      {
	  fprintf (stderr, "Error MapConfiguration #4: %s\n\n",
		   "expected failure");
	  return -20;
      }
    free (hexBlob);

    xml = load_xml ("mapconfig.xml", &len);
    if (xml == NULL)
	return -22;
    gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL);
    free (xml);
    if (blob == NULL)
      {
	  fprintf (stderr, "this is not a well-formed XML !!!\n");
	  return -23;
      }
    hexBlob = build_hex_blob (blob, blob_len);
    free (blob);
    if (hexBlob == NULL)
	return -24;
    sql =
	sqlite3_mprintf ("SELECT RL2_ReloadMapConfiguration(1, x%Q)", hexBlob);
    ret = execute_check (handle, sql, &err_msg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error ReloadMapConfiguration #6: %s\n\n", err_msg);
	  sqlite3_free (err_msg);
	  return -25;
      }

    sql =
	sqlite3_mprintf ("SELECT RL2_ReloadMapConfiguration('my-map', x%Q)",
			 hexBlob);
    ret = execute_check (handle, sql, &err_msg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error ReloadMapConfiguration #7: %s\n\n", err_msg);
	  sqlite3_free (err_msg);
	  return -26;
      }
    free (hexBlob);

/* testing Name, Title and Abstract */
    sql = sqlite3_mprintf ("SELECT RL2_NumMapConfigurations()");
    intval = execute_check_int (handle, sql);
    if (intval != 2)
      {
	  fprintf (stderr,
		   "Error RL2_NumMapConfigurations() = %d (expected 2)\n",
		   intval);
	  return -27;
      }

    sql = sqlite3_mprintf ("SELECT RL2_MapConfigurationNameN(2)");
    txtval = execute_check_text (handle, sql);
    if (txtval == NULL)
      {
	  fprintf (stderr,
		   "Error RL2_MapConfigurationNameN(1): unexpected NULL\n");
	  return -28;
      }
    if (strcmp (txtval, "my-map") != 0)
      {
	  fprintf (stderr,
		   "Error RL2_MapConfigurationNameN(1): unexpected \"%s\"\n",
		   txtval);
	  free (txtval);
	  return -29;
      }
    free (txtval);

    sql = sqlite3_mprintf ("SELECT RL2_MapConfigurationTitleN(2)");
    txtval = execute_check_text (handle, sql);
    if (txtval == NULL)
      {
	  fprintf (stderr,
		   "Error RL2_MapConfigurationTitleN(1): unexpected NULL\n");
	  return -30;
      }
    if (strcmp (txtval, "another arbitrary title") != 0)
      {
	  fprintf (stderr,
		   "Error RL2_MapConfigurationTitleN(1): unexpected \"%s\"\n",
		   txtval);
	  free (txtval);
	  return -31;
      }
    free (txtval);

    sql = sqlite3_mprintf ("SELECT RL2_MapConfigurationAbstractN(2)");
    txtval = execute_check_text (handle, sql);
    if (txtval == NULL)
      {
	  fprintf (stderr,
		   "Error RL2_MapConfigurationAbstractN(1): unexpected NULL\n");
	  return -32;
      }
    if (strcmp (txtval, "an abstract as any other") != 0)
      {
	  fprintf (stderr,
		   "Error RL2_MapConfigurationAbstractN(1): unexpected \"%s\"\n",
		   txtval);
	  free (txtval);
	  return -33;
      }
    free (txtval);

/* Unregister Map Configuration */
    sql = sqlite3_mprintf ("SELECT RL2_UnRegisterMapConfiguration(5)");
    ret = execute_check (handle, sql, NULL);
    sqlite3_free (sql);
    if (ret == SQLITE_OK)
      {
	  fprintf (stderr, "Error UnRegisterMapConfiguration #1: %s\n\n",
		   "expected failure");
	  return -34;
      }

    sql = sqlite3_mprintf ("SELECT RL2_UnRegisterMapConfiguration('alpha')");
    ret = execute_check (handle, sql, &err_msg);
    sqlite3_free (sql);
    if (ret == SQLITE_OK)
      {
	  fprintf (stderr, "Error UnRegisterMapConfiguration #2: %s\n\n",
		   "expected failure");
	  return -35;
      }

    sql =
	sqlite3_mprintf
	("SELECT RL2_UnRegisterMapConfiguration('another-map')");
    ret = execute_check (handle, sql, &err_msg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error UnRegisterMapConfiguration #4: %s\n\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -36;
      }

    return 0;
}

#endif
#endif

int
main (int argc, char *argv[])
{
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    char *sql;
    void *cache = spatialite_alloc_connection ();

    if (argc > 1 || argv[0] == NULL)
	argc = 1;		/* silencing stupid compiler warnings */

    ret =
	sqlite3_open_v2 (":memory:", &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open in-memory db: %s\n",
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return -1;
      }

    spatialite_init_ex (handle, cache, 0);

    sql = "SELECT InitSpatialMetadata(1, 'WGS84')";
    ret = execute_check (handle, sql, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr,
		   "Unexpected InitSpatialMetadata result: %i, (%s)\n", ret,
		   err_msg);
	  sqlite3_free (err_msg);
	  return -2;
      }

#ifdef ENABLE_LIBXML2		/* only if LIBXML2 is supported */
#ifndef OMIT_ICONV		/* only if ICONV is supported */

/* creating the Styling Tables */
    sql = "SELECT CreateStylingTables(1)";
    ret = execute_check (handle, sql, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error CreateStylingTables %s\n\n", err_msg);
	  sqlite3_free (err_msg);
	  return -3;
      }

    ret = check_map_config (handle, cache);
    if (ret != 0)
	return -100 - ret;

#endif
#endif

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "sqlite3_close() error: %s\n",
		   sqlite3_errmsg (handle));
	  return -57;
      }

    spatialite_cleanup_ex (cache);

    spatialite_shutdown ();
    return 0;
}