Fix wrong strncpy usage
[platform/upstream/at-spi2-core.git] / test / at_spi2_tool.c
index 7a7b9a5..95c726c 100644 (file)
@@ -4,9 +4,10 @@
 #include <string.h>
 #include <getopt.h>
 #include <stdbool.h>
+#include <assert.h>
 
 #define ERROR_STATE -1
-#define SAFE_BUFFER_SIZE 255
+#define SAFE_BUFFER_SIZE 2048
 #define CHECK_OUTPUT_WIDTH 42
 #define MINIMAL_MODULE_WIDTH 3
 #define ATTR_WIDTH 50
@@ -17,6 +18,8 @@
 #define DUMP 0
 #define CHECK 1
 #define FIRST_MATCH 2
+#define RELATION_TABLE_COLUMN_COUNT 7
+#define RELATION_COLUMN_WIDTH 20
 #define VERSION "1.1"
 
 static unsigned indent_width = 2;
@@ -79,12 +82,6 @@ typedef struct _Box_Size {
        char *width;
 } Box_Size;
 
-char *_strdup(const char *c)
-{
-       char *result = (char *)calloc(1, strlen(c) + 1);
-       return result ? strcpy(result, c) : result;
-}
-
 static char *_multiply_string(char c, unsigned number)
 {
        char *result = (char *)calloc(1, number + 1);
@@ -99,7 +96,7 @@ static char *_multiply_string(char c, unsigned number)
 
 static void _print_module_legend()
 {
-       printf("\n[[node role name],[attributes list],[x,y,width,height],[node name],[states list]]\n\n");
+       printf("\n[[node],[node role name],[attributes list],[x,y,width,height],[node name],[states list][relations]\n\n");
 }
 
 static void _print_atspi_states_legend()
@@ -236,33 +233,35 @@ static Box_Size *_get_box_size(AtspiAccessible *node)
        return box_size;
 }
 
-static char *_get_states(AtspiAccessible *node)
+static char *_get_states(AtspiAccessible *node, int length_limit)
 {
        AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
        GArray *states = atspi_state_set_get_states(node_state_set);
+       if (!states) return NULL;
        g_array_sort(states, _int_sort_function);
 
        AtspiStateType state_type;
        char *state_string = NULL;
 
-       for (int i = 0; states && (i < states->len); i++) {
+       for (int i = 0; i < states->len; i++) {
                state_type = g_array_index(states, AtspiStateType, i);
 
                char node_state_str[8];
-               sprintf(node_state_str, "(%d)", state_type);
+               snprintf(node_state_str, 8, "(%d)", state_type);
                _combine_strings(&state_string, node_state_str);
        }
 
        g_array_free(states, 0);
        g_object_unref(node_state_set);
 
+       _truncate_string(state_string, length_limit);
+
        return state_string;
 }
 
-static char *_get_attributes(AtspiAccessible *node)
+static char *_get_attributes(AtspiAccessible *node, int length_limit)
 {
-       GHashTable *attributes = NULL;
-       attributes = atspi_accessible_get_attributes(node, NULL);
+       GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
 
        if (!attributes)
                return NULL;
@@ -274,15 +273,15 @@ static char *_get_attributes(AtspiAccessible *node)
        char *result = NULL;
        while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
                char attributes_string[ATTR_WIDTH];
-               int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char*)attr_key, (char*)attr_value);
-               if (ret >= ATTR_WIDTH) {
-                       fprintf(stderr, "_get_attributes: generated string is too long. Buffer overflow");
-                       abort();
-               }
+               int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char *)attr_key, (char *)attr_value);
+               if (ret >= ATTR_WIDTH)
+                       fprintf(stderr, "\n_get_attributes: generated string is too long. Buffer overflow\n");
+
                _combine_strings(&result, attributes_string);
        }
        g_hash_table_unref(attributes);
 
+       _truncate_string(result, length_limit);
        return result;
 }
 
@@ -293,31 +292,33 @@ static char *_get_info(AtspiAccessible *node, int length_limit)
 
        char *node_name = atspi_accessible_get_name(node, NULL);
        char *node_role_name = atspi_accessible_get_role_name(node, NULL);
+       char *path = atspi_accessible_get_path(node, NULL);
 
-       char *attributes = _get_attributes(node);
-       _truncate_string(attributes, length_limit);
+       char *attributes = _get_attributes(node, length_limit);
        Box_Size *box_size = _get_box_size(node);
-       char *states = _get_states(node);
-       _truncate_string(states, length_limit);
+       char *states = _get_states(node, length_limit);
+
+       GArray *relations = atspi_accessible_get_relation_set(node, NULL);
 
        char result[SAFE_BUFFER_SIZE];
-       int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s,%s,%s,%s],[%s],[%s]]",
-                                                                               node_role_name,
-                                                                               attributes,
-                                                                               box_size->x,
-                                                                               box_size->y,
-                                                                               box_size->width,
-                                                                               box_size->height,
-                                                                               node_name,
-                                                                               states);
-
-       if (ret >= SAFE_BUFFER_SIZE) {
-               fprintf(stderr, "_get_info: generated string is too long. Buffer overflow");
-               abort();
-       }
+       int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s]]",
+                                               path,
+                                               node_role_name,
+                                               attributes,
+                                               box_size->x,
+                                               box_size->y,
+                                               box_size->width,
+                                               box_size->height,
+                                               node_name,
+                                               states,
+                                               (relations && relations->len) ? "*" : "");
+
+       if (ret >= SAFE_BUFFER_SIZE)
+               fprintf(stderr, "\n%s, %s %s: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
 
        free(node_name);
        free(node_role_name);
+       free(path);
        free(attributes);
        if (box_size) {
                free(box_size->width);
@@ -325,8 +326,9 @@ static char *_get_info(AtspiAccessible *node, int length_limit)
                free(box_size);
        }
        free(states);
+       g_array_free(relations, TRUE);
 
-       return _strdup(result);
+       return g_strdup(result);
 }
 
 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
@@ -339,11 +341,16 @@ static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessi
                char parent_status[NUMBER_WIDTH];
 
                if (parent == NULL)
-                       strcpy(parent_status, "_");
+                       strncpy(parent_status, "_", NUMBER_WIDTH);
                else
                        snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
 
-               snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%p,%p>]", parent_candidate_index, parent_status, parent_candidate, parent);
+               char *parent_path = atspi_accessible_get_path(parent, NULL);
+               char *parent_candidate_path = atspi_accessible_get_path(parent_candidate, NULL);
+               snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%s,%s>]", parent_candidate_index, parent_status,
+                               parent_candidate_path, parent_path);
+               free(parent_path);
+               free(parent_candidate_path);
 
        } else {
                snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
@@ -399,40 +406,128 @@ void _print_help()
        printf("\tat_spi2_tool -i1 -c starter\n");
        printf("\t  show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
 }
+
 void _print_version()
 {
        printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
 }
 
+static void _print_horizontal_line_in_relation_table() {
+       for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
+               for (int j = 0; j < RELATION_COLUMN_WIDTH; j++)
+                       printf("-");
+               printf("+");
+       }
+       printf("\n");
+}
+
+static void _print_legend_for_relation_table() {
+       char *table[] = {"OBJECT", "CONTROLLER_FOR", "CONTROLLED_BY", "FLOWS_TO", "FLOWS_FROM", "DESCRIBED_BY", "OTHER RELATION [ID]"};
+       assert(sizeof(table) / sizeof(table[0]) == RELATION_TABLE_COLUMN_COUNT);
+       for(int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++)
+               printf("%*s|", RELATION_COLUMN_WIDTH , table[i]);
+       printf("\n");
+       _print_horizontal_line_in_relation_table();
+}
+
+static char *_get_path_to_object_in_relation(AtspiRelation *relation) {
+       if (!relation)
+               return NULL;
+       gint last_index = atspi_relation_get_n_targets(relation) - 1;
+       AtspiAccessible *target = atspi_relation_get_target(relation, last_index);
+       return atspi_accessible_get_path(target, NULL);
+}
+
+static void _print_relations_for_object(AtspiAccessible *node) {
+       GArray *relations = atspi_accessible_get_relation_set(node, NULL);
+       if (!relations)
+               return;
+
+       if (relations->len) {
+               int size = RELATION_TABLE_COLUMN_COUNT * sizeof(char *);
+               char **table = malloc(size);
+               memset(table, 0, size);
+               table[0] = atspi_accessible_get_path(node, NULL);
+               char buf[8] = "";
+               for (int i = 0; i < relations->len; i++) {
+                       AtspiRelation *relation = g_array_index(relations, AtspiRelation *, i);
+                       AtspiRelationType type = atspi_relation_get_relation_type(relation);
+                       switch (type) {
+                               case ATSPI_RELATION_CONTROLLER_FOR:
+                                       table[1] = _get_path_to_object_in_relation(relation);
+                                       break;
+                               case ATSPI_RELATION_CONTROLLED_BY:
+                                       table[2] = _get_path_to_object_in_relation(relation);
+                                       break;
+                               case ATSPI_RELATION_FLOWS_TO:
+                                       table[3] = _get_path_to_object_in_relation(relation);
+                                       break;
+                               case ATSPI_RELATION_FLOWS_FROM:
+                                       table[4] = _get_path_to_object_in_relation(relation);
+                                       break;
+                               case ATSPI_RELATION_DESCRIBED_BY:
+                                       table[5] = _get_path_to_object_in_relation(relation);
+                                       break;
+                               default:
+                                       snprintf(buf, 8, " [%d]", type);
+                                       _combine_strings(&table[RELATION_TABLE_COLUMN_COUNT - 1], buf);
+                       }
+               }
+
+               for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
+                       printf("%*s|", RELATION_COLUMN_WIDTH, table[i] ? table[i] : "");
+                       free(table[i]);
+               }
+               free(table);
+
+               printf("\n");
+               _print_horizontal_line_in_relation_table();
+       }
+       g_array_free(relations, TRUE);
+
+       int count = atspi_accessible_get_child_count(node, NULL);
+       for (int i = 0; i < count; i++) {
+               AtspiAccessible *child = atspi_accessible_get_child_at_index(node, i, NULL);
+               if (child)
+                       _print_relations_for_object(child);
+       }
+}
+
 static void _atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool dump, bool check, bool first_match, int length_limit)
 {
        int count = atspi_accessible_get_child_count(desktop, NULL);
        bool app_name_matched = false;
        for (int i = 0; i < count; i++) {
                AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
-               if (child) {
-                       char *name = atspi_accessible_get_name(child, NULL);
+               if (child == NULL) {
+                       fprintf(stderr, "\n%s, %s %s: Null child occured. Results may be misleading.\n", __FILE__, __FUNCTION__, __LINE__);
+                       continue;
+               }
 
-                       if (!dump && !check) {
-                               printf("%s\n", name);
-                       }
+               char *name = atspi_accessible_get_name(child, NULL);
 
-                       if ((check || dump) && name && !strcmp(name, app_name)) {
-                               app_name_matched = true;
+               if (!dump && !check)
+                       printf("%s\n", name);
 
-                               _print_module_legend();
+               if ((check || dump) && name && !strcmp(name, app_name)) {
+                       app_name_matched = true;
 
-                               if (check)
-                                       _test_atspi_parent_child_relation(child, desktop, i);
+                       _print_module_legend();
 
-                               _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
+                       if (check)
+                               _test_atspi_parent_child_relation(child, desktop, i);
 
-                               if (first_match) {
-                                       free(name);
-                                       break;
-                               } else
-                                       printf("\n");
+                       _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
+
+                       if (first_match) {
+                               free(name);
+                               break;
+                       } else {
+                               printf("\n");
+                               _print_legend_for_relation_table();
+                               _print_relations_for_object(child);
                        }
+
                        free(name);
                }
        }