diff options
Diffstat (limited to 'src/testing/testing_api_cmd_hello_world_birth.c')
-rw-r--r-- | src/testing/testing_api_cmd_hello_world_birth.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_hello_world_birth.c b/src/testing/testing_api_cmd_hello_world_birth.c new file mode 100644 index 000000000..a3157b6de --- /dev/null +++ b/src/testing/testing_api_cmd_hello_world_birth.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file testing/testing_api_cmd_hello_world.c | ||
23 | * @brief implementation of a hello world command. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | |||
30 | struct HelloWorldBirthState | ||
31 | { | ||
32 | struct GNUNET_TIME_Absolute *date; | ||
33 | char *what_am_i; | ||
34 | }; | ||
35 | |||
36 | /** | ||
37 | * | ||
38 | * | ||
39 | * @param cls closure | ||
40 | * @param cmd current CMD being cleaned up. | ||
41 | */ | ||
42 | static void | ||
43 | hello_world_birth_cleanup (void *cls, | ||
44 | const struct GNUNET_TESTING_Command *cmd) | ||
45 | { | ||
46 | struct HelloWorldBirthState *hbs = cls; | ||
47 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
48 | "Finished birth of %s", | ||
49 | hbs->what_am_i); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * | ||
54 | * | ||
55 | * @param cls closure. | ||
56 | * @param[out] ret result | ||
57 | * @param trait name of the trait. | ||
58 | * @param index index number of the object to offer. | ||
59 | * @return #GNUNET_OK on success. | ||
60 | */ | ||
61 | static int | ||
62 | hello_world_birth_traits (void *cls, | ||
63 | const void **ret, | ||
64 | const char *trait, | ||
65 | unsigned int index) | ||
66 | { | ||
67 | struct HelloWorldBirthState *hbs = cls; | ||
68 | struct GNUNET_TESTING_Trait traits[] = { | ||
69 | { | ||
70 | .index = 0, | ||
71 | .trait_name = "what_am_i", | ||
72 | .ptr = (const void *) hbs->what_am_i, | ||
73 | }, | ||
74 | GNUNET_TESTING_trait_end () | ||
75 | }; | ||
76 | |||
77 | return GNUNET_TESTING_get_trait (traits, | ||
78 | ret, | ||
79 | trait, | ||
80 | index); | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Run the "hello world" CMD. | ||
85 | * | ||
86 | * @param cls closure. | ||
87 | * @param cmd CMD being run. | ||
88 | * @param is interpreter state. | ||
89 | */ | ||
90 | static void | ||
91 | hello_world_birth_run (void *cls, | ||
92 | const struct GNUNET_TESTING_Command *cmd, | ||
93 | struct GNUNET_TESTING_Interpreter *is) | ||
94 | { | ||
95 | struct HelloWorldBirthState *hbs = cls; | ||
96 | struct GNUNET_TIME_Relative relativ; | ||
97 | |||
98 | relativ = GNUNET_TIME_absolute_get_difference (*hbs->date, | ||
99 | GNUNET_TIME_absolute_get ()); | ||
100 | |||
101 | if (0 == relativ.rel_value_us % 10) | ||
102 | { | ||
103 | hbs->what_am_i = "Hello World, I am a creature!"; | ||
104 | } | ||
105 | else if (0 == relativ.rel_value_us % 2) | ||
106 | { | ||
107 | hbs->what_am_i = "Hello World, I am a girl!"; | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | hbs->what_am_i = "Hello World, I am a boy!"; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Offer data from trait | ||
117 | * | ||
118 | * @param cmd command to extract the url from. | ||
119 | * @param pt which url is to be picked, in case | ||
120 | * multiple are offered. | ||
121 | * @param[out] url where to write the url. | ||
122 | * @return #GNUNET_OK on success. | ||
123 | */ | ||
124 | int | ||
125 | GNUNET_TESTING_get_trait_what_am_i (const struct | ||
126 | GNUNET_TESTING_Command *cmd, | ||
127 | char *what_am_i) | ||
128 | { | ||
129 | return cmd->traits (cmd->cls, | ||
130 | (const void **) what_am_i, | ||
131 | "what_am_i", | ||
132 | (unsigned int) 0); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Create command. | ||
137 | * | ||
138 | * @param label name for command. | ||
139 | * @param now when the command was started. | ||
140 | * @return command. | ||
141 | */ | ||
142 | struct GNUNET_TESTING_Command | ||
143 | GNUNET_TESTING_cmd_hello_world_birth (const char *label, | ||
144 | struct GNUNET_TIME_Absolute *now) | ||
145 | { | ||
146 | struct HelloWorldBirthState *hbs; | ||
147 | |||
148 | hbs = GNUNET_new (struct HelloWorldBirthState); | ||
149 | hbs->date = now; | ||
150 | |||
151 | struct GNUNET_TESTING_Command cmd = { | ||
152 | .cls = hbs, | ||
153 | .label = label, | ||
154 | .run = &hello_world_birth_run, | ||
155 | .cleanup = &hello_world_birth_cleanup, | ||
156 | .traits = &hello_world_birth_traits | ||
157 | }; | ||
158 | |||
159 | return cmd; | ||
160 | } | ||