| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "ws/cockpitcreds.h" |
| 9 |
|
| 10 |
#include "cockpitjson.h" |
| 11 |
#include "testlib/cockpittest.h" |
| 12 |
|
| 13 |
static void |
| 14 |
assert_all_zeros (GBytes *bytes) |
| 15 |
{ |
| 16 |
gsize size; |
| 17 |
const gchar *data = g_bytes_get_data (bytes, &size); |
| 18 |
|
| 19 |
for (gsize i = 0; i < size; i++) |
| 20 |
g_assert (data[i] == '\0'); |
| 21 |
} |
| 22 |
|
| 23 |
static void |
| 24 |
test_password (void) |
| 25 |
{ |
| 26 |
CockpitCreds *creds; |
| 27 |
GBytes *password; |
| 28 |
|
| 29 |
password = g_bytes_new_take (g_strdup ("password"), 8); |
| 30 |
creds = cockpit_creds_new ("test", |
| 31 |
COCKPIT_CRED_USER, "user", |
| 32 |
COCKPIT_CRED_PASSWORD, password, |
| 33 |
NULL); |
| 34 |
g_bytes_unref (password); |
| 35 |
|
| 36 |
g_assert (creds != NULL); |
| 37 |
|
| 38 |
g_assert_cmpstr ("user", ==, cockpit_creds_get_user (creds)); |
| 39 |
g_assert_cmpstr ("password", ==, g_bytes_get_data (cockpit_creds_get_password (creds), NULL)); |
| 40 |
g_assert_cmpstr ("test", ==, cockpit_creds_get_application (creds)); |
| 41 |
|
| 42 |
cockpit_creds_unref (creds); |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
test_set_password (void) |
| 47 |
{ |
| 48 |
CockpitCreds *creds; |
| 49 |
GBytes *password; |
| 50 |
GBytes *out; |
| 51 |
GBytes *two; |
| 52 |
|
| 53 |
password = g_bytes_new_take (g_strdup ("password"), 8); |
| 54 |
creds = cockpit_creds_new ("app", COCKPIT_CRED_PASSWORD, password, NULL); |
| 55 |
g_bytes_unref (password); |
| 56 |
|
| 57 |
g_assert (creds != NULL); |
| 58 |
|
| 59 |
out = cockpit_creds_get_password (creds); |
| 60 |
g_assert (out != NULL); |
| 61 |
g_assert_cmpstr ("password", ==, g_bytes_get_data (out, NULL)); |
| 62 |
|
| 63 |
password = g_bytes_new_take (g_strdup ("second"), 6); |
| 64 |
cockpit_creds_set_password (creds, password); |
| 65 |
g_bytes_unref (password); |
| 66 |
|
| 67 |
two = cockpit_creds_get_password (creds); |
| 68 |
g_assert (two != NULL); |
| 69 |
g_assert_cmpstr ("second", ==, g_bytes_get_data (two, NULL)); |
| 70 |
|
| 71 |
cockpit_creds_set_password (creds, NULL); |
| 72 |
g_assert (NULL == cockpit_creds_get_password (creds)); |
| 73 |
|
| 74 |
/* Still hold references to all old passwords, but they are cleared */ |
| 75 |
assert_all_zeros (out); |
| 76 |
assert_all_zeros (two); |
| 77 |
|
| 78 |
cockpit_creds_unref (creds); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
test_poison (void) |
| 83 |
{ |
| 84 |
CockpitCreds *creds; |
| 85 |
GBytes *password; |
| 86 |
GBytes *out; |
| 87 |
|
| 88 |
password = g_bytes_new_take (g_strdup ("password"), 8); |
| 89 |
creds = cockpit_creds_new ("app", COCKPIT_CRED_PASSWORD, password, NULL); |
| 90 |
g_bytes_unref (password); |
| 91 |
|
| 92 |
g_assert (creds != NULL); |
| 93 |
|
| 94 |
g_assert_cmpstr ("password", ==, g_bytes_get_data (cockpit_creds_get_password (creds), NULL)); |
| 95 |
g_assert_cmpstr ("app", ==, cockpit_creds_get_application (creds)); |
| 96 |
|
| 97 |
out = cockpit_creds_get_password (creds); |
| 98 |
cockpit_creds_poison (creds); |
| 99 |
|
| 100 |
g_assert (NULL == cockpit_creds_get_password (creds)); |
| 101 |
|
| 102 |
password = g_bytes_new_take (g_strdup ("second"), 6); |
| 103 |
cockpit_creds_set_password (creds, password); |
| 104 |
g_bytes_unref (password); |
| 105 |
|
| 106 |
/* Even though we set a new password, still NULL */ |
| 107 |
g_assert (NULL == cockpit_creds_get_password (creds)); |
| 108 |
assert_all_zeros (out); |
| 109 |
|
| 110 |
cockpit_creds_unref (creds); |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
test_rhost (void) |
| 115 |
{ |
| 116 |
CockpitCreds *creds; |
| 117 |
|
| 118 |
creds = cockpit_creds_new ("app", COCKPIT_CRED_RHOST, "remote", NULL); |
| 119 |
g_assert (creds != NULL); |
| 120 |
|
| 121 |
g_assert_cmpstr ("remote", ==, cockpit_creds_get_rhost (creds)); |
| 122 |
g_assert_cmpstr ("app", ==, cockpit_creds_get_application (creds)); |
| 123 |
|
| 124 |
cockpit_creds_unref (creds); |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
test_multiple (void) |
| 129 |
{ |
| 130 |
CockpitCreds *creds; |
| 131 |
GBytes *password; |
| 132 |
|
| 133 |
password = g_bytes_new_take (g_strdup ("password"), 8); |
| 134 |
creds = cockpit_creds_new ("app", |
| 135 |
COCKPIT_CRED_PASSWORD, password, |
| 136 |
COCKPIT_CRED_RHOST, "remote", |
| 137 |
NULL); |
| 138 |
g_assert (creds != NULL); |
| 139 |
|
| 140 |
g_assert_cmpstr ("remote", ==, cockpit_creds_get_rhost (creds)); |
| 141 |
g_assert_cmpstr ("password", ==, g_bytes_get_data (cockpit_creds_get_password (creds), NULL)); |
| 142 |
g_assert_cmpstr ("app", ==, cockpit_creds_get_application (creds)); |
| 143 |
|
| 144 |
g_bytes_unref (password); |
| 145 |
cockpit_creds_unref (creds); |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
test_login_data (void) |
| 150 |
{ |
| 151 |
JsonObject *object; |
| 152 |
const gchar *valid = "{ \"login-data\" : { \"login\": \"data\" } }"; |
| 153 |
CockpitCreds *creds; |
| 154 |
|
| 155 |
creds = cockpit_creds_new ("app", NULL); |
| 156 |
g_assert (cockpit_creds_get_login_data (creds) == NULL); |
| 157 |
|
| 158 |
object = cockpit_json_parse_object (valid, -1, NULL); |
| 159 |
cockpit_creds_set_login_data (creds, object); |
| 160 |
json_object_unref (object); |
| 161 |
cockpit_assert_json_eq (cockpit_creds_get_login_data (creds), valid); |
| 162 |
|
| 163 |
object = cockpit_json_parse_object (valid, -1, NULL); |
| 164 |
cockpit_creds_set_login_data (creds, object); |
| 165 |
json_object_unref (object); |
| 166 |
cockpit_assert_json_eq (cockpit_creds_get_login_data (creds), valid); |
| 167 |
|
| 168 |
cockpit_creds_set_login_data (creds, NULL); |
| 169 |
g_assert (cockpit_creds_get_login_data (creds) == NULL); |
| 170 |
cockpit_creds_unref (creds); |
| 171 |
} |
| 172 |
|
| 173 |
int |
| 174 |
main (int argc, |
| 175 |
char *argv[]) |
| 176 |
{ |
| 177 |
cockpit_test_init (&argc, &argv); |
| 178 |
|
| 179 |
g_test_add_func ("/creds/basic-password", test_password); |
| 180 |
g_test_add_func ("/creds/set-password", test_set_password); |
| 181 |
g_test_add_func ("/creds/poison", test_poison); |
| 182 |
g_test_add_func ("/creds/rhost", test_rhost); |
| 183 |
g_test_add_func ("/creds/multiple", test_multiple); |
| 184 |
g_test_add_func ("/creds/login-data", test_login_data); |
| 185 |
|
| 186 |
return g_test_run (); |
| 187 |
} |