Skip to content

Commit

Permalink
Fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Nov 5, 2024
1 parent 011d61f commit 7542297
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions misc/ipc_openvas_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Ensure (ipc_openvas, ipc_data_from_json_ua_ok)
gchar *ua = "localhost";

// Preapre data to be sent
data_s = g_malloc0 (sizeof (ipc_data_t *));
data_s = g_malloc0 (sizeof (ipc_data_t));
data_s = ipc_data_type_from_user_agent (ua, strlen (ua));

const char *json = ipc_data_to_json (data_s);
Expand All @@ -48,7 +48,7 @@ Ensure (ipc_openvas, ipc_data_from_json_hostname_ok)
gchar *hns = "TLS certificate";

// Preapre data to be sent
data_s = g_malloc0 (sizeof (ipc_data_t *));
data_s = g_malloc0 (sizeof (ipc_data_t));
data_s = ipc_data_type_from_hostname (hns, strlen (hns), hn, strlen (hn));

const char *json = ipc_data_to_json (data_s);
Expand Down Expand Up @@ -93,7 +93,7 @@ Ensure (ipc_openvas, ipc_data_from_json_parse_error)
"[en] (X11, U; Greenbone OS 22.04.4)\"}{\"type\":");

// Read received data
data_r = g_malloc0 (sizeof (ipc_data_t *));
data_r = g_malloc0 (sizeof (ipc_data_t));
data_r = ipc_data_from_json (json_fake, strlen (json_fake));
assert_that (ipc_get_hostname_from_data (data_r), is_null);
assert_that (ipc_get_hostname_source_from_data (data_r), is_null);
Expand All @@ -104,24 +104,48 @@ Ensure (ipc_openvas, ipc_data_from_json_parse_many_objects)
{
ipc_data_t *data_r = NULL;
char *json_fake = NULL;
int len = 0;
int pos = 0;

// malformed json string
// json string with more than one objects
json_fake =
g_strdup ("{\"type\":1,\"source\":\"TLS "
"certificate\",\"hostname\":\"localhost\"}{\"type\":2,\"user-"
"agent\":\"Mozilla/5.0 [en] (X11, U; Greenbone OS "
"22.04.4)\"}");

// Read received data
data_r = g_malloc0 (sizeof (ipc_data_t *));
data_r = ipc_data_from_json (json_fake, strlen (json_fake));

assert_that (ipc_get_hostname_from_data (data_r),
is_equal_to_string ("localhost"));
assert_that (ipc_get_hostname_source_from_data (data_r),
is_equal_to_string ("TLS certificate"));

ipc_data_destroy (&data_r);
for (int j = 0; json_fake[j] != '\0'; j++)
{
if (json_fake[j] == '}')
{
gchar *message = NULL;
len = j - pos + 1;

message = g_malloc0 (sizeof (gchar) * (len + 1));
memcpy (message, &json_fake[pos], len);
printf ("\n\nel mensaje %s\n\n", message);
pos = j + 1;
len = 0;
data_r = g_malloc0 (sizeof (ipc_data_t));
data_r = ipc_data_from_json (message, strlen (message));
if (ipc_get_data_type_from_data (data_r) == IPC_DT_HOSTNAME)
{
assert_that (ipc_get_hostname_from_data (data_r),
is_equal_to_string ("localhost"));
assert_that (ipc_get_hostname_source_from_data (data_r),
is_equal_to_string ("TLS certificate"));

ipc_data_destroy (&data_r);
}
else
{
assert_that (ipc_get_user_agent_from_data (data_r),
is_equal_to_string ("Mozilla/5.0 [en] (X11, U; Greenbone OS 22.04.4)"));
ipc_data_destroy (&data_r);
}
g_free (message);
}
}
assert_that (data_r, is_null);
}

Expand Down

0 comments on commit 7542297

Please sign in to comment.