summaryrefslogtreecommitdiff
path: root/source3/torture/test_dbwrap_do_locked.c
blob: 7226ec1412875bff384f0c006f28fd73eaf28886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Unix SMB/CIFS implementation.
 * Test dbwrap_watch API
 * Copyright (C) Volker Lendecke 2017
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "torture/proto.h"
#include "system/filesys.h"
#include "lib/dbwrap/dbwrap.h"
#include "lib/dbwrap/dbwrap_open.h"
#include "lib/dbwrap/dbwrap_watch.h"
#include "lib/util/util_tdb.h"
#include "source3/include/util_tdb.h"

struct do_locked1_state {
	TDB_DATA value;
	NTSTATUS status;
};

static void do_locked1_cb(
	struct db_record *rec,
	TDB_DATA value,
	void *private_data)
{
	struct do_locked1_state *state =
		(struct do_locked1_state *)private_data;

	state->status = dbwrap_record_store(rec, state->value, 0);
}

static void do_locked1_check(TDB_DATA key, TDB_DATA value,
			     void *private_data)
{
	struct do_locked1_state *state =
		(struct do_locked1_state *)private_data;
	int ret;

	ret = tdb_data_cmp(value, state->value);
	if (ret != 0) {
		state->status = NT_STATUS_DATA_ERROR;
		return;
	}

	state->status = NT_STATUS_OK;
}

static void do_locked1_del(
	struct db_record *rec,
	TDB_DATA value,
	void *private_data)
{
	struct do_locked1_state *state =
		(struct do_locked1_state *)private_data;

	state->status = dbwrap_record_delete(rec);
}

bool run_dbwrap_do_locked1(int dummy)
{
	struct tevent_context *ev;
	struct messaging_context *msg;
	struct db_context *backend;
	struct db_context *db;
	const char *dbname = "test_do_locked.tdb";
	const char *keystr = "key";
	TDB_DATA key = string_term_tdb_data(keystr);
	const char *valuestr = "value";
	TDB_DATA value = string_term_tdb_data(valuestr);
	struct do_locked1_state state = { .value = value };
	int ret = false;
	NTSTATUS status;

	ev = global_event_context();
	if (ev == NULL) {
		fprintf(stderr, "global_event_context() failed\n");
		return false;
	}
	msg = global_messaging_context();
	if (msg == NULL) {
		fprintf(stderr, "global_messaging_context() failed\n");
		return false;
	}

	backend = db_open(talloc_tos(), dbname, 0,
			  TDB_CLEAR_IF_FIRST, O_CREAT|O_RDWR, 0644,
			  DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
	if (backend == NULL) {
		fprintf(stderr, "db_open failed: %s\n", strerror(errno));
		return false;
	}

	db = db_open_watched(talloc_tos(), &backend, msg);
	if (db == NULL) {
		fprintf(stderr, "db_open_watched failed: %s\n",
			strerror(errno));
		return false;
	}

	status = dbwrap_do_locked(db, key, do_locked1_cb, &state);
	if (!NT_STATUS_IS_OK(status)) {
		fprintf(stderr, "dbwrap_do_locked failed: %s\n",
			nt_errstr(status));
		goto fail;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		fprintf(stderr, "store returned %s\n",
			nt_errstr(state.status));
		goto fail;
	}

	status = dbwrap_parse_record(db, key, do_locked1_check, &state);
	if (!NT_STATUS_IS_OK(status)) {
		fprintf(stderr, "dbwrap_parse_record failed: %s\n",
			nt_errstr(status));
		goto fail;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		fprintf(stderr, "data compare returned %s\n",
			nt_errstr(status));
		goto fail;
	}

	status = dbwrap_do_locked(db, key, do_locked1_del, &state);
	if (!NT_STATUS_IS_OK(status)) {
		fprintf(stderr, "dbwrap_do_locked failed: %s\n",
			nt_errstr(status));
		goto fail;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		fprintf(stderr, "delete returned %s\n", nt_errstr(status));
		goto fail;
	}

	status = dbwrap_parse_record(db, key, do_locked1_check, &state);
	if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		fprintf(stderr, "parse_record returned %s, "
			"expected NOT_FOUND\n", nt_errstr(status));
		goto fail;
	}

	ret = true;
fail:
	TALLOC_FREE(db);
	unlink(dbname);
	return ret;
}