diff mbox series

[2/3] lib: Add TST_ASSERT_FILE_INT and TST_ASSERT_FILE_STR

Message ID 1587450108-31100-2-git-send-email-xuyang2018.jy@cn.fujitsu.com
State Changes Requested
Headers show
Series [1/3] lib/safe_file_ops: print file info when file_lines_scanf parsed fail | expand

Commit Message

Yang Xu April 21, 2020, 6:21 a.m. UTC
These functions are similar to TST_ASSERT_INT/STR, but they
are designed to get field value of proc or sys file. ie
NoNewPrivs field in the /proc/[pid]/status.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 include/tst_assert.h | 28 ++++++++++++++++++++++++++--
 lib/tst_assert.c     | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 2 deletions(-)

Comments

Cyril Hrubis April 29, 2020, 12:06 p.m. UTC | #1
Hi!
>  #endif /* TST_ASSERT_H__ */
> diff --git a/lib/tst_assert.c b/lib/tst_assert.c
> index 8ef3cd72e..65ee76473 100644
> --- a/lib/tst_assert.c
> +++ b/lib/tst_assert.c
> @@ -4,6 +4,7 @@
>   * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>   * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
>   */
> +#include <stdio.h>
>  #define TST_NO_DEFAULT_MAIN
>  #include "tst_assert.h"
>  #include "tst_test.h"
> @@ -22,6 +23,22 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
>  	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
>  }
>  
> +void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *buf, int val)
> +{
> +	int sys_val;
> +	char fmt[1024];
> +
> +	sprintf(fmt, "%s: %%d", buf);

If we want to keep the function as generic as possible we shouldn't add
the colon and space after the %s here.

There is no standard on proc files, for instance it wouldn't be possible
to parse /proc/vmstat if we hardcode the format string like that.

So I would just change this to "%s%%d" instead and pass "Foo: " instead
just "Foo" in the testcases.

Also I guess that we should call it prefix rather than buf, but that's
minor.
Yang Xu April 30, 2020, 8:22 a.m. UTC | #2
Hi Cyril


> Hi!
>>   #endif /* TST_ASSERT_H__ */
>> diff --git a/lib/tst_assert.c b/lib/tst_assert.c
>> index 8ef3cd72e..65ee76473 100644
>> --- a/lib/tst_assert.c
>> +++ b/lib/tst_assert.c
>> @@ -4,6 +4,7 @@
>>    * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>>    * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
>>    */
>> +#include <stdio.h>
>>   #define TST_NO_DEFAULT_MAIN
>>   #include "tst_assert.h"
>>   #include "tst_test.h"
>> @@ -22,6 +23,22 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
>>   	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
>>   }
>>   
>> +void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *buf, int val)
>> +{
>> +	int sys_val;
>> +	char fmt[1024];
>> +
>> +	sprintf(fmt, "%s: %%d", buf);
> 
> If we want to keep the function as generic as possible we shouldn't add
> the colon and space after the %s here.
> 
> There is no standard on proc files, for instance it wouldn't be possible
> to parse /proc/vmstat if we hardcode the format string like that.
> 
> So I would just change this to "%s%%d" instead and pass "Foo: " instead
> just "Foo" in the testcases.
Yes, good suggestion!!
> 
> Also I guess that we should call it prefix rather than buf, but that's
> minor.
Agree. I will send a v2 patch.

Best Regards
Yang Xu
>
diff mbox series

Patch

diff --git a/include/tst_assert.h b/include/tst_assert.h
index 04e80777c..913fff1b5 100644
--- a/include/tst_assert.h
+++ b/include/tst_assert.h
@@ -16,7 +16,20 @@ 
  * values in sysfs, procfs, etc.
  */
 void tst_assert_int(const char *file, const int lineno,
-                    const char *path, int val);
+		    const char *path, int val);
+
+#define TST_ASSERT_FILE_INT(path, buf, val) \
+	tst_assert_file_int(__FILE__, __LINE__, path, buf, val)
+
+/*
+ * Asserts that integer value stored in the buf field of file pointed by path
+ * equals to the value passed to this function. This is mostly useful for
+ * asserting correct field values in sysfs, procfs, etc.
+ */
+
+void tst_assert_file_int(const char *file, const int lineno,
+			 const char *path, const char *buf, int val);
+
 
 #define TST_ASSERT_STR(path, val) \
 	tst_assert_str(__FILE__, __LINE__, path, val)
@@ -27,6 +40,17 @@  void tst_assert_int(const char *file, const int lineno,
  * values in sysfs, procfs, etc.
  */
 void tst_assert_str(const char *file, const int lineno,
-                    const char *path, const char *val);
+		    const char *path, const char *val);
+
+#define TST_ASSERT_FILE_STR(path, buf, val) \
+	tst_assert_file_str(__FILE__, __LINE__, path, buf, val)
+
+/*
+ * Asserts that a string value stored in the buf field of file pointed by path
+ * equals to the value passed to this function. This is mostly useful for
+ * asserting correct field values in sysfs, procfs, etc.
+ */
+void tst_assert_file_str(const char *file, const int lineno,
+			 const char *path, const char *buf, const char *val);
 
 #endif /* TST_ASSERT_H__ */
diff --git a/lib/tst_assert.c b/lib/tst_assert.c
index 8ef3cd72e..65ee76473 100644
--- a/lib/tst_assert.c
+++ b/lib/tst_assert.c
@@ -4,6 +4,7 @@ 
  * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
  * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
  */
+#include <stdio.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -22,6 +23,22 @@  void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
+void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *buf, int val)
+{
+	int sys_val;
+	char fmt[1024];
+
+	sprintf(fmt, "%s: %%d", buf);
+	SAFE_FILE_LINES_SCANF(path, fmt, &sys_val);
+
+	if (val == sys_val) {
+		tst_res_(file, lineno, TPASS, "%s %s = %d", path, buf, sys_val);
+		return;
+	}
+
+	tst_res_(file, lineno, TFAIL, "%s %s != %d got %d", path, buf, val, sys_val);
+}
+
 void tst_assert_str(const char *file, const int lineno, const char *path, const char *val)
 {
 	char sys_val[1024];
@@ -34,3 +51,19 @@  void tst_assert_str(const char *file, const int lineno, const char *path, const
 
 	tst_res_(file, lineno, TFAIL, "%s != '%s' got '%s'", path, val, sys_val);
 }
+
+void tst_assert_file_str(const char *file, const int lineno, const char *path, const char *buf, const char *val)
+{
+	char sys_val[1024];
+	char fmt[2048];
+
+	sprintf(fmt, "%s: %%1024s", buf);
+	SAFE_FILE_LINES_SCANF(path, fmt, sys_val);
+
+	if (!strcmp(val, sys_val)) {
+		tst_res_(file, lineno, TPASS, "%s %s = '%s'", path, buf, sys_val);
+		return;
+	}
+
+	tst_res_(file, lineno, TFAIL, "%s %s != '%s' got '%s'", path, buf, val, sys_val);
+}