summaryrefslogtreecommitdiff
path: root/tests/unit/jt9
diff options
context:
space:
mode:
authorhibby <d@vehibberd.com>2026-10-06 21:24:40 +0100
committerhibby <d@vehibberd.com>2026-10-06 21:24:40 +0100
commitc25cbdf6fb6d946175f4335fab77bfea601b56db (patch)
tree6f7a94b7b81f999e434f61b73d47e337dfd26bf2 /tests/unit/jt9
parent71f5689252c9f13e70e1c24856904475b2307809 (diff)
parent0685fcada9f95fe90f70b387cd7d757da8209624 (diff)
Merge tag 'debian/3.2.0_rc1+dfsg-2' into debian/trixie-backportsdebian/trixie-backports
tagging wsjtx 3.2.0~rc1+dfsg-2
Diffstat (limited to 'tests/unit/jt9')
-rw-r--r--tests/unit/jt9/CMakeLists.txt11
-rw-r--r--tests/unit/jt9/test_jt9_cli_validation.cmake17
-rw-r--r--tests/unit/jt9/test_jt9_input_validation.f9063
-rw-r--r--tests/unit/jt9/test_readwav_validation.f90162
4 files changed, 253 insertions, 0 deletions
diff --git a/tests/unit/jt9/CMakeLists.txt b/tests/unit/jt9/CMakeLists.txt
new file mode 100644
index 00000000..724bff19
--- /dev/null
+++ b/tests/unit/jt9/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_executable (test_jt9_input_validation test_jt9_input_validation.f90)
+wsjt_link_common_fortran (test_jt9_input_validation)
+add_test (NAME test_jt9_input_validation COMMAND test_jt9_input_validation)
+
+add_executable (test_readwav_validation test_readwav_validation.f90)
+wsjt_link_common_fortran (test_readwav_validation)
+add_test (NAME test_readwav_validation COMMAND test_readwav_validation)
+if (WSJT_ENABLE_TSAN)
+ set_tests_properties (test_readwav_validation PROPERTIES
+ ENVIRONMENT "TSAN_OPTIONS=${wsjt_tsan_fortran_options}")
+endif ()
diff --git a/tests/unit/jt9/test_jt9_cli_validation.cmake b/tests/unit/jt9/test_jt9_cli_validation.cmake
new file mode 100644
index 00000000..16f2499e
--- /dev/null
+++ b/tests/unit/jt9/test_jt9_cli_validation.cmake
@@ -0,0 +1,17 @@
+function (require_rejection option value)
+ execute_process (
+ COMMAND "${JT9}" "${option}" "${value}"
+ RESULT_VARIABLE result
+ ERROR_VARIABLE stderr)
+ if (result EQUAL 0)
+ message (FATAL_ERROR "${option}=${value} unexpectedly succeeded")
+ endif ()
+ if (NOT stderr MATCHES "jt9: invalid value")
+ message (FATAL_ERROR "${option}=${value} did not report a validation error: ${stderr}")
+ endif ()
+endfunction ()
+
+require_rejection (-Q 6)
+require_rejection (-Q invalid)
+require_rejection (-p invalid)
+require_rejection (-m invalid)
diff --git a/tests/unit/jt9/test_jt9_input_validation.f90 b/tests/unit/jt9/test_jt9_input_validation.f90
new file mode 100644
index 00000000..c9c88370
--- /dev/null
+++ b/tests/unit/jt9/test_jt9_input_validation.f90
@@ -0,0 +1,63 @@
+program test_jt9_input_validation
+ use jt9_input_validation, only: parse_integer, parse_real, parse_wav_filename_nutc
+ implicit none
+
+ integer :: integer_value, nutc
+ real(kind=8) :: real_value
+ logical :: ok
+
+ call parse_integer('42', integer_value, ok)
+ call assert_true(ok .and. integer_value == 42, 'integer parsing accepts integers')
+
+ call parse_integer('-42', integer_value, ok)
+ call assert_true(ok .and. integer_value == -42, 'integer parsing accepts signs')
+
+ call parse_integer('not-a-number', integer_value, ok)
+ call assert_true(.not. ok, 'integer parsing rejects text')
+
+ call parse_integer('42x', integer_value, ok)
+ call assert_true(.not. ok, 'integer parsing requires one complete token')
+
+ call parse_real('7.5', real_value, ok)
+ call assert_true(ok .and. real_value == 7.5d0, 'real parsing accepts finite values')
+
+ call parse_real('60', real_value, ok)
+ call assert_true(ok .and. real_value == 60.d0, 'real parsing accepts whole numbers')
+
+ call parse_real('7.5e1', real_value, ok)
+ call assert_true(ok .and. real_value == 75.d0, 'real parsing accepts decimal exponents')
+
+ call parse_real('NaN', real_value, ok)
+ call assert_true(.not. ok, 'real parsing rejects NaN')
+
+ call parse_real('7.5x', real_value, ok)
+ call assert_true(.not. ok, 'real parsing requires one complete token')
+
+ call parse_wav_filename_nutc('240101_1234.wav', nutc)
+ call assert_true(nutc == 1234, 'four-digit timestamp is read')
+
+ call parse_wav_filename_nutc('240101_123456.WAV', nutc)
+ call assert_true(nutc == 123456, 'six-digit timestamp is read')
+
+ call parse_wav_filename_nutc('recording2026.wav', nutc)
+ call assert_true(nutc == 0, 'four-digit suffix without separator defaults to midnight')
+
+ call parse_wav_filename_nutc('valid-audio', nutc)
+ call assert_true(nutc == 0, 'extensionless files default to midnight')
+
+ call parse_wav_filename_nutc('a.wav', nutc)
+ call assert_true(nutc == 0, 'short names default to midnight')
+
+contains
+
+ subroutine assert_true(condition, message)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: message
+
+ if (.not. condition) then
+ write(*, '(A)') message
+ error stop 1
+ end if
+ end subroutine assert_true
+
+end program test_jt9_input_validation
diff --git a/tests/unit/jt9/test_readwav_validation.f90 b/tests/unit/jt9/test_readwav_validation.f90
new file mode 100644
index 00000000..fd3d4603
--- /dev/null
+++ b/tests/unit/jt9/test_readwav_validation.f90
@@ -0,0 +1,162 @@
+program test_readwav_validation
+ use readwav, only: wav_header
+ implicit none
+
+ type(wav_header) :: wav
+ integer :: samples_read, status, unit
+ integer*2 :: samples(4)
+ character(len=128) :: message
+
+ call write_valid_wav('test_readwav_validation.wav')
+ call wav%read('test_readwav_validation.wav', status, message)
+ call assert_true(status == 0, 'valid WAV is accepted')
+ call assert_true(wav%audio_format%sample_rate == 12000, 'format chunk is read')
+ close(wav%lun)
+
+ call write_format_wav('test_readwav_11025.wav', 1, 1, 11025, 16)
+ call wav%read('test_readwav_11025.wav', status, message)
+ call assert_true(status == 0, 'supported JT4 sample rate is accepted')
+ close(wav%lun)
+
+ call write_format_wav('test_readwav_stereo.wav', 1, 2, 12000, 16)
+ call wav%read('test_readwav_stereo.wav', status, message)
+ call assert_true(status /= 0, 'unsupported sample representation is rejected')
+
+ call write_format_wav('test_readwav_48000.wav', 1, 1, 48000, 16)
+ call wav%read('test_readwav_48000.wav', status, message)
+ call assert_true(status /= 0, 'unsupported sample rate is rejected')
+
+ call write_short_file('test_readwav_short.wav')
+ call wav%read('test_readwav_short.wav', status, message)
+ call assert_true(status /= 0, 'short WAV is rejected')
+
+ call write_non_wave_file('test_readwav_not_wave.wav')
+ call wav%read('test_readwav_not_wave.wav', status, message)
+ call assert_true(status /= 0, 'non-WAVE RIFF file is rejected')
+
+ call write_unpadded_odd_data('test_readwav_odd_data.wav')
+ call wav%read('test_readwav_odd_data.wav', status, message)
+ call assert_true(status == 0 .and. wav%data_size == 1, 'unpadded final data chunk is accepted')
+ close(wav%lun)
+
+ call write_wav_with_trailing_chunk('test_readwav_trailing_chunk.wav')
+ call wav%read('test_readwav_trailing_chunk.wav', status, message)
+ call assert_true(status == 0, 'WAV with trailing metadata is accepted')
+ call wav%read_samples(samples, samples_read, status, message)
+ call assert_true(status == 0 .and. samples_read == 1, 'only data chunk samples are read')
+ call assert_true(samples(1) == 1234 .and. all(samples(2:) == 0), &
+ 'sample reads zero-fill without consuming metadata')
+ call wav%read_samples(samples, samples_read, status, message)
+ call assert_true(status == 0 .and. samples_read == 0, 'data chunk exhaustion is reported')
+ close(wav%lun)
+
+ call write_truncated_chunk('test_readwav_truncated.wav')
+ call wav%read('test_readwav_truncated.wav', status, message)
+ call assert_true(status /= 0, 'truncated chunk is rejected')
+
+contains
+
+ subroutine write_valid_wav(filename)
+ character(len=*), intent(in) :: filename
+ integer*2 :: audio_format, channels, block_align, bits_per_sample
+ integer :: sample_rate, byte_rate
+
+ audio_format = 1
+ channels = 1
+ sample_rate = 12000
+ byte_rate = 24000
+ block_align = 2
+ bits_per_sample = 16
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF', 36, 'WAVE', 'fmt ', 16, audio_format, channels, sample_rate, &
+ byte_rate, block_align, bits_per_sample, 'data', 0
+ close(unit)
+ end subroutine write_valid_wav
+
+ subroutine write_short_file(filename)
+ character(len=*), intent(in) :: filename
+
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF'
+ close(unit)
+ end subroutine write_short_file
+
+ subroutine write_format_wav(filename, audio_format, channels, sample_rate, bits_per_sample)
+ character(len=*), intent(in) :: filename
+ integer, intent(in) :: audio_format, channels, sample_rate, bits_per_sample
+ integer*2 :: format_value, channel_value, block_align, bits_value
+ integer :: byte_rate
+
+ format_value = audio_format
+ channel_value = channels
+ bits_value = bits_per_sample
+ block_align = channels * bits_per_sample / 8
+ byte_rate = sample_rate * block_align
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF', 36, 'WAVE', 'fmt ', 16, format_value, channel_value, sample_rate, &
+ byte_rate, block_align, bits_value, 'data', 0
+ close(unit)
+ end subroutine write_format_wav
+
+ subroutine write_non_wave_file(filename)
+ character(len=*), intent(in) :: filename
+
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF', 36, 'NOPE'
+ close(unit)
+ end subroutine write_non_wave_file
+
+ subroutine write_unpadded_odd_data(filename)
+ character(len=*), intent(in) :: filename
+ integer*2 :: audio_format, channels, block_align, bits_per_sample
+ integer :: sample_rate, byte_rate
+
+ audio_format = 1
+ channels = 1
+ sample_rate = 12000
+ byte_rate = 24000
+ block_align = 2
+ bits_per_sample = 16
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF', 37, 'WAVE', 'fmt ', 16, audio_format, channels, sample_rate, &
+ byte_rate, block_align, bits_per_sample, 'data', 1, 'x'
+ close(unit)
+ end subroutine write_unpadded_odd_data
+
+ subroutine write_truncated_chunk(filename)
+ character(len=*), intent(in) :: filename
+
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF', 36, 'WAVE', 'fmt ', 32
+ close(unit)
+ end subroutine write_truncated_chunk
+
+ subroutine write_wav_with_trailing_chunk(filename)
+ character(len=*), intent(in) :: filename
+ integer*2 :: audio_format, channels, block_align, bits_per_sample, sample
+ integer :: sample_rate, byte_rate
+
+ audio_format = 1
+ channels = 1
+ sample_rate = 12000
+ byte_rate = 24000
+ block_align = 2
+ bits_per_sample = 16
+ sample = 1234
+ open(newunit=unit, file=filename, access='stream', form='unformatted', status='replace')
+ write(unit) 'RIFF', 50, 'WAVE', 'fmt ', 16, audio_format, channels, sample_rate, &
+ byte_rate, block_align, bits_per_sample, 'data', 2, sample, 'JUNK', 4, 'abcd'
+ close(unit)
+ end subroutine write_wav_with_trailing_chunk
+
+ subroutine assert_true(condition, text)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: text
+
+ if (.not. condition) then
+ write(*, '(A)') text
+ error stop 1
+ end if
+ end subroutine assert_true
+
+end program test_readwav_validation