Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f46824b
Showing
8 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
/test/out/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
os: | ||
- linux | ||
- osx | ||
language: c | ||
sudo: false | ||
env: | ||
- TEST_MODE=run_tests | ||
install: true | ||
script: test/run.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2015 Josua Krause | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/bin/bash | ||
|
||
REAL_CURL='curl' | ||
|
||
last_arg= | ||
url_pattern='[#\?](.*\&)*md5=([0-9a-fA-F]+)' | ||
|
||
usage() { | ||
$REAL_CURL "$@" | ||
local x=$? | ||
echo <<EOF | ||
This wrapper of curl will verify the contents of URLs specified with --url using | ||
the md5 sum of the output file which must be specified using -o. This only works | ||
for single files without filename patterns. The expected md5 sum can either be | ||
passed with --md5 or can be defined in the URL via the following regex: | ||
EOF | ||
echo "/${url_pattern}/" | ||
echo "Explicit md5 sums via --md5 take higher precedence." | ||
return $x | ||
} | ||
|
||
array_contains() { | ||
last_arg= | ||
local request=$1; shift | ||
local seeking=$1; shift | ||
local in=1 | ||
local fin=0 | ||
for element; do | ||
if [ $fin != 0 ]; then | ||
last_arg="${element}" | ||
fin=0 | ||
break | ||
fi | ||
if [ "${element}" == "${seeking}" ]; then | ||
in=0 | ||
if [ $request == 0 ]; then | ||
break | ||
else | ||
fin=1 | ||
fi | ||
fi | ||
done | ||
if [ $fin != 0 ]; then | ||
usage "--help" | ||
echo "Error: expected argument after ${seeking}" | ||
exit 42 | ||
fi | ||
return $in | ||
} | ||
|
||
args=("$@") | ||
|
||
array_contains 0 "--help" "${args[@]}" | ||
if [ $? == 0 ]; then | ||
usage "${args[@]}" | ||
exit $? | ||
fi | ||
|
||
md5_sum= | ||
output_file= | ||
|
||
array_contains 1 "--md5" "${args[@]}" | ||
if [ $? == 0 ]; then | ||
md5_sum="${last_arg}" | ||
# remove --md5 | ||
arr=() | ||
hold=0 | ||
for cur in "${args[@]}" ; do | ||
if [ "${cur}" == "--md5" ]; then | ||
hold=1 | ||
elif [ $hold != 0 ]; then | ||
hold=0 | ||
else | ||
arr+=("${cur}") | ||
fi | ||
done | ||
args=("${arr[@]}") | ||
else | ||
# try parsing the URL from --url | ||
array_contains 1 "--url" "${args[@]}" | ||
if [ $? == 0 ]; then | ||
url="${last_arg}" | ||
md5_sum=`echo "${url}" | sed -E "s/^.*${url_pattern}[^\/]*\$/\2/"` | ||
fi | ||
fi | ||
|
||
array_contains 1 "-o" "${args[@]}" | ||
if [ $? == 0 ]; then | ||
output_file="${last_arg}" | ||
else | ||
array_contains 1 "--output" "${args[@]}" | ||
if [ $? == 0 ]; then | ||
output_file="${last_arg}" | ||
fi | ||
fi | ||
|
||
$REAL_CURL "${args[@]}" | ||
if [ $? != 0 ]; then | ||
exit $? | ||
fi | ||
|
||
if [ ! -z "${md5_sum}" ] && [ -f "${output_file}" ]; then | ||
echo "verifying file ${output_file} to have md5: '${md5_sum}'" | ||
md5_cmp=`md5 -q "${output_file}"` | ||
echo "${md5_cmp}" | xargs test "${md5_sum}" = | ||
if [ $? != 0 ]; then | ||
echo "invalid md5 sum!" | ||
echo "expected ${md5_sum} got ${md5_cmp}" | ||
exit 69 | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# curl_chk – checksum wrapper for curl | ||
|
||
curl_chk is a wrapper for curl that verifies the content | ||
of the actual file that was downloaded via its md5 sum. | ||
|
||
[![Build Status](https://travis-ci.org/JosuaKrause/curl_chk.svg?branch=master)](https://travis-ci.org/JosuaKrause/curl_chk) | ||
|
||
Currently only one file at a time is allowed. The local | ||
name of this file has to be specified via `-o` and the | ||
md5 sum can either be passed as part of the URL (only | ||
if the URL was specified via `--url`) or explicitely as | ||
argument of curl `--md5`. All other arguments are used | ||
as is and as specified by curl. | ||
|
||
Example calls: | ||
``` | ||
./curl --url "http://example.com/#md5=09b9c392dc1f6e914cea287cb6be34b0" -o "index.html" | ||
./curl --md5 09b9c392dc1f6e914cea287cb6be34b0 -o "index.html" "http://example.com/" | ||
``` | ||
|
||
You can use the wrapper either directly or by renaming | ||
the original, changing `REAL_CURL` in the script, and | ||
moving the script into path. | ||
|
||
Pull requests are welcome! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/bin/bash | ||
|
||
cd "$(dirname $0)" | ||
|
||
TEST_DIR="./out" | ||
STD_OUT="${TEST_DIR}/out.tmp" | ||
GITHUB_USER="JosuaKrause" | ||
GITHUB_REPO="curl_chk" | ||
GITHUB_BRANCH="master" | ||
GITHUB_PREFIX="https://raw.githubusercontent.com/${GITHUB_USER}/${GITHUB_REPO}/${GITHUB_BRANCH}/test" | ||
|
||
echo "running tests.." | ||
echo "the github prefix is: ${GITHUB_PREFIX}" | ||
|
||
if [ ! -d "${TEST_DIR}" ]; then | ||
mkdir -p "${TEST_DIR}" | ||
fi | ||
|
||
check_exit() { | ||
if [ -f "${STD_OUT}" ]; then | ||
cat "${STD_OUT}" | ||
fi | ||
if [ $1 != $2 ]; then | ||
echo "^ unexpected exit status! expected $2 got $1 ^" | ||
exit 1 | ||
fi | ||
} | ||
|
||
check_file() { | ||
diff -q "$1" "$2" | ||
if [ $? -ne 0 ]; then | ||
diff -u "$1" "$2" | ||
print "^ $2 doesn't match $1 ^" | ||
exit 2 | ||
fi | ||
} | ||
|
||
md5_a="d8e8fca2dc0f896fd7cb4cb0031ba249" | ||
md5_b="95b3644556b48a25f3366d82b0e3b349" | ||
|
||
## using explicit --md5 | ||
# correct verification | ||
echo "test 1" | ||
../curl -# -o "${TEST_DIR}/a.tmp" --md5 "${md5_a}" "${GITHUB_PREFIX}/a.test" > "${STD_OUT}" | ||
check_exit $? 0 | ||
check_file "a.test" "${TEST_DIR}/a.tmp" | ||
grep -q "md5: '${md5_a}'" "${STD_OUT}" | ||
x=$? | ||
rm -- "${STD_OUT}" | ||
check_exit $x 0 | ||
|
||
# wrong md5 sum | ||
echo "test 2" | ||
../curl -# -o "${TEST_DIR}/b.tmp" --md5 "${md5_a}" "${GITHUB_PREFIX}/b.test" | ||
check_exit $? 69 | ||
check_file "b.test" "${TEST_DIR}/b.tmp" | ||
|
||
## using URL fragment | ||
# correct verification | ||
echo "test 3" | ||
../curl -# -o "${TEST_DIR}/b.tmp" --url "${GITHUB_PREFIX}/b.test#md5=${md5_b}" > "${STD_OUT}" | ||
check_exit $? 0 | ||
check_file "b.test" "${TEST_DIR}/b.tmp" | ||
grep -q "md5: '${md5_b}'" "${STD_OUT}" | ||
x=$? | ||
rm -- "${STD_OUT}" | ||
check_exit $x 0 | ||
|
||
# wrong md5 sum | ||
echo "test 4" | ||
../curl -# -o "${TEST_DIR}/a.tmp" --url "${GITHUB_PREFIX}/a.test#md5=${md5_b}" | ||
check_exit $? 69 | ||
check_file "a.test" "${TEST_DIR}/a.tmp" | ||
|
||
echo "cleaning up" | ||
rm -r -- "${TEST_DIR}" | ||
exit 0 |