summaryrefslogtreecommitdiff
path: root/lib/backupstore/StoreTestUtils.h
blob: 0740b3ec061c869c9363774d09bb8774b5726643 (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
// --------------------------------------------------------------------------
//
// File
//		Name:    StoreTestUtils.h
//		Purpose: Utilities for housekeeping and checking a test store
//		Created: 18/02/14
//
// --------------------------------------------------------------------------

#ifndef STORETESTUTILS__H
#define STORETESTUTILS__H

#include "Test.h"

class BackupProtocolCallable;
class BackupProtocolClient;
class SocketStreamTLS;
class TLSContext;

//! Holds the expected reference counts of each object.
extern std::vector<uint32_t> ExpectedRefCounts;

//! Holds the PID of the currently running bbstored test server.
extern int bbstored_pid, bbackupd_pid;

//! Sets up (cleans up) test environment at the start of every test.
bool setUp(const char* function_name);

//! Checks account for errors and shuts down daemons at end of every test.
bool tearDown();

//! Like tearDown() but returns false, because a test failure was detected.
bool fail();

//! Sets the expected refcount of an object, resizing vector if necessary.
void set_refcount(int64_t ObjectID, uint32_t RefCount = 1);

//! Initialises a TLSContext object using the standard certficate filenames.
void init_context(TLSContext& rContext);

//! Opens a connection to the server (bbstored).
std::auto_ptr<SocketStream> open_conn(const char *hostname,
	TLSContext& rContext);

//! Opens a connection to the server (bbstored) without logging in.
std::auto_ptr<BackupProtocolCallable> connect_to_bbstored(TLSContext& rContext);

//! Opens a connection to the server (bbstored) and logs in.
std::auto_ptr<BackupProtocolCallable> connect_and_login(TLSContext& rContext,
	int flags = 0);

//! Checks the number of files of each type in the store against expectations.
bool check_num_files(int files, int old, int deleted, int dirs);

//! Checks the number of blocks in files of each type against expectations.
bool check_num_blocks(BackupProtocolCallable& Client, int Current, int Old,
	int Deleted, int Dirs, int Total);

//! Change the soft and hard limits on the test account.
bool change_account_limits(const char* soft, const char* hard);

//! Checks an account for errors, returning the number of errors found and fixed.
int check_account_for_errors(Log::Level log_level = Log::WARNING);

//! Checks an account for errors, returning true if it's OK, for use in assertions.
bool check_account(Log::Level log_level = Log::WARNING);

//! Runs housekeeping on an account, to remove old and deleted files if necessary.
int64_t run_housekeeping(BackupStoreAccountDatabase::Entry& rAccount);

//! Runs housekeeping and checks the account, returning true if it's OK.
bool run_housekeeping_and_check_account();

//! Tests that all object reference counts have the expected values.
bool check_reference_counts();

//! Starts the bbstored test server running, which must not already be running.
bool StartServer();

//! Stops the currently running bbstored test server.
bool StopServer(bool wait_for_process = false);

//! Starts the bbackupd client running, which must not already be running.
bool StartClient(const std::string& bbackupd_conf_file = "testfiles/bbackupd.conf");

//! Stops the currently running bbackupd client.
bool StopClient(bool wait_for_process = false);

//! Creates the standard test account, for example after delete_account().
bool create_account(int soft, int hard);

//! Deletes the standard test account, for testing behaviour with no account.
bool delete_account();

#define TEST_PROTOCOL_ERROR_OR(protocol, error, or_statements) \
	{ \
		int type, subtype; \
		(protocol).GetLastError(type, subtype); \
		if (type == BackupProtocolError::ErrorType) \
		{ \
			TEST_EQUAL_LINE(BackupProtocolError::error, subtype, \
				"command returned error: " << \
				BackupProtocolError::GetMessage(subtype)); \
			if (subtype != BackupProtocolError::error) \
			{ \
				or_statements; \
			} \
		} \
		else \
		{ \
			TEST_FAIL_WITH_MESSAGE("command returned success"); \
			or_statements; \
		} \
	}

#define TEST_COMMAND_RETURNS_ERROR_OR(protocol, command, error, or_statements) \
	TEST_CHECK_THROWS_OR((protocol) . command, ConnectionException, \
		Protocol_UnexpectedReply, or_statements); \
	TEST_PROTOCOL_ERROR_OR(protocol, error, or_statements)

#define TEST_COMMAND_RETURNS_ERROR(protocol, command, error) \
	TEST_COMMAND_RETURNS_ERROR_OR(protocol, command, error,)

#endif // STORETESTUTILS__H