static void
test_LSHubPermissionMapLookup(void *fixture, gconstpointer user_data)
{
    ConfigSetDefaults();

    char const *dirs[] = { TEST_STEADY_ROLES_DIRECTORY, NULL };
    LSError error;
    LSErrorInit(&error);
    g_assert(ProcessRoleDirectories(dirs, NULL, &error));
    g_assert(!LSErrorIsSet(&error));

    _LSTransportCred *cred = _LSTransportCredNew();
    g_assert(cred);
    pid_t pid = getpid();
    _LSTransportCredSetPid(cred, pid);

    struct LSTransportHandlers test_handlers = {};
    _LSTransport *transport = NULL;
    g_assert(_LSTransportInit(&transport, "com.webos.foo", &test_handlers, NULL));
    _LSTransportSetTransportType(transport, _LSTransportTypeLocal);
    g_assert(transport);

    _LSTransportClient test_client =
    {
        .service_name = "com.webos.foo",
        .cred = cred,
        .transport = transport,
    };

    _LSTransportCredSetExePath(cred, "/bin/foo");
    test_client.service_name = "com.webos.foo";
    g_assert(LSHubIsClientAllowedToRequestName(&test_client, "com.webos.foo"));
    LSHubActiveRoleMapUnref(pid);
    g_assert(!LSHubIsClientAllowedToRequestName(&test_client, "com.webos.foo2"));
    LSHubActiveRoleMapUnref(pid);

    _LSTransportCredSetExePath(cred, "/bin/bar");
    test_client.service_name = "com.webos.bar";
    g_assert(LSHubIsClientAllowedToRequestName(&test_client, "com.webos.bar"));
    LSHubActiveRoleMapUnref(pid);
    g_assert(LSHubIsClientAllowedToRequestName(&test_client, "com.webos.bar2"));
    LSHubActiveRoleMapUnref(pid);

    _LSTransportCredSetExePath(cred, "/bin/foo");
    test_client.service_name = "com.webos.foo";
    g_assert(LSHubIsClientAllowedToQueryName(&test_client, "com.webos.bar", "asdf"));
    _LSTransportCredSetExePath(cred, "/bin/bar");
    test_client.service_name = "com.webos.bar";
    g_assert(LSHubIsClientAllowedToQueryName(&test_client, "com.webos.foo", "asdf"));
    _LSTransportCredSetExePath(cred, "/bin/bar");
    test_client.service_name = "com.webos.bar2";
    g_assert(LSHubIsClientAllowedToQueryName(&test_client, "com.webos.foo", "asdf"));

    _LSTransportDeinit(transport);
    _LSTransportCredFree(cred);
    _ConfigFreeSettings();
}

int
main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);

    g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
    g_log_set_fatal_mask ("LunaServiceHub", G_LOG_LEVEL_ERROR);

    g_test_add("/hub/LSHubPermissionMapLookup", void, NULL, NULL, test_LSHubPermissionMapLookup, NULL);

    return g_test_run();
}
Example #2
0
int
main(int argc, char *argv[])
{
    LSError lserror;
    LSErrorInit(&lserror);

    int public = HUB_TYPE_PUBLIC;
    int private = HUB_TYPE_PRIVATE;
    
    if (LSIsRunning(PID_DIR, MONITOR_PID_NAME))
    {
        g_critical("An instance of the monitor is already running");
        exit(EXIT_FAILURE);
    }
    
    mainloop = g_main_loop_new(NULL, FALSE);

    /* send message to hub to let clients know that they should start
     * sending us their messages */
    LSTransportHandlers handler_priv =
    {
        .msg_handler = _LSMonitorMessageHandlerPrivate,
        .msg_context = &private,
        .disconnect_handler = NULL,
        .disconnect_context = NULL,
        .message_failure_handler = NULL,
        .message_failure_context = NULL
    };
    
    LSTransportHandlers handler_pub =
    {
        .msg_handler = _LSMonitorMessageHandlerPublic,
        .msg_context = &public,
        .disconnect_handler = NULL,
        .disconnect_context = NULL,
        .message_failure_handler = NULL,
        .message_failure_context = NULL
    };

    _LSTransportSetupSignalHandler(SIGTERM, _HandleShutdown);
    _LSTransportSetupSignalHandler(SIGINT, _HandleShutdown);

    _HandleCommandline(argc, argv);

    if (list_clients || list_subscriptions || list_malloc)
    {
        handler_priv.msg_handler = _LSMonitorListMessageHandler;
        handler_pub.msg_handler = _LSMonitorListMessageHandler;
    }

    if (!_LSTransportInit(&transport_priv, MONITOR_NAME, &handler_priv, &lserror))
    {
        goto error;
    }
    
    if (!_LSTransportInit(&transport_pub, MONITOR_NAME, &handler_pub, &lserror))
    {
        goto error;
    }
   
    /* connect for "private" messages */ 
    if (!_LSTransportConnect(transport_priv, true, false, &lserror))
    {
        goto error;
    }

    /* connect for "public" messages */
    if (!_LSTransportConnect(transport_pub, true, true, &lserror))
    {
        goto error;
    }
   
    _LSTransportGmainAttach(transport_priv, g_main_loop_get_context(mainloop)); 
    _LSTransportGmainAttach(transport_pub, g_main_loop_get_context(mainloop)); 

    if (_LSTransportGetTransportType(transport_priv) == _LSTransportTypeLocal)
    {
        transport_priv_local = true;

        /* message printing callback */
        //g_idle_add(_LSMonitorIdleHandlerPrivate, NULL);
        private_queue = _LSMonitorQueueNew(false);
        g_timeout_add(500, _LSMonitorIdleHandlerPrivate, private_queue);
    }

    if (_LSTransportGetTransportType(transport_pub) == _LSTransportTypeLocal)
    {
        transport_pub_local = true;
        
        //g_idle_add(_LSMonitorIdleHandlerPublic, NULL);
        public_queue = _LSMonitorQueueNew(true);
        g_timeout_add(500, _LSMonitorIdleHandlerPublic, public_queue);
    }

    if (list_clients || list_subscriptions || list_malloc)
    {
        if (!_LSTransportSendMessageListClients(transport_priv, &lserror))
        {
            goto error;
        }

        if (!_LSTransportSendMessageListClients(transport_pub, &lserror))
        {
            goto error;
        }
    } 
    else
    {
        /* send the message to the hub to tell clients to connect to us */
        if (!LSTransportSendMessageMonitorRequest(transport_priv, &lserror))
        {
            goto error;
        }
        
        if (!LSTransportSendMessageMonitorRequest(transport_pub, &lserror))
        {
            goto error;
        }

        if (debug_output)
        {
            fprintf(stdout, "Debug\tTime\t\tProt\tType\tSerial\t\tSender\t\tDestination\t\tMethod                            \tPayload\n");
        }
        else
        {
            fprintf(stdout, "Time\t\tProt\tType\tSerial\t\tSender\t\tDestination\t\tMethod                            \tPayload\n");
        }
    }

    dup_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
    LS_ASSERT(dup_hash_table);

    g_main_loop_run(mainloop);
    g_main_loop_unref(mainloop);

    _DisconnectCustomTransport();

    g_hash_table_destroy(dup_hash_table);

    exit(EXIT_SUCCESS);

error:
    LSErrorPrint(&lserror, stderr);
    LSErrorFree(&lserror);
    exit(EXIT_FAILURE);
}