Skip to contentSkip to conversation

igt/drv_hangman: Fix clear_error_state

PATCH Superseded Michel Thierry 2 participants 6 messages Feb 23, 2017 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 igt/drv_hangman: Fix clear_error_state

clear_error_state was not doing anything (igt_sysfs_set was not writing to the error file).

Also fix assert_entry to catch this issue; strcasecmp returns 0 when there's a match, or an integer in a mismatch.

Finally, only print part of the error string when the assert fails.

Cc Chris Wilson <[email protected]>
Fixes 79c6a84ca85b ("igt/drv_hangman: Migrate to sysfs")
---
 tests/drv_hangman.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tests/drv_hangman.c +10 -4
index cafdf4c1..43f73661 100644
@@ -58,9 +58,15 @@ static void assert_entry(const char *s, bool expect)
 	error = igt_sysfs_get(sysfs, "error");
 	igt_assert(error);
 
-	igt_assert_f(strcasecmp(error, s) != expect,
-		     "contents of error: '%s' (expected %s '%s')\n",
-		     error, expect ? "": "not", s);
+	if (expect) {
+		igt_assert_f(strcasecmp(error, s) == 0,
+			     "contents of error: '%.24s' (expected %s '%s')\n",
+			     error, expect ? "": "not", s);
+	} else {
+		igt_assert_f(strcasecmp(error, s) != 0,
+			     "contents of error: '%.24s' (expected %s '%s')\n",
+			     error, expect ? "": "not", s);
+	}
 
 	free(error);
 }
@@ -77,7 +83,7 @@ static void assert_error_state_collected(void)
 
 static void clear_error_state(void)
 {
-	igt_sysfs_set(sysfs, "error", "");
+	igt_sysfs_set(sysfs, "error", " ");
 }
 
 static void test_error_state_basic(void)
Signature · 6 lines
2.11.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw
Quoted 20 lines · Michel Thierry

clear_error_state was not doing anything (igt_sysfs_set was not writing to the error file).

Also fix assert_entry to catch this issue; strcasecmp returns 0 when there's a match, or an integer in a mismatch.

Finally, only print part of the error string when the assert fails.

Cc Chris Wilson <[email protected]>
Fixes 79c6a84ca85b ("igt/drv_hangman: Migrate to sysfs")
---
 tests/drv_hangman.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tests/drv_hangman.c +0 -0
index cafdf4c1..43f73661 100644

tests/drv_hangman.c static void assert_entry(const char *s, bool expect) · line 61

Quoted 4 lines
tests/drv_hangman.c +0 -1
@@ -58,9 +58,15 @@ static void assert_entry(const char *s, bool expect)
 	error = igt_sysfs_get(sysfs, "error");
 	igt_assert(error);
 
-	igt_assert_f(strcasecmp(error, s) != expect,

!!strcasecmp != expect
or (bool)strcasecmp != expect, type promotion working in the opposite direction.

tests/drv_hangman.c static void assert_entry(const char *s, bool expect) · line 64

Quoted 6 lines
tests/drv_hangman.c +4 -2
-		     "contents of error: '%s' (expected %s '%s')\n",
-		     error, expect ? "": "not", s);
+	if (expect) {
+		igt_assert_f(strcasecmp(error, s) == 0,
+			     "contents of error: '%.24s' (expected %s '%s')\n",
+			     error, expect ? "": "not", s);

Keep the full error state, it can be very informative. 24 characters isn't enough to determine what.

Quoted 9 lines
tests/drv_hangman.c +5 -0
+	} else {
+		igt_assert_f(strcasecmp(error, s) != 0,
+			     "contents of error: '%.24s' (expected %s '%s')\n",
+			     error, expect ? "": "not", s);
+	}
 
 	free(error);
 }

tests/drv_hangman.c static void assert_error_state_collected(void) · line 86

Quoted 5 lines
tests/drv_hangman.c +1 -1
@@ -77,7 +83,7 @@ static void assert_error_state_collected(void)
 
 static void clear_error_state(void)
 {
-	igt_sysfs_set(sysfs, "error", "");
+	igt_sysfs_set(sysfs, "error", " ");

My bad, I was expecting it write the nul character. "\0" would be my preference
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw
Quoted 8 lines · Chris Wilson

tests/drv_hangman.c static void assert_error_state_collected(void) · line 86

Quoted 4 lines · Michel Thierry

static void clear_error_state(void)
{

tests/drv_hangman.c +1 -1
-	igt_sysfs_set(sysfs, "error", "");
+	igt_sysfs_set(sysfs, "error", " ");

My bad, I was expecting it write the nul character. "\0" would be my preference

Still needs an explicit igt_sysfs_write() to avoid strlen. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw
Quoted 10 lines · Chris Wilson
Quoted 8 lines · Chris Wilson

tests/drv_hangman.c static void assert_error_state_collected(void) · line 86

Quoted 4 lines · Michel Thierry

static void clear_error_state(void)
{

tests/drv_hangman.c +1 -1
-	igt_sysfs_set(sysfs, "error", "");
+	igt_sysfs_set(sysfs, "error", " ");

My bad, I was expecting it write the nul character. "\0" would be my preference

Quoted 3 lines

Still needs an explicit igt_sysfs_write() to avoid strlen. -Chris

Yes, strlen(\0) is 0, which at the end is write(x, 0) and ignored.
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#4 lore raw

PATCH v2 1/1 igt/drv_hangman: Fix clear_error_state

clear_error_state was not doing anything (igt_sysfs_set was not writing to the error file because strlen was 0).

Also fix assert_entry to catch this issue; strcasecmp returns 0 when there's a match, or an integer in a mismatch.

v2: Use new igt_sysfs_write, add len > 0 assert in igt_sysfs_set to catch future errors, clean-up strcasecmp logic (Chris).

Cc Chris Wilson <[email protected]>
Fixes 79c6a84ca85b ("igt/drv_hangman: Migrate to sysfs")
---
 lib/igt_sysfs.c     | 26 ++++++++++++++++++++++++++
 lib/igt_sysfs.h     |  1 +
 tests/drv_hangman.c |  4 ++--
 3 files changed, 29 insertions(+), 2 deletions(-)
lib/igt_sysfs.c +26 -0
index ded10a67..d244d9b6 100644
@@ -172,6 +172,31 @@ int igt_sysfs_open_parameters(int device)
 
 	return params;
 }
+
+/**
+ * igt_sysfs_write:
+ * @dir: directory for the device from igt_sysfs_open()
+ * @attr: name of the sysfs node to open
+ *
+ * This writes a single byte to the sysfs file.
+ *
+ * Returns:
+ * True on success, false on failure.
+ */
+bool igt_sysfs_write(int dir, const char *attr)
+{
+	int fd, ret;
+
+	fd = openat(dir, attr, O_WRONLY);
+	if (fd < 0)
+		return false;
+
+	ret = write(fd, "\0", 1);
+	close(fd);
+
+	return ret;
+}
+
 /**
  * igt_sysfs_set:
  * @dir: directory for the device from igt_sysfs_open()
@@ -192,6 +217,7 @@ bool igt_sysfs_set(int dir, const char *attr, const char *value)
 		return false;
 
 	len = strlen(value);
+	igt_assert(len > 0);
 	ret = writeN(fd, value, len);
 	close(fd);
 
lib/igt_sysfs.h +1 -0
index 69b2e5ec..145a9be6 100644
@@ -29,6 +29,7 @@
 
 int igt_sysfs_open(int device, int *idx);
 int igt_sysfs_open_parameters(int device);
+bool igt_sysfs_write(int dir, const char *attr);
 bool igt_sysfs_set(int dir, const char *attr, const char *value);
 char *igt_sysfs_get(int dir, const char *attr);
 
tests/drv_hangman.c +2 -2
index cafdf4c1..df9c763f 100644
@@ -58,7 +58,7 @@ static void assert_entry(const char *s, bool expect)
 	error = igt_sysfs_get(sysfs, "error");
 	igt_assert(error);
 
-	igt_assert_f(strcasecmp(error, s) != expect,
+	igt_assert_f(!!strcasecmp(error, s) != expect,
 		     "contents of error: '%s' (expected %s '%s')\n",
 		     error, expect ? "": "not", s);
 
@@ -77,7 +77,7 @@ static void assert_error_state_collected(void)
 
 static void clear_error_state(void)
 {
-	igt_sysfs_set(sysfs, "error", "");
+	igt_sysfs_write(sysfs, "error");
 }
 
 static void test_error_state_basic(void)
Signature · 6 lines
2.11.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx