1{ image
2, method
3, python3Packages
4, runCommand
5, testName
6, withOnnx
7}:
8
9# This file runs one test case.
10# There are six test cases in total. method can have three possible values and
11# withOnnx two possible values. 3 * 2 = 6.
12#
13# The case where the method is rivaGan and invisible-watermark is built
14# without onnx is expected to fail and this case is handled accordingly.
15#
16# The test works by first encoding a message into a test image,
17# then decoding the message from the image again and checking
18# if the message was decoded correctly.
19
20let
21 message = if method == "rivaGan" then
22 "asdf" # rivaGan only supports 32 bits
23 else
24 "fnörd1";
25 length = (builtins.stringLength message) * 8;
26 invisible-watermark' = python3Packages.invisible-watermark.override { inherit withOnnx; };
27 expected-exit-code = if method == "rivaGan" && !withOnnx then "1" else "0";
28in
29runCommand "invisible-watermark-test-${testName}" { nativeBuildInputs = [ invisible-watermark' ]; } ''
30 set +e
31 invisible-watermark \
32 --verbose \
33 --action encode \
34 --type bytes \
35 --method '${method}' \
36 --watermark '${message}' \
37 --output output.png \
38 '${image}'
39 exit_code="$?"
40 set -euf -o pipefail
41 if [ "$exit_code" != '${expected-exit-code}' ]; then
42 echo "Exit code of invisible-watermark was $exit_code while ${expected-exit-code} was expected."
43 exit 1
44 fi
45 if [ '${expected-exit-code}' == '1' ]; then
46 echo 'invisible-watermark failed as expected.'
47 touch "$out"
48 exit 0
49 fi
50 decoded_message="$(invisible-watermark \
51 --action decode \
52 --type bytes \
53 --method '${method}' \
54 --length '${toString length}' \
55 output.png \
56 )"
57
58 if [ '${message}' != "$decoded_message" ]; then
59 echo "invisible-watermark did not decode the watermark correctly."
60 echo "The original message was ${message} but the decoded message was $decoded_message."
61 exit 1
62 fi
63 touch "$out"
64''