diff mbox

[3/3] devicetree/dt_sysinfo: Add device tree system information tests

Message ID 1432192507.647628.90360957379.3.gpush@pablo
State Rejected
Headers show

Commit Message

Jeremy Kerr May 21, 2015, 7:15 a.m. UTC
Check that we have some basic system identifier properties.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>

---
 src/Makefile.am                        |    1 
 src/devicetree/dt_sysinfo/dt_sysinfo.c |  137 +++++++++++++++++++++++++
 2 files changed, 138 insertions(+)

Comments

Colin Ian King May 26, 2015, 8:50 a.m. UTC | #1
On 21/05/15 08:15, Jeremy Kerr wrote:
> Check that we have some basic system identifier properties.
> 
> Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
> 
> ---
>  src/Makefile.am                        |    1 
>  src/devicetree/dt_sysinfo/dt_sysinfo.c |  137 +++++++++++++++++++++++++
>  2 files changed, 138 insertions(+)
> 
> diff --git a/src/Makefile.am b/src/Makefile.am
> index 4a2622b..c0a8104 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -79,6 +79,7 @@ fwts_SOURCES = main.c 				\
>  	cpu/msr/msr.c 				\
>  	cpu/microcode/microcode.c 		\
>  	devicetree/dtc/dtc.c			\
> +	devicetree/dt_sysinfo/dt_sysinfo.c	\
>  	dmi/dmicheck/dmicheck.c 		\
>  	hotkey/hotkey/hotkey.c 			\
>  	hpet/hpet_check/hpet_check.c 		\
> diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> new file mode 100644
> index 0000000..aa5514c
> --- /dev/null
> +++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> @@ -0,0 +1,137 @@
> +/*
> + * Copyright (C) 2014 Jeremy Kerr <jk@ozlabs.org>
> + *
> + * 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.
> + *
> + */
> +
> +#define _GNU_SOURCE
> +#include <assert.h>
> +#include <stdio.h>
> +#include <ctype.h>
> +
> +#include "fwts.h"
> +
> +static const char *devicetree_path = "/proc/device-tree";
> +
> +static int read_property(const char *name, char **buf, size_t *len)
> +{
> +	char *path;
> +	int rc;
> +
> +	rc = asprintf(&path, "%s/%s", devicetree_path, name);
> +	assert(rc > 0);

..and same here about asserts.

> +
> +	rc = fwts_file_open_and_read_binary(path, buf, len);
> +	free(path);
> +
> +	return rc;
> +}
> +
> +static int check_property_printable(fwts_framework *fw, const char *name,
> +		char *buf, size_t len)
> +{
> +	bool printable = true;
> +	unsigned int i;
> +	int rc;
> +
> +	/* we need at least one character plus a nul */
> +	if (len < 2) {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyShort",
> +				"property %s is too short", name);
> +		rc = FWTS_ERROR;
> +		goto out;
> +	}
> +
> +	/* check all characters are printable */
> +	for (i = 0; i < len - 1; i++) {
> +		printable = printable && isprint(buf[i]);
> +		if (!printable)
> +			break;
> +	}
> +
> +	if (!printable) {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyInvalid",
> +				"property %s contains unprintable characters",
> +				name);
> +		rc = FWTS_ERROR;
> +		goto out;
> +	}
> +
> +	/* check for a trailing nul */
> +	if (buf[len-1] != '\0') {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyNoNul",
> +				"property %s isn't nul-terminated", name);
> +		rc = FWTS_ERROR;
> +	}
> +
> +	rc = FWTS_OK;
> +
> +out:
> +	return rc;
> +}
> +
> +static int dt_sysinfo_check_property(fwts_framework *fw, const char *name)
> +{
> +	size_t len;
> +	char *buf;
> +	int rc;
> +
> +	rc = read_property(name, &buf, &len);
> +	if (rc != FWTS_OK) {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTSyinfoPropertyMissing",
> +				"property %s is missing", name);
> +		return rc;
> +	}
> +
> +	rc = check_property_printable(fw, name, buf, len);
> +	free(buf);
> +
> +	if (rc == FWTS_OK)
> +		fwts_passed(fw, "Sysinfo property %s is valid", name);
> +
> +	return rc;
> +}
> +
> +static int dt_sysinfo_check_vendor(fwts_framework *fw)
> +{
> +	return dt_sysinfo_check_property(fw, "vendor");
> +}
> +
> +static int dt_sysinfo_check_model(fwts_framework *fw)
> +{
> +	return dt_sysinfo_check_property(fw, "model");
> +}
> +
> +static int dt_sysinfo_check_name(fwts_framework *fw)
> +{
> +	return dt_sysinfo_check_property(fw, "name");
> +}
> +
> +static fwts_framework_minor_test dt_sysinfo_tests[] = {
> +	{ dt_sysinfo_check_name,	"Check name property" },
> +	{ dt_sysinfo_check_vendor,	"Check vendor property" },
> +	{ dt_sysinfo_check_model,	"Check model property" },
> +	{ NULL, NULL },
> +};
> +
> +static fwts_framework_ops dt_sysinfo_ops = {
> +	.description	= "Device tree system information test",
> +	.minor_tests	= dt_sysinfo_tests,
> +};
> +
> +FWTS_REGISTER_FEATURES("dt_sysinfo", &dt_sysinfo_ops, FWTS_TEST_ANYTIME,
> +		FWTS_FLAG_BATCH, FWTS_FW_FEATURE_DEVICETREE);
>
Colin Ian King May 26, 2015, 9:17 a.m. UTC | #2
On 21/05/15 08:15, Jeremy Kerr wrote:
> Check that we have some basic system identifier properties.
> 
> Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
> 
> ---
>  src/Makefile.am                        |    1 
>  src/devicetree/dt_sysinfo/dt_sysinfo.c |  137 +++++++++++++++++++++++++
>  2 files changed, 138 insertions(+)
> 
> diff --git a/src/Makefile.am b/src/Makefile.am
> index 4a2622b..c0a8104 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -79,6 +79,7 @@ fwts_SOURCES = main.c 				\
>  	cpu/msr/msr.c 				\
>  	cpu/microcode/microcode.c 		\
>  	devicetree/dtc/dtc.c			\
> +	devicetree/dt_sysinfo/dt_sysinfo.c	\
>  	dmi/dmicheck/dmicheck.c 		\
>  	hotkey/hotkey/hotkey.c 			\
>  	hpet/hpet_check/hpet_check.c 		\
> diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> new file mode 100644
> index 0000000..aa5514c
> --- /dev/null
> +++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> @@ -0,0 +1,137 @@
> +/*
> + * Copyright (C) 2014 Jeremy Kerr <jk@ozlabs.org>
> + *
> + * 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.
> + *
> + */
> +
> +#define _GNU_SOURCE
> +#include <assert.h>
> +#include <stdio.h>
> +#include <ctype.h>
> +
> +#include "fwts.h"
> +
> +static const char *devicetree_path = "/proc/device-tree";
> +
> +static int read_property(const char *name, char **buf, size_t *len)
> +{
> +	char *path;
> +	int rc;
> +
> +	rc = asprintf(&path, "%s/%s", devicetree_path, name);
> +	assert(rc > 0);
> +
> +	rc = fwts_file_open_and_read_binary(path, buf, len);
> +	free(path);
> +
> +	return rc;
> +}
> +
> +static int check_property_printable(fwts_framework *fw, const char *name,
> +		char *buf, size_t len)
> +{
> +	bool printable = true;
> +	unsigned int i;
> +	int rc;
> +
> +	/* we need at least one character plus a nul */
> +	if (len < 2) {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyShort",
> +				"property %s is too short", name);
> +		rc = FWTS_ERROR;
> +		goto out;
> +	}
> +
> +	/* check all characters are printable */
> +	for (i = 0; i < len - 1; i++) {
> +		printable = printable && isprint(buf[i]);
> +		if (!printable)
> +			break;
> +	}
> +
> +	if (!printable) {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyInvalid",
> +				"property %s contains unprintable characters",
> +				name);
> +		rc = FWTS_ERROR;
> +		goto out;
> +	}
> +
> +	/* check for a trailing nul */
> +	if (buf[len-1] != '\0') {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyNoNul",
> +				"property %s isn't nul-terminated", name);
> +		rc = FWTS_ERROR;

CoverityScan spotted an issue above ^

Assigning value "FWTS_ERROR" to "rc" here, but that stored value is
overwritten before it can be used

> +	}
> +
> +	rc = FWTS_OK;
> +
> +out:
> +	return rc;
> +}
> +
> +static int dt_sysinfo_check_property(fwts_framework *fw, const char *name)
> +{
> +	size_t len;
> +	char *buf;
> +	int rc;
> +
> +	rc = read_property(name, &buf, &len);
> +	if (rc != FWTS_OK) {
> +		fwts_failed(fw, LOG_LEVEL_LOW, "DTSyinfoPropertyMissing",
> +				"property %s is missing", name);
> +		return rc;
> +	}
> +
> +	rc = check_property_printable(fw, name, buf, len);
> +	free(buf);
> +
> +	if (rc == FWTS_OK)
> +		fwts_passed(fw, "Sysinfo property %s is valid", name);
> +
> +	return rc;
> +}
> +
> +static int dt_sysinfo_check_vendor(fwts_framework *fw)
> +{
> +	return dt_sysinfo_check_property(fw, "vendor");
> +}
> +
> +static int dt_sysinfo_check_model(fwts_framework *fw)
> +{
> +	return dt_sysinfo_check_property(fw, "model");
> +}
> +
> +static int dt_sysinfo_check_name(fwts_framework *fw)
> +{
> +	return dt_sysinfo_check_property(fw, "name");
> +}
> +
> +static fwts_framework_minor_test dt_sysinfo_tests[] = {
> +	{ dt_sysinfo_check_name,	"Check name property" },
> +	{ dt_sysinfo_check_vendor,	"Check vendor property" },
> +	{ dt_sysinfo_check_model,	"Check model property" },
> +	{ NULL, NULL },
> +};
> +
> +static fwts_framework_ops dt_sysinfo_ops = {
> +	.description	= "Device tree system information test",
> +	.minor_tests	= dt_sysinfo_tests,
> +};
> +
> +FWTS_REGISTER_FEATURES("dt_sysinfo", &dt_sysinfo_ops, FWTS_TEST_ANYTIME,
> +		FWTS_FLAG_BATCH, FWTS_FW_FEATURE_DEVICETREE);
>
diff mbox

Patch

diff --git a/src/Makefile.am b/src/Makefile.am
index 4a2622b..c0a8104 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -79,6 +79,7 @@  fwts_SOURCES = main.c 				\
 	cpu/msr/msr.c 				\
 	cpu/microcode/microcode.c 		\
 	devicetree/dtc/dtc.c			\
+	devicetree/dt_sysinfo/dt_sysinfo.c	\
 	dmi/dmicheck/dmicheck.c 		\
 	hotkey/hotkey/hotkey.c 			\
 	hpet/hpet_check/hpet_check.c 		\
diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c
new file mode 100644
index 0000000..aa5514c
--- /dev/null
+++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c
@@ -0,0 +1,137 @@ 
+/*
+ * Copyright (C) 2014 Jeremy Kerr <jk@ozlabs.org>
+ *
+ * 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.
+ *
+ */
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#include "fwts.h"
+
+static const char *devicetree_path = "/proc/device-tree";
+
+static int read_property(const char *name, char **buf, size_t *len)
+{
+	char *path;
+	int rc;
+
+	rc = asprintf(&path, "%s/%s", devicetree_path, name);
+	assert(rc > 0);
+
+	rc = fwts_file_open_and_read_binary(path, buf, len);
+	free(path);
+
+	return rc;
+}
+
+static int check_property_printable(fwts_framework *fw, const char *name,
+		char *buf, size_t len)
+{
+	bool printable = true;
+	unsigned int i;
+	int rc;
+
+	/* we need at least one character plus a nul */
+	if (len < 2) {
+		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyShort",
+				"property %s is too short", name);
+		rc = FWTS_ERROR;
+		goto out;
+	}
+
+	/* check all characters are printable */
+	for (i = 0; i < len - 1; i++) {
+		printable = printable && isprint(buf[i]);
+		if (!printable)
+			break;
+	}
+
+	if (!printable) {
+		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyInvalid",
+				"property %s contains unprintable characters",
+				name);
+		rc = FWTS_ERROR;
+		goto out;
+	}
+
+	/* check for a trailing nul */
+	if (buf[len-1] != '\0') {
+		fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyNoNul",
+				"property %s isn't nul-terminated", name);
+		rc = FWTS_ERROR;
+	}
+
+	rc = FWTS_OK;
+
+out:
+	return rc;
+}
+
+static int dt_sysinfo_check_property(fwts_framework *fw, const char *name)
+{
+	size_t len;
+	char *buf;
+	int rc;
+
+	rc = read_property(name, &buf, &len);
+	if (rc != FWTS_OK) {
+		fwts_failed(fw, LOG_LEVEL_LOW, "DTSyinfoPropertyMissing",
+				"property %s is missing", name);
+		return rc;
+	}
+
+	rc = check_property_printable(fw, name, buf, len);
+	free(buf);
+
+	if (rc == FWTS_OK)
+		fwts_passed(fw, "Sysinfo property %s is valid", name);
+
+	return rc;
+}
+
+static int dt_sysinfo_check_vendor(fwts_framework *fw)
+{
+	return dt_sysinfo_check_property(fw, "vendor");
+}
+
+static int dt_sysinfo_check_model(fwts_framework *fw)
+{
+	return dt_sysinfo_check_property(fw, "model");
+}
+
+static int dt_sysinfo_check_name(fwts_framework *fw)
+{
+	return dt_sysinfo_check_property(fw, "name");
+}
+
+static fwts_framework_minor_test dt_sysinfo_tests[] = {
+	{ dt_sysinfo_check_name,	"Check name property" },
+	{ dt_sysinfo_check_vendor,	"Check vendor property" },
+	{ dt_sysinfo_check_model,	"Check model property" },
+	{ NULL, NULL },
+};
+
+static fwts_framework_ops dt_sysinfo_ops = {
+	.description	= "Device tree system information test",
+	.minor_tests	= dt_sysinfo_tests,
+};
+
+FWTS_REGISTER_FEATURES("dt_sysinfo", &dt_sysinfo_ops, FWTS_TEST_ANYTIME,
+		FWTS_FLAG_BATCH, FWTS_FW_FEATURE_DEVICETREE);