1 // Copyright 2017 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #pragma once 6 7 #include <crypto/aead.h> 8 #include <crypto/bytes.h> 9 #include <crypto/secret.h> 10 #include <crypto/cipher.h> 11 #include <crypto/hkdf.h> 12 #include <zircon/status.h> 13 #include <zircon/types.h> 14 15 // For expressions that return a zx_status_t, this macro will print the symbolic names when a 16 // mismatch is encountered. Don't use this directly; use the wrappers below. 17 #define UT_ZX(lhs, rhs, ret) \ 18 do { \ 19 UT_ASSERT_VALID_TEST_STATE; \ 20 zx_status_t _lhs_val = (lhs); \ 21 zx_status_t _rhs_val = (rhs); \ 22 if (_lhs_val != _rhs_val) { \ 23 UNITTEST_FAIL_TRACEF("%s returned %s; expected %s\n", #lhs, \ 24 zx_status_get_string(_lhs_val), zx_status_get_string(_rhs_val)); \ 25 current_test_info->all_ok = false; \ 26 ret; \ 27 } \ 28 } while (0) 29 30 // Wrappers for UT_ZX. 31 #define EXPECT_ZX(expr, status) UT_ZX(expr, status, DONOT_RET) 32 #define ASSERT_ZX(expr, status) UT_ZX(expr, status, RET_FALSE) 33 #define EXPECT_OK(expr) UT_ZX(expr, ZX_OK, DONOT_RET) 34 #define ASSERT_OK(expr) UT_ZX(expr, ZX_OK, RET_FALSE) 35 36 // Value-parameterized tests: Consumers of this file can define an 'EACH_PARAM' macro as follows: 37 // #define EACH_PARAM(OP, Test) 38 // OP(Test, Class, Param1) 39 // OP(Test, Class, Param2) 40 // ... 41 // OP(Test, Class, ParamN) 42 // where |Param1| corresponds to an enum constant |Class::kParam1|, etc. 43 // 44 // Consumers can then use the following macros to automatically define and run tests for each 45 // parameter: 46 // bool TestSomething(Param param) { 47 // BEGIN_TEST; 48 // ... 49 // END_TEST; 50 // } 51 // DEFINE_EACH(TestSomething) 52 // ... 53 // BEGIN_TEST_CASE(SomeTest) 54 // RUN_EACH(TestSomething) 55 // END_TEST_CASE(SomeTest) 56 #define DEFINE_TEST_PARAM(Test, Class, Param) bool Test ## _ ## Param(void) { return Test(Class::k ## Param); } 57 #define RUN_TEST_PARAM(Test, Class, Param) RUN_TEST(Test ## _ ## Param) 58 #define DEFINE_EACH(Test) EACH_PARAM(DEFINE_TEST_PARAM, Test) 59 #define RUN_EACH(Test) EACH_PARAM(RUN_TEST_PARAM, Test) 60 61 namespace crypto { 62 namespace testing { 63 64 // Resizes |out| and sets its contents to match the given |hex| string. 65 zx_status_t HexToBytes(const char* hex, Bytes* out); 66 zx_status_t HexToSecret(const char* hex, Secret* out); 67 68 // Fills the given |key| and |iv| with as much random data as indicated by |Cipher::GetKeyLen| and 69 // |Cipher::GetIVLen| for the given |cipher|. |iv| may be null. 70 zx_status_t GenerateKeyMaterial(Cipher::Algorithm cipher, Secret* key, Bytes* iv); 71 72 // Fills the given |key|, |iv| with as much random data as indicated by |AEAD::GetKeyLen| and 73 //|AEAD::GetIVLen| for the given |aead|. |iv| may be null. 74 zx_status_t GenerateKeyMaterial(AEAD::Algorithm aead, Secret* key, Bytes* iv); 75 76 } // namespace testing 77 } // namespace crypto 78