Update Makefile and code style in tests

This commit is contained in:
Zhi Guan
2022-07-28 16:42:54 +08:00
parent f1da2d5528
commit 5dadb639a8
13 changed files with 340 additions and 358 deletions

View File

@@ -53,6 +53,7 @@
#include <gmssl/asn1.h>
#include <gmssl/error.h>
static void print_buf(const uint8_t *a, size_t len)
{
size_t i;
@@ -103,7 +104,7 @@ static int test_asn1_tag(void)
format_print(stderr, 0, 4, "%s (0x%02x)\n", asn1_tag_name(i), i);
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_length(void)
@@ -153,7 +154,7 @@ static int test_asn1_length(void)
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_boolean(void)
@@ -188,7 +189,7 @@ static int test_asn1_boolean(void)
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_int(void)
@@ -249,7 +250,7 @@ static int test_asn1_int(void)
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_bits(void)
@@ -298,7 +299,7 @@ static int test_asn1_bits(void)
if (asn1_bits_from_der(&bits, &cp, &len) != 1
|| asn1_check(bits == tests[i]) != 1) {
error_print();
return 1;
return -1;
}
format_print(stderr, 0, 4, "%x\n", bits);
}
@@ -307,7 +308,7 @@ static int test_asn1_bits(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_null(void)
@@ -338,15 +339,15 @@ static int test_asn1_null(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_object_identifier(void)
{
int err = 0;
format_print(stderr, 0, 0, "%s\n", asn1_tag_name(ASN1_TAG_OBJECT_IDENTIFIER));
if (1) {
// test 1
{
char *name = "sm2";
uint32_t oid[] = { 1,2,156,10197,1,301 };
uint8_t der[] = { 0x06, 0x08, 0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x82, 0x2D };
@@ -364,15 +365,16 @@ static int test_asn1_object_identifier(void)
|| asn1_object_identifier_from_der(nodes, &nodes_cnt, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1
|| asn1_object_identifier_equ(nodes, nodes_cnt, oid, sizeof(oid)/sizeof(int)) != 1) {
printf("failed\n");
fprintf(stderr, "failed\n");
error_print();
err++;
return -1;
} else {
printf("ok\n");
}
}
if (2) {
// test 2
{
char *name = "x9.62-ecPublicKey";
uint32_t oid[] = { 1,2,840,10045,2,1 };
uint8_t der[] = { 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01 };
@@ -390,16 +392,16 @@ static int test_asn1_object_identifier(void)
|| asn1_object_identifier_from_der(nodes, &nodes_cnt, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1
|| asn1_object_identifier_equ(nodes, nodes_cnt, oid, sizeof(oid)/sizeof(int)) != 1) {
printf("failed\n");
fprintf(stderr, "failed\n");
error_print();
err++;
return -1;
} else {
printf("ok\n");
}
}
if (!err) printf("%s() ok\n", __FUNCTION__);
return err;
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_asn1_printable_string(void)
@@ -419,7 +421,7 @@ static int test_asn1_printable_string(void)
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if (asn1_printable_string_to_der(tests[i], strlen(tests[i]), &p, &len) != 1) {
error_print();
return 1;
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
@@ -430,7 +432,7 @@ static int test_asn1_printable_string(void)
|| strlen(tests[i]) != dlen
|| memcmp(tests[i], d, dlen) != 0) {
error_print();
return 1;
return -1;
}
format_string(stderr, 0, 4, "", (uint8_t *)d, dlen);
}
@@ -439,7 +441,7 @@ static int test_asn1_printable_string(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_utf8_string(void)
@@ -459,7 +461,7 @@ static int test_asn1_utf8_string(void)
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if (asn1_utf8_string_to_der(tests[i], strlen(tests[i]), &p, &len) != 1) {
error_print();
return 1;
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
@@ -470,7 +472,7 @@ static int test_asn1_utf8_string(void)
|| strlen(tests[i]) != dlen
|| memcmp(tests[i], d, dlen) != 0) {
error_print();
return 1;
return -1;
}
format_string(stderr, 0, 4, "", (uint8_t *)d, dlen);
}
@@ -479,7 +481,7 @@ static int test_asn1_utf8_string(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_ia5_string(void)
@@ -499,7 +501,7 @@ static int test_asn1_ia5_string(void)
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if (asn1_ia5_string_to_der(tests[i], strlen(tests[i]), &p, &len) != 1) {
error_print();
return 1;
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
@@ -510,7 +512,7 @@ static int test_asn1_ia5_string(void)
|| strlen(tests[i]) != dlen
|| memcmp(tests[i], d, dlen) != 0) {
error_print();
return 1;
return -1;
}
format_string(stderr, 0, 4, "", (uint8_t *)d, dlen);
}
@@ -519,7 +521,7 @@ static int test_asn1_ia5_string(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_time(void)
@@ -531,7 +533,7 @@ static int test_time(void)
printf("%08x%08x\n", (uint32_t)(tval >> 32), (uint32_t)tval);
return 0;
return 1;
}
static int test_asn1_utc_time(void)
@@ -571,7 +573,7 @@ static int test_asn1_utc_time(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_asn1_generalized_time(void)
@@ -592,7 +594,7 @@ static int test_asn1_generalized_time(void)
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if (asn1_generalized_time_to_der(tests[i], &p, &len) != 1) {
error_print();
return 1;
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
@@ -601,7 +603,7 @@ static int test_asn1_generalized_time(void)
if (asn1_generalized_time_from_der(&tv, &cp, &len) != 1
|| asn1_check(tv == tests[i]) != 1) {
error_print();
return 1;
return -1;
}
format_print(stderr, 0, 4, "%s", ctime(&tv));
}
@@ -610,24 +612,26 @@ static int test_asn1_generalized_time(void)
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
int main(void)
{
int err = 0;
err += test_asn1_tag();
err += test_asn1_length();
err += test_asn1_boolean();
err += test_asn1_int();
err += test_asn1_bits();
err += test_asn1_null();
err += test_asn1_object_identifier();
err += test_asn1_printable_string();
err += test_asn1_utf8_string();
err += test_asn1_ia5_string();
err += test_asn1_utc_time();
err += test_asn1_generalized_time();
return err;
if (test_asn1_tag() != 1) goto err;
if (test_asn1_length() != 1) goto err;
if (test_asn1_boolean() != 1) goto err;
if (test_asn1_int() != 1) goto err;
if (test_asn1_bits() != 1) goto err;
if (test_asn1_null() != 1) goto err;
if (test_asn1_object_identifier() != 1) goto err;
if (test_asn1_printable_string() != 1) goto err;
if (test_asn1_utf8_string() != 1) goto err;
if (test_asn1_ia5_string() != 1) goto err;
if (test_asn1_utc_time() != 1) goto err;
if (test_asn1_generalized_time() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -53,10 +53,9 @@
#include <gmssl/base64.h>
#include <gmssl/error.h>
int test_base64(void)
{
int err = 0;
static int test_base64(void)
{
uint8_t bin1[50];
uint8_t bin2[100];
uint8_t bin3[200];
@@ -92,17 +91,20 @@ int test_base64(void)
|| memcmp(buf2 + sizeof(bin1), bin2, sizeof(bin2)) != 0
|| memcmp(buf2 + sizeof(bin1) + sizeof(bin2), bin3, sizeof(bin3)) != 0) {
printf("failed\n");
err++;
return -1;
} else {
printf("ok\n");
}
return err;
return 1;
}
int main(void)
{
int err = 0;
err += test_base64();
return err;
if (test_base64() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -53,6 +53,5 @@
int main(void)
{
int err = 0;
return err;
return 0;
}

View File

@@ -96,7 +96,7 @@ static int test_ec_named_curve(void)
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_ec_point_print(void)
@@ -117,7 +117,7 @@ static int test_ec_point_print(void)
ec_point_print(stderr, 0, 4, "ECPoint", buf, len);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_ec_private_key_print(void)
@@ -143,14 +143,17 @@ static int test_ec_private_key_print(void)
ec_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
int main(void)
{
int err = 0;
err += test_ec_named_curve();
err += test_ec_point_print();
err += test_ec_private_key_print();
return err;
if (test_ec_named_curve() != 1) goto err;
if (test_ec_point_print() != 1) goto err;
if (test_ec_private_key_print() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -94,16 +94,16 @@ static int test_tls_encode(void)
|| tls_uint24array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 7 || memcmp(pdata, data, 7) != 0
|| len > 0) {
error_print();
return 1;
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_cbc(void)
{
uint8_t key[32];
uint8_t key[32] = {0};
SM3_HMAC_CTX hmac_ctx;
SM4_KEY sm4_key;
uint8_t seq_num[8] = { 0,0,0,0,0,0,0,1 };
@@ -114,22 +114,23 @@ static int test_tls_cbc(void)
size_t len;
size_t buflen;
header[0] = TLS_record_handshake;
header[1] = TLS_protocol_tls12 >> 8;
header[2] = TLS_protocol_tls12 & 0xff;
header[3] = sizeof(in) >> 8;
header[4] = sizeof(in) & 0xff;
sm3_hmac_init(&hmac_ctx, key, 32);
sm4_set_encrypt_key(&sm4_key, key);
tls_cbc_encrypt(&hmac_ctx, &sm4_key, seq_num, header, in, sizeof(in), out, &len);
printf("%zu\n", len);
print_der(out, len);
printf("\n");
sm3_hmac_init(&hmac_ctx, key, 32);
sm4_set_decrypt_key(&sm4_key, key);
tls_cbc_decrypt(&hmac_ctx, &sm4_key, seq_num, header, out, len, buf, &buflen);
printf("%s\n", buf);
return 0;
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_random(void)
@@ -139,7 +140,7 @@ static int test_tls_random(void)
tls_random_print(stdout, random, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_client_hello(void)
@@ -147,24 +148,25 @@ static int test_tls_client_hello(void)
uint8_t record[512];
size_t recordlen = 0;
int version = TLS_version_tlcp;
int version = TLS_protocol_tlcp;
uint8_t random[32];
int cipher_suites[] = {
TLCP_cipher_ecc_sm4_cbc_sm3,
TLCP_cipher_ecc_sm4_gcm_sm3,
TLCP_cipher_ecdhe_sm4_cbc_sm3,
TLCP_cipher_ecdhe_sm4_gcm_sm3,
TLCP_cipher_ibsdh_sm4_cbc_sm3,
TLCP_cipher_ibsdh_sm4_gcm_sm3,
TLCP_cipher_ibc_sm4_cbc_sm3,
TLCP_cipher_ibc_sm4_gcm_sm3,
TLCP_cipher_rsa_sm4_cbc_sm3,
TLCP_cipher_rsa_sm4_gcm_sm3,
TLCP_cipher_rsa_sm4_cbc_sha256,
TLCP_cipher_rsa_sm4_gcm_sha256,
TLS_cipher_ecc_sm4_cbc_sm3,
TLS_cipher_ecc_sm4_gcm_sm3,
TLS_cipher_ecdhe_sm4_cbc_sm3,
TLS_cipher_ecdhe_sm4_gcm_sm3,
TLS_cipher_ibsdh_sm4_cbc_sm3,
TLS_cipher_ibsdh_sm4_gcm_sm3,
TLS_cipher_ibc_sm4_cbc_sm3,
TLS_cipher_ibc_sm4_gcm_sm3,
TLS_cipher_rsa_sm4_cbc_sm3,
TLS_cipher_rsa_sm4_gcm_sm3,
TLS_cipher_rsa_sm4_cbc_sha256,
TLS_cipher_rsa_sm4_gcm_sha256,
};
int comp_meths[] = {0};
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tls_record_set_handshake_client_hello(record, &recordlen,
version,
random,
@@ -177,7 +179,7 @@ static int test_tls_client_hello(void)
tls_client_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 4);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_server_hello(void)
@@ -186,12 +188,12 @@ static int test_tls_server_hello(void)
size_t recordlen = 0;
uint8_t random[32];
uint16_t cipher_suite = TLCP_cipher_ecdhe_sm4_cbc_sm3;
uint16_t cipher_suite = TLS_cipher_ecdhe_sm4_cbc_sm3;
tls_record_set_version(record, TLS_version_tlcp);
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tls_record_set_handshake_server_hello(record, &recordlen,
TLS_version_tlcp,
TLS_protocol_tlcp,
random,
NULL, 0,
cipher_suite,
@@ -202,7 +204,7 @@ static int test_tls_server_hello(void)
tls_server_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_certificate(void)
@@ -212,6 +214,7 @@ static int test_tls_certificate(void)
FILE *fp = NULL;
// 测试函数不要有外部的依赖
// TODO: 输出一些握手过程的record字节数组和handshake字节数组作为后续测试的测试数据
/*
if (!(fp = fopen("cacert.pem", "r"))) {
@@ -226,51 +229,53 @@ static int test_tls_certificate(void)
*/
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_server_key_exchange(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t sig[77] = {0xAA, 0xBB};
uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0xAA, 0xBB};
const uint8_t *psig;
size_t siglen;
tls_record_set_version(record, TLS_version_tlcp);
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tlcp_record_set_handshake_server_key_exchange_pke(record, &recordlen, sig, sizeof(sig)) != 1) {
error_print();
return -1;
}
if (tlcp_record_get_handshake_server_key_exchange_pke(record, sig, &siglen) != 1) {
if (tlcp_record_get_handshake_server_key_exchange_pke(record, &psig, &siglen) != 1) {
error_print();
return -1;
}
tls_server_key_exchange_print(stdout, sig, siglen, TLCP_cipher_ecc_sm4_gcm_sm3 << 8, 0);
format_bytes(stdout, 0, 0, "server_key_exchange siganture", psig, siglen);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_certificate_verify(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t sig[77];
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
const uint8_t *psig;
size_t siglen;
tls_record_set_version(record, TLS_version_tls12);
tls_record_set_protocol(record, TLS_protocol_tls12);
if (tls_record_set_handshake_certificate_verify(record, &recordlen, sig, sizeof(sig)) != 1) {
error_print();
return -1;
}
if (tls_record_get_handshake_certificate_verify(record, sig, &siglen) != 1) {
if (tls_record_get_handshake_certificate_verify(record, &psig, &siglen) != 1) {
error_print();
return -1;
}
tls_certificate_verify_print(stdout, sig, siglen, 0, 0);
tls_certificate_verify_print(stdout, psig, siglen, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_finished(void)
@@ -278,19 +283,21 @@ static int test_tls_finished(void)
uint8_t record[1024];
size_t recordlen = 0;
uint8_t verify_data[12];
const uint8_t *verify_data_ptr;
size_t verify_data_len;
if (tls_record_set_handshake_finished(record, &recordlen, verify_data) != 1) {
if (tls_record_set_handshake_finished(record, &recordlen, verify_data, sizeof(verify_data)) != 1) {
error_print();
return -1;
}
if (tls_record_get_handshake_finished(record, verify_data) != 1) {
if (tls_record_get_handshake_finished(record, &verify_data_ptr, &verify_data_len) != 1) {
error_print();
return -1;
}
tls_finished_print(stdout, verify_data, 12, 0, 0);
tls_finished_print(stdout, verify_data_ptr, verify_data_len, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_alert(void)
@@ -311,7 +318,7 @@ static int test_tls_alert(void)
tls_alert_print(stdout, record + 5, recordlen - 5, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_change_cipher_spec(void)
@@ -330,7 +337,7 @@ static int test_tls_change_cipher_spec(void)
tls_change_cipher_spec_print(stdout, record + 5, recordlen - 5, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
static int test_tls_application_data(void)
@@ -352,24 +359,26 @@ static int test_tls_application_data(void)
tls_application_data_print(stdout, p, len, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 0;
return 1;
}
int main(void)
{
int err = 0;
err += test_tls_encode();
err += test_tls_cbc();
err += test_tls_random();
err += test_tls_client_hello();
err += test_tls_server_hello();
err += test_tls_certificate();
err += test_tls_server_key_exchange();
err += test_tls_certificate_verify();
err += test_tls_finished();
err += test_tls_alert();
err += test_tls_change_cipher_spec();
err += test_tls_application_data();
if (err == 0) printf("%s all tests passed\n", __FILE__);
return err;
if (test_tls_encode() != 1) goto err;
if (test_tls_cbc() != 1) goto err;
if (test_tls_random() != 1) goto err;
if (test_tls_client_hello() != 1) goto err;
if (test_tls_server_hello() != 1) goto err;
if (test_tls_certificate() != 1) goto err;
if (test_tls_server_key_exchange() != 1) goto err;
if (test_tls_certificate_verify() != 1) goto err;
if (test_tls_finished() != 1) goto err;
if (test_tls_alert() != 1) goto err;
if (test_tls_change_cipher_spec() != 1) goto err;
if (test_tls_application_data() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -506,18 +506,10 @@ static int test_x509_policy_mapping(void)
// 这里的一些OID应该在RFC中有但是我们不实现
static int test_x509_attribute(void)
{
return -1;
// TODO
return 1;
}
static int test_x509_basic_constraints(void)
{
uint8_t buf[256];
@@ -880,10 +872,6 @@ static int test_x509_cert_with_exts(void)
return 1;
}
int main(int argc, char **argv)
{
if (test_x509_other_name() != 1) goto err;