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

Subscribers

People subscribed via source and target branches

to all changes:
to status/vote changes: