#include #include #include #include #define ACCOUNT_USERNAME "username" /* Account login. */ #define ACCOUNT_PWD "password" /* Ordinary password. */ #define ACCOUNT_CODE "account_code" /* Account code generated in web portal after mobile key regisstration. */ #define TIMEOUT_MS 3000 #define TIMEOUT2_MS 60000 static void print_library_version(void) { char *ver_str = isds_version(); if (NULL != ver_str) { fprintf(stderr, "Library version: %s\n", ver_str); free(ver_str); } else { fprintf(stderr, "Error: obtaining library version.\n"); } } static void log_callback(isds_log_facility facility, isds_log_level level, const char *message, int length, void *data) { (void) facility; (void) level; (void) data; if ((NULL != message) && (length > 0)) { for (int i = 0; i < length; ++i) { fputc(message[i], stderr); } } } static void log_isds_error(const struct isds_ctx *ctx) { if (NULL != ctx) { const char *str = isds_long_message(ctx); if (NULL != str) { fputs("Long message: ", stderr); fputs(str, stderr); fputs("\n", stderr); } } } static int test_login_pwd(void) { struct isds_ctx *ctx = isds_ctx_create(); if (ctx == NULL) { fprintf(stderr, "Error: creating context.\n"); goto fail; } if (IE_SUCCESS != isds_set_timeout(ctx, TIMEOUT_MS)) { fprintf(stderr, "Error: settings timeout.\n"); goto fail; } if (IE_SUCCESS != isds_login(ctx, isds_testing_locator, ACCOUNT_USERNAME, ACCOUNT_PWD, NULL, NULL, NULL)) { fprintf(stderr, "Error: logging in.\n"); log_isds_error(ctx); goto fail; } if (IE_SUCCESS != isds_ping(ctx)) { fprintf(stderr, "Error: contacting server.\n"); log_isds_error(ctx); goto fail; } if (IE_SUCCESS != isds_logout(ctx)) { fprintf(stderr, "Error: logging out.\n"); goto fail; } if (IE_SUCCESS != isds_ctx_free(&ctx)) { fprintf(stderr, "Error: freeing context.\n"); goto fail; } return 0; fail: return -1; } static int test_login_mep(void) { struct isds_ctx *ctx = isds_ctx_create(); if (ctx == NULL) { fputs("Error: creating context.\n", stderr); goto fail; } if (IE_SUCCESS != isds_set_timeout(ctx, TIMEOUT2_MS)) { fputs("Error: settings timeout.\n", stderr); goto fail; } struct isds_mep mep = { .mep_code = ACCOUNT_CODE, .app_name = "Test-application", .intermediate_uri = NULL, .resolution = MEP_RESOLUTION_SUCCESS }; int ret = IE_SUCCESS; if (IE_PARTIAL_SUCCESS != (ret = isds_login(ctx, isds_mep_testing_locator, ACCOUNT_USERNAME, NULL, NULL, NULL, &mep))) { fprintf(stderr, "Error: initialising login '%d'.\n", ret); log_isds_error(ctx); goto fail; } #if 1 while (ret == IE_PARTIAL_SUCCESS) { sleep(1); fprintf(stderr, "Retrying.----------------------------------------------------------------------------------------------------------------------------------------\n"); ret = isds_login(ctx, isds_mep_testing_locator, ACCOUNT_USERNAME, NULL, NULL, NULL, &mep); } if (ret != IE_SUCCESS) { fprintf(stderr, "Error: logging in.\n"); } #endif if (IE_SUCCESS != isds_ping(ctx)) { fprintf(stderr, "Error: contacting server.\n"); log_isds_error(ctx); goto fail; } if (IE_SUCCESS != isds_logout(ctx)) { fprintf(stderr, "Error: logging out.\n"); goto fail; } if (IE_SUCCESS != isds_ctx_free(&ctx)) { fputs("Error: freeing context.\n", stderr); goto fail; } return 0; fail: return -1; } int main(void) { if (IE_SUCCESS != isds_init()) { fputs("Error: initialising library.\n", stderr); goto fail; } isds_set_logging(ILF_ALL, ILL_ALL); isds_set_log_callback(log_callback, NULL); print_library_version(); #if 0 if (0 != test_login_pwd()) { fputs("Error: logging in using a password.\n", stderr); goto fail; } #endif fputs("========================================================\n", stderr); #if 1 if (0 != test_login_mep()) { fputs("Error: logging in using a mobile key.\n", stderr); goto fail; } #endif if (IE_SUCCESS != isds_cleanup()) { fputs("Error: cleaning up library.\n", stderr); goto fail; } return EXIT_SUCCESS; fail: return EXIT_FAILURE; }