From: Vincent Bernat Date: Tue, 7 Jul 2009 07:01:45 +0000 (+0200) Subject: fail_* macros do not break execution flow. X-Git-Tag: 0.5.0~49 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9360858c8b83279fbe1bfaa43b714f17b3d2bf5b;p=thirdparty%2Flldpd.git fail_* macros do not break execution flow. Unlike what happen with unit tests framework in other languages, fail_* macro does not jump to next test. Therefore, we have to break flow ourselves. --- diff --git a/tests/check_lldp.c b/tests/check_lldp.c index e23c4cc4..810cebf3 100644 --- a/tests/check_lldp.c +++ b/tests/check_lldp.c @@ -57,15 +57,24 @@ pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware, hdr.ts_usec = 0; hdr.incl_len = hdr.orig_len = size; n = write(dump, &hdr, sizeof(hdr)); - fail_unless(n != -1, "unable to write pcap record header to %s", filename); + if (n == 1) { + fail("unable to write pcap record header to %s", filename); + return -1; + } /* Write data */ n = write(dump, buffer, size); - fail_unless(n != -1, "unable to write pcap data to %s", filename); + if (n == -1) { + fail("unable to write pcap data to %s", filename); + return -1; + } /* Append to list of packets */ pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int)); - fail_unless(pkt != NULL); + if (!pkt) { + fail("unable to allocate packet"); + return -1; + } memcpy(pkt->data, buffer, size); pkt->size = size; TAILQ_INSERT_TAIL(&pkts, pkt, next); @@ -89,9 +98,15 @@ setup() TAILQ_INIT(&pkts); /* Open a new dump file */ n = asprintf(&filename, "lldp_send_%04d.pcap", serial++); - fail_unless(n != -1, "unable to compute filename"); + if (n == -1) { + fail("unable to compute filename"); + return; + } dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - fail_unless(dump != -1); + if (dump == -1) { + fail("unable to open %s", filename); + return; + } /* Write a PCAP header */ hdr.magic_number = 0xa1b2c3d4; hdr.version_major = 2; @@ -101,7 +116,10 @@ setup() hdr.snaplen = 65535; hdr.network = 1; n = write(dump, &hdr, sizeof(hdr)); - fail_unless(n != -1, "unable to write pcap header to %s", filename); + if (n == -1) { + fail("unable to write pcap header to %s", filename); + return; + } /* Prepare hardware */ memset(&hardware, 0, sizeof(struct lldpd_hardware)); TAILQ_INIT(&hardware.h_rports); @@ -245,8 +263,14 @@ Link Layer Discovery Protocol /* Build packet */ n = lldp_send(NULL, &hardware); - fail_unless(n == 0, "unable to build packet"); - fail_unless(!TAILQ_EMPTY(&pkts)); + if (n != 0) { + fail("unable to build packet"); + return; + } + if (TAILQ_EMPTY(&pkts)) { + fail("no packets sent"); + return; + } pkt = TAILQ_FIRST(&pkts); ck_assert_int_eq(pkt->size, sizeof(pkt1)); fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0); @@ -404,8 +428,14 @@ Link Layer Discovery Protocol /* Build packet */ n = lldp_send(NULL, &hardware); - fail_unless(n == 0, "unable to build packet"); - fail_unless(!TAILQ_EMPTY(&pkts)); + if (n != 0) { + fail("unable to build packet"); + return; + } + if (TAILQ_EMPTY(&pkts)) { + fail("no packets sent"); + return; + } pkt = TAILQ_FIRST(&pkts); ck_assert_int_eq(pkt->size, sizeof(pkt1)); fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0); @@ -634,8 +664,14 @@ Link Layer Discovery Protocol /* Build packet */ n = lldp_send(NULL, &hardware); - fail_unless(n == 0, "unable to build packet"); - fail_unless(!TAILQ_EMPTY(&pkts)); + if (n != 0) { + fail("unable to build packet"); + return; + } + if (TAILQ_EMPTY(&pkts)) { + fail("no packets sent"); + return; + } pkt = TAILQ_FIRST(&pkts); ck_assert_int_eq(pkt->size, sizeof(pkt1)); fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0); @@ -767,8 +803,14 @@ Link Layer Discovery Protocol /* Build packet */ n = lldp_send(NULL, &hardware); - fail_unless(n == 0, "unable to build packet"); - fail_unless(!TAILQ_EMPTY(&pkts)); + if (n != 0) { + fail("unable to build packet"); + return; + } + if (TAILQ_EMPTY(&pkts)) { + fail("no packets sent"); + return; + } pkt = TAILQ_FIRST(&pkts); ck_assert_int_eq(pkt->size, sizeof(pkt1)); fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0); @@ -818,8 +860,10 @@ Link Layer Discovery Protocol fail_unless(lldp_decode(NULL, pkt1, sizeof(pkt1), &hardware, &nchassis, &nport) != -1); - fail_unless(nchassis != NULL); - fail_unless(nport != NULL); + if (!nchassis || !nport) { + fail("unable to decode packet"); + return; + } ck_assert_int_eq(nchassis->c_id_subtype, LLDP_CHASSISID_SUBTYPE_LLADDR); ck_assert_int_eq(nchassis->c_id_len, ETH_ALEN); @@ -1021,8 +1065,10 @@ Link Layer Discovery Protocol fail_unless(lldp_decode(NULL, pkt1, sizeof(pkt1), &hardware, &nchassis, &nport) != -1); - fail_unless(nchassis != NULL); - fail_unless(nport != NULL); + if (!nchassis || !nport) { + fail("unable to decode packet"); + return; + } ck_assert_int_eq(nchassis->c_id_subtype, LLDP_CHASSISID_SUBTYPE_LLADDR); ck_assert_int_eq(nchassis->c_id_len, ETH_ALEN); @@ -1216,8 +1262,10 @@ Link Layer Discovery Protocol fail_unless(lldp_decode(NULL, pkt1, sizeof(pkt1), &hardware, &nchassis, &nport) != -1); - fail_unless(nchassis != NULL); - fail_unless(nport != NULL); + if (!nchassis || !nport) { + fail("unable to decode packet"); + return; + } ck_assert_int_eq(nchassis->c_id_subtype, LLDP_CHASSISID_SUBTYPE_LLADDR); ck_assert_int_eq(nchassis->c_id_len, ETH_ALEN); @@ -1238,16 +1286,25 @@ Link Layer Discovery Protocol ck_assert_int_eq(nchassis->c_mgmt.s_addr, (u_int32_t)inet_addr("172.20.3.2")); ck_assert_int_eq(nchassis->c_mgmt_if, 0); - fail_unless(!TAILQ_EMPTY(&nport->p_vlans)); + if (TAILQ_EMPTY(&nport->p_vlans)) { + fail("no VLAN"); + return; + } vlan = TAILQ_FIRST(&nport->p_vlans); ck_assert_int_eq(vlan->v_vid, 500); ck_assert_str_eq(vlan->v_name, "TestVlan"); vlan = TAILQ_NEXT(vlan, v_entries); - fail_unless(vlan != NULL); + if (!vlan) { + fail("no more VLAN"); + return; + } ck_assert_int_eq(vlan->v_vid, 501); ck_assert_str_eq(vlan->v_name, "TestVlan2"); vlan = TAILQ_NEXT(vlan, v_entries); - fail_unless(vlan != NULL); + if (!vlan) { + fail("no more VLAN"); + return; + } ck_assert_int_eq(vlan->v_vid, 502); ck_assert_str_eq(vlan->v_name, "TestVlan3"); vlan = TAILQ_NEXT(vlan, v_entries); @@ -1404,8 +1461,10 @@ Link Layer Discovery Protocol fail_unless(lldp_decode(NULL, pkt1, sizeof(pkt1), &hardware, &nchassis, &nport) != -1); - fail_unless(nchassis != NULL); - fail_unless(nport != NULL); + if (!nchassis || !nport) { + fail("unable to decode packet"); + return; + } ck_assert_int_eq(nchassis->c_id_subtype, LLDP_CHASSISID_SUBTYPE_LLADDR); ck_assert_int_eq(nchassis->c_id_len, ETH_ALEN); diff --git a/tests/check_pack.c b/tests/check_pack.c index ef1940ca..cde7a44a 100644 --- a/tests/check_pack.c +++ b/tests/check_pack.c @@ -27,12 +27,18 @@ START_TEST (test_pack_byte) char byte = 18; void *p; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("b", &byte, sizeof(char), - h, &p) != -1); + if (ctl_msg_pack_structure("b", &byte, sizeof(char), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("b", &byte, sizeof(char), - h, &p) != -1); + if (ctl_msg_unpack_structure("b", &byte, sizeof(char), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(byte, 18); } END_TEST @@ -43,12 +49,18 @@ START_TEST (test_pack_word) u_int16_t word = 7874; void *p; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("w", &word, sizeof(u_int16_t), - h, &p) != -1); + if (ctl_msg_pack_structure("w", &word, sizeof(u_int16_t), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("w", &word, sizeof(u_int16_t), - h, &p) != -1); + if (ctl_msg_unpack_structure("w", &word, sizeof(u_int16_t), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(word, 7874); } END_TEST @@ -59,12 +71,18 @@ START_TEST (test_pack_long) u_int32_t l = 14523657; void *p; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("l", &l, sizeof(u_int32_t), - h, &p) != -1); + if (ctl_msg_pack_structure("l", &l, sizeof(u_int32_t), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("l", &l, sizeof(u_int32_t), - h, &p) != -1); + if (ctl_msg_unpack_structure("l", &l, sizeof(u_int32_t), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(l, 14523657); } END_TEST @@ -77,12 +95,18 @@ START_TEST (test_pack_time) void *p; t2 = t; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("t", &t, sizeof(time_t), - h, &p) != -1); + if (ctl_msg_pack_structure("t", &t, sizeof(time_t), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("t", &t, sizeof(time_t), - h, &p) != -1); + if (ctl_msg_unpack_structure("t", &t, sizeof(time_t), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(t, t2); } END_TEST @@ -94,12 +118,18 @@ START_TEST (test_pack_string) char *rs; void *p; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("s", &s, sizeof(char *), - h, &p) != -1); + if (ctl_msg_pack_structure("s", &s, sizeof(char *), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("s", &rs, sizeof(char *), - h, &p) != -1); + if (ctl_msg_unpack_structure("s", &rs, sizeof(char *), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_str_eq(s, rs); ck_assert_str_eq(rs, "My simple string"); free(rs); @@ -113,12 +143,18 @@ START_TEST (test_pack_null_string) char *rs; void *p; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("s", &s, sizeof(char *), - h, &p) != -1); + if (ctl_msg_pack_structure("s", &s, sizeof(char *), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("s", &rs, sizeof(char *), - h, &p) != -1); + if (ctl_msg_unpack_structure("s", &rs, sizeof(char *), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_str_eq(s, rs); ck_assert_int_eq(strlen(rs), 0); free(rs); @@ -140,12 +176,18 @@ START_TEST (test_pack_len_string) t.l = strlen(t.s); p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("C", &t, sizeof(struct tpls), - h, &p) != -1); + if (ctl_msg_pack_structure("C", &t, sizeof(struct tpls), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("C", &t, sizeof(struct tpls), - h, &p) != -1); + if (ctl_msg_unpack_structure("C", &t, sizeof(struct tpls), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(t.l, strlen("My string")); fail_unless(memcmp(t.s, "My string", t.l) == 0); free(t.s); @@ -176,12 +218,18 @@ START_TEST (test_pack_structures1) t.g = 1246799447; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("bwlbPbt", &t, sizeof(struct tps1), - h, &p) != -1); + if (ctl_msg_pack_structure("bwlbPbt", &t, sizeof(struct tps1), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("bwlbPbt", &t, sizeof(struct tps1), - h, &p) != -1); + if (ctl_msg_unpack_structure("bwlbPbt", &t, sizeof(struct tps1), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(t.a, 129); ck_assert_int_eq(t.b, 37814); ck_assert_int_eq(t.c, 3456781258); @@ -215,12 +263,18 @@ START_TEST (test_pack_structures2) t.i = 12; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("bPwPlPtPb", &t, sizeof(struct tps2), - h, &p) != -1); + if (ctl_msg_pack_structure("bPwPlPtPb", &t, sizeof(struct tps2), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("bPwPlPtPb", &t, sizeof(struct tps2), - h, &p) != -1); + if (ctl_msg_unpack_structure("bPwPlPtPb", &t, sizeof(struct tps2), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(t.a, 129); ck_assert_int_eq(t.c, 37814); ck_assert_int_eq(t.e, 3456781258); @@ -265,12 +319,18 @@ START_TEST (test_pack_structures3) t.m = "Last string"; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure("bswslstsbCbs", &t, sizeof(struct tps3), - h, &p) != -1); + if (ctl_msg_pack_structure("bswslstsbCbs", &t, sizeof(struct tps3), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure("bswslstsbCbs", &t, sizeof(struct tps3), - h, &p) != -1); + if (ctl_msg_unpack_structure("bswslstsbCbs", &t, sizeof(struct tps3), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(t.a, 129); ck_assert_str_eq(t.b, "First string"); ck_assert_int_eq(t.c, 37814); @@ -366,12 +426,18 @@ START_TEST (test_pack_structures4) t.j = 12; p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure(TPS4, &t, sizeof(struct tps4), - h, &p) != -1); + if (ctl_msg_pack_structure(TPS4, &t, sizeof(struct tps4), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure(TPS4, &t, sizeof(struct tps4), - h, &p) != -1); + if (ctl_msg_unpack_structure(TPS4, &t, sizeof(struct tps4), + h, &p) == -1) { + fail("unable to unpack"); + return; + } ck_assert_int_eq(t.a, 129); ck_assert_int_eq(t.b.a, 178); @@ -479,21 +545,33 @@ START_TEST (test_pack_structures5) void *p; f = open("/dev/urandom", O_RDONLY); - fail_unless(f != -1); + if (f == -1) { + fail("unable to open /dev/urandom"); + return; + } n = read(f, &t, sizeof(struct tps5)); - fail_unless(n == sizeof(struct tps5), - "Should have read %d bytes from /dev/random but got %d", - sizeof(struct tps5), n); + if (n != sizeof(struct tps5)) { + fail("Should have read %d bytes from /dev/random but got %d", + sizeof(struct tps5), n); + close(f); + return; + } memcpy(&tc, &t, sizeof(struct tps5)); close(f); p = (char*)&h->data; - fail_unless(ctl_msg_pack_structure(TPS5, &t, sizeof(struct tps5), - h, &p) != -1); + if (ctl_msg_pack_structure(TPS5, &t, sizeof(struct tps5), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_structure(TPS5, &t, sizeof(struct tps5), - h, &p) != -1); + if (ctl_msg_unpack_structure(TPS5, &t, sizeof(struct tps5), + h, &p) == -1) { + fail("unable to unpack"); + return; + } fail_unless(memcmp(&t, &tc, sizeof(struct tps5)) == 0); } @@ -516,12 +594,18 @@ START_TEST (test_pack_empty_list) TAILQ_INIT(&l); p = (char*)&h->data; - fail_unless(ctl_msg_pack_list(TPL, &l, sizeof(struct tpl), - h, &p) != -1); + if (ctl_msg_pack_list(TPL, &l, sizeof(struct tpl), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_list(TPL, &l, sizeof(struct tpl), - h, &p) != -1); + if (ctl_msg_unpack_list(TPL, &l, sizeof(struct tpl), + h, &p) == -1) { + fail("unable to unpack"); + return; + } fail_unless(TAILQ_EMPTY(&l)); } @@ -560,12 +644,18 @@ START_TEST (test_pack_list) mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_pack_list(TPL, &l, sizeof(struct tpl), - h, &p) != -1); + if (ctl_msg_pack_list(TPL, &l, sizeof(struct tpl), + h, &p) == -1) { + fail("unable to pack"); + return; + } mark_point(); p = (char*)&h->data; - fail_unless(ctl_msg_unpack_list(TPL, &l, sizeof(struct tpl), - h, &p) != -1); + if (ctl_msg_unpack_list(TPL, &l, sizeof(struct tpl), + h, &p) == -1) { + fail("unable to unpack"); + return; + } count = 0; TAILQ_FOREACH(tpl4, &l, next) {