Add conditional unit testing interface.
[openssl.git] / ssl / heartbeat_test.c
index a0a3690096d504e74881c062852f3a6cdfa065c6..a98372f959de6938651833d8628111340b729962 100644 (file)
  * http://mike-bland.com/tags/heartbleed.html
  */
 
+#define OPENSSL_UNIT_TEST
+
 #include "../test/testutil.h"
+
 #include "../ssl/ssl_locl.h"
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_SYS_WINDOWS)
+#if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_NO_UNIT_TEST)
 
 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
 #define MIN_PADDING_SIZE       16
@@ -273,7 +276,8 @@ static int test_dtls1_not_bleeding()
        {
        SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
        /* Three-byte pad at the beginning for type and payload length */
-       unsigned char payload_buf[] = "   Not bleeding, sixteen spaces of padding"
+       unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
+               "   Not bleeding, sixteen spaces of padding"
                "                ";
        const int payload_buf_len = honest_payload_size(payload_buf);
 
@@ -292,9 +296,9 @@ static int test_dtls1_not_bleeding_empty_payload()
        SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
        /* Three-byte pad at the beginning for type and payload length, plus a NUL
         * at the end */
-       unsigned char payload_buf[4 + MIN_PADDING_SIZE];
-       memset(payload_buf, ' ', sizeof(payload_buf));
-       payload_buf[sizeof(payload_buf) - 1] = '\0';
+       unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
+       memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
+       payload_buf[MIN_PADDING_SIZE+3] = '\0';
        payload_buf_len = honest_payload_size(payload_buf);
 
        fixture.payload = &payload_buf[0];
@@ -309,7 +313,8 @@ static int test_dtls1_heartbleed()
        {
        SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
        /* Three-byte pad at the beginning for type and payload length */
-       unsigned char payload_buf[] = "   HEARTBLEED                ";
+       unsigned char payload_buf[4+MAX_PRINTABLE_CHARACTERS] =
+               "   HEARTBLEED                ";
 
        fixture.payload = &payload_buf[0];
        fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
@@ -324,9 +329,9 @@ static int test_dtls1_heartbleed_empty_payload()
        SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
        /* Excluding the NUL at the end, one byte short of type + payload length +
         * minimum padding */
-       unsigned char payload_buf[MIN_PADDING_SIZE + 3];
-       memset(payload_buf, ' ', sizeof(payload_buf));
-       payload_buf[sizeof(payload_buf) - 1] = '\0';
+       unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
+       memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
+       payload_buf[MIN_PADDING_SIZE+2] = '\0';
 
        fixture.payload = &payload_buf[0];
        fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
@@ -357,8 +362,9 @@ static int test_tls1_not_bleeding()
        {
        SETUP_HEARTBEAT_TEST_FIXTURE(tls);
        /* Three-byte pad at the beginning for type and payload length */
-       unsigned char payload_buf[] = "   Not bleeding, sixteen spaces of padding"
-                                       "                ";
+       unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
+                       "   Not bleeding, sixteen spaces of padding"
+                       "                ";
        const int payload_buf_len = honest_payload_size(payload_buf);
 
        fixture.payload = &payload_buf[0];
@@ -376,9 +382,9 @@ static int test_tls1_not_bleeding_empty_payload()
        SETUP_HEARTBEAT_TEST_FIXTURE(tls);
        /* Three-byte pad at the beginning for type and payload length, plus a NUL
         * at the end */
-       unsigned char payload_buf[4 + MIN_PADDING_SIZE];
-       memset(payload_buf, ' ', sizeof(payload_buf));
-       payload_buf[sizeof(payload_buf) - 1] = '\0';
+       unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
+       memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
+       payload_buf[MIN_PADDING_SIZE+3] = '\0';
        payload_buf_len = honest_payload_size(payload_buf);
 
        fixture.payload = &payload_buf[0];
@@ -393,7 +399,8 @@ static int test_tls1_heartbleed()
        {
        SETUP_HEARTBEAT_TEST_FIXTURE(tls);
        /* Three-byte pad at the beginning for type and payload length */
-       unsigned char payload_buf[] = "   HEARTBLEED                ";
+       unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
+                       "   HEARTBLEED                ";
 
        fixture.payload = &payload_buf[0];
        fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
@@ -408,9 +415,9 @@ static int test_tls1_heartbleed_empty_payload()
        SETUP_HEARTBEAT_TEST_FIXTURE(tls);
        /* Excluding the NUL at the end, one byte short of type + payload length +
         * minimum padding */
-       unsigned char payload_buf[MIN_PADDING_SIZE + 3];
-       memset(payload_buf, ' ', sizeof(payload_buf));
-       payload_buf[sizeof(payload_buf) - 1] = '\0';
+       unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
+       memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
+       payload_buf[MIN_PADDING_SIZE+2] = '\0';
 
        fixture.payload = &payload_buf[0];
        fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;