From a57558736387a9a2768fc7d069f257fe89d8e992 Mon Sep 17 00:00:00 2001
From: Carl Michael Skog <cm@äger.com>
Date: Wed, 16 Oct 2019 17:05:45 +0200
Subject: [PATCH 3/4] Add a test for init'ing with multiple subkey key
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This test simulates the scenario where we have two pass repositories
using the same key, and one of them adds a subkey to the key, and does
an init, and then does a 'pass git push'.
If the other repository does an 'pass git pull', it will not be able to
read its passwords, because the password file is encrypted with a subkey
it does not have.
The error message is: "gpg: decryption failed: No secret key".

Signed-off-by: Carl Michael Skog <cm@äger.com>
---
 ...06EFA1387A43F03768F13E6B25D345AD342F5386.key | Bin 0 -> 978 bytes
 ...0E3D633B6519FCE7187F66277EEE66F174BECFA0.key | Bin 0 -> 978 bytes
 ...A5745D43D2A2AF9D646DEC73F62109861A59C5E7.key | Bin 0 -> 978 bytes
 tests/oldmultiplesubkeygnupg/pubring.kbx        | Bin 0 -> 1433 bytes
 tests/oldmultiplesubkeygnupg/pubring.kbx~       | Bin 0 -> 2058 bytes
 tests/oldmultiplesubkeygnupg/tofu.db            | Bin 0 -> 49152 bytes
 tests/oldmultiplesubkeygnupg/trustdb.gpg        | Bin 0 -> 1200 bytes
 tests/setup.sh                                  |   1 +
 tests/t0300-reencryption.sh                     |   9 +++++++++
 9 files changed, 10 insertions(+)
 create mode 100644 tests/oldmultiplesubkeygnupg/private-keys-v1.d/06EFA1387A43F03768F13E6B25D345AD342F5386.key
 create mode 100644 tests/oldmultiplesubkeygnupg/private-keys-v1.d/0E3D633B6519FCE7187F66277EEE66F174BECFA0.key
 create mode 100644 tests/oldmultiplesubkeygnupg/private-keys-v1.d/A5745D43D2A2AF9D646DEC73F62109861A59C5E7.key
 create mode 100644 tests/oldmultiplesubkeygnupg/pubring.kbx
 create mode 100644 tests/oldmultiplesubkeygnupg/pubring.kbx~
 create mode 100644 tests/oldmultiplesubkeygnupg/tofu.db
 create mode 100644 tests/oldmultiplesubkeygnupg/trustdb.gpg

diff --git a/tests/oldmultiplesubkeygnupg/private-keys-v1.d/06EFA1387A43F03768F13E6B25D345AD342F5386.key b/tests/oldmultiplesubkeygnupg/private-keys-v1.d/06EFA1387A43F03768F13E6B25D345AD342F5386.key
new file mode 100644
index 0000000000000000000000000000000000000000..daf876b26e23c3098f0c859872d15e21a370d3c0
GIT binary patch
diff --git a/tests/oldmultiplesubkeygnupg/private-keys-v1.d/0E3D633B6519FCE7187F66277EEE66F174BECFA0.key b/tests/oldmultiplesubkeygnupg/private-keys-v1.d/0E3D633B6519FCE7187F66277EEE66F174BECFA0.key
new file mode 100644
index 0000000000000000000000000000000000000000..666aa3140be551125cfb6c4ce2c98862aa4747ed
GIT binary patch
diff --git a/tests/oldmultiplesubkeygnupg/private-keys-v1.d/A5745D43D2A2AF9D646DEC73F62109861A59C5E7.key b/tests/oldmultiplesubkeygnupg/private-keys-v1.d/A5745D43D2A2AF9D646DEC73F62109861A59C5E7.key
new file mode 100644
index 0000000000000000000000000000000000000000..30a660c226359e850a4eb1f68ccba91a5693ac51
GIT binary patch
diff --git a/tests/oldmultiplesubkeygnupg/pubring.kbx b/tests/oldmultiplesubkeygnupg/pubring.kbx
new file mode 100644
index 0000000000000000000000000000000000000000..368084bb192dafbe80077a2c16a8ec2132d03a2b
GIT binary patch
diff --git a/tests/oldmultiplesubkeygnupg/pubring.kbx~ b/tests/oldmultiplesubkeygnupg/pubring.kbx~
new file mode 100644
index 0000000000000000000000000000000000000000..4db9241099f83a1b1a19961d5b5d85a94f3e7f20
GIT binary patch
diff --git a/tests/oldmultiplesubkeygnupg/tofu.db b/tests/oldmultiplesubkeygnupg/tofu.db
new file mode 100644
index 0000000000000000000000000000000000000000..ae1acde5dc13307516982f61266bcaf315235dfb
GIT binary patch
diff --git a/tests/oldmultiplesubkeygnupg/trustdb.gpg b/tests/oldmultiplesubkeygnupg/trustdb.gpg
new file mode 100644
index 0000000000000000000000000000000000000000..ebfaf7b95379618f1028fb59f4705e6cfe7db061
GIT binary patch
diff --git a/tests/setup.sh b/tests/setup.sh
index e49d8cc..6452cf9 100644
--- a/tests/setup.sh
+++ b/tests/setup.sh
@@ -51,6 +51,7 @@ fi
 # Note: the assumption is the test key is unencrypted.
 export GNUPGHOME="$TEST_HOME/gnupg/"
 chmod 700 "$GNUPGHOME"
+chmod 700 "$TEST_HOME/oldmultiplesubkeygnupg/"
 GPG="gpg"
 which gpg2 &>/dev/null && GPG="gpg2"
 
diff --git a/tests/t0300-reencryption.sh b/tests/t0300-reencryption.sh
index 0379be7..dae895f 100755
--- a/tests/t0300-reencryption.sh
+++ b/tests/t0300-reencryption.sh
@@ -50,6 +50,15 @@ test_expect_success 'Reencryption root single key with multiple subkeys' '
 	[[ $(canonicalize_gpg_keys "$KEY6") == "$(gpg_keys_from_encrypted_file "$PASSWORD_STORE_DIR/folder/cred1.gpg")" ]]
 '
 
+# When init'ing with a key with multiple encryption subkeys(as in the test before), this test will fail
+# This test simulates the scenario where we have two pass repositories using the same key, and one of them adds a subkey to the key,
+# and does an init, and then does a 'pass git push'. If the other repository does an 'pass git pull', it will not be able to read
+# its passwords, because the password file is encrypted with a subkey it does not have. The error message is:
+# "gpg: decryption failed: No secret key".
+test_expect_success 'Simulates pass show after reencryption with key with multiple subkeys' '
+	GNUPGHOME="$TEST_HOME/oldmultiplesubkeygnupg/" "$PASS" show folder/cred1
+'
+
 test_expect_success 'Reencryption root group' '
 	"$PASS" init group1 &&
 	[[ $(gpg_keys_from_group group1) == "$(gpg_keys_from_encrypted_file "$PASSWORD_STORE_DIR/folder/cred1.gpg")" ]]
-- 
2.23.0

