/*
   Unix SMB/CIFS implementation.
   test alternate data streams
   Copyright (C) Andrew Tridgell 2004
   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 3 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, see .
*/
#include "includes.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "torture/torture.h"
#include "torture/smb2/proto.h"
#include "system/filesys.h"
#include "system/locale.h"
#include "lib/util/tsort.h"
#define DNAME "teststreams"
#define CHECK_STATUS(status, correct) do { \
	if (!NT_STATUS_EQUAL(status, correct)) { \
		torture_result(tctx, TORTURE_FAIL, \
		    "(%s) Incorrect status %s - should be %s\n", \
		    __location__, nt_errstr(status), nt_errstr(correct)); \
		ret = false; \
		goto done; \
	}} while (0)
#define CHECK_VALUE(v, correct) do { \
	if ((v) != (correct)) { \
		torture_result(tctx, TORTURE_FAIL, \
		    "(%s) Incorrect value %s=%d - should be %d\n", \
		    __location__, #v, (int)v, (int)correct); \
		ret = false; \
	}} while (0)
#define CHECK_NTTIME(v, correct) do { \
	if ((v) != (correct)) { \
		torture_result(tctx, TORTURE_FAIL, \
		    "(%s) Incorrect value %s=%llu - should be %llu\n", \
		    __location__, #v, (unsigned long long)v, \
		    (unsigned long long)correct); \
		ret = false; \
	}} while (0)
#define CHECK_STR(v, correct) do { \
	bool ok; \
	if ((v) && !(correct)) { \
		ok = false; \
	} else if (!(v) && (correct)) { \
		ok = false; \
	} else if (!(v) && !(correct)) { \
		ok = true; \
	} else if (strcmp((v), (correct)) == 0) { \
		ok = true; \
	} else { \
		ok = false; \
	} \
	if (!ok) { \
		torture_comment(tctx,"(%s) Incorrect value %s='%s' - " \
		    "should be '%s'\n", \
		    __location__, #v, (v)?(v):"NULL", \
		    (correct)?(correct):"NULL"); \
		ret = false; \
	}} while (0)
static int qsort_string(char * const *s1, char * const *s2)
{
	return strcmp(*s1, *s2);
}
static int qsort_stream(const struct stream_struct * s1, const struct stream_struct *s2)
{
	return strcmp(s1->stream_name.s, s2->stream_name.s);
}
static bool check_stream(struct smb2_tree *tree,
			 const char *location,
			 TALLOC_CTX *mem_ctx,
			 const char *fname,
			 const char *sname,
			 const char *value)
{
	struct smb2_handle handle;
	struct smb2_create create;
	struct smb2_read r;
	NTSTATUS status;
	const char *full_name;
	full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
	ZERO_STRUCT(create);
	create.in.desired_access = SEC_RIGHTS_FILE_ALL;
	create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
	create.in.create_disposition = NTCREATEX_DISP_OPEN;
	create.in.fname = full_name;
	status = smb2_create(tree, mem_ctx, &create);
	if (!NT_STATUS_IS_OK(status)) {
		if (value == NULL) {
			return true;
		} else {
			torture_comment(mem_ctx, "Unable to open stream %s\n",
			    full_name);
			return false;
		}
	}
	handle = create.out.file.handle;
	if (value == NULL) {
		return true;
	}
	ZERO_STRUCT(r);
	r.in.file.handle = handle;
	r.in.length      = strlen(value)+11;
	r.in.offset      = 0;
	status = smb2_read(tree, tree, &r);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(mem_ctx, "(%s) Failed to read %lu bytes from "
		    "stream '%s'\n", location, (long)strlen(value), full_name);
		return false;
	}
	if (memcmp(r.out.data.data, value, strlen(value)) != 0) {
		torture_comment(mem_ctx, "(%s) Bad data in stream\n", location);
		return false;
	}
	smb2_util_close(tree, handle);
	return true;
}
static bool check_stream_list(struct smb2_tree *tree,
			      struct torture_context *tctx,
			      const char *fname,
			      int num_exp,
			      const char **exp,
			      struct smb2_handle h)
{
	union smb_fileinfo finfo;
	NTSTATUS status;
	int i;
	TALLOC_CTX *tmp_ctx = talloc_new(tctx);
	char **exp_sort;
	struct stream_struct *stream_sort;
	bool ret = false;
	finfo.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
	finfo.generic.in.file.handle = h;
	status = smb2_getinfo_file(tree, tctx, &finfo);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "(%s) smb_raw_pathinfo failed: %s\n",
		    __location__, nt_errstr(status));
		goto fail;
	}
	if (finfo.stream_info.out.num_streams != num_exp) {
		torture_comment(tctx, "(%s) expected %d streams, got %d\n",
		    __location__, num_exp, finfo.stream_info.out.num_streams);
		goto fail;
	}
	if (num_exp == 0) {
		ret = true;
		goto fail;
	}
	exp_sort = talloc_memdup(tmp_ctx, exp, num_exp * sizeof(*exp));
	if (exp_sort == NULL) {
		goto fail;
	}
	TYPESAFE_QSORT(exp_sort, num_exp, qsort_string);
	stream_sort = talloc_memdup(tmp_ctx, finfo.stream_info.out.streams,
				    finfo.stream_info.out.num_streams *
				    sizeof(*stream_sort));
	if (stream_sort == NULL) {
		goto fail;
	}
	TYPESAFE_QSORT(stream_sort, finfo.stream_info.out.num_streams, qsort_stream);
	for (i=0; i expected[%s]\n",
			    __location__, fname, isprint(i)?(char)i:' ', i,
			    isprint(i)?"":" (not printable)",
			    nt_errstr(expected));
		}
		CHECK_STATUS(status, expected);
		talloc_free(path);
	}
done:
	smb2_util_close(tree, h1);
	status = smb2_util_unlink(tree, fname);
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);
	return ret;
}
#define CHECK_CALL_HANDLE(call, rightstatus) do { \
	check_handle = true; \
	call_name = #call; \
	sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
	sfinfo.generic.in.file.handle = h1; \
	status = smb2_setinfo_file(tree, &sfinfo); \
	if (!NT_STATUS_EQUAL(status, rightstatus)) { \
		torture_comment(tctx,"(%s) %s - %s (should be %s)\n", \
		    __location__, #call, \
		    nt_errstr(status), nt_errstr(rightstatus)); \
		ret = false; \
	} \
	finfo1.generic.level = RAW_FILEINFO_ALL_INFORMATION; \
	finfo1.generic.in.file.handle = h1; \
	status2 = smb2_getinfo_file(tree, tctx, &finfo1); \
	if (!NT_STATUS_IS_OK(status2)) { \
		torture_comment(tctx,"(%s) %s pathinfo - %s\n", \
		    __location__, #call, nt_errstr(status)); \
		ret = false; \
	}} while (0)
/*
  test stream renames
*/
static bool test_stream_rename(struct torture_context *tctx,
			       struct smb2_tree *tree)
{
	TALLOC_CTX *mem_ctx = talloc_new(tctx);
	NTSTATUS status, status2;
	union smb_open io;
	const char *fname = DNAME "\\stream_rename.txt";
	const char *sname1, *sname2;
	union smb_fileinfo finfo1;
	union smb_setfileinfo sfinfo;
	bool ret = true;
	struct smb2_handle h, h1;
	bool check_handle;
	const char *call_name;
	sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
	sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname,
				 "Second Stream");
	smb2_util_unlink(tree, fname);
	smb2_deltree(tree, DNAME);
	status = torture_smb2_testdir(tree, DNAME, &h);
	CHECK_STATUS(status, NT_STATUS_OK);
	torture_comment(tctx, "(%s) testing stream renames\n", __location__);
	ZERO_STRUCT(io.smb2);
	io.smb2.in.create_flags = 0;
	io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
				      SEC_FILE_WRITE_ATTRIBUTE |
				    SEC_RIGHTS_FILE_ALL;
	io.smb2.in.create_options = 0;
	io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
	io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
			NTCREATEX_SHARE_ACCESS_WRITE |
			NTCREATEX_SHARE_ACCESS_DELETE;
	io.smb2.in.alloc_size = 0;
	io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
	io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
	io.smb2.in.security_flags = 0;
	io.smb2.in.fname = sname1;
	/* Create two streams. */
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	smb2_util_close(tree, h1);
	io.smb2.in.fname = sname2;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	smb2_util_close(tree, h1);
	/*
	 * Open the second stream.
	 */
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	/*
	 * Now rename the second stream onto the first.
	 */
	ZERO_STRUCT(sfinfo);
	sfinfo.rename_information.in.overwrite = 1;
	sfinfo.rename_information.in.root_fid  = 0;
	sfinfo.rename_information.in.new_name  = ":Stream One";
	CHECK_CALL_HANDLE(RENAME_INFORMATION, NT_STATUS_OK);
done:
	smb2_util_close(tree, h1);
	status = smb2_util_unlink(tree, fname);
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);
	return ret;
}
static bool test_stream_rename2(struct torture_context *tctx,
				struct smb2_tree *tree)
{
	TALLOC_CTX *mem_ctx = talloc_new(tctx);
	NTSTATUS status;
	union smb_open io;
	const char *fname1 = DNAME "\\stream_rename2.txt";
	const char *fname2 = DNAME "\\stream2_rename2.txt";
	const char *stream_name1 = ":Stream One:$DATA";
	const char *stream_name2 = ":Stream Two:$DATA";
	const char *stream_name_default = "::$DATA";
	const char *sname1;
	const char *sname2;
	bool ret = true;
	struct smb2_handle h, h1;
	union smb_setfileinfo sinfo;
	sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname1, "Stream One");
	sname2 = talloc_asprintf(mem_ctx, "%s:%s", fname1, "Stream Two");
	smb2_util_unlink(tree, fname1);
	smb2_util_unlink(tree, fname2);
	smb2_deltree(tree, DNAME);
	status = torture_smb2_testdir(tree, DNAME, &h);
	CHECK_STATUS(status, NT_STATUS_OK);
	ZERO_STRUCT(io.smb2);
	io.smb2.in.create_flags = 0;
	io.smb2.in.desired_access = SEC_FILE_READ_DATA |
				SEC_FILE_WRITE_DATA |
				SEC_STD_DELETE |
				SEC_FILE_APPEND_DATA |
				SEC_STD_READ_CONTROL;
	io.smb2.in.create_options = 0;
	io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
	io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
				NTCREATEX_SHARE_ACCESS_WRITE |
				NTCREATEX_SHARE_ACCESS_DELETE;
	io.smb2.in.alloc_size = 0;
	io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
	io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
	io.smb2.in.security_flags = 0;
	io.smb2.in.fname = sname1;
	/* Open/create new stream. */
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	smb2_util_close(tree, io.smb2.out.file.handle);
	/*
	 * Reopen the stream for SMB2 renames.
	 */
	io.smb2.in.fname = sname1;
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	/*
	 * Check SMB2 rename of a stream using :.
	 */
	torture_comment(tctx, "(%s) Checking SMB2 rename of a stream using "
			":\n", __location__);
	ZERO_STRUCT(sinfo);
	sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION_SMB2;
	sinfo.rename_information.in.file.handle = h1;
	sinfo.rename_information.in.overwrite = 1;
	sinfo.rename_information.in.root_fid = 0;
	sinfo.rename_information.in.new_name = stream_name1;
	status = smb2_setinfo_file(tree, &sinfo);
	CHECK_STATUS(status, NT_STATUS_OK);
	/*
	 * Check SMB2 rename of an overwriting stream using :.
	 */
	torture_comment(tctx, "(%s) Checking SMB2 rename of an overwriting "
			"stream using :\n", __location__);
	/* Create second stream. */
	io.smb2.in.fname = sname2;
	io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	smb2_util_close(tree, io.smb2.out.file.handle);
	/* Rename the first stream onto the second. */
	sinfo.rename_information.in.file.handle = h1;
	sinfo.rename_information.in.new_name = stream_name2;
	status = smb2_setinfo_file(tree, &sinfo);
	CHECK_STATUS(status, NT_STATUS_OK);
	smb2_util_close(tree, h1);
	/*
	 * Reopen the stream with the new name.
	 */
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.smb2.in.fname = sname2;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	/*
	 * Check SMB2 rename of a stream using :.
	 */
	torture_comment(tctx, "(%s) Checking SMB2 rename of a stream using "
			":\n", __location__);
	sinfo.rename_information.in.file.handle = h1;
	sinfo.rename_information.in.new_name = sname1;
	status = smb2_setinfo_file(tree, &sinfo);
	CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
	if (!torture_setting_bool(tctx, "samba4", false)) {
		/*
		 * Check SMB2 rename to the default stream using :.
		 */
		torture_comment(tctx, "(%s) Checking SMB2 rename to default stream "
				"using :\n", __location__);
		sinfo.rename_information.in.file.handle = h1;
		sinfo.rename_information.in.new_name = stream_name_default;
		status = smb2_setinfo_file(tree, &sinfo);
		CHECK_STATUS(status, NT_STATUS_OK);
	}
	smb2_util_close(tree, h1);
 done:
	smb2_util_close(tree, h1);
	status = smb2_util_unlink(tree, fname1);
	status = smb2_util_unlink(tree, fname2);
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);
	return ret;
}
static bool create_file_with_stream(struct torture_context *tctx,
				    struct smb2_tree *tree,
				    TALLOC_CTX *mem_ctx,
				    const char *base_fname,
				    const char *stream)
{
	NTSTATUS status;
	bool ret = true;
	union smb_open io;
	/* Create a file with a stream */
	ZERO_STRUCT(io.smb2);
	io.smb2.in.create_flags = 0;
	io.smb2.in.desired_access = SEC_FILE_READ_DATA |
				SEC_FILE_WRITE_DATA |
				SEC_FILE_APPEND_DATA |
				SEC_STD_READ_CONTROL;
	io.smb2.in.create_options = 0;
	io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
	io.smb2.in.share_access = 0;
	io.smb2.in.alloc_size = 0;
	io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
	io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
	io.smb2.in.security_flags = 0;
	io.smb2.in.fname = stream;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
 done:
	smb2_util_close(tree, io.smb2.out.file.handle);
	return ret;
}
/* Test how streams interact with create dispositions */
static bool test_stream_create_disposition(struct torture_context *tctx,
					   struct smb2_tree *tree)
{
	TALLOC_CTX *mem_ctx = talloc_new(tctx);
	NTSTATUS status;
	union smb_open io;
	const char *fname = DNAME "\\stream_create_disp.txt";
	const char *stream = "Stream One:$DATA";
	const char *fname_stream;
	const char *default_stream_name = "::$DATA";
	const char *stream_list[2];
	bool ret = true;
	struct smb2_handle h, h1;
	/* clean slate .. */
	smb2_util_unlink(tree, fname);
	smb2_deltree(tree, fname);
	smb2_deltree(tree, DNAME);
	status = torture_smb2_testdir(tree, DNAME, &h);
	CHECK_STATUS(status, NT_STATUS_OK);
	fname_stream = talloc_asprintf(mem_ctx, "%s:%s", fname, stream);
	stream_list[0] = talloc_asprintf(mem_ctx, ":%s", stream);
	stream_list[1] = default_stream_name;
	if (!create_file_with_stream(tctx, tree, mem_ctx, fname,
				     fname_stream)) {
		goto done;
	}
	/* Open the base file with OPEN */
	ZERO_STRUCT(io.smb2);
	io.smb2.in.create_flags = 0;
	io.smb2.in.desired_access = SEC_FILE_READ_DATA |
				SEC_FILE_WRITE_DATA |
				SEC_FILE_APPEND_DATA |
				SEC_STD_READ_CONTROL;
	io.smb2.in.create_options = 0;
	io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
	io.smb2.in.share_access = 0;
	io.smb2.in.alloc_size = 0;
	io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
	io.smb2.in.security_flags = 0;
	io.smb2.in.fname = fname;
	/*
	 * check create open: sanity check
	 */
	torture_comment(tctx, "(%s) Checking create disp: open\n",
			__location__);
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	if (!check_stream_list(tree, tctx, fname, 2, stream_list,
			       io.smb2.out.file.handle)) {
		goto done;
	}
	smb2_util_close(tree, io.smb2.out.file.handle);
	/*
	 * check create overwrite
	 */
	torture_comment(tctx, "(%s) Checking create disp: overwrite\n",
			__location__);
	io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	if (!check_stream_list(tree, tctx, fname, 1, &default_stream_name,
			       io.smb2.out.file.handle)) {
		goto done;
	}
	smb2_util_close(tree, io.smb2.out.file.handle);
	/*
	 * check create overwrite_if
	 */
	torture_comment(tctx, "(%s) Checking create disp: overwrite_if\n",
			__location__);
	smb2_util_unlink(tree, fname);
	if (!create_file_with_stream(tctx, tree, mem_ctx, fname, fname_stream))
		goto done;
	io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	if (!check_stream_list(tree, tctx, fname, 1, &default_stream_name,
			       io.smb2.out.file.handle)) {
		goto done;
	}
	smb2_util_close(tree, io.smb2.out.file.handle);
	/*
	 * check create supersede
	 */
	torture_comment(tctx, "(%s) Checking create disp: supersede\n",
			__location__);
	smb2_util_unlink(tree, fname);
	if (!create_file_with_stream(tctx, tree, mem_ctx, fname,
				     fname_stream)) {
		goto done;
	}
	io.smb2.in.create_disposition = NTCREATEX_DISP_SUPERSEDE;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	if (!check_stream_list(tree, tctx, fname, 1, &default_stream_name,
			       io.smb2.out.file.handle)) {
		goto done;
	}
	smb2_util_close(tree, io.smb2.out.file.handle);
	/*
	 * check create overwrite_if on a stream.
	 */
	torture_comment(tctx, "(%s) Checking create disp: overwrite_if on "
			"stream\n", __location__);
	smb2_util_unlink(tree, fname);
	if (!create_file_with_stream(tctx, tree, mem_ctx, fname,
				     fname_stream)) {
		goto done;
	}
	io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
	io.smb2.in.fname = fname_stream;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	if (!check_stream_list(tree, tctx, fname, 2, stream_list,
			       io.smb2.out.file.handle)) {
		goto done;
	}
	smb2_util_close(tree, io.smb2.out.file.handle);
 done:
	smb2_util_close(tree, h1);
	smb2_util_unlink(tree, fname);
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);
	return ret;
}
static bool open_stream(struct smb2_tree *tree,
			struct torture_context *mem_ctx,
			const char *fname,
			struct smb2_handle *h_out)
{
	NTSTATUS status;
	union smb_open io;
	ZERO_STRUCT(io.smb2);
	io.smb2.in.create_flags = 0;
	io.smb2.in.desired_access = SEC_FILE_READ_DATA |
				SEC_FILE_WRITE_DATA |
				SEC_FILE_APPEND_DATA |
				SEC_STD_READ_CONTROL |
				SEC_FILE_WRITE_ATTRIBUTE;
	io.smb2.in.create_options = 0;
	io.smb2.in.file_attributes = 0;
	io.smb2.in.share_access = 0;
	io.smb2.in.alloc_size = 0;
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
	io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
	io.smb2.in.security_flags = 0;
	io.smb2.in.fname = fname;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	if (!NT_STATUS_IS_OK(status)) {
		return false;
	}
	*h_out = io.smb2.out.file.handle;
	return true;
}
/* Test the effect of setting attributes on a stream. */
static bool test_stream_attributes(struct torture_context *tctx,
				   struct smb2_tree *tree)
{
	TALLOC_CTX *mem_ctx = talloc_new(tctx);
	bool ret = true;
	NTSTATUS status;
	union smb_open io;
	const char *fname = DNAME "\\stream_attr.txt";
	const char *stream = "Stream One:$DATA";
	const char *fname_stream;
	struct smb2_handle h, h1;
	union smb_fileinfo finfo;
	union smb_setfileinfo sfinfo;
	time_t basetime = (time(NULL) - 86400) & ~1;
	torture_comment(tctx, "(%s) testing attribute setting on stream\n",
			__location__);
	/* clean slate .. */
	smb2_util_unlink(tree, fname);
	smb2_deltree(tree, fname);
	smb2_deltree(tree, DNAME);
	status = torture_smb2_testdir(tree, DNAME, &h);
	CHECK_STATUS(status, NT_STATUS_OK);
	fname_stream = talloc_asprintf(mem_ctx, "%s:%s", fname, stream);
	/* Create a file with a stream with attribute FILE_ATTRIBUTE_ARCHIVE. */
	ret = create_file_with_stream(tctx, tree, mem_ctx, fname,
				      fname_stream);
	if (!ret) {
		goto done;
	}
	ZERO_STRUCT(io.smb2);
	io.smb2.in.fname = fname;
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
	io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
				 NTCREATEX_SHARE_ACCESS_WRITE |
				 NTCREATEX_SHARE_ACCESS_DELETE;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	ZERO_STRUCT(finfo);
	finfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
	finfo.generic.in.file.handle = io.smb2.out.file.handle;
	status = smb2_getinfo_file(tree, mem_ctx, &finfo);
	CHECK_STATUS(status, NT_STATUS_OK);
	if (finfo.basic_info.out.attrib != FILE_ATTRIBUTE_ARCHIVE) {
		torture_comment(tctx, "(%s) Incorrect attrib %x - should be "
		    "%x\n", __location__,
		    (unsigned int)finfo.basic_info.out.attrib,
		    (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
		ret = false;
		goto done;
	}
	smb2_util_close(tree, io.smb2.out.file.handle);
	/* Now open the stream name. */
	if (!open_stream(tree, tctx, fname_stream, &h1)) {
		goto done;
	}
	/* Change the time on the stream. */
	ZERO_STRUCT(sfinfo);
	unix_to_nt_time(&sfinfo.basic_info.in.write_time, basetime);
	sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
	sfinfo.generic.in.file.handle = h1;
	status = smb2_setinfo_file(tree, &sfinfo);
	if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
		torture_comment(tctx, "(%s) %s - %s (should be %s)\n",
		    __location__, "SETATTR",
		    nt_errstr(status), nt_errstr(NT_STATUS_OK));
		ret = false;
		goto done;
	}
	smb2_util_close(tree, h1);
	ZERO_STRUCT(io.smb2);
	io.smb2.in.fname = fname;
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	ZERO_STRUCT(finfo);
	finfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
	finfo.generic.in.file.handle = h1;
	status = smb2_getinfo_file(tree, mem_ctx, &finfo);
	if (!NT_STATUS_IS_OK(status)) {
		torture_comment(tctx, "(%s) %s pathinfo - %s\n",
		    __location__, "SETATTRE", nt_errstr(status));
		ret = false;
		goto done;
	}
	if (nt_time_to_unix(finfo.basic_info.out.write_time) != basetime) {
		torture_comment(tctx, "(%s) time incorrect.\n", __location__);
		ret = false;
		goto done;
	}
	smb2_util_close(tree, h1);
	if (!open_stream(tree, tctx, fname_stream, &h1)) {
		goto done;
	}
	/* Changing attributes on stream */
	ZERO_STRUCT(sfinfo);
	sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
	sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
	sfinfo.generic.in.file.handle = h1;
	status = smb2_setinfo_file(tree, &sfinfo);
	if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
		torture_comment(tctx, "(%s) %s - %s (should be %s)\n",
			__location__, "SETATTR",
			nt_errstr(status), nt_errstr(NT_STATUS_OK));
		ret = false;
		goto done;
	}
	smb2_util_close(tree, h1);
	ZERO_STRUCT(io.smb2);
	io.smb2.in.fname = fname;
	io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
	io.smb2.in.desired_access = SEC_FILE_READ_DATA;
	status = smb2_create(tree, mem_ctx, &(io.smb2));
	CHECK_STATUS(status, NT_STATUS_OK);
	h1 = io.smb2.out.file.handle;
	ZERO_STRUCT(finfo);
	finfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
	finfo.generic.in.file.handle = h1;
	status = smb2_getinfo_file(tree, mem_ctx, &finfo);
	CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
done:
	smb2_util_close(tree, h1);
	smb2_util_unlink(tree, fname);
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);
	return ret;
}
/*
   basic testing of streams calls SMB2
*/
struct torture_suite *torture_smb2_streams_init(void)
{
	struct torture_suite *suite =
		torture_suite_create(talloc_autofree_context(), "streams");
	torture_suite_add_1smb2_test(suite, "dir", test_stream_dir);
	torture_suite_add_1smb2_test(suite, "io", test_stream_io);
	torture_suite_add_1smb2_test(suite, "sharemodes", test_stream_sharemodes);
	torture_suite_add_1smb2_test(suite, "names", test_stream_names);
	torture_suite_add_1smb2_test(suite, "names2", test_stream_names2);
	torture_suite_add_1smb2_test(suite, "rename", test_stream_rename);
	torture_suite_add_1smb2_test(suite, "rename2", test_stream_rename2);
	torture_suite_add_1smb2_test(suite, "create-disposition", test_stream_create_disposition);
	torture_suite_add_1smb2_test(suite, "attributes", test_stream_attributes);
	torture_suite_add_1smb2_test(suite, "delete", test_stream_delete);
	suite->description = talloc_strdup(suite, "SMB2-STREAM tests");
	return suite;
}