trinity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/7] add random_pathstring()
@ 2015-03-16 20:25 tyson.w.smith
  2015-03-17 14:26 ` Dave Jones
  0 siblings, 1 reply; 2+ messages in thread
From: tyson.w.smith @ 2015-03-16 20:25 UTC (permalink / raw)
  To: davej; +Cc: trinity, tysmith, Tyson Smith

From: Tyson Smith <tyson.w.smith@gmail.com>

This will generate a path that will likely not exist but may
look somewhat valid or totally crazy depending on rand.
---
 random-pathname.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/random-pathname.c b/random-pathname.c
index 4d093d1..d8f5094 100644
--- a/random-pathname.c
+++ b/random-pathname.c
@@ -9,6 +9,52 @@
 
 #define MAX_PATH_LEN 4096
 
+/*
+ * Generate a path that will likely not exist but may look
+ * somewhat valid or totally crazy depending on rand.
+ */
+static void random_pathstring(char *path, unsigned int len) {
+	const char *fmts = "dns";
+	unsigned int i;
+
+	if (len < 1)
+		return;
+
+	switch(rand() % 5) {
+	case 0:
+		// single repeating random ASCII character
+		(void) memset(path, (rand() % 95) + 32, rand() % len);
+		path[len-1] = '\0';
+		break;
+	case 1:
+		// random ASCII characters 32(space) -> 126(~)
+		for (i=0; i < len; i++)
+			path[i] = (char) ((rand() % 95) + 32);
+		path[len-1] = '\0';
+		break;
+	case 2:
+		// random . or /
+		for (i=0; i < len; i++)
+			path[i] = RAND_BOOL() ? '.' : '/';
+		path[len-1] = '\0';
+		break;
+	case 3:
+		// format strings
+		for (i=0; i < (len - 2); i+=2) {
+			path[i] = '%';
+			path[i+1] = fmts[rand() % 3];
+		}
+		path[len-1] = '\0';
+		break;
+	case 4:
+		// junk
+		for (i=0; i < len; i++)
+			path[i] = (char) RAND_BYTE();
+		path[len-1] = '\0';
+		break;
+	}
+}
+
 const char * generate_pathname(void)
 {
 	const char *pathname = get_filename();
@@ -25,6 +71,13 @@ const char * generate_pathname(void)
 	/* Create a bogus filename. */
 	newpath = zmalloc(MAX_PATH_LEN);	// FIXME: We leak this.
 
+	/* 50/50 chance using a random path string */
+	if (RAND_BOOL()) {
+		len = RAND_BOOL() ? RAND_BYTE() : rand() % MAX_PATH_LEN;
+		random_pathstring(newpath, len);
+		return newpath;
+	}
+
 	len = strlen(pathname);
 
 	if (RAND_BOOL())
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-17 14:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-16 20:25 [PATCH 1/7] add random_pathstring() tyson.w.smith
2015-03-17 14:26 ` Dave Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).