Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / test / pscanning.c
1 /*-------------------------------------------------------------------------
2  * C-Pluff, a plug-in framework for C
3  * Copyright 2007 Johannes Lehtinen
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *-----------------------------------------------------------------------*/
23
24 #include <stdio.h>
25 #include <string.h>
26 #include "test.h"
27
28 /*
29  * Basic plug-in scanning tests were already performed in collections.c.
30  * Here we test some more complex things like upgrade and restart behavior.
31  */
32
33 static void scanupgrade_checkpver(cp_context_t *ctx, const char *plugin, const char *ver) {
34         cp_plugin_info_t *pi;
35         cp_status_t status;
36         
37         check((pi = cp_get_plugin_info(ctx, plugin, &status)) != NULL && status == CP_OK);
38         check(ver == NULL ? pi->version == NULL : (pi->version != NULL && strcmp(pi->version, ver) == 0));
39         cp_release_info(ctx, pi); 
40 }
41
42 void scanupgrade(void) {
43         cp_context_t *ctx;
44         int errors;
45         
46         ctx = init_context(CP_LOG_ERROR, &errors);
47         check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
48         check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
49         check(cp_scan_plugins(ctx, 0) == CP_OK);
50         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
51         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
52         check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_INSTALLED);
53         scanupgrade_checkpver(ctx, "plugin1", NULL);
54         
55         // Register newer version of plugin1 but do not allow upgrades
56         check(cp_start_plugin(ctx, "plugin1") == CP_OK);
57         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
58         check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
59         check(cp_scan_plugins(ctx, 0) == CP_OK);
60         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
61         scanupgrade_checkpver(ctx, "plugin1", NULL);
62
63         // Now allow upgrade of plugin1
64         check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
65         scanupgrade_checkpver(ctx, "plugin1", "2");
66         
67         // Register even new version and upgrade while running
68         check(cp_register_pcollection(ctx, pcollectiondir("collection1v3")) == CP_OK);
69         check(cp_start_plugin(ctx, "plugin1") == CP_OK);
70         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
71         check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
72         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
73         scanupgrade_checkpver(ctx, "plugin1", "3");
74         
75         // Check that plug-in is not downgraded when newer versions are unregistered
76         cp_unregister_pcollection(ctx, pcollectiondir("collection1v3"));
77         check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
78         scanupgrade_checkpver(ctx, "plugin1", "3");
79
80         cp_destroy();
81         check(errors == 0);
82 }
83
84 void scanstoponupgrade(void) {
85         cp_context_t *ctx;
86         int errors;
87         
88         ctx = init_context(CP_LOG_ERROR, &errors);
89         check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
90         check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
91         check(cp_scan_plugins(ctx, 0) == CP_OK);
92         
93         // First check upgrade without stopping other plug-ins
94         check(cp_start_plugin(ctx, "plugin1") == CP_OK);
95         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
96         check(cp_start_plugin(ctx, "plugin2a") == CP_OK);
97         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_ACTIVE);
98         check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
99         check(cp_scan_plugins(ctx, CP_SP_UPGRADE) == CP_OK);
100         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
101         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_ACTIVE);
102         
103         // Then check upgrade with stop flag
104         check(cp_start_plugin(ctx, "plugin1") == CP_OK);
105         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
106         check(cp_register_pcollection(ctx, pcollectiondir("collection1v3")) == CP_OK);
107         check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_STOP_ALL_ON_UPGRADE) == CP_OK);
108         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_INSTALLED);
109         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_RESOLVED);
110
111         cp_destroy();
112         check(errors == 0);
113 }
114
115 void scanstoponinstall(void) {
116         cp_context_t *ctx;
117         int errors;
118         
119         ctx = init_context(CP_LOG_ERROR, &errors);
120         check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
121         check(cp_scan_plugins(ctx, 0) == CP_OK);
122         
123         // First check install without stopping other plug-ins
124         check(cp_start_plugin(ctx, "plugin1") == CP_OK);
125         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
126         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_UNINSTALLED);
127         check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
128         check(cp_scan_plugins(ctx, 0) == CP_OK);
129         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
130         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
131         
132         // Then check install and stopping of other plug-ins
133         check(cp_uninstall_plugin(ctx, "plugin2a") == CP_OK);
134         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
135         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_UNINSTALLED);
136         check(cp_scan_plugins(ctx, CP_SP_STOP_ALL_ON_INSTALL) == CP_OK);
137         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_RESOLVED);
138         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
139         
140         // Then check upgrade and stopping of other plug-ins
141         check(cp_start_plugin(ctx, "plugin2a") == CP_OK);
142         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_ACTIVE);
143         check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
144         check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_STOP_ALL_ON_INSTALL) == CP_OK);
145         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_RESOLVED);
146
147         cp_destroy();
148         check(errors == 0);
149 }
150
151 void scanrestart(void) {
152         cp_context_t *ctx;
153         int errors;
154         
155         ctx = init_context(CP_LOG_ERROR, &errors);
156         check(cp_register_pcollection(ctx, pcollectiondir("collection1")) == CP_OK);
157         check(cp_register_pcollection(ctx, pcollectiondir("collection2")) == CP_OK);
158         check(cp_scan_plugins(ctx, 0) == CP_OK);
159         check(cp_start_plugin(ctx, "plugin2b") == CP_OK);
160         check(cp_start_plugin(ctx, "plugin1") == CP_OK);
161         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
162         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
163         check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_ACTIVE);
164
165         // Check that upgraded plug-in is correctly restarted after upgrade
166         check(cp_register_pcollection(ctx, pcollectiondir("collection1v2")) == CP_OK);
167         check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_RESTART_ACTIVE) == CP_OK);
168         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
169         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
170         check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_ACTIVE);
171
172         // Check that other plug-ins are correctly restarted after upgrade
173         check(cp_register_pcollection(ctx, pcollectiondir("collection1v3")) == CP_OK);
174         check(cp_scan_plugins(ctx, CP_SP_UPGRADE | CP_SP_STOP_ALL_ON_UPGRADE | CP_SP_RESTART_ACTIVE) == CP_OK);
175         check(cp_get_plugin_state(ctx, "plugin1") == CP_PLUGIN_ACTIVE);
176         check(cp_get_plugin_state(ctx, "plugin2a") == CP_PLUGIN_INSTALLED);
177         check(cp_get_plugin_state(ctx, "plugin2b") == CP_PLUGIN_ACTIVE);
178         
179         cp_destroy();
180         check(errors == 0);
181 }