From d4bcefb1d6a70ab22336f2311b3dea678f823ec8 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 18 Oct 2012 12:42:05 -0300 Subject: [PATCH] Added RC4EAPOL tests. --- include/eapol.h | 16 ++++-- src/eapol.cpp | 4 +- tests/depends.d | 17 +++++++ tests/src/radiotap.cpp | 3 -- tests/src/rc4eapol.cpp | 113 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 tests/src/rc4eapol.cpp diff --git a/include/eapol.h b/include/eapol.h index d11efc7..e2176b4 100644 --- a/include/eapol.h +++ b/include/eapol.h @@ -187,9 +187,19 @@ namespace Tins { typedef std::vector key_type; /** - * \brief This PDU's flag. + * This PDU's flag. */ static const PDU::PDUType pdu_flag = PDU::RC4EAPOL; + + /** + * The length of the key IV field + */ + static const size_t key_iv_size = 16; + + /** + * The length of the key sign field + */ + static const size_t key_sign_size = 16; /** * \brief Creates an instance of RC4EAPOL @@ -259,7 +269,7 @@ namespace Tins { * \brief Sets the replay counter field. * \param new_replay_counter The new replay counter to be set. */ - void replay_counter(uint16_t new_replay_counter); + void replay_counter(uint64_t new_replay_counter); /** * \brief Sets the key IV field. @@ -330,7 +340,7 @@ namespace Tins { struct rc4hdr { uint16_t key_length; uint64_t replay_counter; - uint8_t key_iv[16]; + uint8_t key_iv[key_iv_size]; uint8_t key_index:7, key_flag:1; uint8_t key_sign[16]; diff --git a/src/eapol.cpp b/src/eapol.cpp index 1e52ef2..e6e2e6a 100644 --- a/src/eapol.cpp +++ b/src/eapol.cpp @@ -80,7 +80,7 @@ void EAPOL::packet_type(uint8_t new_ptype) { } void EAPOL::length(uint16_t new_length) { - _header.length = new_length; + _header.length = Endian::host_to_be(new_length); } void EAPOL::type(uint8_t new_type) { @@ -127,7 +127,7 @@ void RC4EAPOL::key_length(uint16_t new_key_length) { _header.key_length = Endian::host_to_be(new_key_length); } -void RC4EAPOL::replay_counter(uint16_t new_replay_counter) { +void RC4EAPOL::replay_counter(uint64_t new_replay_counter) { _header.replay_counter = Endian::host_to_be(new_replay_counter); } diff --git a/tests/depends.d b/tests/depends.d index c0dd524..89d68e7 100644 --- a/tests/depends.d +++ b/tests/depends.d @@ -197,6 +197,23 @@ src/radiotap.o: src/radiotap.cpp ../include/radiotap.h ../include/pdu.h \ ../include/ip_address.h: ../include/utils.h: +src/rc4eapol.o: src/rc4eapol.cpp ../include/eapol.h ../include/pdu.h \ + ../include/small_uint.h ../include/endianness.h ../include/utils.h \ + ../include/ip_address.h ../include/hw_address.h + +../include/eapol.h: + +../include/pdu.h: + +../include/small_uint.h: + +../include/endianness.h: + +../include/utils.h: + +../include/ip_address.h: + +../include/hw_address.h: src/snap.o: src/snap.cpp ../include/snap.h ../include/pdu.h \ ../include/endianness.h ../include/small_uint.h ../include/utils.h \ ../include/ip_address.h ../include/hw_address.h diff --git a/tests/src/radiotap.cpp b/tests/src/radiotap.cpp index cb03bee..164d72a 100644 --- a/tests/src/radiotap.cpp +++ b/tests/src/radiotap.cpp @@ -11,8 +11,6 @@ using namespace Tins; class RadioTapTest : public testing::Test { public: static const uint8_t expected_packet[]; - - void test_equals(const RadioTap &radio1, const RadioTap &radio2); }; const uint8_t RadioTapTest::expected_packet[] = { @@ -55,7 +53,6 @@ TEST_F(RadioTapTest, ConstructorFromBuffer) { EXPECT_EQ(radio.dbm_signal(), 0xda); EXPECT_EQ(radio.dbm_noise(), 0xa0); EXPECT_EQ(radio.antenna(), 2); - EXPECT_EQ(radio.rx_flags(), 0); } TEST_F(RadioTapTest, Serialize) { diff --git a/tests/src/rc4eapol.cpp b/tests/src/rc4eapol.cpp new file mode 100644 index 0000000..468442e --- /dev/null +++ b/tests/src/rc4eapol.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include "eapol.h" +#include "utils.h" + +using namespace std; +using namespace Tins; + +class RC4EAPOLTest : public testing::Test { +public: + static const uint8_t expected_packet[]; +}; + +const uint8_t RC4EAPOLTest::expected_packet[] = { + +}; + +TEST_F(RC4EAPOLTest, DefaultConstructor) { + uint8_t empty_iv[RC4EAPOL::key_iv_size] = { 0 }; + + RC4EAPOL eapol; + EXPECT_EQ(1, eapol.version()); + EXPECT_EQ(0x3, eapol.packet_type()); + EXPECT_EQ(EAPOL::RC4, eapol.type()); + EXPECT_EQ(0, eapol.length()); + EXPECT_EQ(0, eapol.key_length()); + EXPECT_EQ(0, eapol.replay_counter()); + EXPECT_TRUE(std::equal(empty_iv, empty_iv + sizeof(empty_iv), eapol.key_iv())); + EXPECT_EQ(0, eapol.key_flag()); + EXPECT_EQ(0, eapol.key_index()); + EXPECT_TRUE(std::equal(empty_iv, empty_iv + sizeof(empty_iv), eapol.key_sign())); + EXPECT_EQ(RC4EAPOL::key_type(), eapol.key()); +} + +TEST_F(RC4EAPOLTest, Version) { + RC4EAPOL eapol; + eapol.version(0x7a); + EXPECT_EQ(0x7a, eapol.version()); +} + +TEST_F(RC4EAPOLTest, PacketType) { + RC4EAPOL eapol; + eapol.packet_type(0x7a); + EXPECT_EQ(0x7a, eapol.packet_type()); +} + +TEST_F(RC4EAPOLTest, Length) { + RC4EAPOL eapol; + eapol.length(0x7af2); + EXPECT_EQ(0x7af2, eapol.length()); +} + +TEST_F(RC4EAPOLTest, Type) { + RC4EAPOL eapol; + eapol.type(0x7a); + EXPECT_EQ(0x7a, eapol.type()); +} + +TEST_F(RC4EAPOLTest, KeyLength) { + RC4EAPOL eapol; + eapol.key_length(0x7af3); + EXPECT_EQ(0x7af3, eapol.key_length()); +} + +TEST_F(RC4EAPOLTest, ReplayCounter) { + RC4EAPOL eapol; + eapol.replay_counter(0x7af3d91a1fd3abLL); + EXPECT_EQ(0x7af3d91a1fd3abLL, eapol.replay_counter()); +} + +TEST_F(RC4EAPOLTest, KeyIV) { + uint8_t iv[RC4EAPOL::key_iv_size]; + for(unsigned i = 0; i < RC4EAPOL::key_iv_size; ++i) + iv[i] = i; + + RC4EAPOL eapol; + eapol.key_iv(iv); + EXPECT_TRUE(std::equal(iv, iv + sizeof(iv), eapol.key_iv())); +} + +TEST_F(RC4EAPOLTest, KeyFlag) { + RC4EAPOL eapol; + eapol.key_flag(1); + EXPECT_EQ(1, eapol.key_flag()); + eapol.key_flag(0); + EXPECT_EQ(0, eapol.key_flag()); +} + +TEST_F(RC4EAPOLTest, KeyIndex) { + RC4EAPOL eapol; + eapol.key_index(0x7d); + EXPECT_EQ(0x7d, eapol.key_index()); +} + +TEST_F(RC4EAPOLTest, KeySign) { + uint8_t sign[RC4EAPOL::key_sign_size]; + for(unsigned i = 0; i < RC4EAPOL::key_sign_size; ++i) + sign[i] = i; + + RC4EAPOL eapol; + eapol.key_sign(sign); + EXPECT_TRUE(std::equal(sign, sign + sizeof(sign), eapol.key_sign())); +} + +TEST_F(RC4EAPOLTest, Key) { + RC4EAPOL eapol; + uint8_t arr[] = { 1, 9, 2, 0x71, 0x87, 0xfa, 0xdf }; + RC4EAPOL::key_type key(arr, arr + sizeof(arr)); + eapol.key(key); + EXPECT_EQ(key, eapol.key()); +}