1 #include <fstream> 2 #include <iostream> 3 #include <random> 4 #include <sstream> 5 6 #include "gtest/gtest.h" 7 #include "nugget/app/protoapi/control.pb.h" 8 #include "nugget/app/protoapi/header.pb.h" 9 #include "nugget/app/protoapi/testing_api.pb.h" 10 #include "src/macros.h" 11 #include "src/util.h" 12 13 using nugget::app::protoapi::AesCmacTest; 14 using nugget::app::protoapi::AesCmacTestResult; 15 using nugget::app::protoapi::APImessageID; 16 using nugget::app::protoapi::DcryptError; 17 using nugget::app::protoapi::Notice; 18 using nugget::app::protoapi::NoticeCode; 19 using nugget::app::protoapi::OneofTestParametersCase; 20 using nugget::app::protoapi::OneofTestResultsCase; 21 using std::cout; 22 using std::stringstream; 23 using std::unique_ptr; 24 25 #define ASSERT_MSG_TYPE(msg, type_) \ 26 do{ \ 27 if (type_ != APImessageID::NOTICE && msg.type == APImessageID::NOTICE) { \ 28 Notice received; \ 29 received.ParseFromArray(reinterpret_cast<char *>(msg.data), msg.data_len); \ 30 ASSERT_EQ(msg.type, type_) \ 31 << msg.type << " is " << APImessageID_Name((APImessageID) msg.type) \ 32 << "\n" << received.DebugString(); \ 33 }else{ \ 34 ASSERT_EQ(msg.type, type_) \ 35 << msg.type << " is " << APImessageID_Name((APImessageID) msg.type); \ 36 }}while(0) 37 38 #define ASSERT_SUBTYPE(msg, type_) \ 39 do { \ 40 EXPECT_GT(msg.data_len, 2); \ 41 uint16_t subtype = (msg.data[0] << 8) | msg.data[1]; \ 42 EXPECT_EQ(subtype, type_); \ 43 } while(0) 44 45 namespace { 46 47 using test_harness::BYTE_TIME; 48 49 class DcryptoTest: public testing::Test { 50 protected: 51 static void SetUpTestCase(); 52 static void TearDownTestCase(); 53 54 public: 55 static unique_ptr<test_harness::TestHarness> harness; 56 static std::random_device random_number_generator; 57 }; 58 59 unique_ptr<test_harness::TestHarness> DcryptoTest::harness; 60 std::random_device DcryptoTest::random_number_generator; 61 62 void DcryptoTest::SetUpTestCase() { 63 harness = unique_ptr<test_harness::TestHarness>( 64 new test_harness::TestHarness()); 65 } 66 67 void DcryptoTest::TearDownTestCase() { 68 harness = unique_ptr<test_harness::TestHarness>(); 69 } 70 71 #include "src/test-data/dcrypto/aes-cmac-rfc4493.h" 72 73 TEST_F(DcryptoTest, AesCmacRfc4493Test) { 74 const int verbosity = harness->getVerbosity(); 75 harness->setVerbosity(verbosity - 1); 76 77 for (size_t i = 0; i < ARRAYSIZE(RFC4493_AES_CMAC_DATA); i++) { 78 const cmac_data *test_case = &RFC4493_AES_CMAC_DATA[i]; 79 AesCmacTest request; 80 request.set_key(test_case->K, sizeof(test_case->K)); 81 82 if (test_case->M_len > 0) { 83 request.set_plain_text(test_case->M, test_case->M_len); 84 } 85 86 ASSERT_NO_ERROR(harness->SendOneofProto( 87 APImessageID::TESTING_API_CALL, 88 OneofTestParametersCase::kAesCmacTest, 89 request), ""); 90 91 test_harness::raw_message msg; 92 ASSERT_NO_ERROR(harness->GetData(&msg, 4096 * BYTE_TIME), ""); 93 ASSERT_MSG_TYPE(msg, APImessageID::TESTING_API_RESPONSE); 94 ASSERT_SUBTYPE(msg, OneofTestResultsCase::kAesCmacTestResult); 95 96 AesCmacTestResult result; 97 ASSERT_TRUE(result.ParseFromArray(reinterpret_cast<char *>(msg.data + 2), 98 msg.data_len - 2)); 99 EXPECT_EQ(result.result_code(), DcryptError::DE_NO_ERROR) 100 << result.result_code() << " is " 101 << DcryptError_Name(result.result_code()); 102 103 for (size_t j = 0; j < sizeof(test_case->CMAC); j++) { 104 ASSERT_EQ(result.cmac()[j] & 0x00ff, test_case->CMAC[j] & 0x00ff); 105 } 106 } 107 108 harness->setVerbosity(verbosity); 109 } 110 111 } // namespace 112