gnome-keyring r1709 - in trunk: . egg egg/tests



Author: stefw
Date: Thu Apr  2 03:30:11 2009
New Revision: 1709
URL: http://svn.gnome.org/viewvc/gnome-keyring?rev=1709&view=rev

Log:
Put in a semi-random test excercising 1000 iterations of allocation,
reallocation, freeing.

Validates between each step.

Modified:
   trunk/ChangeLog
   trunk/egg/egg-secure-memory.c
   trunk/egg/tests/unit-test-secmem.c

Modified: trunk/egg/egg-secure-memory.c
==============================================================================
--- trunk/egg/egg-secure-memory.c	(original)
+++ trunk/egg/egg-secure-memory.c	Thu Apr  2 03:30:11 2009
@@ -78,7 +78,7 @@
 	egg_memory_unlock ();
 
 static int lock_warning = 1;
-
+int egg_secure_warnings = 1;
 
 /* 
  * We allocate all memory in units of sizeof(void*). This 
@@ -791,7 +791,7 @@
 #if defined(HAVE_MLOCK)
 	pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 	if (pages == MAP_FAILED) {
-		if (lock_warning)
+		if (lock_warning && egg_secure_warnings)
 			fprintf (stderr, "couldn't map %lu bytes of private memory: %s\n", 
 			         (unsigned long)*sz, strerror (errno));
 		lock_warning = 0;
@@ -799,7 +799,7 @@
 	}
 	
 	if (mlock (pages, *sz) < 0) {
-		if (lock_warning && errno != EPERM) {
+		if (lock_warning && egg_secure_warnings && errno != EPERM) {
 			fprintf (stderr, "couldn't lock %lu bytes of private memory: %s\n", 
 			         (unsigned long)*sz, strerror (errno));
 			lock_warning = 0;
@@ -814,7 +814,7 @@
 	return pages;
 	
 #else
-	if (lock_warning)
+	if (lock_warning && egg_secure_warnings)
 		fprintf (stderr, "your system does not support private memory");
 	lock_warning = 0;
 	return NULL;
@@ -829,10 +829,10 @@
 	ASSERT (sz % getpagesize () == 0);
 	
 #if defined(HAVE_MLOCK)
-	if (munlock (pages, sz) < 0)
+	if (munlock (pages, sz) < 0 && egg_secure_warnings)
 		fprintf (stderr, "couldn't unlock private memory: %s\n", strerror (errno));
 		
-	if (munmap (pages, sz) < 0)
+	if (munmap (pages, sz) < 0 && egg_secure_warnings)
 		fprintf (stderr, "couldn't unmap private anonymous memory: %s\n", strerror (errno));
 		
 	DEBUG_ALLOC ("gkr-secure-memory: freed block ", sz);
@@ -949,8 +949,9 @@
 	void *memory = NULL;
 		
 	if (length > 0xFFFFFFFF / 2) {
-		fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", 
-		         (unsigned long)length);   
+		if (egg_secure_warnings)
+			fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", 
+			         (unsigned long)length);   
 		return NULL;
 	}
 
@@ -1007,9 +1008,9 @@
 	void *alloc = NULL;
 	
 	if (length > 0xFFFFFFFF / 2) {
-		fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", 
-		         (unsigned long)length);
-		ASSERT (0 && "tried to allocate an insane amount of memory");
+		if (egg_secure_warnings)
+			fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", 
+			         (unsigned long)length);
 		return NULL;
 	}
 	
@@ -1061,8 +1062,9 @@
 			 */
 			return egg_memory_fallback (memory, length);
 		} else {
-			fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", 
-			         (unsigned long)memory);
+			if (egg_secure_warnings)
+				fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", 
+				         (unsigned long)memory);
 			ASSERT (0 && "memory does does not belong to gnome-keyring");
 			return NULL;
 		}
@@ -1122,8 +1124,9 @@
 		if ((flags & GKR_SECURE_USE_FALLBACK)) {
 			egg_memory_fallback (memory, 0);
 		} else {
-			fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", 
-			         (unsigned long)memory);
+			if (egg_secure_warnings)
+				fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", 
+				         (unsigned long)memory);
 			ASSERT (0 && "memory does does not belong to gnome-keyring");
 		}
 	}

Modified: trunk/egg/tests/unit-test-secmem.c
==============================================================================
--- trunk/egg/tests/unit-test-secmem.c	(original)
+++ trunk/egg/tests/unit-test-secmem.c	Thu Apr  2 03:30:11 2009
@@ -29,6 +29,9 @@
 
 #include "egg/egg-secure-memory.h"
 
+/* Declared in egg-secure-memory.c */
+extern int egg_secure_warnings;
+
 /* 
  * Each test looks like (on one line):
  *     void unit_test_xxxxx (CuTest* cu)
@@ -135,3 +138,69 @@
 	g_assert (p == NULL);
 }
 
+DEFINE_TEST(secmem_multialloc)
+{
+	GPtrArray *memory;
+	gpointer data;
+	gsize size;
+	int i, action, index;
+
+	/* A predetermined seed to get a predetermined pattern */
+	g_random_set_seed (15);
+	memory = g_ptr_array_new ();
+	
+	/* Don't print "can't allocate" warnings */
+	egg_secure_warnings = 0;
+
+	for (i = 0; TRUE; ++i) {
+
+		/* Determine what we want to do */
+		if (memory->len > 0) {
+			if (i > 1000) /* Once we've done 1000 alocations start freeing */
+				action = 2;
+			else
+				action = g_random_int_range (0, 3);
+		} else {
+			action = 0; /* No allocations, so allocate */
+		}
+
+		switch (action) {
+		case 0: /* Allocate some memory */
+			size = g_random_int_range (1, 16384);
+			data = egg_secure_alloc (size);
+			g_assert (data);
+			memset (data, 0xCAFEBABE, size);
+			g_ptr_array_add (memory, data);
+			break;
+		case 1: /* Reallocate some memory */
+			index = g_random_int_range (0, memory->len);
+			data = g_ptr_array_index (memory, index);
+			g_assert (data);
+			size = g_random_int_range (1, 16384);
+			data = egg_secure_realloc (data, size);
+			g_assert (data);
+			memset (data, 0xCAFEBABE, size);
+			g_ptr_array_index (memory, index) = data;
+			break;
+		case 2: /* Free some memory */
+			index = g_random_int_range (0, memory->len);
+			data = g_ptr_array_index (memory, index);
+			g_assert (data);
+			egg_secure_free (data);
+			g_ptr_array_remove_index_fast (memory, index);
+			break;
+		default:
+			g_assert_not_reached ();
+		}
+		
+		egg_secure_validate ();
+		
+		if (i > 1000 && !memory->len)
+			break;
+	}
+	
+	g_assert (memory->len == 0);
+	g_ptr_array_free (memory, TRUE);
+	
+	egg_secure_warnings = 1;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]