-
Notifications
You must be signed in to change notification settings - Fork 102
Add more tests courtesy of Claude #432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
18295c7
8f6f573
8322d77
76af7c3
fe94d07
e73d854
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| #!/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.ctest.sh | ||
|
|
||
| label="corrupted_messages" | ||
|
|
||
| # ----------------------------------------------- | ||
| # Test that corrupted/truncated GRIB/BUFR messages | ||
| # are handled gracefully (no crashes, proper error codes) | ||
| # ----------------------------------------------- | ||
|
|
||
| tempGrib=temp.$label.grib | ||
| tempBufr=temp.$label.bufr | ||
| tempCorrupt=temp.$label.corrupt | ||
| tempOut=temp.$label.out | ||
|
|
||
| # --- Test 1: Truncated GRIB message --- | ||
| echo "Test: Truncated GRIB message..." | ||
| ${tools_dir}/grib_set -s edition=2 $ECCODES_SAMPLES_PATH/GRIB2.tmpl $tempGrib | ||
| size=$(wc -c < $tempGrib | tr -d ' ') | ||
| half=$((size / 2)) | ||
|
|
||
| # Truncate to first half | ||
| dd if=$tempGrib of=$tempCorrupt bs=1 count=$half 2>/dev/null | ||
| set +e | ||
| ${tools_dir}/grib_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| # Should fail (non-zero exit), not crash | ||
| echo "Truncated GRIB: grib_ls exit code=$status" | ||
|
|
||
|
Comment on lines
+33
to
+39
|
||
| # --- Test 2: Truncated BUFR message --- | ||
| echo "Test: Truncated BUFR message..." | ||
| # Copy BUFR sample directly (grib_set cannot handle BUFR files) | ||
| cp $ECCODES_SAMPLES_PATH/BUFR4.tmpl $tempBufr | ||
| size=$(wc -c < $tempBufr | tr -d ' ') | ||
| half=$((size / 2)) | ||
|
|
||
| dd if=$tempBufr of=$tempCorrupt bs=1 count=$half 2>/dev/null | ||
| set +e | ||
| ${tools_dir}/bufr_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| echo "Truncated BUFR: bufr_ls exit code=$status" | ||
|
|
||
| # --- Test 3: Corrupted magic number --- | ||
| echo "Test: Corrupted magic number..." | ||
| cp $tempGrib $tempCorrupt | ||
| # Replace first byte 'G' (0x47) with 'X' (0x58) | ||
| printf '\x58' | dd of=$tempCorrupt bs=1 count=1 conv=notrunc 2>/dev/null | ||
| set +e | ||
| ${tools_dir}/grib_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| echo "Corrupted magic: grib_ls exit code=$status" | ||
| # Should fail or report no messages, not crash | ||
|
|
||
| # --- Test 4: File with just "GRIB" header and nothing else --- | ||
| echo "Test: GRIB header only (4 bytes)..." | ||
| printf 'GRIB' > $tempCorrupt | ||
| set +e | ||
| ${tools_dir}/grib_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| echo "GRIB header only: grib_ls exit code=$status" | ||
|
|
||
| # --- Test 5: Empty file --- | ||
| echo "Test: Empty file..." | ||
| > $tempCorrupt | ||
| set +e | ||
| ${tools_dir}/grib_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| echo "Empty file: grib_ls exit code=$status" | ||
|
|
||
| # --- Test 6: Random bytes file --- | ||
| echo "Test: Random bytes..." | ||
| dd if=/dev/urandom of=$tempCorrupt bs=1 count=256 2>/dev/null | ||
| set +e | ||
| ${tools_dir}/grib_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| echo "Random bytes: grib_ls exit code=$status" | ||
|
|
||
| # --- Test 7: Missing 7777 end marker --- | ||
| echo "Test: Missing 7777 end marker..." | ||
| cp $tempGrib $tempCorrupt | ||
| size=$(wc -c < $tempCorrupt | tr -d ' ') | ||
| truncsize=$((size - 4)) | ||
| dd if=$tempGrib of=$tempCorrupt bs=1 count=$truncsize 2>/dev/null | ||
| # Append something that is NOT 7777 | ||
| printf '\x00\x00\x00\x00' >> $tempCorrupt | ||
| set +e | ||
| ${tools_dir}/grib_ls $tempCorrupt > $tempOut 2>&1 | ||
| status=$? | ||
| set -e | ||
| echo "Bad end marker: grib_ls exit code=$status" | ||
|
|
||
| echo "" | ||
| echo "All corrupted message tests completed without crashes." | ||
|
|
||
| rm -f $tempGrib $tempBufr $tempCorrupt $tempOut | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||||||
| #!/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.ctest.sh | ||||||||||
|
|
||||||||||
| label="packing_roundtrip" | ||||||||||
|
|
||||||||||
| # ----------------------------------------------- | ||||||||||
| # Test packing round-trip: encode values, decode, compare | ||||||||||
| # This ensures different packing types preserve data integrity | ||||||||||
| # ----------------------------------------------- | ||||||||||
|
Comment on lines
+11
to
+18
|
||||||||||
|
|
||||||||||
| tempGrib=temp.$label.grib | ||||||||||
| tempOut=temp.$label.out | ||||||||||
| tempRef=temp.$label.ref | ||||||||||
| tempFilt=temp.$label.filt | ||||||||||
|
|
||||||||||
| # Create a GRIB2 message from sample with known grid dimensions | ||||||||||
| ${tools_dir}/grib_set -s Ni=36,Nj=18,numberOfDataPoints=648,numberOfValues=648 \ | ||||||||||
| $ECCODES_SAMPLES_PATH/GRIB2.tmpl $tempGrib | ||||||||||
|
|
||||||||||
| # Generate ascending test values (temperature-like) | ||||||||||
| cat > $tempFilt << EOF | ||||||||||
| set bitsPerValue = 16; | ||||||||||
| set Ni = 36; | ||||||||||
| set Nj = 18; | ||||||||||
| set numberOfDataPoints = 648; | ||||||||||
| set numberOfValues = 648; | ||||||||||
| # Set values to a ramp from 200 to 320 (temperature range in K) | ||||||||||
| set values = {$(seq 200 0.1855 320.1 | head -648 | tr '\n' ',' | sed 's/,$//')}; | ||||||||||
| write; | ||||||||||
| EOF | ||||||||||
|
|
||||||||||
| # Test simple packing (grid_simple) - the default | ||||||||||
| ${tools_dir}/grib_filter -o $tempGrib $tempFilt $ECCODES_SAMPLES_PATH/GRIB2.tmpl | ||||||||||
| grib_check_key_equals $tempGrib packingType grid_simple | ||||||||||
|
|
||||||||||
| stats=$(${tools_dir}/grib_get -p max,min,avg $tempGrib) | ||||||||||
| echo "Simple packing stats: $stats" | ||||||||||
|
|
||||||||||
| # Verify we can read back the values | ||||||||||
| ${tools_dir}/grib_get_data $tempGrib > $tempOut | ||||||||||
| count=$(wc -l < $tempOut | tr -d ' ') | ||||||||||
| # account for header line | ||||||||||
| test "$count" -gt 0 | ||||||||||
|
Comment on lines
+51
to
+52
|
||||||||||
| # account for header line | |
| test "$count" -gt 0 | |
| # expect 1 header line + 648 data lines | |
| test "$count" -eq 649 |
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test generates the constant field values via python3 -c ..., introducing a new runtime dependency on Python for this shell test. Other existing tests avoid requiring Python at runtime (e.g. the python execution is commented out in bufr_dump_decode_python.sh). Consider generating the repeated values using pure shell/awk to keep the test environment requirements minimal.
| set values = {$(python3 -c "print(','.join(['273.15']*100))")}; | |
| set values = {$(awk 'BEGIN{for(i=1;i<=100;i++){printf "%s", "273.15"; if(i<100) printf ","}}')}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test uses Unix-specific tooling (
dd,/dev/urandom, etc.) but has no Windows skip guard. Many other scripts bail out early when$ECCODES_ON_WINDOWS -eq 1; consider adding the same here to avoid breaking Windows CTest runs.