summaryrefslogtreecommitdiff
path: root/service/protocol-plugin/lib/cpluff/test/pscanning.c
blob: ae1987a0808f122ad87ebcaef37002e972f1c0c3 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*-------------------------------------------------------------------------
 * C-Pluff, a plug-in framework for C
 * Copyright 2007 Johannes Lehtinen
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *-----------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include "test.h"

/*
 * Basic plug-in scanning tests were already performed in collections.c.
 * Here we test some more complex things like upgrade and restart behavior.
 */

static void scanupgrade_checkpver(cp_context_t *ctx, const char *plugin, const char *ver) {
	cp_plugin_info_t *pi;
	cp_status_t status;
	
	check((pi = cp_get_plugin_info(ctx, plugin, &status)) != NULL && status == CP_OK);
	check(ver == NULL ? pi->version == NULL : (pi->version != NULL && strcmp(pi->version, ver) == 0));
	cp_release_info(ctx, pi); 
}

void scanupgrade(void) {
	cp_context_t *ctx;
	int errors;
	
	ctx = init_context(CP_LOG_ERROR, &errors);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
	check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
	check(cp_scan_plugins(ctx, 0) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_INSTALLED);
	scanupgrade_checkpver(ctx, "plugin1", NULL);
	
	// Register newer version of plugin1 but do not allow upgrades
	check(cp_start_plugin(ctx, "plugin1") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
	check(cp_scan_plugins(ctx, 0) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	scanupgrade_checkpver(ctx, "plugin1", NULL);

	// Now allow upgrade of plugin1
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
	scanupgrade_checkpver(ctx, "plugin1", "2");
	
	// Register even new version and upgrade while running
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v3")) == CP_OK);
	check(cp_start_plugin(ctx, "plugin1") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
	scanupgrade_checkpver(ctx, "plugin1", "3");
	
	// Check that plug-in is not downgraded when newer versions are unregistered
	cp_unregister_pcollection(ctx, pcollectiondir("collection1v3"));
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
	scanupgrade_checkpver(ctx, "plugin1", "3");

	cp_destroy();
	check(errors == 0);
}

void scanstoponupgrade(void) {
	cp_context_t *ctx;
	int errors;
	
	ctx = init_context(CP_LOG_ERROR, &errors);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
	check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
	check(cp_scan_plugins(ctx, 0) == CP_OK);
	
	// First check upgrade without stopping other plug-ins
	check(cp_start_plugin(ctx, "plugin1") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_start_plugin(ctx, "plugin2a") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_ACTIVE);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_ACTIVE);
	
	// Then check upgrade with stop flag
	check(cp_start_plugin(ctx, "plugin1") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v3")) == CP_OK);
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_STOP_ALL_ON_UPGRADE) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_RESOLVED);

	cp_destroy();
	check(errors == 0);
}

void scanstoponinstall(void) {
	cp_context_t *ctx;
	int errors;
	
	ctx = init_context(CP_LOG_ERROR, &errors);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
	check(cp_scan_plugins(ctx, 0) == CP_OK);
	
	// First check install without stopping other plug-ins
	check(cp_start_plugin(ctx, "plugin1") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_UNINSTALLED);
	check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
	check(cp_scan_plugins(ctx, 0) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
	
	// Then check install and stopping of other plug-ins
	check(cp_uninstall_plugin(ctx, "plugin2a") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_UNINSTALLED);
	check(cp_scan_plugins(ctx, CP_SP_STOP_ALL_ON_INSTALL) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_RESOLVED);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
	
	// Then check upgrade and stopping of other plug-ins
	check(cp_start_plugin(ctx, "plugin2a") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_ACTIVE);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_STOP_ALL_ON_INSTALL) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_RESOLVED);

	cp_destroy();
	check(errors == 0);
}

void scanrestart(void) {
	cp_context_t *ctx;
	int errors;
	
	ctx = init_context(CP_LOG_ERROR, &errors);
	check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
	check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
	check(cp_scan_plugins(ctx, 0) == CP_OK);
	check(cp_start_plugin(ctx, "plugin2b") == CP_OK);
	check(cp_start_plugin(ctx, "plugin1") == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_ACTIVE);

	// Check that upgraded plug-in is correctly restarted after upgrade
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_RESTART_ACTIVE) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_ACTIVE);

	// Check that other plug-ins are correctly restarted after upgrade
	check(cp_register_pcollection(ctx, pcollectiondir("collection1v3")) == CP_OK);
	check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_STOP_ALL_ON_UPGRADE | CP_SP_RESTART_ACTIVE) == CP_OK);
	check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
	check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
	check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_ACTIVE);
	
	cp_destroy();
	check(errors == 0);
}