|
| 1 | +! Copyright (c) 2013, Los Alamos National Security, LLC (LANS) |
| 2 | +! and the University Corporation for Atmospheric Research (UCAR). |
| 3 | +! |
| 4 | +! Unless noted otherwise source code is licensed under the BSD license. |
| 5 | +! Additional copyright and license information can be found in the LICENSE file |
| 6 | +! distributed with this code, or at http://mpas-dev.github.com/license.html |
| 7 | +! |
| 8 | +module test_core_io |
| 9 | + |
| 10 | +#define ERROR_WRITE(M) call mpas_log_write( M , messageType=MPAS_LOG_ERR) |
| 11 | +#define ERROR_WRITE_ARGS(M, ARGS) call mpas_log_write( M , ARGS, messageType=MPAS_LOG_ERR) |
| 12 | + use mpas_log |
| 13 | + use mpas_io |
| 14 | + |
| 15 | + implicit none |
| 16 | + private |
| 17 | + public :: test_core_io_test |
| 18 | + |
| 19 | + contains |
| 20 | + |
| 21 | + !*********************************************************************** |
| 22 | + ! |
| 23 | + ! routine close_file_with_message |
| 24 | + ! |
| 25 | + !> \brief closes the provided file handle and writes an error message. |
| 26 | + !----------------------------------------------------------------------- |
| 27 | + subroutine close_file_with_message(fileHandle, message, args) |
| 28 | + type(MPAS_IO_Handle_type), intent(inout) :: fileHandle |
| 29 | + character (len=*), intent(in), optional :: message |
| 30 | + integer, dimension(:), intent(in), optional :: args |
| 31 | + |
| 32 | + integer :: local_ierr |
| 33 | + |
| 34 | + ! log an error message |
| 35 | + if (present(message)) then |
| 36 | + if (present(args)) then |
| 37 | + ERROR_WRITE_ARGS(message, intArgs=args) |
| 38 | + else |
| 39 | + ERROR_WRITE(message) |
| 40 | + end if |
| 41 | + end if |
| 42 | + |
| 43 | + ! close the provided file |
| 44 | + call MPAS_io_close(fileHandle, local_ierr) |
| 45 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 46 | + ERROR_WRITE_ARGS('MPAS_io_close failed with error code:$i', intArgs=(/local_ierr/)) |
| 47 | + return |
| 48 | + endif |
| 49 | + |
| 50 | + end subroutine close_file_with_message |
| 51 | + |
| 52 | + !*********************************************************************** |
| 53 | + ! |
| 54 | + ! routine test_read_string_buffer_check |
| 55 | + ! |
| 56 | + !> \brief verifies attempts to read strings into buffers which are too small |
| 57 | + !> to hold the value fails safely. |
| 58 | + !> \details |
| 59 | + !> Run these tests with valgrind to ensure there are no buffer overflows when |
| 60 | + !> attempting to read strings into undersized buffers. |
| 61 | + !----------------------------------------------------------------------- |
| 62 | + subroutine test_read_string_buffer_check(domain, threadErrs, ierr) |
| 63 | + |
| 64 | + type (domain_type), intent(inout) :: domain |
| 65 | + integer, dimension(:), intent(out) :: threadErrs |
| 66 | + integer, intent(out) :: ierr |
| 67 | + |
| 68 | + integer :: local_ierr, i |
| 69 | + type(MPAS_IO_Handle_type) :: fileHandle |
| 70 | + character (len=StrKIND), dimension(1), parameter :: dimNamesString = ['StrLen'] |
| 71 | + character (len=StrKIND), dimension(2), parameter :: dimNamesStringTime = ['StrLen', 'Time '] |
| 72 | + character (len=32), parameter :: varName1 = 'stringVar' |
| 73 | + character (len=32), parameter :: varName2 = 'stringTimeVar' |
| 74 | + character (len=*), parameter :: varValue1 = 'This is a string' |
| 75 | + character (len=32), dimension(2), parameter :: varNames = [varName1, varName2] |
| 76 | + integer, parameter :: bufferSize=128 |
| 77 | + integer, parameter :: smallBufferSize=bufferSize/2 |
| 78 | + character (len=bufferSize) :: buffer |
| 79 | + character (len=smallBufferSize) :: smallBuffer |
| 80 | + character (len=*), parameter :: filename = 'char_data.nc' |
| 81 | + |
| 82 | + ierr = 0 |
| 83 | + |
| 84 | + ! open a file to write char variables to |
| 85 | + fileHandle = MPAS_io_open(filename, MPAS_IO_WRITE, MPAS_IO_NETCDF4, domain % ioContext, & |
| 86 | + clobber_file=.true., truncate_file=.true., ierr=local_ierr) |
| 87 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 88 | + ierr = 1 |
| 89 | + ERROR_WRITE('Error opening file ' // trim(filename)) |
| 90 | + return |
| 91 | + end if |
| 92 | + |
| 93 | + ! define dimensions and char variables |
| 94 | + call MPAS_io_def_dim(fileHandle, dimNamesStringTime(1), bufferSize, local_ierr) |
| 95 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 96 | + ierr = 1 |
| 97 | + call close_file_with_message(fileHandle, 'Error defining '//trim(dimNamesStringTime(1))//', error=$i', (/local_ierr/)) |
| 98 | + return |
| 99 | + end if |
| 100 | + call MPAS_io_def_dim(fileHandle, dimNamesStringTime(2), MPAS_IO_UNLIMITED_DIM, local_ierr) |
| 101 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 102 | + ierr = 1 |
| 103 | + call close_file_with_message(fileHandle, 'Error defining '//trim(dimNamesStringTime(2))//', error=$i', (/local_ierr/)) |
| 104 | + return |
| 105 | + end if |
| 106 | + call MPAS_io_def_var(fileHandle, varNames(1), MPAS_IO_CHAR, dimNamesString, ierr=local_ierr) |
| 107 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 108 | + ierr = 1 |
| 109 | + call close_file_with_message(fileHandle, 'Error defining var "'//trim(varNames(1))//'" error=$i', (/local_ierr/)) |
| 110 | + return |
| 111 | + end if |
| 112 | + call MPAS_io_def_var(fileHandle, varNames(2), MPAS_IO_CHAR, dimNamesStringTime, ierr=local_ierr) |
| 113 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 114 | + ierr = 1 |
| 115 | + call close_file_with_message(fileHandle, 'Error defining var "'//trim(varNames(2))//'" error=$i', (/local_ierr/)) |
| 116 | + return |
| 117 | + end if |
| 118 | + |
| 119 | + ! write the string values |
| 120 | + do i=1,2 |
| 121 | + call MPAS_io_put_var_char0d(fileHandle, varNames(i), varValue1, local_ierr) |
| 122 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 123 | + ierr = 1 |
| 124 | + call close_file_with_message(fileHandle, 'Error writing "'//trim(varNames(i))// & |
| 125 | + '", error=$i', (/local_ierr/)) |
| 126 | + return |
| 127 | + end if |
| 128 | + |
| 129 | + ! verify the strings are read into buffers which are large enough for the strin values |
| 130 | + call MPAS_io_get_var_char0d(fileHandle, varNames(i), buffer, local_ierr) |
| 131 | + if (local_ierr /= MPAS_IO_NOERR) then |
| 132 | + ierr = 1 |
| 133 | + call close_file_with_message(fileHandle, 'Error reading "'//trim(varNames(i))// & |
| 134 | + '", error=$i', (/local_ierr/)) |
| 135 | + return |
| 136 | + end if |
| 137 | + end do |
| 138 | + |
| 139 | + ! verify attempts to read strings into buffers which are too small generates an error |
| 140 | + call mpas_log_write(' ') |
| 141 | + call mpas_log_write('Expect to see the following error:') |
| 142 | + call MPAS_io_err_mesg(domain % ioContext, MPAS_IO_ERR_INSUFFICIENT_ARG, .false.) |
| 143 | + call mpas_log_write(' ') |
| 144 | + do i=1,2 |
| 145 | + ! this should return an error |
| 146 | + call MPAS_io_get_var_char0d(fileHandle, varNames(i), smallBuffer, local_ierr) |
| 147 | + call mpas_log_write(' ') |
| 148 | + |
| 149 | + if (local_ierr /= MPAS_IO_ERR_INSUFFICIENT_ARG) then |
| 150 | + ierr = 1 |
| 151 | + if (local_ierr == MPAS_IO_NOERR) then |
| 152 | + call close_file_with_message(fileHandle, 'Expected MPAS_IO_ERR_INSUFFICIENT_ARG ($i)'& |
| 153 | + //' but recieved no error reading "'//trim(varName1), (/local_ierr/)) |
| 154 | + else |
| 155 | + call close_file_with_message(fileHandle, 'Expected MPAS_IO_ERR_INSUFFICIENT_ARG ($i)'& |
| 156 | + //' but recieved error $i reading "'//trim(varName1)//'"', & |
| 157 | + (/MPAS_IO_ERR_INSUFFICIENT_ARG, local_ierr/)) |
| 158 | + end if |
| 159 | + return |
| 160 | + end if |
| 161 | + end do |
| 162 | + call close_file_with_message(fileHandle) |
| 163 | + |
| 164 | + end subroutine test_read_string_buffer_check |
| 165 | + |
| 166 | + |
| 167 | + !*********************************************************************** |
| 168 | + ! Subroutine test_core_io_test |
| 169 | + ! |
| 170 | + !> \brief Core test suite for I/O |
| 171 | + !> |
| 172 | + !> \details This subroutine tests mpas_io features. |
| 173 | + !> It calls individual tests for I/O operations. |
| 174 | + !> See the subroutine body for details. |
| 175 | + !> The results of each test are logged with a success or failure message. |
| 176 | + !> |
| 177 | + !> \param domain The domain object that contains the I/O context |
| 178 | + !> \param threadErrs An array to store any errors encountered during |
| 179 | + !> the test. |
| 180 | + !> \param ierr The error code that indicates the result of the test. |
| 181 | + ! |
| 182 | + !----------------------------------------------------------------------- |
| 183 | + subroutine test_core_io_test(domain, threadErrs, ierr) |
| 184 | + |
| 185 | + use mpas_log |
| 186 | + |
| 187 | + type (domain_type), intent(inout) :: domain |
| 188 | + integer, dimension(:), intent(out) :: threadErrs |
| 189 | + integer, intent(out) :: ierr |
| 190 | + |
| 191 | + integer :: test_status |
| 192 | + |
| 193 | + ierr = 0 |
| 194 | + test_status = 0 |
| 195 | + |
| 196 | + call mpas_log_write('Testing char-0 buffer reads') |
| 197 | + call test_read_string_buffer_check(domain, threadErrs, test_status) |
| 198 | + if (test_status == 0) then |
| 199 | + call mpas_log_write('char-0 buffer tests: SUCCESS') |
| 200 | + else |
| 201 | + call mpas_log_write('char-0 buffer tests: FAILURE', MPAS_LOG_ERR) |
| 202 | + ierr = ierr + abs(test_status) |
| 203 | + end if |
| 204 | + |
| 205 | + |
| 206 | + end subroutine test_core_io_test |
| 207 | + |
| 208 | +end module test_core_io |
0 commit comments