iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / test / pcallbacks.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 <stdlib.h>
26 #include <string.h>
27 #include "plugins-source/callbackcounter/callbackcounter.h"
28 #include "test.h"
29
30 static char *argv[] = { "testarg0", NULL };
31
32 void plugincallbacks(void) {
33         cp_context_t *ctx;
34         cp_status_t status;
35         cp_plugin_info_t *plugin;
36         int errors;
37         cbc_counters_t *counters;
38         
39         ctx = init_context(CP_LOG_ERROR, &errors);
40         cp_set_context_args(ctx, argv);
41         check((plugin = cp_load_plugin_descriptor(ctx, "tmp/install/plugins/callbackcounter", &status)) != NULL && status == CP_OK);
42         check(cp_install_plugin(ctx, plugin) == CP_OK);
43         cp_release_info(ctx, plugin);
44         
45         // Start plug-in implicitly by resolving a symbol
46         check((counters = cp_resolve_symbol(ctx, "callbackcounter", "cbc_counters", &status)) != NULL && status == CP_OK);
47         check(counters->create == 1);
48         check(counters->start == 1);
49         check(counters->logger == 0);
50         check(counters->listener == 1);
51         check(counters->run == 0);
52         check(counters->stop == 0);
53         check(counters->destroy == 0);
54         check(counters->context_arg_0 != NULL && strcmp(counters->context_arg_0, argv[0]) == 0);
55
56         // Cause warning
57         check(cp_start_plugin(ctx, "nonexisting") == CP_ERR_UNKNOWN);
58         check(counters->create == 1);
59         check(counters->start == 1);
60         check(counters->logger == 1);
61         check(counters->listener == 1);
62         check(counters->run == 0);
63         check(counters->stop == 0);
64         check(counters->destroy == 0);
65
66         // Run run function once
67         check(cp_run_plugins_step(ctx));
68         check(counters->create == 1);
69         check(counters->start == 1);
70         check(counters->logger == 1);
71         check(counters->listener == 1);
72         check(counters->run == 1);
73         check(counters->stop == 0);
74         check(counters->destroy == 0);
75
76         // Run run function until no more work to be done (run = 3)
77         cp_run_plugins(ctx);
78         check(counters->create == 1);
79         check(counters->start == 1);
80         check(counters->logger == 1);
81         check(counters->listener == 1);
82         check(counters->run == 3);
83         check(counters->stop == 0);
84         check(counters->destroy == 0);
85
86         /*
87          * Normally symbols must not be accessed after they have been released.
88          * We still access counters here because we know that the plug-in
89          * implementation does not free the counter data.
90          */
91         cp_release_symbol(ctx, counters);
92
93         // Stop plug-in
94         check(cp_stop_plugin(ctx, "callbackcounter") == CP_OK);
95         check(counters->create == 1);
96         check(counters->start == 1);
97         check(counters->logger == 1);
98         check(counters->listener == 2);
99         check(counters->run == 3);
100         check(counters->stop == 1);
101         // for now 1 but might be 0 in future (delay destroy)
102         check(counters->destroy == 0 || counters->destroy == 1);
103         
104         // Uninstall plugin
105         check(cp_uninstall_plugin(ctx, "callbackcounter") == CP_OK);
106         check(counters->create == 1);
107         check(counters->start == 1);
108         check(counters->logger == 1);
109         check(counters->listener == 2);
110         check(counters->run == 3);
111         check(counters->stop == 1);
112         check(counters->destroy == 1);
113
114         cp_destroy();
115         check(errors == 0);
116         
117         /* Free the counter data that was intentionally leaked by the plug-in */
118         free(counters);
119 }