Commit 6fc2ef20 authored by Matt Caswell's avatar Matt Caswell
Browse files

PACKET unit tests



Add some unit tests for the new PACKET API

Reviewed-by: default avatarTim Hudson <tjh@openssl.org>
parent 7e729bb5
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ HEARTBEATTEST= heartbeat_test
CONSTTIMETEST=  constant_time_test
VERIFYEXTRATEST=	verify_extra_test
CLIENTHELLOTEST=	clienthellotest
PACKETTEST=	packettest

TESTS=		alltests

@@ -87,7 +88,7 @@ EXE= $(BNTEST)$(EXE_EXT) $(ECTEST)$(EXE_EXT) $(ECDSATEST)$(EXE_EXT) $(ECDHTEST)
	$(SRPTEST)$(EXE_EXT) $(V3NAMETEST)$(EXE_EXT) \
	$(HEARTBEATTEST)$(EXE_EXT) $(P5_CRPT2_TEST)$(EXE_EXT) \
	$(CONSTTIMETEST)$(EXE_EXT) $(VERIFYEXTRATEST)$(EXE_EXT) \
	$(CLIENTHELLOTEST)$(EXE_EXT)
	$(CLIENTHELLOTEST)$(EXE_EXT) $(PACKETTEST)$(EXE_EXT)

# $(METHTEST)$(EXE_EXT)

@@ -101,7 +102,8 @@ OBJ= $(BNTEST).o $(ECTEST).o $(ECDSATEST).o $(ECDHTEST).o $(IDEATEST).o \
	$(BFTEST).o  $(SSLTEST).o  $(DSATEST).o  $(EXPTEST).o $(RSATEST).o \
	$(EVPTEST).o $(EVPEXTRATEST).o $(IGETEST).o $(JPAKETEST).o $(V3NAMETEST).o \
	$(GOST2814789TEST).o $(HEARTBEATTEST).o $(P5_CRPT2_TEST).o \
	$(CONSTTIMETEST).o $(VERIFYEXTRATEST).o $(CLIENTHELLOTEST).o testutil.o
	$(CONSTTIMETEST).o $(VERIFYEXTRATEST).o $(CLIENTHELLOTEST).o \
	$(PACKETTEST).o testutil.o

SRC=	$(BNTEST).c $(ECTEST).c  $(ECDSATEST).c $(ECDHTEST).c $(IDEATEST).c \
	$(MD2TEST).c  $(MD4TEST).c $(MD5TEST).c \
@@ -112,7 +114,8 @@ SRC= $(BNTEST).c $(ECTEST).c $(ECDSATEST).c $(ECDHTEST).c $(IDEATEST).c \
	$(BFTEST).c  $(SSLTEST).c $(DSATEST).c   $(EXPTEST).c $(RSATEST).c \
	$(EVPTEST).c $(EVPEXTRATEST).c $(IGETEST).c $(JPAKETEST).c $(V3NAMETEST).c \
	$(GOST2814789TEST).c $(HEARTBEATTEST).c $(P5_CRPT2_TEST).c \
	$(CONSTTIMETEST).c $(VERIFYEXTRATEST).c $(CLIENTHELLOTEST).c testutil.c
	$(CONSTTIMETEST).c $(VERIFYEXTRATEST).c $(CLIENTHELLOTEST).c \
	$(PACKETTEST).c testutil.c

HEADER=	testutil.h

@@ -153,7 +156,7 @@ alltests: \
	test_ige test_jpake test_secmem \
	test_srp test_cms test_v3name test_ocsp \
	test_gost2814789 test_heartbeat test_p5_crpt2 \
	test_constant_time test_verify_extra test_clienthello
	test_constant_time test_verify_extra test_clienthello test_packet

test_evp: $(EVPTEST)$(EXE_EXT) evptests.txt
	@echo $(START) $@
@@ -410,6 +413,10 @@ test_clienthello: $(CLIENTHELLOTEST)$(EXE_EXT)
	@echo $(START) $@
	../util/shlib_wrap.sh ./$(CLIENTHELLOTEST)

test_packet: $(PACKETTEST)$(EXE_EXT)
	@echo $(START) $@
	../util/shlib_wrap.sh ./$(PACKETTEST)

update: local_depend
	@if [ -z "$(THIS)" ]; then $(MAKE) -f $(TOP)/Makefile reflect THIS=$@; fi

@@ -603,6 +610,9 @@ $(VERIFYEXTRATEST)$(EXE_EXT): $(VERIFYEXTRATEST).o
$(CLIENTHELLOTEST)$(EXE_EXT): $(CLIENTHELLOTEST).o
	@target=$(CLIENTHELLOTEST) $(BUILD_CMD)

$(PACKETTEST)$(EXE_EXT): $(PACKETTEST).o
	@target=$(PACKETTEST) $(BUILD_CMD)

#$(AESTEST).o: $(AESTEST).c
#	$(CC) -c $(CFLAGS) -DINTERMEDIATE_VALUE_KAT -DTRACE_KAT_MCT $(AESTEST).c

@@ -847,6 +857,12 @@ p5_crpt2_test.o: ../include/openssl/pkcs7.h ../include/openssl/safestack.h
p5_crpt2_test.o: ../include/openssl/sha.h ../include/openssl/stack.h
p5_crpt2_test.o: ../include/openssl/symhacks.h ../include/openssl/x509.h
p5_crpt2_test.o: ../include/openssl/x509_vfy.h p5_crpt2_test.c
packettest.o: ../e_os.h ../include/openssl/bn.h ../include/openssl/buffer.h
packettest.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
packettest.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
packettest.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
packettest.o: ../include/openssl/stack.h ../include/openssl/symhacks.h
packettest.o: ../ssl/packet_locl.h packettest.c
randtest.o: ../e_os.h ../include/openssl/e_os2.h
randtest.o: ../include/openssl/opensslconf.h ../include/openssl/ossl_typ.h
randtest.o: ../include/openssl/rand.h randtest.c

test/packettest.c

0 → 100644
+317 −0
Original line number Diff line number Diff line
/* test/packettest.c */
/*
 * Written by Matt Caswell for the OpenSSL project.
 */
/* ====================================================================
 * Copyright (c) 2015 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */


#include "../ssl/packet_locl.h"

#define BUF_LEN 255

static int test_PACKET_remaining(PACKET *pkt)
{
    if (        PACKET_remaining(pkt) != BUF_LEN
            || !PACKET_forward(pkt, BUF_LEN - 1)
            ||  PACKET_remaining(pkt) != 1
            || !PACKET_forward(pkt, 1)
            ||  PACKET_remaining(pkt)) {
        fprintf(stderr, "test_PACKET_remaining() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_1(PACKET *pkt, size_t start)
{
    unsigned int i;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_1(pkt, &i)
            ||  i != 0x01
            || !PACKET_forward(pkt, BUF_LEN - 2)
            || !PACKET_get_1(pkt, &i)
            ||  i != 0xff
            ||  PACKET_get_1(pkt, &i)) {
        fprintf(stderr, "test_PACKET_get_1() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_4(PACKET *pkt, size_t start)
{
    unsigned long i;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_4(pkt, &i)
            ||  i != 0x04030201UL
            || !PACKET_forward(pkt, BUF_LEN - 8)
            || !PACKET_get_4(pkt, &i)
            ||  i != 0xfffefdfcUL
            ||  PACKET_get_4(pkt, &i)) {
        fprintf(stderr, "test_PACKET_get_4() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_net_2(PACKET *pkt, size_t start)
{
    unsigned int i;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_net_2(pkt, &i)
            ||  i != 0x0102
            || !PACKET_forward(pkt, BUF_LEN - 4)
            || !PACKET_get_net_2(pkt, &i)
            ||  i != 0xfeff
            ||  PACKET_get_net_2(pkt, &i)) {
        fprintf(stderr, "test_PACKET_get_net_2() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_net_3(PACKET *pkt, size_t start)
{
    unsigned long i;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_net_3(pkt, &i)
            ||  i != 0x010203UL
            || !PACKET_forward(pkt, BUF_LEN - 6)
            || !PACKET_get_net_3(pkt, &i)
            ||  i != 0xfdfeffUL
            ||  PACKET_get_net_3(pkt, &i)) {
        fprintf(stderr, "test_PACKET_get_net_3() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_net_4(PACKET *pkt, size_t start)
{
    unsigned long i;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_net_4(pkt, &i)
            ||  i != 0x01020304UL
            || !PACKET_forward(pkt, BUF_LEN - 8)
            || !PACKET_get_net_4(pkt, &i)
            ||  i != 0xfcfdfeffUL
            ||  PACKET_get_net_4(pkt, &i)) {
        fprintf(stderr, "test_PACKET_get_net_4() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_sub_packet(PACKET *pkt, size_t start)
{
    PACKET subpkt;
    unsigned long i;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_sub_packet(pkt, &subpkt, 4)
            || !PACKET_get_net_4(&subpkt, &i)
            ||  i != 0x01020304UL
            ||  PACKET_remaining(&subpkt)
            || !PACKET_forward(pkt, BUF_LEN - 8)
            || !PACKET_get_sub_packet(pkt, &subpkt, 4)
            || !PACKET_get_net_4(&subpkt, &i)
            ||  i != 0xfcfdfeffUL
            ||  PACKET_remaining(&subpkt)
            ||  PACKET_get_sub_packet(pkt, &subpkt, 4)) {
        fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_get_bytes(PACKET *pkt, size_t start)
{
    unsigned char *bytes;

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_get_bytes(pkt, &bytes, 4)
            ||  bytes[0] != 1 || bytes[1] != 2
            ||  bytes[2] != 3 || bytes[3] != 4
            ||  PACKET_remaining(pkt) != BUF_LEN -4
            || !PACKET_forward(pkt, BUF_LEN - 8)
            || !PACKET_get_bytes(pkt, &bytes, 4)
            ||  bytes[0] != 0xfc || bytes[1] != 0xfd
            ||  bytes[2] != 0xfe || bytes[3] != 0xff
            ||  PACKET_remaining(pkt)) {
        fprintf(stderr, "test_PACKET_get_bytes() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_copy_bytes(PACKET *pkt, size_t start)
{
    unsigned char bytes[4];

    if (       !PACKET_goto_bookmark(pkt, start)
            || !PACKET_copy_bytes(pkt, bytes, 4)
            ||  bytes[0] != 1 || bytes[1] != 2
            ||  bytes[2] != 3 || bytes[3] != 4
            ||  PACKET_remaining(pkt) != BUF_LEN - 4
            || !PACKET_forward(pkt, BUF_LEN - 8)
            || !PACKET_copy_bytes(pkt, bytes, 4)
            ||  bytes[0] != 0xfc || bytes[1] != 0xfd
            ||  bytes[2] != 0xfe || bytes[3] != 0xff
            ||  PACKET_remaining(pkt)) {
        fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_move_funcs(PACKET *pkt, size_t start)
{
    unsigned char *byte;
    size_t bm;

    if (       !PACKET_goto_bookmark(pkt, start)
            ||  PACKET_back(pkt, 1)
            || !PACKET_forward(pkt, 1)
            || !PACKET_get_bytes(pkt, &byte, 1)
            ||  byte[0] != 2
            || !PACKET_get_bookmark(pkt, &bm)
            || !PACKET_forward(pkt, BUF_LEN - 2)
            ||  PACKET_forward(pkt, 1)
            || !PACKET_back(pkt, 1)
            || !PACKET_get_bytes(pkt, &byte, 1)
            ||  byte[0] != 0xff
            || !PACKET_goto_bookmark(pkt, bm)
            || !PACKET_get_bytes(pkt, &byte, 1)
            ||  byte[0] != 3) {
        fprintf(stderr, "test_PACKET_move_funcs() failed\n");
        return 0;
    }

    return 1;
}

static int test_PACKET_buf_init()
{
    unsigned char buf[BUF_LEN];
    size_t len;
    PACKET pkt;

    /* Also tests PACKET_get_len() */
    if (       !PACKET_buf_init(&pkt, buf, 4)
            || !PACKET_length(&pkt, &len)
            ||  len != 4
            || !PACKET_buf_init(&pkt, buf, BUF_LEN)
            || !PACKET_length(&pkt, &len)
            ||  len != BUF_LEN
            ||  pkt.end - pkt.start != BUF_LEN
            ||  pkt.end < pkt.start
            ||  pkt.curr < pkt.start
            ||  pkt.curr > pkt.end
            ||  PACKET_buf_init(&pkt, buf, -1)) {
        fprintf(stderr, "test_PACKET_buf_init() failed\n");
        return 0;
        }

    return 1;
}

int main(int argc, char **argv)
{
    unsigned char buf[BUF_LEN];
    unsigned int i;
    size_t start = 0;
    PACKET pkt;

    for (i=1; i<=BUF_LEN; i++) {
        buf[i-1] = i;
    }
    i = 0;

    if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
            || !PACKET_get_bookmark(&pkt, &start)) {
        fprintf(stderr, "setup failed\n");
        return 0;
    }

    if (       !test_PACKET_buf_init()
            || !test_PACKET_remaining(&pkt)
            || !test_PACKET_get_1(&pkt, start)
            || !test_PACKET_get_4(&pkt, start)
            || !test_PACKET_get_net_2(&pkt, start)
            || !test_PACKET_get_net_3(&pkt, start)
            || !test_PACKET_get_net_4(&pkt, start)
            || !test_PACKET_get_sub_packet(&pkt, start)
            || !test_PACKET_get_bytes(&pkt, start)
            || !test_PACKET_copy_bytes(&pkt, start)
            || !test_PACKET_move_funcs(&pkt, start)) {
        return 1;
    }
    printf("PASS\n");
    return 0;
}