From bddded25b3732316a9e50d10de18f93304634c54 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 5 Oct 2021 23:02:36 +0100 Subject: [PATCH] ECC-1288: Add test --- tests/CMakeLists.txt | 7 ++++++ tests/bufr_ecc-1288.c | 51 ++++++++++++++++++++++++++++++++++++++++++ tests/bufr_ecc-1288.sh | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 tests/bufr_ecc-1288.c create mode 100755 tests/bufr_ecc-1288.sh diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c5716238e..b94632b94 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,6 +30,7 @@ list(APPEND test_bins grib_optimize_scaling_sh grib_ecc-386 bufr_ecc-517 + bufr_ecc-1288 bufr_get_element bufr_extract_headers extract_offsets @@ -317,6 +318,12 @@ if( HAVE_BUILD_TOOLS ) CONDITION NOT ECCODES_ON_WINDOWS AND ENABLE_EXTRA_TESTS COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bufr_ecc-197.sh TEST_DEPENDS eccodes_download_bufrs ) + ecbuild_add_test( TARGET eccodes_t_bufr_ecc-1288 + TYPE SCRIPT + CONDITION NOT ECCODES_ON_WINDOWS AND ENABLE_EXTRA_TESTS + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bufr_ecc-1288.sh + TEST_DEPENDS eccodes_download_bufrs ) + if( ENABLE_EXTRA_TESTS AND HAVE_ECCODES_THREADS ) ecbuild_add_executable( TARGET grib_encode_pthreads diff --git a/tests/bufr_ecc-1288.c b/tests/bufr_ecc-1288.c new file mode 100644 index 000000000..981ca49a9 --- /dev/null +++ b/tests/bufr_ecc-1288.c @@ -0,0 +1,51 @@ +#include "eccodes.h" +#include + +const char* KEY = "encryptedShipOrMobileLandStationIdentifier"; + +int main(int argc, char* argv[]) +{ + FILE* in = NULL; + codes_handle* h = NULL; + int err = 0; + size_t slen = 0; + char buf[256] = {0,}; + char** strArray = NULL; /* array of strings */ + + const char* infile = argv[1]; + const char* DEFS_PATH_LOCAL = argv[2]; + + assert(infile); + in = fopen(infile, "rb"); + assert(in); + + h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err); + assert(h && !err); + printf("Phase 1: unpacking...\n"); + codes_set_long(h, "unpack", 1); /* An error should be issued */ + assert ( !codes_is_defined(h, KEY) ); + codes_handle_delete(h); + + printf("Wipe the cache and extend the definitions path...\n"); + sprintf(buf, "%s:%s", DEFS_PATH_LOCAL, codes_definition_path(NULL)); + codes_context_delete(NULL); + codes_context_set_definitions_path(NULL, buf); + printf("ECCODES_DEFINITION_PATH is now = |%s|\n", buf); + + /* Now rewind and decode the BUFR again */ + fseeko(in, 0, SEEK_SET); + h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err); assert(h); assert(!err); + printf("Phase 2: unpacking...\n"); + err = codes_set_long(h, "unpack", 1); + + CODES_CHECK(codes_get_size(h, KEY, &slen), 0); + assert( slen == 40 ); + strArray = (char**)malloc(slen * sizeof(char*)); + CODES_CHECK(codes_get_string_array(h, KEY, strArray, &slen), 0); + assert( slen == 40 ); + assert( strcmp(strArray[0], "ABCDEFHIJ0123456789ABCDEFGHIJ0123456789ABCD ") == 0 ); + codes_handle_delete(h); + + fclose(in); + return 0; +} diff --git a/tests/bufr_ecc-1288.sh b/tests/bufr_ecc-1288.sh new file mode 100755 index 000000000..435254e94 --- /dev/null +++ b/tests/bufr_ecc-1288.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# (C) Copyright 2005- ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by +# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. +# + +. ./include.sh + +# --------------------------------------------------------- +# This is the test for the JIRA issue ECC-1288. +# It tests decoding a BUFR file which cannot be done unless +# we extend the definitions to include extra files +# --------------------------------------------------------- +label="bufr_ecc-1288-test" + +TEMP=${label}.temp +LOG=${label}.log +set -u + +# Create a temporary directory which holds the tables etc +TEMP_DIR=${label}.temp-dir.$$ +rm -rf $TEMP_DIR +mkdir -p $TEMP_DIR/definitions/bufr/tables/0/local/0/65535/0 + +# Copy the definition files needed to decode our BUFR file +def_files="${proj_dir}/tests/ECC-197/*" +for df in ${def_files}; do + cp -f $df $TEMP_DIR/definitions/bufr/tables/0/local/0/65535/0 +done +LOCAL_DEFS=`pwd`/$TEMP_DIR/definitions + +input=${data_dir}/bufr/vos308014_v3_26.bufr +$EXEC ${test_dir}/bufr_ecc-1288 $input "$LOCAL_DEFS" 2>$LOG + +grep -q "ECCODES ERROR : hash_array: no match for sequences=308014" $LOG + +# Clean up +rm -rf $TEMP_DIR +rm -f $TEMP $LOG