afb-export: Fix requirement on multiple apis 45/21245/1
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 3 May 2019 13:08:10 +0000 (15:08 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 3 May 2019 13:19:51 +0000 (15:19 +0200)
The test 'test-apiv3' was failing because the required
api list "albert armel" wasn't splitted but set as-is.

At the same time, test suite is improved.

Bug-AGL: SPEC-1130
Bug-AGL: SPEC-2377

Change-Id: Ia4ab23bf666b5b4b1db3e1da2654d9387c2c01e5
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api-v3.c
src/afb-export.c
src/tests/apiset/CMakeLists.txt
src/tests/apiset/test-apiset.c
src/tests/apiv3/test-apiv3.c
src/tests/globset/CMakeLists.txt
src/tests/globset/globset.out
src/tests/globset/test-globset.c

index 7b889ee..6469b88 100644 (file)
@@ -365,7 +365,7 @@ int afb_api_v3_set_binding_fields(const struct afb_binding_v3 *desc, struct afb_
        if (!rc && desc->require_class)
                rc =  afb_api_x3_require_class(api, desc->require_class);
        if (!rc && desc->require_api)
-               rc =  afb_api_x3_require_api(api, desc->require_api, 1);
+               rc =  afb_api_x3_require_api(api, desc->require_api, 0);
        return rc;
 }
 
index 2c6522f..8de9f3b 100644 (file)
@@ -345,9 +345,11 @@ static int require_api_cb(struct afb_api_x3 *closure, const char *name, int init
        char *iter, *end, save;
 
        /* emit a warning about unexpected require in preinit */
-       if (export->state == Api_State_Pre_Init)
-               WARNING("[API %s] requiring apis in pre-init may lead to unexpected result (requires%s: %s)",
-                       export->api.apiname, initialized ? " initialized" : "", name);
+       if (export->state == Api_State_Pre_Init && initialized) {
+               ERROR("[API %s] requiring initialized apis in pre-init is forbiden", export->api.apiname);
+               errno = EINVAL;
+               return -1;
+       }
 
        /* scan the names in a local copy */
        rc = 0;
@@ -367,10 +369,20 @@ static int require_api_cb(struct afb_api_x3 *closure, const char *name, int init
                *end = 0;
 
                /* check the required api */
-               if (export->state == Api_State_Pre_Init)
-                       rc2 = afb_apiset_require(export->declare_set, export->api.apiname, name);
-               else
+               if (export->state == Api_State_Pre_Init) {
+                       rc2 = afb_apiset_require(export->declare_set, export->api.apiname, iter);
+                       if (rc2 < 0) {
+                               if (rc == 0)
+                                       WARNING("[API %s] requiring apis pre-init may lead to unexpected result", export->api.apiname);
+                               ERROR("[API %s] requiring api %s in pre-init failed", export->api.apiname, iter);
+                       }
+               } else {
                        rc2 = -!((initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->call_set, iter, 1));
+                       if (rc2 < 0) {
+                               ERROR("[API %s] requiring api %s%s failed", export->api.apiname,
+                                        iter, initialized ? " initialized" : "");
+                       }
+               }
                if (rc2 < 0)
                        rc = rc2;
 
index 62ca5e4..59b5131 100644 (file)
 # limitations under the License.
 ###########################################################################
 
+add_definitions(
+  -DAGL_DEVEL=$<BOOL:${AGL_DEVEL}>
+  -DWITH_AFB_HOOK=$<BOOL:${WITH_AFB_HOOK}>
+)
 add_executable(test-apiset test-apiset.c)
 target_include_directories(test-apiset PRIVATE ../..)
 target_link_libraries(test-apiset afb-lib ${link_libraries})
index 96fbf4b..8c9ddd2 100644 (file)
@@ -429,11 +429,11 @@ START_TEST (check_settings)
        afb_apiset_start_all_services(a);
        ck_assert_int_eq(nn, set_count);
 
-       set_count = 0;
 #if WITH_AFB_HOOK
+       set_count = 0;
        afb_apiset_update_hooks(a, NULL);
-#endif
        ck_assert_int_eq(nn, set_count);
+#endif
 
        for (mask = 1 ; !(mask >> 10) ; mask <<= 1) {
                set_count = 0;
index 1472e70..9f89e74 100644 (file)
@@ -16,6 +16,7 @@
 #include "afb-api.h"
 #include "afb-apiset.h"
 #include "afb-api-v3.h"
+#include "verbose.h"
 
 struct inapis {
        struct afb_binding_v3 desc;
@@ -148,19 +149,21 @@ START_TEST (test)
 {
        int rc;
 
+       verbosity_set(-1);
        apiset = afb_apiset_create("test-apiv3", 1);
        ck_assert_ptr_nonnull(apiset);
 
-       out_v3 = afb_api_v3_create(apiset,
-               apiset,
-               out_apiname,
-               NULL,
-               0,
-               out_preinit,
-               out_apiname,
-               0,
-               NULL,
-               NULL);
+       out_v3 = afb_api_v3_create(
+                       apiset,
+                       apiset,
+                       out_apiname,
+                       NULL,
+                       0,
+                       out_preinit,
+                       out_apiname,
+                       0,
+                       NULL,
+                       NULL);
        ck_assert_ptr_nonnull(out_v3);
        ck_assert_ptr_nonnull(out_api);
 
index d0b3808..d7a05dc 100644 (file)
@@ -19,5 +19,5 @@
 add_executable(test-globset test-globset.c)
 target_include_directories(test-globset PRIVATE ../..)
 target_link_libraries(test-globset afb-lib ${link_libraries})
-add_test(NAME globset COMMAND test-globset)
+add_test(NAME globset COMMAND test-globset ${CMAKE_CURRENT_SOURCE_DIR}/globset.in ${CMAKE_CURRENT_SOURCE_DIR}/globset.out)
 
index e0f4c07..426f20a 100644 (file)
@@ -2,7 +2,7 @@ match [kilil]: NOT FOUND
 search [kilil]: NOT FOUND
 add [kilil]: 0, Success
 match [kilil]: found by kilil
-search [kilil]: found 
+search [kilil]: found kilil
 match [kil]: NOT FOUND
 del [error]: -1, No such file or directory
 add [kilil]: -1, File exists
index 86e778e..630fb89 100644 (file)
 
 #include "globset.h"
 
-int main()
+void process(FILE *in, FILE *out)
 {
        int rc;
        char buffer[1024], *str;
        const struct globset_handler *gh;
        struct globset *set;
 
-       setvbuf(stdout, NULL, _IOLBF, 0);
        set = globset_create();
-       while (fgets(buffer, sizeof buffer, stdin)) {
+       while (fgets(buffer, sizeof buffer, in)) {
                str = strchr(buffer,'\n');
                if (str) *str = 0;
                errno = 0;
                switch (buffer[0]) {
                case '+':
                        rc = globset_add(set, &buffer[1], NULL, NULL);
-                       printf("add [%s]: %d, %m\n", &buffer[1], rc);
+                       fprintf(out, "add [%s]: %d, %m\n", &buffer[1], rc);
                        break;
                case '-':
                        rc = globset_del(set, &buffer[1], NULL);
-                       printf("del [%s]: %d, %m\n", &buffer[1], rc);
+                       fprintf(out, "del [%s]: %d, %m\n", &buffer[1], rc);
                        break;
                case '?':
                        gh = globset_search(set, &buffer[1]);
-                       printf("search [%s]: %s%s\n", &buffer[1], gh ? "found " : "NOT FOUND", gh ? gh->pattern : "");
+                       fprintf(out, "search [%s]: %s%s\n", &buffer[1], gh ? "found " : "NOT FOUND", gh ? gh->pattern : "");
                        break;
                default:
                        gh = globset_match(set, buffer);
-                       printf("match [%s]: %s%s\n", buffer, gh ? "found by " : "NOT FOUND", gh ? gh->pattern : "");
+                       fprintf(out, "match [%s]: %s%s\n", buffer, gh ? "found by " : "NOT FOUND", gh ? gh->pattern : "");
                        break;
                }
        }
        globset_destroy(set);
+}
+
+int compare(FILE *f1, FILE *f2)
+{
+       int l = 0, n = 0;
+       char b1[1024], b2[1024];
+       char *s1, *s2;
+
+       for(;;) {
+               l++;
+               s1 = fgets(b1, sizeof b1, f1);
+               s2 = fgets(b2, sizeof b2, f2);
+               if (s1 == NULL || s2 == NULL) {
+                       if (s1 != s2) {
+                               fprintf(stderr, "Length of outputs differ\n");
+                               n++;
+                       }
+                       return n;
+               }
+               if (strcmp(s1, s2)) {
+                       fprintf(stderr, "Line %d differ\n\t%s\t%s", l, s1, s2);
+                       n++;
+               }
+       }
+}
+
+int main(int ac, char **av)
+{
+       FILE *in = stdin;
+       FILE *out = stdout;
+       FILE *ref = NULL;
+
+       if (ac >= 2) {
+               in = fopen(av[1], "r");
+               if (in == NULL) {
+                       fprintf(stderr, "Can't open file %s: %m\n", av[1]);
+                       return 1;
+               }
+       }
+
+       if (ac < 3)
+               setvbuf(stdout, NULL, _IOLBF, 0);
+       else {
+               ref = fopen(av[2], "r");
+               if (ref == NULL) {
+                       fprintf(stderr, "Can't open file %s: %m\n", av[2]);
+                       return 1;
+               }
+               out = tmpfile();
+               if (out == NULL) {
+                       fprintf(stderr, "Can't create temporary file: %m\n");
+                       return 1;
+               }
+       }
+
+       process(in, out);
+
+       if (ref) {
+               rewind(out);
+               if (compare(out, ref))
+                       return 1;
+       }
+
        return 0;
 }