From 457a75170e061c872f64840df6b3e548b887ff7a Mon Sep 17 00:00:00 2001 From: Zane Beckwith Date: Wed, 27 Sep 2017 11:05:19 -0500 Subject: [PATCH] Test serialization of group-public-key. --- test/CMakeLists.txt | 1 + test/group_public_key-tests.c | 85 +++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 test/group_public_key-tests.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 03fec67..bb2518c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -19,6 +19,7 @@ include(CTest) set(CURRENT_TEST_BINARY_DIR ${CMAKE_BINARY_DIR}/testBin/) set(TEST_SRCS + group_public_key-tests.c sign-and-verify-tests.c issuer_keypair-tests.c credential-tests.c diff --git a/test/group_public_key-tests.c b/test/group_public_key-tests.c new file mode 100644 index 0000000..35079c7 --- /dev/null +++ b/test/group_public_key-tests.c @@ -0,0 +1,85 @@ +/****************************************************************************** + * + * Copyright 2017 Xaptum, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + * + *****************************************************************************/ + +#include "ecdaa-test-utils.h" + +#include + +#include "../src/amcl-extensions/ecp2_BN254.h" + +#include +#include + +static void serialize_then_deserialize_basepoints(); +static void lengths_same(); +static void deserialize_garbage_fails(); + +int main() +{ + serialize_then_deserialize_basepoints(); + lengths_same(); + deserialize_garbage_fails(); + + return 0; +} + +static void serialize_then_deserialize_basepoints() +{ + printf("Starting group_public_key::serialize_then_deserialize_basepoints...\n"); + + struct ecdaa_group_public_key_BN254 gpk; + + ecp2_BN254_set_to_generator(&gpk.X); + ecp2_BN254_set_to_generator(&gpk.Y); + + + uint8_t buffer[ECDAA_GROUP_PUBLIC_KEY_BN254_LENGTH]; + + ecdaa_group_public_key_BN254_serialize(buffer, &gpk); + + struct ecdaa_group_public_key_BN254 gpk_deserialized; + TEST_ASSERT(0 == ecdaa_group_public_key_BN254_deserialize(&gpk_deserialized, buffer)); + + TEST_ASSERT(ECP2_BN254_equals(&gpk.X, &gpk_deserialized.X)); + TEST_ASSERT(ECP2_BN254_equals(&gpk.Y, &gpk_deserialized.Y)); + + printf("\tsuccess\n"); +} + +static void lengths_same() +{ + printf("Starting group_public_key::lengths_same...\n"); + + TEST_ASSERT(ECDAA_GROUP_PUBLIC_KEY_BN254_LENGTH == ecdaa_group_public_key_BN254_length()); + + printf("\tsuccess\n"); +} + +static void deserialize_garbage_fails() +{ + printf("Starting group_public_key::deserialize_garbage_fails...\n"); + + uint8_t buffer[ECDAA_GROUP_PUBLIC_KEY_BN254_LENGTH] = {0}; + memset(buffer, 1, sizeof(buffer)); // All 1's + buffer[0] = 0x4; + + struct ecdaa_group_public_key_BN254 gpk_deserialized; + TEST_ASSERT(-1 == ecdaa_group_public_key_BN254_deserialize(&gpk_deserialized, buffer)); + + printf("\tsuccess\n"); +}