libnm: add mapping functions between string and NMClientPermission enum

This commit is contained in:
Thomas Haller 2019-12-05 14:16:48 +01:00
parent 5ad095374f
commit 09e17888f7
3 changed files with 143 additions and 0 deletions

View file

@ -22,6 +22,7 @@
#include "nm-libnm-utils.h"
#include "nm-object.h"
#include "nm-vpn-service-plugin.h"
#include "nm-libnm-core-intern/nm-libnm-core-utils.h"
#include "nm-utils/nm-test-utils.h"
@ -3064,6 +3065,49 @@ test_dbus_meta_types (void)
g_assert (meta_iface->get_type_fcn() == d->gtype);
}
}
/*****************************************************************************/
static void
test_nm_auth_permissions (void)
{
int i, j;
G_STATIC_ASSERT (G_N_ELEMENTS (nm_auth_permission_names_by_idx) == NM_CLIENT_PERMISSION_LAST);
G_STATIC_ASSERT (G_N_ELEMENTS (nm_auth_permission_sorted) == NM_CLIENT_PERMISSION_LAST);
for (i = 0; i < NM_CLIENT_PERMISSION_LAST; i++) {
g_assert (nm_auth_permission_names_by_idx[i]);
g_assert (NM_STR_HAS_PREFIX (nm_auth_permission_names_by_idx[i], "org.freedesktop.NetworkManager."));
g_assert_cmpint (nm_auth_permission_sorted[i], >, 0);
g_assert_cmpint (nm_auth_permission_sorted[i], <=, NM_CLIENT_PERMISSION_LAST);
for (j = i + 1; j < NM_CLIENT_PERMISSION_LAST; j++) {
g_assert_cmpint (nm_auth_permission_sorted[i], !=, nm_auth_permission_sorted[j]);
g_assert_cmpstr (nm_auth_permission_names_by_idx[i], !=, nm_auth_permission_names_by_idx[j]);
}
}
for (i = 1; i < NM_CLIENT_PERMISSION_LAST; i++) {
NMClientPermission a = nm_auth_permission_sorted[i - 1];
NMClientPermission b = nm_auth_permission_sorted[i];
const char *s_a = nm_auth_permission_names_by_idx[a - 1];
const char *s_b = nm_auth_permission_names_by_idx[b - 1];
g_assert_cmpstr (s_a, <, s_b);
g_assert (a != b);
g_assert (s_a != s_b);
}
for (i = 1; i <= NM_CLIENT_PERMISSION_LAST; i++) {
const char *s = nm_auth_permission_to_string (i);
g_assert_cmpstr (s, ==, nm_auth_permission_names_by_idx[i - 1]);
g_assert (s == nm_auth_permission_names_by_idx[i - 1]);
g_assert_cmpint (nm_auth_permission_from_string (s), ==, i);
}
return;
for (i = 0; i < NM_CLIENT_PERMISSION_LAST; i++)
g_assert_cmpint (nm_auth_permission_from_string (nm_auth_permission_names_by_idx[i]), ==, i + 1);
}
/*****************************************************************************/
NMTST_DEFINE ();
@ -3078,6 +3122,7 @@ int main (int argc, char **argv)
g_test_add_func ("/libnm/general/test_types", test_types);
g_test_add_func ("/libnm/general/test_nml_dbus_meta", test_nml_dbus_meta);
g_test_add_func ("/libnm/general/test_dbus_meta_types", test_dbus_meta_types);
g_test_add_func ("/libnm/general/test_nm_auth_permissions", test_nm_auth_permissions);
return g_test_run ();
}

View file

@ -4,6 +4,8 @@
#include "nm-libnm-core-utils.h"
#include "nm-common-macros.h"
/*****************************************************************************/
gboolean
@ -59,3 +61,91 @@ nm_utils_vlan_priority_map_parse_str (NMVlanPriorityMap map_type,
NM_SET_OUT (out_has_wildcard_to, v2 < 0);
return TRUE;
}
/*****************************************************************************/
const char *const nm_auth_permission_names_by_idx[NM_CLIENT_PERMISSION_LAST] = {
[NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK - 1] = NM_AUTH_PERMISSION_CHECKPOINT_ROLLBACK,
[NM_CLIENT_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK,
[NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK,
[NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_STATISTICS,
[NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI,
[NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX,
[NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN,
[NM_CLIENT_PERMISSION_NETWORK_CONTROL - 1] = NM_AUTH_PERMISSION_NETWORK_CONTROL,
[NM_CLIENT_PERMISSION_RELOAD - 1] = NM_AUTH_PERMISSION_RELOAD,
[NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS,
[NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME,
[NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN,
[NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM,
[NM_CLIENT_PERMISSION_SLEEP_WAKE - 1] = NM_AUTH_PERMISSION_SLEEP_WAKE,
[NM_CLIENT_PERMISSION_WIFI_SCAN - 1] = NM_AUTH_PERMISSION_WIFI_SCAN,
[NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN - 1] = NM_AUTH_PERMISSION_WIFI_SHARE_OPEN,
[NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED - 1] = NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED,
};
const NMClientPermission nm_auth_permission_sorted[NM_CLIENT_PERMISSION_LAST] = {
NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK,
NM_CLIENT_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK,
NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK,
NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS,
NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI,
NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX,
NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN,
NM_CLIENT_PERMISSION_NETWORK_CONTROL,
NM_CLIENT_PERMISSION_RELOAD,
NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS,
NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME,
NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN,
NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM,
NM_CLIENT_PERMISSION_SLEEP_WAKE,
NM_CLIENT_PERMISSION_WIFI_SCAN,
NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN,
NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED,
};
const char *
nm_auth_permission_to_string (NMClientPermission permission)
{
if (permission < 1)
return NULL;
if (permission > NM_CLIENT_PERMISSION_LAST)
return NULL;
return nm_auth_permission_names_by_idx[permission - 1];
}
#define AUTH_PERMISSION_PREFIX "org.freedesktop.NetworkManager."
static int
_nm_auth_permission_from_string_cmp (gconstpointer a, gconstpointer b, gpointer user_data)
{
const NMClientPermission *const p = a;
const char *const needle = b;
const char *ss = nm_auth_permission_names_by_idx[*p - 1];
nm_assert (NM_STR_HAS_PREFIX (ss, AUTH_PERMISSION_PREFIX));
nm_assert (ss[NM_STRLEN (AUTH_PERMISSION_PREFIX)] != '\0');
return strcmp (&ss[NM_STRLEN (AUTH_PERMISSION_PREFIX)], needle);
}
NMClientPermission
nm_auth_permission_from_string (const char *str)
{
gssize idx;
if (!str)
return NM_CLIENT_PERMISSION_NONE;
if (!NM_STR_HAS_PREFIX (str, AUTH_PERMISSION_PREFIX))
return NM_CLIENT_PERMISSION_NONE;
idx = nm_utils_array_find_binary_search (nm_auth_permission_sorted,
sizeof (nm_auth_permission_sorted[0]),
G_N_ELEMENTS (nm_auth_permission_sorted),
&str[NM_STRLEN (AUTH_PERMISSION_PREFIX)],
_nm_auth_permission_from_string_cmp,
NULL);
if (idx < 0)
return NM_CLIENT_PERMISSION_NONE;
return nm_auth_permission_sorted[idx];
}

View file

@ -95,4 +95,12 @@ nm_setting_ip_config_get_addr_family (NMSettingIPConfig *s_ip)
* depends on other factors. */
#define NM_INFINIBAND_MAX_MTU ((guint) 65520)
/*****************************************************************************/
extern const char *const nm_auth_permission_names_by_idx[NM_CLIENT_PERMISSION_LAST];
extern const NMClientPermission nm_auth_permission_sorted[NM_CLIENT_PERMISSION_LAST];
const char *nm_auth_permission_to_string (NMClientPermission permission);
NMClientPermission nm_auth_permission_from_string (const char *str);
#endif /* __NM_LIBNM_SHARED_UTILS_H__ */