/*
* Copyright (c) 2016 Fujitsu Ltd.
* Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* alone with this program.
*/

/*
* Test Name: llistxattr03
*
* Description:
* llistxattr is identical to listxattr. an empty buffer of size zero
* can return the current size of the list of extended attribute names,
* which can be used to estimate a suitable buffer.
*/

#include "config.h"
#include <errno.h>
#include <sys/types.h>

#ifdef HAVE_ATTR_XATTR_H
# include <attr/xattr.h>
#endif

#include "tst_test.h"

#ifdef HAVE_ATTR_XATTR_H

#define SECURITY_KEY	"security.ltptest"
#define VALUE	"test"
#define VALUE_SIZE	4

static const char *filename[] = {"testfile1", "testfile2"};

static int check_suitable_buf(const char *name, long size)
{
	int n;
	char buf[size];

	n = llistxattr(name, buf, size);
	if (n == -1)
		return 0;
	else
		return 1;
}

static void verify_llistxattr(unsigned int n)
{
	const char *name = filename[n];

	TEST(llistxattr(name, NULL, 0));
	if (TEST_RETURN == -1) {
		tst_res(TFAIL | TERRNO, "llistxattr() failed");
		return;
	}

	if (check_suitable_buf(name, TEST_RETURN))
		tst_res(TPASS, "llistxattr() succeed with suitable buffer");
	else
		tst_res(TFAIL, "llistxattr() failed with small buffer");
}

static void setup(void)
{
	int ret;

	SAFE_TOUCH(filename[0], 0644, NULL);

	SAFE_TOUCH(filename[1], 0644, NULL);

	ret = lsetxattr(filename[1], SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
	if (ret == -1) {
		if (errno == ENOTSUP) {
			tst_brk(TCONF, "no xattr support in fs or "
				 "mounted without user_xattr option");
		} else {
			tst_brk(TBROK | TERRNO, "lsetxattr() failed");
		}
	}
}

static struct tst_test test = {
	.tid = "llistxattr02",
	.needs_tmpdir = 1,
	.needs_root = 1,
	.test = verify_llistxattr,
	.tcnt = ARRAY_SIZE(filename),
	.setup = setup,
};

#else /* HAVE_ATTR_XATTR_H */
	TST_TEST_TCONF("<attr/xattr.h> does not exist.");
#endif
