[v1,5/6] test/efivar: Rework for efi_mount

Message ID bf7554c092fc7c38fa1a442afd272416b58a8e89.1533921283.git.geoff@infradead.org
State Accepted
Headers show
Series
  • [v1,1/6] lib/efi: Add EFI_DEFALT_ATTRIBUTES macro
Related show

Commit Message

Geoff Levand Aug. 10, 2018, 5:29 p.m.
---
 test/lib/test-efivar.c | 128 +++++++++++++++++++------------------------------
 1 file changed, 49 insertions(+), 79 deletions(-)

Comments

Samuel Mendoza-Jonas Aug. 14, 2018, 1:13 a.m. | #1
On Fri, 2018-08-10 at 17:29 +0000, Geoff Levand wrote:
> ---
>  test/lib/test-efivar.c | 128 +++++++++++++++++++------------------------------
>  1 file changed, 49 insertions(+), 79 deletions(-)

Hi Geoff, excuse my being a bit pedantic, but can you add your SOB to
this please?

Cheers,
Sam

> 
> diff --git a/test/lib/test-efivar.c b/test/lib/test-efivar.c
> index a85b73c..1be95fd 100644
> --- a/test/lib/test-efivar.c
> +++ b/test/lib/test-efivar.c
> @@ -16,117 +16,87 @@
>   *  reserved.
>   *  Author: Ge Song <ge.song@hxt-semitech.com>
>   */
> +
>  #include <assert.h>
> -#include <dirent.h>
>  #include <errno.h>
> -#include <fcntl.h>
> -#include <linux/limits.h>
>  #include <stdbool.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> +
>  #include <sys/stat.h>
>  #include <sys/statfs.h>
> -#include <unistd.h>
> +#include <sys/types.h>
>  
>  #include "efi/efivar.h"
> +#include "log/log.h"
>  #include "talloc/talloc.h"
>  
> -#define DEF_ATTR	(EFI_VARIABLE_NON_VOLATILE | \
> -	EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
> -
> -static const char *test_efivar_guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7";
> -static const char *test_varname = "efivartest";
> -static const char *test_data = "petitboot";
> +static const struct efi_mount efi_mount = {
> +	.path = "/tmp/pb-test-efivar",
> +	.guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7",
> +};
> +static const char v_name[] = "petitboot-test-one";
> +static const char v_data[] = "petitboot-efi-tester";
>  
> -static char* find_efitest_path(void)
> +void finish(int code)
>  {
> -	static char dir[PATH_MAX] = {0};
> -	static bool run = false;
> -	char *rest_path = "/efivarfs_data/";
> -	char *pos = NULL;
> -
> -	if (run)
> -		return dir;
> -
> -	readlink("/proc/self/exe", dir, PATH_MAX);
> -
> -	pos = strrchr(dir, '/');
> -	*pos = '\0';
> -
> -	strcat(dir, rest_path);
> -	run = true;
> -
> -	return dir;
> +	efi_del_variable(&efi_mount, v_name);
> +	rmdir(efi_mount.path);
> +	exit(code);
>  }
>  
> -static bool probe(void)
> +int main(void)
>  {
> -	char *path;
> +	struct efi_data *efi_data;
>  	int rc;
>  
> -	path = find_efitest_path();
> +	__pb_log_init(stderr, true);
>  
> -	rc = access(path, F_OK);
> +	rc = mkdir(efi_mount.path, 0755);
>  	if (rc) {
> -		if (errno == ENOENT) {
> -			rc = mkdir(path, 0755);
> -			if(rc)
> -				return false;
> -		} else {
> -			return false;
> +		if (errno == EEXIST)
> +			pb_log("mkdir exists\n");
> +		else {
> +			pb_log("mkdir failed: (%d) %s\n", errno,
> +			       strerror(errno));
> +			finish(__LINE__);
>  		}
>  	}
>  
> -	set_efivarfs_path(path);
> +	if (!efi_check_mount_magic(&efi_mount, false))
> +		finish(__LINE__);
>  
> -	return true;
> -}
> +	efi_data = talloc_zero(NULL, struct efi_data);
> +	efi_data->attributes = EFI_DEFALT_ATTRIBUTES;
> +	efi_data->data = talloc_strdup(efi_data, v_data);
> +	efi_data->data_size = sizeof(v_data);
>  
> -int main(void)
> -{
> -	void *ctx = NULL;
> -	int rc, errno_value;
> -	uint32_t attr = DEF_ATTR;
> -	char *path = NULL;
> -	struct efi_data *efi_data;
> -
> -	if(!probe())
> -		return ENOENT;
> -
> -	talloc_new(ctx);
> -
> -	efi_data = talloc_zero(ctx, struct efi_data);
> -	efi_data->attributes = attr;
> -	efi_data->data = talloc_strdup(efi_data, test_data);
> -	efi_data->data_size = strlen(test_data) + 1;
> -
> -	rc = efi_set_variable(test_efivar_guid, test_varname,
> -				efi_data);
> +	if (efi_set_variable(&efi_mount, v_name, efi_data))
> +		finish(__LINE__);
>  
>  	talloc_free(efi_data);
> -	rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
> -				&efi_data);
>  
> -	assert(efi_data->data != NULL);
> -	rc = strcmp((char *)efi_data->data, test_data);
> -	if (rc) {
> -		talloc_free(ctx);
> -		assert(0);
> -	}
> -
> -	rc = efi_del_variable(test_efivar_guid, test_varname);
> +	if (efi_get_variable(NULL, &efi_mount, v_name, &efi_data))
> +		finish(__LINE__);
>  
> -	rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
> -				&efi_data);
> +	if (!efi_data->data) {
> +		pb_log("No efi_data->data\n");
> +		finish(__LINE__);
> +	}
>  
> -	errno_value = errno;
> -	talloc_free(ctx);
> +	if (strcmp((char *)efi_data->data, v_data)) {
> +		pb_log("Bad efi_data->data: '%s' != '%s'\n",
> +		       (char *)efi_data->data, v_data);
> +		finish(__LINE__);
> +	}
>  
> -	assert(errno_value == ENOENT);
> +	if (efi_del_variable(&efi_mount, v_name))
> +		finish(__LINE__);
>  
> -	path = find_efitest_path();
> -	rmdir(path);
> +	/* Get after delete should fail. */
> +	if (!efi_get_variable(NULL, &efi_mount, v_name, &efi_data))
> +		finish(__LINE__);
>  
> -	return EXIT_SUCCESS;
> +	finish(EXIT_SUCCESS);
>  }

Patch

diff --git a/test/lib/test-efivar.c b/test/lib/test-efivar.c
index a85b73c..1be95fd 100644
--- a/test/lib/test-efivar.c
+++ b/test/lib/test-efivar.c
@@ -16,117 +16,87 @@ 
  *  reserved.
  *  Author: Ge Song <ge.song@hxt-semitech.com>
  */
+
 #include <assert.h>
-#include <dirent.h>
 #include <errno.h>
-#include <fcntl.h>
-#include <linux/limits.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
 #include <sys/stat.h>
 #include <sys/statfs.h>
-#include <unistd.h>
+#include <sys/types.h>
 
 #include "efi/efivar.h"
+#include "log/log.h"
 #include "talloc/talloc.h"
 
-#define DEF_ATTR	(EFI_VARIABLE_NON_VOLATILE | \
-	EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
-
-static const char *test_efivar_guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7";
-static const char *test_varname = "efivartest";
-static const char *test_data = "petitboot";
+static const struct efi_mount efi_mount = {
+	.path = "/tmp/pb-test-efivar",
+	.guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7",
+};
+static const char v_name[] = "petitboot-test-one";
+static const char v_data[] = "petitboot-efi-tester";
 
-static char* find_efitest_path(void)
+void finish(int code)
 {
-	static char dir[PATH_MAX] = {0};
-	static bool run = false;
-	char *rest_path = "/efivarfs_data/";
-	char *pos = NULL;
-
-	if (run)
-		return dir;
-
-	readlink("/proc/self/exe", dir, PATH_MAX);
-
-	pos = strrchr(dir, '/');
-	*pos = '\0';
-
-	strcat(dir, rest_path);
-	run = true;
-
-	return dir;
+	efi_del_variable(&efi_mount, v_name);
+	rmdir(efi_mount.path);
+	exit(code);
 }
 
-static bool probe(void)
+int main(void)
 {
-	char *path;
+	struct efi_data *efi_data;
 	int rc;
 
-	path = find_efitest_path();
+	__pb_log_init(stderr, true);
 
-	rc = access(path, F_OK);
+	rc = mkdir(efi_mount.path, 0755);
 	if (rc) {
-		if (errno == ENOENT) {
-			rc = mkdir(path, 0755);
-			if(rc)
-				return false;
-		} else {
-			return false;
+		if (errno == EEXIST)
+			pb_log("mkdir exists\n");
+		else {
+			pb_log("mkdir failed: (%d) %s\n", errno,
+			       strerror(errno));
+			finish(__LINE__);
 		}
 	}
 
-	set_efivarfs_path(path);
+	if (!efi_check_mount_magic(&efi_mount, false))
+		finish(__LINE__);
 
-	return true;
-}
+	efi_data = talloc_zero(NULL, struct efi_data);
+	efi_data->attributes = EFI_DEFALT_ATTRIBUTES;
+	efi_data->data = talloc_strdup(efi_data, v_data);
+	efi_data->data_size = sizeof(v_data);
 
-int main(void)
-{
-	void *ctx = NULL;
-	int rc, errno_value;
-	uint32_t attr = DEF_ATTR;
-	char *path = NULL;
-	struct efi_data *efi_data;
-
-	if(!probe())
-		return ENOENT;
-
-	talloc_new(ctx);
-
-	efi_data = talloc_zero(ctx, struct efi_data);
-	efi_data->attributes = attr;
-	efi_data->data = talloc_strdup(efi_data, test_data);
-	efi_data->data_size = strlen(test_data) + 1;
-
-	rc = efi_set_variable(test_efivar_guid, test_varname,
-				efi_data);
+	if (efi_set_variable(&efi_mount, v_name, efi_data))
+		finish(__LINE__);
 
 	talloc_free(efi_data);
-	rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
-				&efi_data);
 
-	assert(efi_data->data != NULL);
-	rc = strcmp((char *)efi_data->data, test_data);
-	if (rc) {
-		talloc_free(ctx);
-		assert(0);
-	}
-
-	rc = efi_del_variable(test_efivar_guid, test_varname);
+	if (efi_get_variable(NULL, &efi_mount, v_name, &efi_data))
+		finish(__LINE__);
 
-	rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
-				&efi_data);
+	if (!efi_data->data) {
+		pb_log("No efi_data->data\n");
+		finish(__LINE__);
+	}
 
-	errno_value = errno;
-	talloc_free(ctx);
+	if (strcmp((char *)efi_data->data, v_data)) {
+		pb_log("Bad efi_data->data: '%s' != '%s'\n",
+		       (char *)efi_data->data, v_data);
+		finish(__LINE__);
+	}
 
-	assert(errno_value == ENOENT);
+	if (efi_del_variable(&efi_mount, v_name))
+		finish(__LINE__);
 
-	path = find_efitest_path();
-	rmdir(path);
+	/* Get after delete should fail. */
+	if (!efi_get_variable(NULL, &efi_mount, v_name, &efi_data))
+		finish(__LINE__);
 
-	return EXIT_SUCCESS;
+	finish(EXIT_SUCCESS);
 }