Merge lp:~chipaca/whoopsie/failure-flags into lp:whoopsie

Proposed by John Lenton
Status: Merged
Approved by: Evan
Approved revision: 602
Merged at revision: 599
Proposed branch: lp:~chipaca/whoopsie/failure-flags
Merge into: lp:whoopsie
Diff against target: 290 lines (+170/-13)
3 files modified
src/identifier.c (+37/-8)
src/identifier.h (+7/-0)
src/tests/test_identifier.c (+126/-5)
To merge this branch: bzr merge lp:~chipaca/whoopsie/failure-flags
Reviewer Review Type Date Requested Status
Evan (community) Approve
Review via email: mp+219193@code.launchpad.net

Commit message

Fixes lp:1311571.
Added failure flags for easier testing.

Description of the change

Added a couple of failure flags to improve testing.

Fixed a bug (lp:1311571) while in there.

To post a comment you must log in.
Revision history for this message
Evan (ev) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/identifier.c'
--- src/identifier.c 2013-08-14 08:52:35 +0000
+++ src/identifier.c 2014-05-12 13:58:41 +0000
@@ -11,6 +11,9 @@
1111
12#include "identifier.h"12#include "identifier.h"
1313
14static gint fail_next_get_mac = 0;
15static gint fail_next_get_uuid = 0;
16
14void17void
15whoopsie_hex_to_char (char* buf, const char *str, int len)18whoopsie_hex_to_char (char* buf, const char *str, int len)
16{19{
@@ -41,6 +44,12 @@
4144
42 g_return_if_fail (res);45 g_return_if_fail (res);
4346
47 if (g_atomic_int_compare_and_exchange (&fail_next_get_mac, WHOOPSIE_FAIL_GENERIC, 0)) {
48 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), WHOOPSIE_FAILED_BY_REQUEST,
49 "Failed by tester request");
50 return;
51 }
52
44 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);53 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
45 if (sock == -1) {54 if (sock == -1) {
46 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,55 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
@@ -59,13 +68,15 @@
5968
60 it = ifc.ifc_req;69 it = ifc.ifc_req;
6170
62 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; it++) {71 if (!g_atomic_int_compare_and_exchange (&fail_next_get_mac, WHOOPSIE_FAIL_MAC_SIOCGIFFLAGS, 0)) {
63 strcpy(ifr.ifr_name, it->ifr_name);72 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; it++) {
64 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {73 strcpy(ifr.ifr_name, it->ifr_name);
65 if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback74 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
66 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {75 if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
67 success = TRUE;76 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
68 break;77 success = TRUE;
78 break;
79 }
69 }80 }
70 }81 }
71 }82 }
@@ -73,8 +84,11 @@
7384
74 close (sock);85 close (sock);
7586
76 if (!success)87 if (!success) {
88 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
89 "Unable to find a hardware address");
77 return;90 return;
91 }
7892
79 *res = g_malloc (18);93 *res = g_malloc (18);
80 sprintf (*res, "%02x:%02x:%02x:%02x:%02x:%02x",94 sprintf (*res, "%02x:%02x:%02x:%02x:%02x:%02x",
@@ -93,6 +107,12 @@
93107
94 g_return_if_fail (res);108 g_return_if_fail (res);
95109
110 if (g_atomic_int_compare_and_exchange (&fail_next_get_uuid, WHOOPSIE_FAIL_GENERIC, 0)) {
111 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), WHOOPSIE_FAILED_BY_REQUEST,
112 "Failed by tester request");
113 return;
114 }
115
96 fp = open ("/sys/class/dmi/id/product_uuid", O_RDONLY);116 fp = open ("/sys/class/dmi/id/product_uuid", O_RDONLY);
97 if (fp < 0) {117 if (fp < 0) {
98 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,118 g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
@@ -176,3 +196,12 @@
176 g_free (identifier);196 g_free (identifier);
177}197}
178198
199void whoopsie_identifier_fail_next_get_mac (gint how)
200{
201 g_atomic_int_set (&fail_next_get_mac, how);
202}
203
204void whoopsie_identifier_fail_next_get_uuid ()
205{
206 g_atomic_int_set (&fail_next_get_uuid, 1);
207}
179208
=== modified file 'src/identifier.h'
--- src/identifier.h 2012-05-26 18:27:50 +0000
+++ src/identifier.h 2014-05-12 13:58:41 +0000
@@ -4,3 +4,10 @@
4void whoopsie_identifier_sha512 (char* source, char* res, GError** error);4void whoopsie_identifier_sha512 (char* source, char* res, GError** error);
5void whoopsie_hex_to_char (char* buf, const char *str, int len);5void whoopsie_hex_to_char (char* buf, const char *str, int len);
6#define HASHLEN 1286#define HASHLEN 128
7
8/* for testing: */
9void whoopsie_identifier_fail_next_get_mac (gint how) __attribute__ ((__warning__ ("Only for testing")));
10void whoopsie_identifier_fail_next_get_uuid () __attribute__ ((__warning__ ("Only for testing")));
11#define WHOOPSIE_FAIL_GENERIC 1
12#define WHOOPSIE_FAIL_MAC_SIOCGIFFLAGS 2
13#define WHOOPSIE_FAILED_BY_REQUEST 42
714
=== modified file 'src/tests/test_identifier.c'
--- src/tests/test_identifier.c 2013-08-16 09:44:43 +0000
+++ src/tests/test_identifier.c 2014-05-12 13:58:41 +0000
@@ -28,6 +28,12 @@
2828
29#include "../src/identifier.h"29#include "../src/identifier.h"
3030
31static gboolean
32is_root (void)
33{
34 return !(getuid () || g_getenv ("FAKEROOTKEY"));
35}
36
31static void37static void
32test_hex_to_char (void)38test_hex_to_char (void)
33{39{
@@ -100,13 +106,53 @@
100 106
101 g_free (res);107 g_free (res);
102}108}
109
110static void
111test_get_mac_address_fail_when_told_to (void)
112{
113 char* res = NULL;
114 GError* error = NULL;
115
116 /* check it works before */
117 whoopsie_identifier_get_mac_address (&res, &error);
118 g_assert (res != NULL);
119 g_assert (error == NULL);
120 g_free (res);
121 res = NULL;
122
123 /* set the flag */
124 whoopsie_identifier_fail_next_get_mac (WHOOPSIE_FAIL_GENERIC);
125 whoopsie_identifier_get_mac_address (&res, &error);
126 g_assert (res == NULL);
127 g_assert (error != NULL);
128 g_assert_error (error, g_quark_from_static_string ("whoopsie-quark"), WHOOPSIE_FAILED_BY_REQUEST);
129 error = NULL;
130
131 /* flag is cleared */
132 whoopsie_identifier_get_mac_address (&res, &error);
133 g_assert (res != NULL);
134 g_assert (error == NULL);
135 g_free (res);
136}
137
138static void
139test_get_mac_address_fail_when_no_siocgifflags (void)
140{
141 char* res = NULL;
142 GError* error = NULL;
143 whoopsie_identifier_fail_next_get_mac (WHOOPSIE_FAIL_MAC_SIOCGIFFLAGS);
144 whoopsie_identifier_get_mac_address (&res, &error);
145 g_assert (error != NULL);
146 g_assert (res == NULL);
147}
148
103static void149static void
104test_get_system_uuid (void)150test_get_system_uuid (void)
105{151{
106 /* DEADBEEF-1234-1234-1234-DEADBEEF1234 */152 /* DEADBEEF-1234-1234-1234-DEADBEEF1234 */
107 char* res = NULL;153 char* res = NULL;
108 whoopsie_identifier_get_system_uuid (&res, NULL);154 whoopsie_identifier_get_system_uuid (&res, NULL);
109 if (getuid () != 0) {155 if (!is_root ()) {
110 g_print ("Need root to run this complete test: ");156 g_print ("Need root to run this complete test: ");
111 goto out;157 goto out;
112 }158 }
@@ -134,6 +180,40 @@
134}180}
135181
136static void182static void
183test_get_system_uuid_fail_when_told_to (void)
184{
185 char* res = NULL;
186 GError* error = NULL;
187
188 if (!is_root ()) {
189 g_print ("Need root to run this complete test: ");
190 } else {
191 /* check it works before */
192 whoopsie_identifier_get_system_uuid (&res, &error);
193 g_assert (res != NULL);
194 g_assert (error == NULL);
195 g_free (res);
196 res = NULL;
197 }
198
199 /* set the flag */
200 whoopsie_identifier_fail_next_get_uuid ();
201 whoopsie_identifier_get_system_uuid (&res, &error);
202 g_assert (res == NULL);
203 g_assert (error != NULL);
204 g_assert_error (error, g_quark_from_static_string ("whoopsie-quark"), WHOOPSIE_FAILED_BY_REQUEST);
205 error = NULL;
206
207 if (is_root ()) {
208 /* flag is cleared */
209 whoopsie_identifier_get_system_uuid (&res, &error);
210 g_assert (res != NULL);
211 g_assert (error == NULL);
212 g_free (res);
213 }
214}
215
216static void
137test_sha512 (void)217test_sha512 (void)
138{218{
139 char res[HASHLEN + 1] = {0};219 char res[HASHLEN + 1] = {0};
@@ -154,7 +234,7 @@
154 char hashed[HASHLEN + 1];234 char hashed[HASHLEN + 1];
155235
156 whoopsie_identifier_generate (&result, NULL);236 whoopsie_identifier_generate (&result, NULL);
157 if (getuid () != 0) {237 if (!is_root ()) {
158 whoopsie_identifier_get_mac_address (&expected, NULL);238 whoopsie_identifier_get_mac_address (&expected, NULL);
159 if (!expected)239 if (!expected)
160 whoopsie_identifier_get_system_uuid (&expected, NULL);240 whoopsie_identifier_get_system_uuid (&expected, NULL);
@@ -168,6 +248,45 @@
168 g_free (result);248 g_free (result);
169 g_free (expected);249 g_free (expected);
170}250}
251
252static void
253test_generate_fails_on_complete_failure (void)
254{
255 char* res = NULL;
256 GError* error = NULL;
257
258 /* check we succeed on uuid failure */
259 whoopsie_identifier_fail_next_get_uuid ();
260 whoopsie_identifier_generate (&res, &error);
261 g_assert (res != NULL);
262 g_assert (error == NULL);
263
264 g_free (res);
265 res = NULL;
266
267 if (g_file_test ("/sys/class/dmi/id/product_uuid", G_FILE_TEST_EXISTS)) {
268 /* the system has a UUID */
269 if (!is_root ()) {
270 g_print ("Need to be root to run this complete test: ");
271 } else {
272 /* test we succeed if we can't get a mac */
273 whoopsie_identifier_fail_next_get_mac (WHOOPSIE_FAIL_GENERIC);
274 whoopsie_identifier_generate (&res, &error);
275 g_assert (res != NULL);
276 g_assert (error == NULL);
277 g_free (res);
278 res = NULL;
279 }
280 } else {
281 g_print ("(skipping UUID test on this system) ");
282 }
283
284 whoopsie_identifier_fail_next_get_mac (WHOOPSIE_FAIL_GENERIC);
285 whoopsie_identifier_fail_next_get_uuid ();
286 whoopsie_identifier_generate (&res, &error);
287 g_assert (res == NULL);
288 g_assert (error != NULL);
289}
171int290int
172main (int argc, char** argv)291main (int argc, char** argv)
173{292{
@@ -177,13 +296,15 @@
177#endif296#endif
178 g_test_init (&argc, &argv, NULL);297 g_test_init (&argc, &argv, NULL);
179298
180 /* This wont work when running under fakeroot. */299 g_test_add_func ("/whoopsie/get-system-uuid", test_get_system_uuid);
181 if (!g_getenv ("FAKEROOTKEY"))300 g_test_add_func ("/whoopsie/get-uuid-fails-when-told", test_get_system_uuid_fail_when_told_to);
182 g_test_add_func ("/whoopsie/get-system-uuid", test_get_system_uuid);
183 g_test_add_func ("/whoopsie/hex-to-char", test_hex_to_char);301 g_test_add_func ("/whoopsie/hex-to-char", test_hex_to_char);
184 g_test_add_func ("/whoopsie/get-mac-address", test_get_mac_address);302 g_test_add_func ("/whoopsie/get-mac-address", test_get_mac_address);
303 g_test_add_func ("/whoopsie/get-mac-fails-when-told", test_get_mac_address_fail_when_told_to);
304 g_test_add_func ("/whoopsie/get-mac-fails-when-no-siocgifflags", test_get_mac_address_fail_when_no_siocgifflags);
185 g_test_add_func ("/whoopsie/sha512", test_sha512);305 g_test_add_func ("/whoopsie/sha512", test_sha512);
186 g_test_add_func ("/whoopsie/identifier-generate", test_identifier_generate);306 g_test_add_func ("/whoopsie/identifier-generate", test_identifier_generate);
307 g_test_add_func ("/whoopsie/identifier-generate-fails-on-failure", test_generate_fails_on_complete_failure);
187308
188 /* Do not consider warnings to be fatal. */309 /* Do not consider warnings to be fatal. */
189 g_log_set_always_fatal (G_LOG_FATAL_MASK);310 g_log_set_always_fatal (G_LOG_FATAL_MASK);

Subscribers

People subscribed via source and target branches

to all changes:
to status/vote changes: