Tests: Move CMake.FileDownload cases into RunCMake.file-DOWNLOAD

The RunCMake infrastructure is more well-suited to check the results of
each case.  It can also report more precisely what went wrong when a
case fails.
This commit is contained in:
Brad King
2022-11-17 16:59:08 -05:00
parent 9dda7c276c
commit 52b9c7b958
21 changed files with 107 additions and 253 deletions

View File

@@ -32,16 +32,6 @@ AddCMakeTest(ProcessorCount "-DKWSYS_TEST_EXE=$<TARGET_FILE:cmsysTestsCxx>")
AddCMakeTest(PushCheckState "")
AddCMakeTest(While "")
AddCMakeTest(FileDownload "")
set_tests_properties(CMake.FileDownload PROPERTIES
PASS_REGULAR_EXPRESSION "file already exists with expected MD5 sum"
FAIL_REGULAR_EXPRESSION "Unexpected status|incorrectly interpreted"
)
AddCMakeTest(FileDownloadBadHash "")
set_property(TEST CMake.FileDownloadBadHash PROPERTY
WILL_FAIL TRUE
)
AddCMakeTest(FileUpload "")
set(EndStuff_PreArgs

View File

@@ -1,13 +0,0 @@
if(NOT "@CMAKE_CURRENT_SOURCE_DIR@" MATCHES "^/")
set(slash /)
endif()
set(url "file://${slash}@CMAKE_CURRENT_SOURCE_DIR@/FileDownloadInput.png")
set(dir "@CMAKE_CURRENT_BINARY_DIR@/downloads")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT 2
STATUS status
EXPECTED_HASH SHA1=5555555555555555555555555555555555555555
)

View File

@@ -1,229 +0,0 @@
# We do not contact any real URLs, but do try a bogus one.
# Remove any proxy configuration that may change behavior.
unset(ENV{http_proxy})
unset(ENV{https_proxy})
set(timeout 4)
if(NOT "@CMAKE_CURRENT_SOURCE_DIR@" MATCHES "^/")
set(slash /)
endif()
set(url "file://${slash}@CMAKE_CURRENT_SOURCE_DIR@/FileDownloadInput.png")
set(dir "@CMAKE_CURRENT_BINARY_DIR@/downloads")
# Beware Windows asynchronous file/directory removal, rename and then
# remove the renamed dir so we can be certain the dir isn't there when
# we get to the file() commands below
if(EXISTS "${dir}")
file(RENAME ${dir} "${dir}_beingRemoved")
file(REMOVE_RECURSE "${dir}_beingRemoved")
endif()
function(__reportIfWrongStatus statusPair expectedStatusCode)
list(GET statusPair 0 statusCode)
if(NOT statusCode EQUAL expectedStatusCode)
message(SEND_ERROR
"Unexpected status: ${statusCode}, expected: ${expectedStatusCode}")
endif()
endfunction()
message(STATUS "FileDownload:1")
file(DOWNLOAD
${url}
${dir}/file1.png
TIMEOUT ${timeout}
STATUS status
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:2")
file(DOWNLOAD
${url}
${dir}/file2.png
TIMEOUT ${timeout}
STATUS status
SHOW_PROGRESS
)
__reportIfWrongStatus("${status}" 0)
# Two calls in a row, exactly the same arguments.
# Since downloaded file should exist already for 2nd call,
# the 2nd call will short-circuit and return early...
#
if(EXISTS ${dir}/file3.png)
file(REMOVE ${dir}/file3.png)
endif()
message(STATUS "FileDownload:3")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:4")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_HASH SHA1=67eee17f79d9ac557284fc0b8ad19f25723fb578
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:5")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_HASH SHA224=ba283726bbb602776818b463943189afd91836cb7ee5dd6e2c7b5ae4
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:6")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_HASH SHA256=cf3334b1275071e1da6e8c396ccb72cf1b2388d8c937526f3af26230affb9423
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:7")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_HASH SHA384=43a5d13978d97c660db44481aee0604cb4ff6ca0775cd5c2cd68cd8000e107e507c4caf6c228941231041e282ffb8950
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:8")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_HASH SHA512=6984e0909a1018030ccaa418e3be1654223cdccff0fe6adc745f9aea7e377f178be53b9fc7d54a6f81c2b62ef9ddcd38ba1978fedf4c5e7139baaf355eefad5b
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:9")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_HASH MD5=dbd330d52f4dbd60115d4191904ded92
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:10")
file(DOWNLOAD
${url}
${dir}/file3.png
TIMEOUT ${timeout}
STATUS status
EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92
)
__reportIfWrongStatus("${status}" 0)
# Print status because we check its message too
message(STATUS "${status}")
# do not use proxy for lookup of invalid site (DNS failure by proxy looks
# different than DNS failure without proxy)
set(ENV{no_proxy} "$ENV{no_proxy},badhostname.invalid")
message(STATUS "FileDownload:11")
file(DOWNLOAD
badhostname.invalid
${dir}/file11.png
TIMEOUT 30
STATUS status
)
message(STATUS "${status}")
__reportIfWrongStatus("${status}" 6) # 6 corresponds to an unresolvable host name
message(STATUS "FileDownload:12")
set(absFile "@CMAKE_CURRENT_BINARY_DIR@/file12.png")
if(EXISTS "${absFile}")
file(RENAME ${absFile} "${absFile}_beingRemoved")
file(REMOVE "${absFile}_beingRemoved")
endif()
file(DOWNLOAD
${url}
file12.png
TIMEOUT ${timeout}
EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92
STATUS status
)
__reportIfWrongStatus("${status}" 0)
if(NOT EXISTS file12.png)
message(SEND_ERROR "file12.png not downloaded: ${status}")
endif()
message(STATUS "FileDownload:13")
file(DOWNLOAD
${url}
TIMEOUT ${timeout}
STATUS status
)
__reportIfWrongStatus("${status}" 0)
if(EXISTS TIMEOUT)
file(REMOVE TIMEOUT)
message(SEND_ERROR "TIMEOUT argument was incorrectly interpreted as a filename")
endif()
message(STATUS "${status}")
message(STATUS "FileDownload:14")
file(DOWNLOAD
${url}
${dir}/file14.bin
TIMEOUT ${timeout}
STATUS status
RANGE_START 0
EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:15")
file(DOWNLOAD
${url}
${dir}/file15.bin
TIMEOUT ${timeout}
STATUS status
RANGE_END 50
EXPECTED_MD5 8592e5665b839b5d23825dc84c135b61
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:16")
file(DOWNLOAD
${url}
${dir}/file16.bin
TIMEOUT ${timeout}
STATUS status
RANGE_START 10
RANGE_END 50
EXPECTED_MD5 36cd52681e6c6c8fef85fcd9e86fc30d
)
__reportIfWrongStatus("${status}" 0)
message(STATUS "FileDownload:17")
file(DOWNLOAD
${url}
${dir}/file17.bin
TIMEOUT ${timeout}
STATUS status
RANGE_START 0
RANGE_END 50
RANGE_START 60
RANGE_END 100
EXPECTED_MD5 c5c9e74e82d493dd901eecccd659cebc
)
__reportIfWrongStatus("${status}" 0)

View File

Before

Width:  |  Height:  |  Size: 194 B

After

Width:  |  Height:  |  Size: 194 B

View File

@@ -9,7 +9,7 @@ endif()
file(MAKE_DIRECTORY "@CMAKE_CURRENT_BINARY_DIR@/uploads")
set(filename "@CMAKE_CURRENT_SOURCE_DIR@/FileDownloadInput.png")
set(filename "@CMAKE_CURRENT_SOURCE_DIR@/FileUploadInput.png")
if(NOT "@CMAKE_CURRENT_BINARY_DIR@" MATCHES "^/")
set(slash /)
endif()

View File

@@ -0,0 +1,8 @@
-- status='0;"No error"'
-- status='0;"skipping download as file already exists with expected MD5 sum"'
-- status='0;"skipping download as file already exists with expected MD5 hash"'
-- status='0;"skipping download as file already exists with expected SHA1 hash"'
-- status='0;"skipping download as file already exists with expected SHA224 hash"'
-- status='0;"skipping download as file already exists with expected SHA256 hash"'
-- status='0;"skipping download as file already exists with expected SHA384 hash"'
-- status='0;"skipping download as file already exists with expected SHA512 hash"'

View File

@@ -0,0 +1,13 @@
include(common.cmake)
# Actually download the file and verify its hash.
file_download(EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92)
# Verify that the local file already exists with expected hash.
file_download(EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92)
file_download(EXPECTED_HASH MD5=dbd330d52f4dbd60115d4191904ded92)
file_download(EXPECTED_HASH SHA1=67eee17f79d9ac557284fc0b8ad19f25723fb578)
file_download(EXPECTED_HASH SHA224=ba283726bbb602776818b463943189afd91836cb7ee5dd6e2c7b5ae4)
file_download(EXPECTED_HASH SHA256=cf3334b1275071e1da6e8c396ccb72cf1b2388d8c937526f3af26230affb9423)
file_download(EXPECTED_HASH SHA384=43a5d13978d97c660db44481aee0604cb4ff6ca0775cd5c2cd68cd8000e107e507c4caf6c228941231041e282ffb8950)
file_download(EXPECTED_HASH SHA512=6984e0909a1018030ccaa418e3be1654223cdccff0fe6adc745f9aea7e377f178be53b9fc7d54a6f81c2b62ef9ddcd38ba1978fedf4c5e7139baaf355eefad5b)

View File

@@ -1,5 +1,10 @@
include(RunCMake)
# We do not contact any real URLs, but do try a bogus one.
# Remove any proxy configuration that may change behavior.
unset(ENV{http_proxy})
unset(ENV{https_proxy})
run_cmake(hash-mismatch)
run_cmake(unused-argument)
run_cmake(httpheader-not-set)
@@ -8,3 +13,11 @@ run_cmake(tls-cainfo-not-set)
run_cmake(tls-verify-not-set)
run_cmake(pass-not-set)
run_cmake(no-save-hash)
run_cmake(bad-hostname)
run_cmake(basic)
run_cmake(EXPECTED_HASH)
run_cmake(file-without-path)
run_cmake(no-file)
run_cmake(range)
run_cmake(SHOW_PROGRESS)

View File

@@ -0,0 +1,2 @@
-- \[download 100% complete\]
-- status='0;"No error"'

View File

@@ -0,0 +1,3 @@
include(common.cmake)
file_download(SHOW_PROGRESS)

View File

@@ -0,0 +1 @@
-- status='6;"Couldn't resolve host name"'

View File

@@ -0,0 +1,9 @@
include(common.cmake)
# Do not use any proxy for lookup of an invalid site.
# DNS failure by proxy looks different than DNS failure without proxy.
set(ENV{no_proxy} "$ENV{no_proxy},badhostname.invalid")
set(url "badhostname.invalid")
file_download()

View File

@@ -0,0 +1 @@
-- status='0;"No error"'

View File

@@ -0,0 +1,3 @@
include(common.cmake)
file_download()

View File

@@ -0,0 +1 @@
-- status='0;"No error"'

View File

@@ -0,0 +1,10 @@
include(common.cmake)
set(file_orig "${file}")
cmake_path(GET file_orig FILENAME file)
file_download()
if(NOT EXISTS "${file_orig}")
message(FATAL_ERROR "file not downloaded to expected path:\n ${file_orig}")
endif()

View File

@@ -0,0 +1,11 @@
include(common.cmake)
set(file "")
file_download()
set(file "${CMAKE_CURRENT_BINARY_DIR}/input.png")
if(NOT EXISTS "${file}")
message(FATAL_ERROR "file not downloaded to expected path:\n ${file}")
endif()

View File

@@ -0,0 +1 @@
-- status='0;"No error"'

View File

@@ -0,0 +1,11 @@
include(common.cmake)
# Test downloading without saving to a file.
set(file "")
file_download()
foreach(name input.png output.png TIMEOUT)
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${name}")
message(FATAL_ERROR "file incorrectly saved to:\n ${CMAKE_CURRENT_BINARY_DIR}/${name}")
endif()
endforeach()

View File

@@ -0,0 +1,4 @@
-- status='0;"No error"'
-- status='0;"No error"'
-- status='0;"No error"'
-- status='0;"No error"'

View File

@@ -0,0 +1,15 @@
include(common.cmake)
set(file ${CMAKE_CURRENT_BINARY_DIR}/output1.png)
file_download(RANGE_START 0 EXPECTED_MD5 dbd330d52f4dbd60115d4191904ded92)
set(file ${CMAKE_CURRENT_BINARY_DIR}/output2.png)
file_download(RANGE_END 50 EXPECTED_MD5 8592e5665b839b5d23825dc84c135b61)
set(file ${CMAKE_CURRENT_BINARY_DIR}/output3.png)
file_download(RANGE_START 10 RANGE_END 50 EXPECTED_MD5 36cd52681e6c6c8fef85fcd9e86fc30d)
set(file ${CMAKE_CURRENT_BINARY_DIR}/output4.png)
file_download(RANGE_START 0 RANGE_END 50
RANGE_START 60 RANGE_END 100
EXPECTED_MD5 c5c9e74e82d493dd901eecccd659cebc)