1 /* Make sure serializing a dict (possibly repeatedly) does not corrupt either
2 type lookup or the string content of the dict. */
3
4 #include <ctf-api.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 int
main(int argc,char * argv[])9 main (int argc, char *argv[])
10 {
11 ctf_dict_t *fp;
12 ctf_id_t zygal, autoschediastic;
13 ctf_snapshot_id_t snap;
14 unsigned char *foo;
15 size_t foo_size;
16 int err;
17 char name[64];
18
19 /* Adding things after serialization should not corrupt names created before
20 serialization. */
21
22 if ((fp = ctf_create (&err)) == NULL)
23 goto create_err;
24
25 if ((zygal = ctf_add_struct (fp, CTF_ADD_ROOT, "zygal")) == CTF_ERR)
26 goto add_err;
27
28 if ((foo = ctf_write_mem (fp, &foo_size, 4096)) == NULL)
29 goto write_err;
30 free (foo);
31
32 if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
33 fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
34 else
35 printf ("zygal's name is %s\n", name);
36
37 if ((autoschediastic = ctf_add_enum (fp, CTF_ADD_ROOT, "autoschediastic")) == CTF_ERR)
38 goto add_err;
39
40 if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
41 fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
42 else
43 printf ("zygal's name is %s\n", name);
44
45 /* Serializing again should not corrupt names either. */
46 if ((foo = ctf_write_mem (fp, &foo_size, 4096)) == NULL)
47 goto write_err;
48 free (foo);
49
50 if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
51 fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
52 else
53 printf ("zygal's name is %s\n", name);
54
55 /* Add another new name, roll back, and make sure the strings are
56 uncorrupted. */
57
58 snap = ctf_snapshot (fp);
59 if (ctf_add_enumerator (fp, autoschediastic, "aichmophobia", 0) < 0)
60 goto add_err;
61
62 if (ctf_rollback (fp, snap) < 0)
63 goto roll_err;
64
65 if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
66 fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
67 else
68 printf ("zygal's name is %s after first rollback\n", name);
69
70 if (ctf_type_name (fp, autoschediastic, name, sizeof (name)) == NULL)
71 fprintf (stderr, "Can't get name of autoschediastic: %s\n", ctf_errmsg (ctf_errno (fp)));
72 else
73 printf ("autoschediastic's name is %s after first rollback\n", name);
74
75 ctf_dict_close (fp);
76 return 0;
77
78 create_err:
79 fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
80 return 1;
81 add_err:
82 fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (fp)));
83 return 1;
84 write_err:
85 fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (fp)));
86 return 1;
87 roll_err:
88 fprintf (stderr, "Cannot roll back: %s\n", ctf_errmsg (ctf_errno (fp)));
89 return 1;
90 }
91