[git] GnuPG - branch, master, updated. gnupg-2.1.7-73-gbd0c902

by Neal H. Walfield cvs at cvs.gnupg.org
Wed Sep 2 15:09:32 CEST 2015


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The GNU Privacy Guard".

The branch, master has been updated
       via  bd0c902f1de46eda03a065da41487e7e01ab4c50 (commit)
       via  1f03d4cd940fed26fc3ffa1742728d68c55ee5d1 (commit)
       via  ee7ec1256b24dc340656c331ef92fc59cad817b6 (commit)
       via  60bc518645d3acfd4dcb79e61a2be6ce001e93aa (commit)
      from  f076fa190e09eab5c586650d81e241e0bb85ce25 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit bd0c902f1de46eda03a065da41487e7e01ab4c50
Author: Neal H. Walfield <neal at g10code.com>
Date:   Tue Sep 1 14:53:47 2015 +0200

    g10: Remove unused field req_algo.
    
    * g10/packet.h (PKT_public_key): Remove unused field req_algo.  Remove
    users.
    * g10/getkey.c (struct getkey_ctx_s): Remove unused field req_algo.
    Remove users.
    
    --
    Signed-off-by: Neal H. Walfield <neal at g10code.com>.

diff --git a/g10/getkey.c b/g10/getkey.c
index 6e85834..7aeaebc 100644
--- a/g10/getkey.c
+++ b/g10/getkey.c
@@ -55,7 +55,6 @@ struct getkey_ctx_s
   KBNODE found_key;	 /* Pointer into some keyblock. */
   strlist_t extra_list;	 /* Will be freed when releasing the context.  */
   int req_usage;
-  int req_algo;
   KEYDB_HANDLE kr_handle;
   int not_allocated;
   int nitems;
@@ -392,7 +391,6 @@ get_pubkey (PKT_public_key * pk, u32 * keyid)
     ctx.items[0].mode = KEYDB_SEARCH_MODE_LONG_KID;
     ctx.items[0].u.kid[0] = keyid[0];
     ctx.items[0].u.kid[1] = keyid[1];
-    ctx.req_algo = pk->req_algo;
     ctx.req_usage = pk->req_usage;
     rc = lookup (&ctx, &kb, 0);
     if (!rc)
@@ -524,7 +522,6 @@ get_seckey (PKT_public_key *pk, u32 *keyid)
   ctx.items[0].mode = KEYDB_SEARCH_MODE_LONG_KID;
   ctx.items[0].u.kid[0] = keyid[0];
   ctx.items[0].u.kid[1] = keyid[1];
-  ctx.req_algo = pk->req_algo;
   ctx.req_usage = pk->req_usage;
   err = lookup (&ctx, &keyblock, 1);
   if (!err)
@@ -661,7 +658,6 @@ key_byname (GETKEY_CTX *retctx, strlist_t namelist,
 
   if (pk)
     {
-      ctx->req_algo = pk->req_algo;
       ctx->req_usage = pk->req_usage;
     }
 
diff --git a/g10/packet.h b/g10/packet.h
index 826963e..1906ec5 100644
--- a/g10/packet.h
+++ b/g10/packet.h
@@ -273,7 +273,6 @@ typedef struct
   byte    pubkey_algo;    /* algorithm used for public key scheme */
   byte    pubkey_usage;   /* for now only used to pass it to getkey() */
   byte    req_usage;      /* hack to pass a request to getkey() */
-  byte    req_algo;       /* Ditto */
   u32     has_expired;    /* set to the expiration date if expired */
   u32     main_keyid[2];  /* keyid of the primary key */
   u32     keyid[2];	    /* calculated by keyid_from_pk() */

commit 1f03d4cd940fed26fc3ffa1742728d68c55ee5d1
Author: Neal H. Walfield <neal at g10code.com>
Date:   Tue Sep 1 10:40:04 2015 +0200

    g10: Use a symbolic constant instead of a literal.
    
    * g10/trustdb.c (KEY_HASH_TABLE_SIZE): Define.
    (new_key_hash_table): Use KEY_HASH_TABLE_SIZE instead of a literal.
    (release_key_hash_table): Likewise.
    (test_key_hash_table): Likewise.
    (add_key_hash_table): Likewise.
    
    --
    Signed-off-by: Neal H. Walfield <neal at g10code.com>.

diff --git a/g10/trustdb.c b/g10/trustdb.c
index 1826e98..ba4ba5f 100644
--- a/g10/trustdb.c
+++ b/g10/trustdb.c
@@ -99,20 +99,22 @@ release_key_items (struct key_item *k)
     }
 }
 
+#define KEY_HASH_TABLE_SIZE 1024
+
 /*
- * For fast keylook up we need a hash table.  Each byte of a KeyIDs
+ * For fast keylook up we need a hash table.  Each byte of a KeyID
  * should be distributed equally over the 256 possible values (except
  * for v3 keyIDs but we consider them as not important here). So we
- * can just use 10 bits to index a table of 1024 key items.
- * Possible optimization: Don not use key_items but other hash_table when the
- * duplicates lists gets too large.
+ * can just use 10 bits to index a table of KEY_HASH_TABLE_SIZE key items.
+ * Possible optimization: Do not use key_items but other hash_table when the
+ * duplicates lists get too large.
  */
 static KeyHashTable
 new_key_hash_table (void)
 {
   struct key_item **tbl;
 
-  tbl = xmalloc_clear (1024 * sizeof *tbl);
+  tbl = xmalloc_clear (KEY_HASH_TABLE_SIZE * sizeof *tbl);
   return tbl;
 }
 
@@ -123,7 +125,7 @@ release_key_hash_table (KeyHashTable tbl)
 
   if (!tbl)
     return;
-  for (i=0; i < 1024; i++)
+  for (i=0; i < KEY_HASH_TABLE_SIZE; i++)
     release_key_items (tbl[i]);
   xfree (tbl);
 }
@@ -136,7 +138,7 @@ test_key_hash_table (KeyHashTable tbl, u32 *kid)
 {
   struct key_item *k;
 
-  for (k = tbl[(kid[1] & 0x03ff)]; k; k = k->next)
+  for (k = tbl[(kid[1] % KEY_HASH_TABLE_SIZE)]; k; k = k->next)
     if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
       return 1;
   return 0;
@@ -148,17 +150,18 @@ test_key_hash_table (KeyHashTable tbl, u32 *kid)
 static void
 add_key_hash_table (KeyHashTable tbl, u32 *kid)
 {
+  int i = kid[1] % KEY_HASH_TABLE_SIZE;
   struct key_item *k, *kk;
 
-  for (k = tbl[(kid[1] & 0x03ff)]; k; k = k->next)
+  for (k = tbl[i]; k; k = k->next)
     if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
       return; /* already in table */
 
   kk = new_key_item ();
   kk->kid[0] = kid[0];
   kk->kid[1] = kid[1];
-  kk->next = tbl[(kid[1] & 0x03ff)];
-  tbl[(kid[1] & 0x03ff)] = kk;
+  kk->next = tbl[i];
+  tbl[i] = kk;
 }
 
 /*

commit ee7ec1256b24dc340656c331ef92fc59cad817b6
Author: Neal H. Walfield <neal at g10code.com>
Date:   Wed Sep 2 15:07:06 2015 +0200

    g10: Add test for keydb as well as new testing infrastructure.
    
    * g10/Makefile.am (EXTRA_DIST): Add test.c.
    (AM_CPPFLAGS): Add -DSOURCE_DIR="\"$(srcdir)\"".
    (module_tests): Add t-keydb.
    (t_keydb_SOURCES): New variable.
    (t_keydb_LDADD): Likewise.
    * g10/t-keydb.c: New file.
    * g10/t-keydb-keyring.kbx: New file.
    * g10/test-stubs.c: New file.
    * g10/test.c: New file.
    
    --
    Signed-off-by: Neal H. Walfield <neal at g10code.com>.

diff --git a/g10/Makefile.am b/g10/Makefile.am
index 0c53eab..421870c 100644
--- a/g10/Makefile.am
+++ b/g10/Makefile.am
@@ -19,9 +19,9 @@
 ## Process this file with automake to produce Makefile.in
 
 EXTRA_DIST = options.skel distsigkey.gpg ChangeLog-2011 gpg-w32info.rc \
-	     gpg.w32-manifest.in
+	     gpg.w32-manifest.in test.c
 
-AM_CPPFLAGS = -I$(top_srcdir)/common
+AM_CPPFLAGS = -I$(top_srcdir)/common -DSOURCE_DIR="\"$(srcdir)\""
 
 include $(top_srcdir)/am/cmacros.am
 
@@ -150,9 +150,12 @@ gpgv2_LDADD = $(LDADD) $(LIBGCRYPT_LIBS) \
 gpgv2_LDFLAGS = $(extra_bin_ldflags)
 
 t_common_ldadd =
-module_tests = t-rmd160
+module_tests = t-rmd160 t-keydb
 t_rmd160_SOURCES = t-rmd160.c rmd160.c
 t_rmd160_LDADD = $(t_common_ldadd)
+t_keydb_SOURCES = t-keydb.c test-stubs.c $(common_source)
+t_keydb_LDADD = $(LDADD) $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) \
+	      $(LIBICONV) $(t_common_ldadd)
 
 
 $(PROGRAMS): $(needed_libs) ../common/libgpgrl.a
diff --git a/g10/t-keydb-keyring.kbx b/g10/t-keydb-keyring.kbx
new file mode 100644
index 0000000..a1d3af0
Binary files /dev/null and b/g10/t-keydb-keyring.kbx differ
diff --git a/g10/t-keydb.c b/g10/t-keydb.c
new file mode 100644
index 0000000..0f17643
--- /dev/null
+++ b/g10/t-keydb.c
@@ -0,0 +1,87 @@
+/* t-keydb.c - Tests for keydb.c.
+ * Copyright (C) 2015 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "test.c"
+
+#include "keydb.h"
+
+static void
+do_test (int argc, char *argv[])
+{
+  int rc;
+  KEYDB_HANDLE hd1, hd2;
+  KEYDB_SEARCH_DESC desc1, desc2;
+  KBNODE kb1, kb2;
+  char *uid1;
+  char *uid2;
+
+  (void) argc;
+  (void) argv;
+
+  rc = keydb_add_resource (SOURCE_DIR "/t-keydb-keyring.kbx", 0);
+  if (rc)
+    ABORT ("Failed to open keyring.");
+
+  hd1 = keydb_new ();
+  hd2 = keydb_new ();
+
+  rc = classify_user_id ("2689 5E25 E844 6D44 A26D  8FAF 2F79 98F3 DBFC 6AD9",
+			 &desc1, 0);
+  if (rc)
+    ABORT ("Failed to convert fingerprint for DBFC6AD9");
+
+  rc = keydb_search (hd1, &desc1, 1, NULL);
+  if (rc)
+    ABORT ("Failed to lookup key associated with DBFC6AD9");
+
+
+  classify_user_id ("8061 5870 F5BA D690 3336  86D0 F2AD 85AC 1E42 B367",
+		    &desc2, 0);
+  if (rc)
+    ABORT ("Failed to convert fingerprint for 1E42B367");
+
+  rc = keydb_search (hd2, &desc2, 1, NULL);
+  if (rc)
+    ABORT ("Failed to lookup key associated with 1E42B367");
+
+  rc = keydb_get_keyblock (hd2, &kb2);
+  if (rc)
+    ABORT ("Failed to get keyblock for 1E42B367");
+
+  rc = keydb_get_keyblock (hd1, &kb1);
+  if (rc)
+    ABORT ("Failed to get keyblock for DBFC6AD9");
+
+  while (kb1 && kb1->pkt->pkttype != PKT_USER_ID)
+    kb1 = kb1->next;
+  if (! kb1)
+    ABORT ("DBFC6AD9 has no user id packet");
+  uid1 = kb1->pkt->pkt.user_id->name;
+
+  while (kb2 && kb2->pkt->pkttype != PKT_USER_ID)
+    kb2 = kb2->next;
+  if (! kb2)
+    ABORT ("1E42B367 has no user id packet");
+  uid2 = kb2->pkt->pkt.user_id->name;
+
+  printf ("user id for DBFC6AD9: %s\n", uid1);
+  printf ("user id for 1E42B367: %s\n", uid2);
+
+  TEST_P ("cache consistency", strcmp (uid1, uid2) != 0);
+}
diff --git a/g10/test-stubs.c b/g10/test-stubs.c
new file mode 100644
index 0000000..c6f6d68
--- /dev/null
+++ b/g10/test-stubs.c
@@ -0,0 +1,413 @@
+/* test-stubs.c - The GnuPG signature verify utility
+ * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2006,
+ *               2008, 2009, 2012 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+
+#define INCLUDED_BY_MAIN_MODULE 1
+#include "gpg.h"
+#include "util.h"
+#include "packet.h"
+#include "iobuf.h"
+#include "main.h"
+#include "options.h"
+#include "keydb.h"
+#include "trustdb.h"
+#include "filter.h"
+#include "ttyio.h"
+#include "i18n.h"
+#include "sysutils.h"
+#include "status.h"
+#include "call-agent.h"
+
+int g10_errors_seen;
+
+
+void
+g10_exit( int rc )
+{
+  rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0;
+  exit(rc );
+}
+
+
+/* Stub:
+ * We have to override the trustcheck from pkclist.c becuase
+ * this utility assumes that all keys in the keyring are trustworthy
+ */
+int
+check_signatures_trust( PKT_signature *sig )
+{
+  (void)sig;
+  return 0;
+}
+
+void
+read_trust_options(byte *trust_model, ulong *created, ulong *nextcheck,
+		   byte *marginals, byte *completes, byte *cert_depth,
+		   byte *min_cert_level)
+{
+  (void)trust_model;
+  (void)created;
+  (void)nextcheck;
+  (void)marginals;
+  (void)completes;
+  (void)cert_depth;
+  (void)min_cert_level;
+}
+
+/* Stub:
+ * We don't have the trustdb , so we have to provide some stub functions
+ * instead
+ */
+
+int
+cache_disabled_value(PKT_public_key *pk)
+{
+  (void)pk;
+  return 0;
+}
+
+void
+check_trustdb_stale(void)
+{
+}
+
+int
+get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
+{
+  (void)pk;
+  (void)uid;
+  return '?';
+}
+
+unsigned int
+get_validity (PKT_public_key *pk, PKT_user_id *uid)
+{
+  (void)pk;
+  (void)uid;
+  return 0;
+}
+
+const char *
+trust_value_to_string (unsigned int value)
+{
+  (void)value;
+  return "err";
+}
+
+const char *
+uid_trust_string_fixed (PKT_public_key *key, PKT_user_id *uid)
+{
+  (void)key;
+  (void)uid;
+  return "err";
+}
+
+int
+get_ownertrust_info (PKT_public_key *pk)
+{
+  (void)pk;
+  return '?';
+}
+
+unsigned int
+get_ownertrust (PKT_public_key *pk)
+{
+  (void)pk;
+  return TRUST_UNKNOWN;
+}
+
+
+/* Stubs:
+ * Because we only work with trusted keys, it does not make sense to
+ * get them from a keyserver
+ */
+
+struct keyserver_spec *
+keyserver_match (struct keyserver_spec *spec)
+{
+  (void)spec;
+  return NULL;
+}
+
+int
+keyserver_import_keyid (u32 *keyid, void *dummy)
+{
+  (void)keyid;
+  (void)dummy;
+  return -1;
+}
+
+int
+keyserver_import_cert (const char *name)
+{
+  (void)name;
+  return -1;
+}
+
+int
+keyserver_import_pka (const char *name,unsigned char *fpr)
+{
+  (void)name;
+  (void)fpr;
+  return -1;
+}
+
+int
+keyserver_import_name (const char *name,struct keyserver_spec *spec)
+{
+  (void)name;
+  (void)spec;
+  return -1;
+}
+
+int
+keyserver_import_ldap (const char *name)
+{
+  (void)name;
+  return -1;
+}
+
+/* Stub:
+ * No encryption here but mainproc links to these functions.
+ */
+gpg_error_t
+get_session_key (PKT_pubkey_enc *k, DEK *dek)
+{
+  (void)k;
+  (void)dek;
+  return GPG_ERR_GENERAL;
+}
+
+/* Stub: */
+gpg_error_t
+get_override_session_key (DEK *dek, const char *string)
+{
+  (void)dek;
+  (void)string;
+  return GPG_ERR_GENERAL;
+}
+
+/* Stub: */
+int
+decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
+{
+  (void)ctrl;
+  (void)procctx;
+  (void)ed;
+  (void)dek;
+  return GPG_ERR_GENERAL;
+}
+
+
+/* Stub:
+ * No interactive commands, so we don't need the helptexts
+ */
+void
+display_online_help (const char *keyword)
+{
+  (void)keyword;
+}
+
+/* Stub:
+ * We don't use secret keys, but getkey.c links to this
+ */
+int
+check_secret_key (PKT_public_key *pk, int n)
+{
+  (void)pk;
+  (void)n;
+  return GPG_ERR_GENERAL;
+}
+
+/* Stub:
+ * No secret key, so no passphrase needed
+ */
+DEK *
+passphrase_to_dek (u32 *keyid, int pubkey_algo,
+                   int cipher_algo, STRING2KEY *s2k, int mode,
+                   const char *tmp, int *canceled)
+{
+  (void)keyid;
+  (void)pubkey_algo;
+  (void)cipher_algo;
+  (void)s2k;
+  (void)mode;
+  (void)tmp;
+
+  if (canceled)
+    *canceled = 0;
+  return NULL;
+}
+
+void
+passphrase_clear_cache (u32 *keyid, const char *cacheid, int algo)
+{
+  (void)keyid;
+  (void)cacheid;
+  (void)algo;
+}
+
+struct keyserver_spec *
+parse_preferred_keyserver(PKT_signature *sig)
+{
+  (void)sig;
+  return NULL;
+}
+
+struct keyserver_spec *
+parse_keyserver_uri (const char *uri, int require_scheme,
+                     const char *configname, unsigned int configlineno)
+{
+  (void)uri;
+  (void)require_scheme;
+  (void)configname;
+  (void)configlineno;
+  return NULL;
+}
+
+void
+free_keyserver_spec (struct keyserver_spec *keyserver)
+{
+  (void)keyserver;
+}
+
+/* Stubs to avoid linking to photoid.c */
+void
+show_photos (const struct user_attribute *attrs, int count, PKT_public_key *pk)
+{
+  (void)attrs;
+  (void)count;
+  (void)pk;
+}
+
+int
+parse_image_header (const struct user_attribute *attr, byte *type, u32 *len)
+{
+  (void)attr;
+  (void)type;
+  (void)len;
+  return 0;
+}
+
+char *
+image_type_to_string (byte type, int string)
+{
+  (void)type;
+  (void)string;
+  return NULL;
+}
+
+#ifdef ENABLE_CARD_SUPPORT
+int
+agent_scd_getattr (const char *name, struct agent_card_info_s *info)
+{
+  (void)name;
+  (void)info;
+  return 0;
+}
+#endif /* ENABLE_CARD_SUPPORT */
+
+/* We do not do any locking, so use these stubs here */
+void
+dotlock_disable (void)
+{
+}
+
+dotlock_t
+dotlock_create (const char *file_to_lock, unsigned int flags)
+{
+  (void)file_to_lock;
+  (void)flags;
+  return NULL;
+}
+
+void
+dotlock_destroy (dotlock_t h)
+{
+  (void)h;
+}
+
+int
+dotlock_take (dotlock_t h, long timeout)
+{
+  (void)h;
+  (void)timeout;
+  return 0;
+}
+
+int
+dotlock_release (dotlock_t h)
+{
+  (void)h;
+  return 0;
+}
+
+void
+dotlock_remove_lockfiles (void)
+{
+}
+
+gpg_error_t
+agent_probe_secret_key (ctrl_t ctrl, PKT_public_key *pk)
+{
+  (void)ctrl;
+  (void)pk;
+  return gpg_error (GPG_ERR_NO_SECKEY);
+}
+
+gpg_error_t
+agent_probe_any_secret_key (ctrl_t ctrl, kbnode_t keyblock)
+{
+  (void)ctrl;
+  (void)keyblock;
+  return gpg_error (GPG_ERR_NO_SECKEY);
+}
+
+gpg_error_t
+agent_get_keyinfo (ctrl_t ctrl, const char *hexkeygrip, char **r_serialno)
+{
+  (void)ctrl;
+  (void)hexkeygrip;
+  *r_serialno = NULL;
+  return gpg_error (GPG_ERR_NO_SECKEY);
+}
+
+gpg_error_t
+gpg_dirmngr_get_pka (ctrl_t ctrl, const char *userid,
+                     unsigned char **r_fpr, size_t *r_fprlen,
+                     char **r_url)
+{
+  (void)ctrl;
+  (void)userid;
+  if (r_fpr)
+    *r_fpr = NULL;
+  if (r_fprlen)
+    *r_fprlen = 0;
+  if (r_url)
+    *r_url = NULL;
+  return gpg_error (GPG_ERR_NOT_FOUND);
+}
diff --git a/g10/test.c b/g10/test.c
new file mode 100644
index 0000000..6910f95
--- /dev/null
+++ b/g10/test.c
@@ -0,0 +1,141 @@
+/* test.c - Infrastructure for unit tests.
+ * Copyright (C) 2015 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include "gpg.h"
+
+/* A unit test consists of one or more tests.  Tests can be broken
+   into groups and each group can consist of one or more tests.  */
+
+/* The number of test groups.  */
+static int test_groups;
+/* The current test group.  */
+static char *test_group;
+
+/* Whether there was already a failure in the current test group.  */
+static int current_test_group_failed;
+/* The number of test groups with a failure.  */
+static int test_groups_failed;
+
+/* The total number of tests.  */
+static int tests;
+/* The total number of tests that failed.  */
+static int tests_failed;
+
+#define TEST_GROUP(description)	     \
+  do {				     \
+    test_group = (description);	     \
+    test_groups ++;		     \
+    current_test_group_failed = 0;   \
+  } while (0)
+
+#define STRINGIFY2(x) #x
+#define STRINGIFY(x) STRINGIFY2(x)
+
+/* Execute a test.  */
+#define TEST(description, test, expected)	\
+  do {						\
+    int test_result;				\
+    int expected_result;			\
+						\
+    tests ++;					\
+						\
+    printf ("%d. Checking %s...",		\
+	    tests, (description) ?: "");	\
+    fflush (stdout);				\
+						\
+    test_result = (test);			\
+    expected_result = (expected);		\
+						\
+    if (test_result == expected_result)		\
+      {						\
+        printf (" ok.\n");			\
+      }						\
+    else					\
+      {						\
+	printf (" failed.\n");			\
+	printf ("  %s == %s failed.\n",		\
+		STRINGIFY(test),		\
+		STRINGIFY(expected));		\
+	tests_failed ++;			\
+	if (! current_test_group_failed)	\
+	  {					\
+	    current_test_group_failed = 1;	\
+	    test_groups_failed ++;		\
+	  }					\
+      }						\
+  } while (0)
+
+/* Test that a condition evaluates to true.  */
+#define TEST_P(description, test)		\
+  TEST(description, !!(test), 1)
+
+/* Like CHECK, but if the test fails, abort the program.  */
+#define ASSERT(description, test, expected)		\
+  do {							\
+    int tests_failed_pre = tests_failed;		\
+    CHECK(description, test, expected);			\
+    if (tests_failed_pre != tests_failed)		\
+      exit (1);						\
+  } while (0)
+
+/* Call this if something went wrong.  */
+#define ABORT(message)				\
+  do {						\
+    printf ("aborting...");			\
+    if (message)				\
+      printf (" %s\n", (message));		\
+						\
+    exit(1);					\
+  } while (0)
+
+/* You need to fill this function in.  */
+static void do_test (int argc, char *argv[]);
+
+static void
+print_results (void)
+{
+  if (tests_failed == 0)
+    {
+      printf ("All %d tests passed.\n", tests);
+      exit (0);
+    }
+  else
+    {
+      printf ("%d of %d tests failed",
+	      tests_failed, tests);
+      if (test_groups > 1)
+	printf (" (%d of %d groups)",
+		test_groups_failed, test_groups);
+      printf ("\n");
+
+      exit (1);
+    }
+}
+
+int
+main (int argc, char *argv[])
+{
+  (void) test_group;
+
+  do_test (argc, argv);
+  atexit (print_results);
+
+  return tests_failed == 0;
+}

commit 60bc518645d3acfd4dcb79e61a2be6ce001e93aa
Author: Neal H. Walfield <neal at g10code.com>
Date:   Mon Aug 31 13:57:07 2015 +0200

    g10: Make the keyblock cache per-handle rather than global.
    
    * g10/keydb.c (keyblock_cache): Don't declare this variable.  Instead...
    (struct keyblock_cache): ... turn its type into this first class
    object...
    (struct keydb_handle): ... and instantiate it once per database
    handle.  Update all users.
    (keydb_rebuild_caches): Don't invalidate the keyblock cache.
    
    --
    Signed-off-by: Neal H. Walfield <neal at g10code.com>.

diff --git a/g10/keydb.c b/g10/keydb.c
index 124e940..da18bc0 100644
--- a/g10/keydb.c
+++ b/g10/keydb.c
@@ -62,6 +62,28 @@ static struct resource_item all_resources[MAX_KEYDB_RESOURCES];
 static int used_resources;
 static void *primary_keyring=NULL;
 
+
+/* This is a simple cache used to return the last result of a
+   successful fingerprint search.  This works only for keybox resources
+   because (due to lack of a copy_keyblock function) we need to store
+   an image of the keyblock which is fortunately instantly available
+   for keyboxes.  */
+enum keyblock_cache_states {
+  KEYBLOCK_CACHE_EMPTY,
+  KEYBLOCK_CACHE_PREPARED,
+  KEYBLOCK_CACHE_FILLED
+};
+
+struct keyblock_cache {
+  enum keyblock_cache_states state;
+  byte fpr[MAX_FINGERPRINT_LEN];
+  iobuf_t iobuf; /* Image of the keyblock.  */
+  u32 *sigstatus;
+  int pk_no;
+  int uid_no;
+};
+
+
 struct keydb_handle
 {
   /* When we locked all of the resources in ACTIVE (using keyring_lock
@@ -94,6 +116,10 @@ struct keydb_handle
   /* The number of resources in ACTIVE.  */
   int used;
 
+  /* Cache of the last found and parsed key block (only used for
+     keyboxes, not keyrings).  */
+  struct keyblock_cache keyblock_cache;
+
   /* Copy of ALL_RESOURCES when keydb_new is called.  */
   struct resource_item active[MAX_KEYDB_RESOURCES];
 };
@@ -126,27 +152,6 @@ struct kid_not_found_cache_bucket
 };
 
 
-/* This is a simple cache used to return the last result of a
-   successful fingerprint search.  This works only for keybox resources
-   because (due to lack of a copy_keyblock function) we need to store
-   an image of the keyblock which is fortunately instantly available
-   for keyboxes.  */
-enum keyblock_cache_states {
-  KEYBLOCK_CACHE_EMPTY,
-  KEYBLOCK_CACHE_PREPARED,
-  KEYBLOCK_CACHE_FILLED
-};
-
-struct {
-  enum keyblock_cache_states state;
-  byte fpr[MAX_FINGERPRINT_LEN];
-  iobuf_t iobuf; /* Image of the keyblock.  */
-  u32 *sigstatus;
-  int pk_no;
-  int uid_no;
-} keyblock_cache;
-
-
 static int lock_all (KEYDB_HANDLE hd);
 static void unlock_all (KEYDB_HANDLE hd);
 
@@ -233,13 +238,13 @@ kid_not_found_flush (void)
 
 
 static void
-keyblock_cache_clear (void)
+keyblock_cache_clear (struct keydb_handle *hd)
 {
-  keyblock_cache.state = KEYBLOCK_CACHE_EMPTY;
-  xfree (keyblock_cache.sigstatus);
-  keyblock_cache.sigstatus = NULL;
-  iobuf_close (keyblock_cache.iobuf);
-  keyblock_cache.iobuf = NULL;
+  hd->keyblock_cache.state = KEYBLOCK_CACHE_EMPTY;
+  xfree (hd->keyblock_cache.sigstatus);
+  hd->keyblock_cache.sigstatus = NULL;
+  iobuf_close (hd->keyblock_cache.iobuf);
+  hd->keyblock_cache.iobuf = NULL;
 }
 
 
@@ -1124,23 +1129,23 @@ keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb)
   if (DBG_CLOCK)
     log_clock ("keydb_get_keybock enter");
 
-  if (keyblock_cache.state == KEYBLOCK_CACHE_FILLED)
+  if (hd->keyblock_cache.state == KEYBLOCK_CACHE_FILLED)
     {
-      err = iobuf_seek (keyblock_cache.iobuf, 0);
+      err = iobuf_seek (hd->keyblock_cache.iobuf, 0);
       if (err)
 	{
 	  log_error ("keydb_get_keyblock: failed to rewind iobuf for cache\n");
-	  keyblock_cache_clear ();
+	  keyblock_cache_clear (hd);
 	}
       else
 	{
-	  err = parse_keyblock_image (keyblock_cache.iobuf,
-				      keyblock_cache.pk_no,
-				      keyblock_cache.uid_no,
-				      keyblock_cache.sigstatus,
+	  err = parse_keyblock_image (hd->keyblock_cache.iobuf,
+				      hd->keyblock_cache.pk_no,
+				      hd->keyblock_cache.uid_no,
+				      hd->keyblock_cache.sigstatus,
 				      ret_kb);
 	  if (err)
-	    keyblock_cache_clear ();
+	    keyblock_cache_clear (hd);
 	  if (DBG_CLOCK)
 	    log_clock (err? "keydb_get_keyblock leave (cached, failed)"
 		       : "keydb_get_keyblock leave (cached)");
@@ -1171,13 +1176,13 @@ keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb)
           {
             err = parse_keyblock_image (iobuf, pk_no, uid_no, sigstatus,
                                         ret_kb);
-            if (!err && keyblock_cache.state == KEYBLOCK_CACHE_PREPARED)
+            if (!err && hd->keyblock_cache.state == KEYBLOCK_CACHE_PREPARED)
               {
-                keyblock_cache.state     = KEYBLOCK_CACHE_FILLED;
-                keyblock_cache.sigstatus = sigstatus;
-                keyblock_cache.iobuf     = iobuf;
-                keyblock_cache.pk_no     = pk_no;
-                keyblock_cache.uid_no    = uid_no;
+                hd->keyblock_cache.state     = KEYBLOCK_CACHE_FILLED;
+                hd->keyblock_cache.sigstatus = sigstatus;
+                hd->keyblock_cache.iobuf     = iobuf;
+                hd->keyblock_cache.pk_no     = pk_no;
+                hd->keyblock_cache.uid_no    = uid_no;
               }
             else
               {
@@ -1189,8 +1194,8 @@ keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb)
       break;
     }
 
-  if (keyblock_cache.state != KEYBLOCK_CACHE_FILLED)
-    keyblock_cache_clear ();
+  if (hd->keyblock_cache.state != KEYBLOCK_CACHE_FILLED)
+    keyblock_cache_clear (hd);
 
   if (DBG_CLOCK)
     log_clock (err? "keydb_get_keyblock leave (failed)"
@@ -1298,7 +1303,7 @@ keydb_update_keyblock (KEYDB_HANDLE hd, kbnode_t kb)
     return gpg_error (GPG_ERR_INV_ARG);
 
   kid_not_found_flush ();
-  keyblock_cache_clear ();
+  keyblock_cache_clear (hd);
 
   if (hd->found < 0 || hd->found >= hd->used)
     return gpg_error (GPG_ERR_VALUE_NOT_FOUND);
@@ -1349,7 +1354,7 @@ keydb_insert_keyblock (KEYDB_HANDLE hd, kbnode_t kb)
     return gpg_error (GPG_ERR_INV_ARG);
 
   kid_not_found_flush ();
-  keyblock_cache_clear ();
+  keyblock_cache_clear (hd);
 
   if (opt.dry_run)
     return 0;
@@ -1409,7 +1414,7 @@ keydb_delete_keyblock (KEYDB_HANDLE hd)
     return gpg_error (GPG_ERR_INV_ARG);
 
   kid_not_found_flush ();
-  keyblock_cache_clear ();
+  keyblock_cache_clear (hd);
 
   if (hd->found < 0 || hd->found >= hd->used)
     return gpg_error (GPG_ERR_VALUE_NOT_FOUND);
@@ -1497,8 +1502,6 @@ keydb_rebuild_caches (int noisy)
 {
   int i, rc;
 
-  keyblock_cache_clear ();
-
   for (i=0; i < used_resources; i++)
     {
       if (!keyring_is_writable (all_resources[i].token))
@@ -1537,7 +1540,7 @@ keydb_search_reset (KEYDB_HANDLE hd)
   if (!hd)
     return gpg_error (GPG_ERR_INV_ARG);
 
-  keyblock_cache_clear ();
+  keyblock_cache_clear (hd);
 
   if (DBG_CLOCK)
     log_clock ("keydb_search_reset");
@@ -1652,8 +1655,8 @@ keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
       && ndesc == 1
       && (desc[0].mode == KEYDB_SEARCH_MODE_FPR20
           || desc[0].mode == KEYDB_SEARCH_MODE_FPR)
-      && keyblock_cache.state  == KEYBLOCK_CACHE_FILLED
-      && !memcmp (keyblock_cache.fpr, desc[0].u.fpr, 20))
+      && hd->keyblock_cache.state  == KEYBLOCK_CACHE_FILLED
+      && !memcmp (hd->keyblock_cache.fpr, desc[0].u.fpr, 20))
     {
       /* (DESCINDEX is already set).  */
       if (DBG_CLOCK)
@@ -1694,14 +1697,14 @@ keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
         ? gpg_error (GPG_ERR_NOT_FOUND)
         : rc);
 
-  keyblock_cache_clear ();
+  keyblock_cache_clear (hd);
   if (!hd->no_caching
       && !rc
       && ndesc == 1 && (desc[0].mode == KEYDB_SEARCH_MODE_FPR20
                         || desc[0].mode == KEYDB_SEARCH_MODE_FPR))
     {
-      keyblock_cache.state = KEYBLOCK_CACHE_PREPARED;
-      memcpy (keyblock_cache.fpr, desc[0].u.fpr, 20);
+      hd->keyblock_cache.state = KEYBLOCK_CACHE_PREPARED;
+      memcpy (hd->keyblock_cache.fpr, desc[0].u.fpr, 20);
     }
 
   if (gpg_err_code (rc) == GPG_ERR_NOT_FOUND

-----------------------------------------------------------------------

Summary of changes:
 g10/Makefile.am              |   9 ++-
 g10/getkey.c                 |   4 -
 g10/keydb.c                  | 111 +++++++++++++-------------
 g10/packet.h                 |   1 -
 g10/t-keydb-keyring.kbx      | Bin 0 -> 5104 bytes
 g10/t-keydb.c                |  87 ++++++++++++++++++++
 g10/{gpgv.c => test-stubs.c} | 185 +------------------------------------------
 g10/test.c                   | 141 +++++++++++++++++++++++++++++++++
 g10/trustdb.c                |  23 +++---
 9 files changed, 306 insertions(+), 255 deletions(-)
 create mode 100644 g10/t-keydb-keyring.kbx
 create mode 100644 g10/t-keydb.c
 copy g10/{gpgv.c => test-stubs.c} (60%)
 create mode 100644 g10/test.c


hooks/post-receive
-- 
The GNU Privacy Guard
http://git.gnupg.org




More information about the Gnupg-commits mailing list