我正在使用pcap.h進行基本的數據包嗅探器。當我單元測試稱爲pcap_dispatch的函數時,我給了它非激活的接口和無效的接口。 pcap_dispatch返回-3,並且就pcap_dispatch的手冊頁而言,它應該只返回-2,-1或更多,但不會更少。是否可以檢查一個接口是否在pcap中被激活?
當然,我可以在pcap_dispatch返回後處理這個-3的返回值,將其作爲一些模糊的邊緣情況處理,但是我想確保它不會返回這樣的值。
只有當我知道如何檢查接口是否被激活時,我纔可以用assert做到這一點。那麼,有人知道嗎?
pcap_t *null_iface = NULL;
pcap_t *not_act = pcap_create("valid", errmsg);
pcap_t *act = pcap_create("valid", errmsg);
pcap_activate(act);
pcap_t *not_act_inv = pcap_create("invalid", errmsg);
pcap_t *act_inv = pcap_create("invalid", errmsg);
pcap_activate(act);
pcap_t *ifaces[ 5 ] = {
null_iface, not_act, act, not_act_inv, act_inv
};
for (int a = 0; a < 5; a++) {
result = pcap_dispatch(iface[ a ], 1, handler, NULL);
// if one - got one
// elif zero - got nothing
// elif -1 - error (doesn't fire, by the way)
// elif -2 - I manually stopped it
// else - impossible - but the assert fires
// - printing result is -3
// - for not_act, not_act_inv, and act_inv
else {
assert(false);
}
}
錯過了,謝謝 – user2738698 2015-02-05 19:56:09