1 /*
2  * Copyright © 2010-2020 Inria.  All rights reserved.
3  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
4  * See COPYING in top-level directory.
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <assert.h>
10 
11 #include "hwloc.h"
12 
one_test(void)13 static int one_test(void)
14 {
15   hwloc_topology_t topology;
16   int size1, size2;
17   char *buf1, *copy1, *buf2;
18   int err = 0, i;
19   char s[129];
20   char t[10];
21 
22   for(i=0; i<128; i++)
23     s[i] = ' ';
24   s[128] = 0;
25   for(i=32; i<=126; i++)
26     s[i] = i;
27   s['\t'] = '\t';
28   s['\n'] = '\n';
29   s['\r'] = '\r';
30 
31   t[0] = 'x';
32   for(i=1; i<=7; i++)
33     t[i] = i;
34   t[8] = 'y';
35   t[9] = '\0';
36 
37   hwloc_topology_init(&topology);
38   hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
39   hwloc_topology_load(topology);
40   assert(hwloc_topology_is_thissystem(topology));
41   hwloc_obj_add_info(hwloc_get_root_obj(topology), "UglyString", s);
42   hwloc_obj_add_info(hwloc_get_root_obj(topology), "UberUglyString", t);
43   hwloc_topology_export_xmlbuffer(topology, &buf1, &size1, 0);
44   hwloc_topology_destroy(topology);
45   printf("exported to buffer %p length %d\n", buf1, size1);
46 
47   /* copy the returned buffer to a newly malloc'd one
48    * to check that the returned length is correct (contains ending 0, etc).
49    */
50   copy1 = malloc(size1);
51   assert(copy1);
52   memcpy(copy1, buf1, size1);
53 
54   hwloc_topology_init(&topology);
55   hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
56   assert(!hwloc_topology_set_xmlbuffer(topology, copy1, size1));
57   hwloc_topology_load(topology);
58   assert(!hwloc_topology_is_thissystem(topology));
59   if (strcmp(hwloc_obj_get_info_by_name(hwloc_get_root_obj(topology), "UglyString"), s))
60     assert(0);
61   if (strcmp(hwloc_obj_get_info_by_name(hwloc_get_root_obj(topology), "UberUglyString"), "xy"))
62     assert(0);
63   hwloc_topology_export_xmlbuffer(topology, &buf2, &size2, 0);
64   printf("re-exported to buffer %p length %d\n", buf2, size2);
65 
66   if (strcmp(buf1, buf2)) {
67     printf("### First exported buffer is:\n");
68     printf("%s", buf1);
69     printf("### End of first export buffer\n");
70     printf("### Second exported buffer is:\n");
71     printf("%s", buf2);
72     printf("### End of second export buffer\n");
73     err = 1;
74   }
75 
76   hwloc_free_xmlbuffer(topology, buf1);
77   hwloc_free_xmlbuffer(topology, buf2);
78   free(copy1);
79 
80   hwloc_topology_destroy(topology);
81 
82   return err;
83 }
84 
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87   int err;
88 
89   if (argc < 3) {
90     fprintf(stderr, "Need 0 or 1 twice as arguments for enabling/disabling libxml import and export\n");
91     fprintf(stderr, "For instance `xmlbuffer 0 1' enables libxml for export only\n");
92     fprintf(stderr, "Those arguments are passed by wrapper.sh during make check\n");
93     exit(EXIT_FAILURE);
94   }
95 
96   putenv((char *) "HWLOC_LIBXML_CLEANUP=1");
97 
98   if (atoi(argv[1])) {
99     putenv((char *) "HWLOC_LIBXML_IMPORT=1");
100     printf("import=libxml   ");
101   } else {
102     putenv((char *) "HWLOC_LIBXML_IMPORT=0");
103     printf("import=nolibxml ");
104   }
105 
106   if (atoi(argv[2])) {
107     putenv((char *) "HWLOC_LIBXML_EXPORT=1");
108     printf("export=libxml\n");
109   } else {
110     putenv((char *) "HWLOC_LIBXML_EXPORT=0");
111     printf("export=nolibxml\n");
112   }
113 
114   err = one_test();
115   if (err < 0)
116     return err;
117 
118   return 0;
119 }
120