Skip to content

Commit

Permalink
Merge pull request #38 from zanebeckwith/improve-test-coverage
Browse files Browse the repository at this point in the history
Add tests for serialization, and a Python script for integration testing
  • Loading branch information
Zane Beckwith authored Sep 27, 2017
2 parents 7f0b3f5 + 1b53926 commit 6827f57
Show file tree
Hide file tree
Showing 10 changed files with 696 additions and 19 deletions.
7 changes: 6 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ include(CTest)
set(CURRENT_TEST_BINARY_DIR ${CMAKE_BINARY_DIR}/testBin/)

set(TEST_SRCS
sign-and-verify-tests.c
group_public_key-tests.c
signature-tests.c
issuer_keypair-tests.c
credential-tests.c
member_keypair-tests.c
Expand Down Expand Up @@ -51,3 +52,7 @@ foreach(case_file ${TEST_SRCS})

add_dependencies(${case_name} ecdaa)
endforeach()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/integration-tests.py ${CURRENT_TEST_BINARY_DIR}/integration-tests.py)
add_test(NAME integration-tests
COMMAND python "${CURRENT_TEST_BINARY_DIR}/integration-tests.py" "${CMAKE_BINARY_DIR}/bin/")
2 changes: 2 additions & 0 deletions test/big_256_56-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

static void hash_not_zero();
static void hash_two_not_zero();
static void hash_two_same_messages();
static void hash_ok_with_no_msg();
static void hash_same_message();
static void mul_and_add_all_zeros();
Expand All @@ -43,6 +44,7 @@ int main()
{
hash_not_zero();
hash_two_not_zero();
hash_two_same_messages();
hash_ok_with_no_msg();
hash_same_message();
mul_and_add_all_zeros();
Expand Down
43 changes: 42 additions & 1 deletion test/credential-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ static void setup(credential_test_fixture* fixture);
static void teardown(credential_test_fixture* fixture);

static void cred_generate_then_validate();
static void lengths_same();
static void cred_generate_then_serialize_deserialize();

int main()
{
cred_generate_then_validate();
lengths_same();
cred_generate_then_serialize_deserialize();
}

static void setup(credential_test_fixture* fixture)
Expand All @@ -76,7 +80,7 @@ static void teardown(credential_test_fixture* fixture)

static void cred_generate_then_validate()
{
printf("Starting join-tests::cred_generate_validate...\n");
printf("Starting credential::cred_generate_validate...\n");

credential_test_fixture fixture;
setup(&fixture);
Expand All @@ -91,3 +95,40 @@ static void cred_generate_then_validate()

printf("\tsuccess\n");
}

static void lengths_same()
{
printf("Starting credential::lengths_same...\n");

TEST_ASSERT(ECDAA_CREDENTIAL_BN254_LENGTH == ecdaa_credential_BN254_length());

TEST_ASSERT(ECDAA_CREDENTIAL_BN254_SIGNATURE_LENGTH == ecdaa_credential_BN254_signature_length());

printf("\tsuccess\n");
}

static void cred_generate_then_serialize_deserialize()
{
printf("Starting credential::cred_generate_then_serialize_deserialize...\n");

credential_test_fixture fixture;
setup(&fixture);

struct ecdaa_credential_BN254 cred;
struct ecdaa_credential_BN254_signature cred_sig;
TEST_ASSERT(0 == ecdaa_credential_BN254_generate(&cred, &cred_sig, &fixture.isk, &fixture.pk, &fixture.prng));

uint8_t cred_buffer[ECDAA_CREDENTIAL_BN254_LENGTH];
uint8_t sig_buffer[ECDAA_CREDENTIAL_BN254_SIGNATURE_LENGTH];

ecdaa_credential_BN254_serialize(cred_buffer, &cred);
ecdaa_credential_BN254_signature_serialize(sig_buffer, &cred_sig);

struct ecdaa_credential_BN254 cred_deserialized;
TEST_ASSERT(0 == ecdaa_credential_BN254_deserialize(&cred_deserialized, cred_buffer));
TEST_ASSERT(0 == ecdaa_credential_BN254_deserialize_with_signature(&cred_deserialized, &fixture.pk, &fixture.ipk.gpk, cred_buffer, sig_buffer));

teardown(&fixture);

printf("\tsuccess\n");
}
66 changes: 64 additions & 2 deletions test/ecp2_BN254-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@
#include "../src/amcl-extensions/ecp2_BN254.h"

#include <stdio.h>
#include <string.h>

static void g2_basepoint_not_inf();

static void g2_serialize_then_deserialize_basepoint();
static void g2_lengths_same();
static void g2_deserialize_badformat_fails();
static void g2_deserialize_badcoords_fails();

int main()
{
g2_basepoint_not_inf();
g2_serialize_then_deserialize_basepoint();
g2_lengths_same();
g2_deserialize_badformat_fails();
g2_deserialize_badcoords_fails();

return 0;
}

void g2_basepoint_not_inf()
{
printf("Starting pairing_curve_utils::g2_basepoint_not_inf...\n");
printf("Starting ecp2_BN254::g2_basepoint_not_inf...\n");

ECP2_BN254 point;
ecp2_BN254_set_to_generator(&point);
Expand All @@ -44,3 +52,57 @@ void g2_basepoint_not_inf()
printf("\tsuccess\n");
}

static void g2_serialize_then_deserialize_basepoint()
{
printf("Starting ecp2_BN254::g2_basepoint_not_inf...\n");

ECP2_BN254 point;
ecp2_BN254_set_to_generator(&point);

uint8_t buffer[ECP2_BN254_LENGTH];

ecp2_BN254_serialize(buffer, &point);

ECP2_BN254 deserialized_point;
TEST_ASSERT(0 == ecp2_BN254_deserialize(&deserialized_point, buffer));

TEST_ASSERT(ECP2_BN254_equals(&point, &deserialized_point));

printf("\tsuccess\n");
}

static void g2_lengths_same()
{
printf("Starting ecp2_BN254::g2_lengths_same...\n");

TEST_ASSERT(ECP2_BN254_LENGTH == ecp2_BN254_length());

printf("\tsuccess\n");
}

static void g2_deserialize_badformat_fails()
{
printf("Starting ecp2_BN254::g2_deserialize_badformat_fails...\n");

uint8_t buffer[ECP2_BN254_LENGTH] = {0};
buffer[0] = 0x3;

ECP2_BN254 point;
TEST_ASSERT(-2 == ecp2_BN254_deserialize(&point, buffer));

printf("\tsuccess\n");
}

static void g2_deserialize_badcoords_fails()
{
printf("Starting ecp2_BN254::g2_deserialize_badcoords_fails...\n");

uint8_t buffer[ECP2_BN254_LENGTH] = {0};
memset(buffer, 1, sizeof(buffer)); // All 1's
buffer[0] = 0x4;

ECP2_BN254 point;
TEST_ASSERT(-1 == ecp2_BN254_deserialize(&point, buffer));

printf("\tsuccess\n");
}
65 changes: 64 additions & 1 deletion test/ecp_BN254-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@
#include "../src/amcl-extensions/ecp_BN254.h"

#include <stdio.h>
#include <string.h>

static void g1_basepoint_not_inf();
static void g1_serialize_then_deserialize_basepoint();
static void g1_lengths_same();
static void g1_deserialize_badformat_fails();
static void g1_deserialize_badcoords_fails();

int main()
{
g1_basepoint_not_inf();
g1_serialize_then_deserialize_basepoint();
g1_lengths_same();
g1_deserialize_badformat_fails();
g1_deserialize_badcoords_fails();

return 0;
}

void g1_basepoint_not_inf()
{
printf("Starting pairing_curve_utils::g1_basepoint_not_inf...\n");
printf("Starting ecp_BN254::g1_basepoint_not_inf...\n");

ECP_BN254 point;
ecp_BN254_set_to_generator(&point);
Expand All @@ -43,3 +52,57 @@ void g1_basepoint_not_inf()
printf("\tsuccess\n");
}

static void g1_serialize_then_deserialize_basepoint()
{
printf("Starting ecp_BN254::g1_serialize_then_deserialize_basepoint...\n");

ECP_BN254 point;
ecp_BN254_set_to_generator(&point);

uint8_t buffer[ECP_BN254_LENGTH];

ecp_BN254_serialize(buffer, &point);

ECP_BN254 deserialized_point;
TEST_ASSERT(0 == ecp_BN254_deserialize(&deserialized_point, buffer));

TEST_ASSERT(ECP_BN254_equals(&point, &deserialized_point));

printf("\tsuccess\n");
}

static void g1_lengths_same()
{
printf("Starting ecp_BN254::g1_lengths_same...\n");

TEST_ASSERT(ECP_BN254_LENGTH == ecp_BN254_length());

printf("\tsuccess\n");
}

static void g1_deserialize_badformat_fails()
{
printf("Starting ecp_BN254::g1_deserialize_badformat_fails...\n");

uint8_t buffer[ECP_BN254_LENGTH] = {0};
buffer[0] = 0x3;

ECP_BN254 point;
TEST_ASSERT(-2 == ecp_BN254_deserialize(&point, buffer));

printf("\tsuccess\n");
}

static void g1_deserialize_badcoords_fails()
{
printf("Starting ecp_BN254::g1_deserialize_badcoords_fails...\n");

uint8_t buffer[ECP_BN254_LENGTH] = {0};
memset(buffer, 1, sizeof(buffer)); // All 1's
buffer[0] = 0x4;

ECP_BN254 point;
TEST_ASSERT(-1 == ecp_BN254_deserialize(&point, buffer));

printf("\tsuccess\n");
}
85 changes: 85 additions & 0 deletions test/group_public_key-tests.c
Original file line number Diff line number Diff line change
@@ -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 <ecdaa/group_public_key_BN254.h>

#include "../src/amcl-extensions/ecp2_BN254.h"

#include <stdio.h>
#include <string.h>

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");
}
Loading

0 comments on commit 6827f57

Please sign in to comment.