Exemplo n.º 1
0
/**
 * Test RP processing of an invalid messages.
 */
static void
rp_msg_neg_test(void **state)
{
    int rc = 0;
    rp_session_t *session = NULL;
    Sr__Msg *msg = NULL;

    rp_ctx_t *rp_ctx = *state;
    assert_non_null(rp_ctx);

    ac_ucred_t credentials = { 0 };
    credentials.e_uid = getuid();
    credentials.e_gid = getgid();

    /* generate some request */
    rc = sr_gpb_req_alloc(SR__OPERATION__GET_ITEM, 123456, &msg);
    assert_int_equal(rc, SR_ERR_OK);
    assert_non_null(msg);

    /* process the message with NULL session */
    rc = rp_msg_process(rp_ctx, NULL, msg);
    assert_int_equal(rc, SR_ERR_OK);

    /* create a session */
    rc = rp_session_start(rp_ctx, 123456, &credentials, SR_DS_STARTUP, false, &session);
    assert_int_equal(rc, SR_ERR_OK);
    assert_non_null(session);

    /* RP does not implement session start request */
    rc = sr_gpb_req_alloc(SR__OPERATION__SESSION_START, 123456, &msg);
    assert_int_equal(rc, SR_ERR_OK);
    assert_non_null(msg);

    /* process the message */
    rc = rp_msg_process(rp_ctx, session, msg);
    assert_int_equal(rc, SR_ERR_OK);

    /* RP does not implement session start response */
    rc = sr_gpb_resp_alloc(SR__OPERATION__SESSION_START, 123456, &msg);
    assert_int_equal(rc, SR_ERR_OK);
    assert_non_null(msg);

    /* process the message */
    rc = rp_msg_process(rp_ctx, session, msg);
    assert_int_equal(rc, SR_ERR_OK);

    /* stop the session */
    rc = rp_session_stop(rp_ctx, session);
    assert_int_equal(rc, SR_ERR_OK);
}
Exemplo n.º 2
0
static void
cm_session_stop_generate(uint32_t session_id, uint8_t **msg_buf, size_t *msg_size)
{
    assert_non_null(msg_buf);
    assert_non_null(msg_size);

    Sr__Msg *msg = NULL;
    sr_gpb_req_alloc(NULL, SR__OPERATION__SESSION_STOP, session_id, &msg);
    assert_non_null(msg);
    assert_non_null(msg->request);
    assert_non_null(msg->request->session_stop_req);

    msg->request->session_stop_req->session_id = session_id;

    cm_msg_pack_to_buff(msg, msg_buf, msg_size);
}
Exemplo n.º 3
0
static void
cm_session_start_generate(const char *user_name, uint8_t **msg_buf, size_t *msg_size)
{
    assert_non_null(msg_buf);
    assert_non_null(msg_size);

    Sr__Msg *msg = NULL;
    sr_gpb_req_alloc(NULL, SR__OPERATION__SESSION_START, 0, &msg);
    assert_non_null(msg);
    assert_non_null(msg->request);
    assert_non_null(msg->request->session_start_req);

    if (NULL != user_name) {
        msg->request->session_start_req->user_name = strdup(user_name);
    }
    msg->request->session_start_req->datastore = SR__DATA_STORE__STARTUP;

    cm_msg_pack_to_buff(msg, msg_buf, msg_size);
}
Exemplo n.º 4
0
static void
cm_get_item_generate(uint32_t session_id, const char *xpath, uint8_t **msg_buf, size_t *msg_size)
{
    assert_non_null(msg_buf);
    assert_non_null(msg_size);

    Sr__Msg *msg = NULL;
    sr_gpb_req_alloc(NULL, SR__OPERATION__GET_ITEM, 0, &msg);
    assert_non_null(msg);
    assert_non_null(msg->request);
    assert_non_null(msg->request->get_item_req);

    msg->session_id = session_id;

    if (NULL != xpath) {
        msg->request->get_item_req->xpath = strdup(xpath);
    }

    cm_msg_pack_to_buff(msg, msg_buf, msg_size);
}
Exemplo n.º 5
0
static void
cm_version_verify_generate(const char *soname, uint8_t **msg_buf, size_t *msg_size)
{
    assert_non_null(msg_buf);
    assert_non_null(msg_size);

    Sr__Msg *msg = NULL;
    sr_gpb_req_alloc(NULL, SR__OPERATION__VERSION_VERIFY, 0, &msg);
    assert_non_null(msg);
    assert_non_null(msg->request);
    assert_non_null(msg->request->version_verify_req);

    if (NULL != soname) {
        msg->request->version_verify_req->soname = strdup(soname);
    } else {
        msg->request->version_verify_req->soname = strdup(SR_COMPAT_VERSION);
    }

    cm_msg_pack_to_buff(msg, msg_buf, msg_size);
}
Exemplo n.º 6
0
int
cl_version_verify(sr_conn_ctx_t *connection)
{
    int rc = SR_ERR_OK;
    Sr__Msg *msg_req = NULL, *msg_resp = NULL;
    sr_mem_ctx_t *sr_mem = NULL;

    /* prepare version-verification request */
    rc = sr_mem_new(0, &sr_mem);
    CHECK_RC_MSG_GOTO(rc, cleanup, "Failed to create a new Sysrepo memory context.");
    rc = sr_gpb_req_alloc(sr_mem, SR__OPERATION__VERSION_VERIFY, /* undefined session id */ 0, &msg_req);
    CHECK_RC_MSG_GOTO(rc, cleanup, "Cannot allocate GPB message.");

    /* set argument */
    sr_mem_edit_string(sr_mem, &msg_req->request->version_verify_req->soname, SR_COMPAT_VERSION);
    CHECK_NULL_NOMEM_GOTO(msg_req->request->version_verify_req->soname, rc, cleanup);

    /* send the request */
    SR_LOG_DBG("Sending %s request.", sr_gpb_operation_name(SR__OPERATION__VERSION_VERIFY));

    pthread_mutex_lock(&connection->lock);
    rc = cl_message_send(connection, msg_req);
    if (SR_ERR_OK != rc) {
        SR_LOG_ERR("Unable to send the message with request (operation=%s).",
                   sr_gpb_operation_name(msg_req->request->operation));
        pthread_mutex_unlock(&connection->lock);
        goto cleanup;
    }

    SR_LOG_DBG("%s request sent, waiting for response.", sr_gpb_operation_name(SR__OPERATION__VERSION_VERIFY));

    /* receive the response */
    rc = cl_message_recv(connection, &msg_resp, NULL);
    if (SR_ERR_OK != rc) {
        SR_LOG_ERR("Unable to receive the message with response (operation=%s).",
                   sr_gpb_operation_name(msg_req->request->operation));
        pthread_mutex_unlock(&connection->lock);
        goto cleanup;
    }
    pthread_mutex_unlock(&connection->lock);

    SR_LOG_DBG("%s response received, processing.", sr_gpb_operation_name(SR__OPERATION__VERSION_VERIFY));

    /* validate the response */
    rc = sr_gpb_msg_validate(msg_resp, SR__MSG__MSG_TYPE__RESPONSE, SR__OPERATION__VERSION_VERIFY);
    if (SR_ERR_OK != rc) {
        SR_LOG_ERR("Malformed message with response received (operation=%s).",
                   sr_gpb_operation_name(msg_req->request->operation));
        goto cleanup;
    }

    /* process the result */
    if (SR_ERR_OK != msg_resp->response->result) {
        SR_LOG_ERR("Sysrepod's \"%s\" version is not compatible with version \""SR_COMPAT_VERSION"\" in use.",
                   msg_resp->response->version_verify_resp->soname);
        rc = msg_resp->response->result;
        goto cleanup;
    }

cleanup:
    if (NULL != msg_req) {
        sr_msg_free(msg_req);
    } else {
        sr_mem_free(sr_mem);
    }
    if (NULL != msg_resp) {
        sr_msg_free(msg_resp);
    }

    return rc;
}
Exemplo n.º 7
0
static void
np_dp_subscriptions_test(void **state)
{
    int rc = SR_ERR_OK;
    test_ctx_t *test_ctx = *state;
    assert_non_null(test_ctx);
    np_ctx_t *np_ctx = test_ctx->rp_ctx->np_ctx;
    assert_non_null(np_ctx);
    sr_list_t *subscriptions_list = NULL;

    np_subscription_t **subscriptions_arr = NULL;
    size_t subscriptions_cnt = 0;

    /* delete old subscriptions, if any */
    np_unsubscribe_destination(np_ctx, "addr3");

    /* subscribe */

    rc = np_notification_subscribe(np_ctx, test_ctx->rp_session_ctx, SR__SUBSCRIPTION_TYPE__DP_GET_ITEMS_SUBS,
            "addr4", 789, "example-module", "/example-module:container", SR__NOTIFICATION_EVENT__VERIFY_EV, 20,
            SR_API_VALUES, NP_SUBSCR_ENABLE_RUNNING);
    assert_int_equal(rc, SR_ERR_OK);

    rc = np_notification_subscribe(np_ctx, test_ctx->rp_session_ctx, SR__SUBSCRIPTION_TYPE__DP_GET_ITEMS_SUBS,
            "addr5", 1011, "example-module", "/example-module:container", SR__NOTIFICATION_EVENT__VERIFY_EV, 20,
            SR_API_VALUES, NP_SUBSCR_ENABLE_RUNNING);
    assert_int_equal(rc, SR_ERR_OK);

    /* get subscriptions */
    rc = np_get_data_provider_subscriptions(np_ctx, "example-module", &subscriptions_arr, &subscriptions_cnt);
    assert_int_equal(rc, SR_ERR_OK);
    assert_int_not_equal(subscriptions_cnt, 0);

    assert_int_equal(subscriptions_cnt, 2);

    rc = sr_list_init(&subscriptions_list);
    assert_int_equal(rc, SR_ERR_OK);

    rc = sr_gpb_req_alloc(NULL, SR__OPERATION__GET_ITEM, test_ctx->rp_session_ctx->id, &test_ctx->rp_session_ctx->req);
    assert_int_equal(rc, SR_ERR_OK);
    for (size_t i = 0; i < subscriptions_cnt; i++) {
        assert_non_null(subscriptions_arr[i]);
        assert_true(SR__SUBSCRIPTION_TYPE__DP_GET_ITEMS_SUBS == subscriptions_arr[i]->type);

        /* notify and add into list */
        rc = np_data_provider_request(np_ctx, subscriptions_arr[i], test_ctx->rp_session_ctx, "/example-module:container");
        assert_int_equal(rc, SR_ERR_OK);

        sr_list_add(subscriptions_list, subscriptions_arr[i]);
    }
    free(subscriptions_arr);

    /* release the subscriptions */
    assert_int_equal(rc, SR_ERR_OK);
    for (size_t i = 0; i < subscriptions_list->count; i++) {
        np_free_subscription(subscriptions_list->data[i]);
    }
    sr_list_cleanup(subscriptions_list);

    /* unsubscribe */
    rc = np_notification_unsubscribe(np_ctx, test_ctx->rp_session_ctx, SR__SUBSCRIPTION_TYPE__DP_GET_ITEMS_SUBS,
            "addr4", 789, "example-module");
    assert_int_equal(rc, SR_ERR_OK);

    rc = np_notification_unsubscribe(np_ctx, test_ctx->rp_session_ctx, SR__SUBSCRIPTION_TYPE__DP_GET_ITEMS_SUBS,
            "addr5", 1011, "example-module");
    assert_int_equal(rc, SR_ERR_OK);
}
Exemplo n.º 8
0
/**
 * Session start / stop negative test.
 */
static void
cm_session_neg_test(void **state) {
    Sr__Msg *msg = NULL;
    uint8_t *msg_buf = NULL;
    size_t msg_size = 0;
    int fd1 = 0, fd2 = 0;
    uint32_t session_id1 = 0, session_id2 = 0;

    fd1 = cm_connect_to_server(0);

    /* try invalid version  */
    cm_version_verify_generate("invalid", &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    msg = cm_message_recv(fd1);
    assert_non_null(msg);
    assert_int_equal(msg->type, SR__MSG__MSG_TYPE__RESPONSE);
    assert_non_null(msg->response);
    assert_int_equal(msg->response->result, SR_ERR_VERSION_MISMATCH);
    assert_int_equal(msg->response->operation, SR__OPERATION__VERSION_VERIFY);
    assert_non_null(msg->response->version_verify_resp);
    assert_string_equal_bt(msg->response->version_verify_resp->soname, SR_COMPAT_VERSION);
    sr__msg__free_unpacked(msg, NULL);

    /* disconnect expected */
    msg = cm_message_recv(fd1);
    assert_null(msg);
    close(fd1);

    fd1 = cm_connect_to_server(1);

    /* try a message with NULL request  */
    msg = calloc(1, sizeof(*msg));
    assert_non_null(msg);
    sr__msg__init(msg);
    msg->type = SR__MSG__MSG_TYPE__REQUEST;
    /* send the message */
    cm_msg_pack_to_buff(msg, &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd1);
    /* disconnect expected */
    assert_null(msg);
    close(fd1);

    fd1 = cm_connect_to_server(1);

    /* try a message with bad session id */
    cm_session_stop_generate(999, &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd1);
    /* disconnect expected */
    assert_null(msg);
    close(fd1);

    fd1 = cm_connect_to_server(1);

    /* try a session_start request with non-existing username */
    cm_session_start_generate("non-existing-username", &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd1);
    assert_non_null(msg);
    assert_non_null(msg->response);
    assert_non_null(msg->response->session_start_resp);
    /* expect invalid user error */
    assert_int_equal(msg->response->result, SR_ERR_INVAL_USER);
    sr__msg__free_unpacked(msg, NULL);
    close(fd1);

    fd1 = cm_connect_to_server(1);
    fd2 = cm_connect_to_server(1);

    /* try to stop session via another connection */
    /* session_start request */
    cm_session_start_generate("nobody", &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd1);
    assert_non_null(msg);
    assert_non_null(msg->response);
    assert_non_null(msg->response->session_start_resp);
    session_id1 = msg->response->session_start_resp->session_id;
    sr__msg__free_unpacked(msg, NULL);
    /* stop via another connection */
    cm_session_stop_generate(session_id1, &msg_buf, &msg_size);
    cm_message_send(fd2, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd2);
    /* disconnect expected */
    assert_null(msg);
    close(fd2);

    fd2 = cm_connect_to_server(1);

    /* try sending a response */
    /* session_start request */
    cm_session_start_generate("nobody", &msg_buf, &msg_size);
    cm_message_send(fd2, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd2);
    assert_non_null(msg);
    assert_non_null(msg->response);
    assert_non_null(msg->response->session_start_resp);
    session_id2 = msg->response->session_start_resp->session_id;
    sr__msg__free_unpacked(msg, NULL);
    /* send BAD response */
    sr_gpb_resp_alloc(NULL, SR__OPERATION__SESSION_STOP, session_id2, &msg);
    cm_msg_pack_to_buff(msg, &msg_buf, &msg_size);
    cm_message_send(fd2, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd2);
    /* disconnect expected */
    assert_null(msg);
    close(fd2);

    /* try to stop another session id */
    sr_gpb_req_alloc(NULL, SR__OPERATION__SESSION_STOP, session_id1, &msg);
    assert_non_null(msg);
    assert_non_null(msg->request);
    assert_non_null(msg->request->session_stop_req);
    msg->request->session_stop_req->session_id = 0; /* should be invalid */
    cm_msg_pack_to_buff(msg, &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response - error is expected */
    msg = cm_message_recv(fd1);
    assert_non_null(msg);
    assert_non_null(msg->response);
    assert_int_not_equal(msg->response->result, SR_ERR_OK);
    assert_non_null(sr_strerror(msg->response->result));
    assert_non_null(msg->response->error);
    assert_non_null(msg->response->error->message);
    sr__msg__free_unpacked(msg, NULL);

    /* try sending a message with invalid type */
    sr_gpb_resp_alloc(NULL, SR__OPERATION__SESSION_STOP, session_id1, &msg);
    msg->type = 53;
    cm_msg_pack_to_buff(msg, &msg_buf, &msg_size);
    cm_message_send(fd1, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd1);
    /* disconnect expected */
    assert_null(msg);
    close(fd1);

    fd2 = cm_connect_to_server(1);

    /* try not closing a connection with an open session (auto cleanup) */
    /* session_start request */
    cm_session_start_generate(NULL, &msg_buf, &msg_size);
    cm_message_send(fd2, msg_buf, msg_size);
    free(msg_buf);
    /* receive the response */
    msg = cm_message_recv(fd2);
    assert_non_null(msg);
    assert_non_null(msg->response);
    assert_non_null(msg->response->session_start_resp);
    session_id2 = msg->response->session_start_resp->session_id;
    sr__msg__free_unpacked(msg, NULL);

    cm_teardown(state);
    close(fd2);
}