socsvn commit: r256139 - soc2013/mattbw/backend/query

mattbw at FreeBSD.org mattbw at FreeBSD.org
Mon Aug 19 12:00:15 UTC 2013


Author: mattbw
Date: Mon Aug 19 12:00:15 2013
New Revision: 256139
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=256139

Log:
  (STILL BROKEN) More work on the ID conversion functions for query.
  
  I'm experimenting with using ATF/kyua to run tests for the backend, with
  this part of the backend being the testbed.
  
  Currently the tests report that my code segfaults, so the next commit will
  hopefully fix that.
  

Added:
  soc2013/mattbw/backend/query/Atffile
  soc2013/mattbw/backend/query/id.h
  soc2013/mattbw/backend/query/id_test.c
Modified:
  soc2013/mattbw/backend/query/id.c

Added: soc2013/mattbw/backend/query/Atffile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/query/Atffile	Mon Aug 19 12:00:15 2013	(r256139)
@@ -0,0 +1,5 @@
+Content-Type: application/X-atf-atffile; version="1"
+
+prop: test-suite = "query"
+
+tp: id_test

Modified: soc2013/mattbw/backend/query/id.c
==============================================================================
--- soc2013/mattbw/backend/query/id.c	Mon Aug 19 11:16:53 2013	(r256138)
+++ soc2013/mattbw/backend/query/id.c	Mon Aug 19 12:00:15 2013	(r256139)
@@ -1,17 +1,52 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw at FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <assert.h>		/* assert */
+#include <glib.h>		/* g... */
+#include <stdlib.h>		/* calloc */
+#include <string.h>		/* strdup */
+#include "../pk-backend.h"	/* pk_... */
+
+#include "id.h"			/* query_id_... */
+
+
 typedef bool	(*id_from_string_ptr) (const char *, struct query_id *);
 
+static char    *join_namever(const char *name, const char *version);
+static char    *null_if_empty(char *in);
+static struct query_id *query_id_array_alloc(unsigned int count);
+static struct query_id *query_id_array_from_strings(char **strings, unsigned int package_count, id_from_string_ptr id_from_string);
+static void	query_id_array_free(struct query_id **query_ids_p, unsigned int count);
+static void	query_id_free_contents(struct query_id *query_id);
+
 /* Converts an array of PackageIDs to query IDs. */
-static struct query_id *
+struct query_id *
 query_id_array_from_package_ids(gchar **package_ids,
     unsigned int package_count)
 {
 
-	return query_id_array_from_strings(package_names, package_count,
+	return query_id_array_from_strings(package_ids, package_count,
 	    query_id_from_package_id);
 }
 
 /* Converts an array of names (or name-version strings) to query IDs. */
-static struct query_id *
+struct query_id *
 query_id_array_from_names(gchar **package_names, unsigned int package_count)
 {
 
@@ -27,14 +62,12 @@
 query_id_array_from_strings(char **strings, unsigned int package_count,
     id_from_string_ptr id_from_string)
 {
-	unsigned int	i;
-	gchar	       *split_package_id;
 	struct query_id *query_ids;
 
 	assert(strings != NULL);
 	assert(id_from_string != NULL);
 
-	query_ids = query_id_alloc_array(package_count);
+	query_ids = query_id_array_alloc(package_count);
 	if (query_ids != NULL) {
 		bool		error;
 		unsigned int	i;
@@ -43,20 +76,24 @@
 		for (i = 0; i < package_count; i++) {
 			error = id_from_string(strings[i], query_ids + i);
 			if (error) {
-				free_split_ids(&query_ids, package_count);
+				query_id_array_free(&query_ids, package_count);
 				break;
 			}
 		}
+
+		if (error) {
+			query_id_array_free(&query_ids, package_count);
+		}
 	}
 
-	return split_ids;
+	return query_ids;
 }
 
 /*
  * Converts a package name or name-version string to a query ID.
  * Overwrites the contents of *query_id with said ID.
  */
-static bool
+bool
 query_id_from_name(const char *name, struct query_id *query_id)
 {
 
@@ -75,24 +112,24 @@
  * Overwrites the contents of *query_id with said ID.
  */
 bool
-query_id_from_package_id(const gchar *package_id, struct query_id *split_id)
+query_id_from_package_id(const gchar *package_id, struct query_id *query_id)
 {
 	bool		success;
 	gchar	      **split_package_id;
 
-	assert(name != NULL);
-	assert(split_id != NULL);
+	assert(package_id != NULL);
+	assert(query_id != NULL);
 
 	success = false;
 
 	split_package_id = pk_package_id_split(package_id);
-	if (split_package_id == NULL) {
-		error = true;
-	} else {
+	if (split_package_id != NULL) {
 		bool		have_name;
 		bool		have_version;
 		gchar	       *name;
 		gchar	       *version;
+		gchar	       *arch;
+		gchar	       *repo;
 
 		/* We're not allowed to have an empty name or version. */
 
@@ -102,21 +139,26 @@
 		version = split_package_id[PK_PACKAGE_ID_VERSION];
 		have_version = (version != NULL && version[0] != '\0');
 
+		/*
+		 * Names and versions are mandatory.
+		 * Anything else is optional.
+		 */
 		if (have_name && have_version) {
-			split_ids[1].namever = join_namever(name, version);
-		}
+			query_id->namever = join_namever(name, version);
 
-		if (*split_package_id[PK_PACKAGE_ID_VERSION] == '\0') {
+			arch = split_package_id[PK_PACKAGE_ID_ARCH];
+			query_id->arch = null_if_empty(arch);
 
+			repo = split_package_id[PK_PACKAGE_ID_DATA];
+			query_id->repo = null_if_empty(repo);
 
-		g_strfreev(split_package_id);
-			}
-		i
-		o
+			success = true;
 		}
+
+		g_strfreev(split_package_id);
 	}
 
-	return split_ids;
+	return success;
 }
 
 /*
@@ -135,13 +177,51 @@
 	assert(version != NULL);
 
 	namever = NULL;
-	asprintf(&namever, "%s-%s", name, version);
+	(void)asprintf(&namever, "%s-%s", name, version);
 
+	return namever;
 }
 
 /* Allocates an array of query IDs. */
 static struct query_id *
-query_id_alloc_array(unsigned int count)
+query_id_array_alloc(unsigned int count)
 {
+
 	return calloc(count, sizeof(struct query_id));
 }
+
+/*
+ * Returns NULL if the given string is NULL or empty, and the string otherwise.
+ */
+static char *
+null_if_empty(char *in)
+{
+
+	return (in == NULL || in[0] == '\0' ? NULL : in);
+}
+
+static void
+query_id_array_free(struct query_id **query_ids_p, unsigned int count)
+{
+	unsigned int	i;
+
+	assert(query_ids_p != NULL);
+	assert(*query_ids_p != NULL);
+
+	for (i = 0; i < count; i++) {
+		query_id_free_contents((*query_ids_p) + i);
+	}
+
+	free(*query_ids_p);
+	*query_ids_p = NULL;
+}
+
+static void
+query_id_free_contents(struct query_id *query_id)
+{
+
+	assert(query_id != NULL);
+	free(query_id->namever);
+	free(query_id->arch);
+	free(query_id->repo);
+}

Added: soc2013/mattbw/backend/query/id.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/query/id.h	Mon Aug 19 12:00:15 2013	(r256139)
@@ -0,0 +1,39 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw at FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _PKGNG_BACKEND_QUERY_ID_H_
+#define _PKGNG_BACKEND_QUERY_ID_H_
+
+#include <stdbool.h>
+#include "../pk-backend.h"
+#include "pkg.h"
+
+struct query_id {
+	char     *namever;
+	char     *arch;
+	char     *repo;
+};
+
+bool		query_id_from_name(const char *name, struct query_id *query_id);
+bool 		query_id_from_package_id(const gchar *package_id, struct query_id *query_id);
+struct query_id *query_id_array_from_names(gchar **package_names, unsigned int package_count);
+struct query_id *query_id_array_from_package_ids(gchar **package_ids, unsigned int package_count);
+
+#endif				/* !_PKGNG_BACKEND_QUERY_ID_H_ */

Added: soc2013/mattbw/backend/query/id_test.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/query/id_test.c	Mon Aug 19 12:00:15 2013	(r256139)
@@ -0,0 +1,116 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw at FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <atf-c.h>
+#include <stdlib.h>		/* free */
+#include <string.h>		/* strcmp */
+
+#include "id.h"			/* query_id_... */
+
+
+/* ATF/kyua tests for 'id.c'. */
+
+ATF_TC(query_id_from_package_id_single_valid);
+ATF_TC_HEAD(query_id_from_package_id_single_valid, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr",
+	    "Ensure single PackageIDs properly convert to query IDs.");
+}
+ATF_TC_BODY(query_id_from_package_id_single_valid, tc)
+{
+	bool		success;
+	struct query_id	result;
+	const char     *id = "pkg;1.1.4;freebsd:9:x86:32;packagesite";
+
+	result.namever = NULL;
+	result.arch = NULL;
+	result.repo = NULL;
+
+	success = query_id_from_package_id(id, &result);
+	ATF_CHECK(success);
+
+	ATF_CHECK_STREQ(result.namever, "pkg-1.1.4");
+	ATF_CHECK_STREQ(result.arch, "freebsd:9:x86:32");
+	ATF_CHECK_STREQ(result.repo, "packagesite");
+
+	free(result.namever);
+	free(result.arch);
+	free(result.repo);
+}
+
+ATF_TC(query_id_from_package_id_single_no_name);
+ATF_TC_HEAD(query_id_from_package_id_single_no_name, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr",
+	    "Ensure PackageIDs with no name fail to convert to query IDs.");
+}
+ATF_TC_BODY(query_id_from_package_id_single_no_name, tc)
+{
+	bool		success;
+	struct query_id	result;
+	const char     *no_name = ";1.1.4;freebsd:9:x86:32;packagesite";
+
+	result.namever = NULL;
+	result.arch = NULL;
+	result.repo = NULL;
+
+	success = query_id_from_package_id(no_name, &result);
+	ATF_CHECK(!success);
+
+	ATF_CHECK_EQ(result.namever, NULL);
+	ATF_CHECK_EQ(result.arch, NULL);
+	ATF_CHECK_EQ(result.repo, NULL);
+}
+
+ATF_TC(query_id_from_package_id_single_no_version);
+ATF_TC_HEAD(query_id_from_package_id_single_no_version, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr",
+	    "Ensure PackageIDs with no version fail to convert to query IDs.");
+}
+ATF_TC_BODY(query_id_from_package_id_single_no_version, tc)
+{
+	bool		success;
+	struct query_id	result;
+	const char     *no_version = "pkg;;freebsd:9:x86:32;packagesite";
+
+	result.namever = NULL;
+	result.arch = NULL;
+	result.repo = NULL;
+
+	success = query_id_from_package_id(no_version, &result);
+	ATF_CHECK(!success);
+
+	ATF_CHECK_EQ(result.namever, NULL);
+	ATF_CHECK_EQ(result.arch, NULL);
+	ATF_CHECK_EQ(result.repo, NULL);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, query_id_from_package_id_single_valid);
+	ATF_TP_ADD_TC(tp, query_id_from_package_id_single_no_name);
+	ATF_TP_ADD_TC(tp, query_id_from_package_id_single_no_version);
+
+	return atf_no_error();
+}


More information about the svn-soc-all mailing list