Fix resource leak coverity 1443711.
[openssl.git] / test / chacha_internal_test.c
index 9b2f361478eb053aedfc5fed7f4955e38b13a19b..40f1f12fcd00f6b4533c235928a840e8fdcf93ff 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
  * complete 32-byte blocks. This test goes per byte...
  */
 
-#include <stdio.h>
 #include <string.h>
-
 #include <openssl/opensslconf.h>
+#include "testutil.h"
 #include "internal/chacha.h"
 
-const static unsigned int key[] = {
+static const unsigned int key[] = {
     0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
     0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c
 };
 
-const static unsigned int ivp[] = {
+static const unsigned int ivp[] = {
     0x00000000, 0x00000000, 0x03020100, 0x07060504
 };
 
-const static unsigned char ref[] = {
+static const unsigned char ref[] = {
     0xf7, 0x98, 0xa1, 0x89, 0xf1, 0x95, 0xe6, 0x69,
     0x82, 0x10, 0x5f, 0xfb, 0x64, 0x0b, 0xb7, 0x75,
     0x7f, 0x57, 0x9d, 0xa3, 0x16, 0x02, 0xfc, 0x93,
@@ -158,34 +157,34 @@ const static unsigned char ref[] = {
     0xd3, 0x3e, 0xa2, 0x15, 0x5d, 0x10, 0x5d, 0x4e
 };
 
-int main(void)
+static int test_cha_cha_internal(int n)
 {
     unsigned char buf[sizeof(ref)];
-    unsigned int i,j;
-    int ret = 0;
-
-#ifdef CPUID_OBJ
-    OPENSSL_cpuid_setup();
-#endif
+    unsigned int i = n + 1, j;
 
-    for (i = 1; i <= sizeof(ref); i++) {
-        memset(buf, 0, i);
-        memcpy(buf + i, ref + i, sizeof(ref) - i);
+    memset(buf, 0, i);
+    memcpy(buf + i, ref + i, sizeof(ref) - i);
 
-        ChaCha20_ctr32(buf, buf, i, key, ivp);
+    ChaCha20_ctr32(buf, buf, i, key, ivp);
 
-        /*
-         * Idea behind checking for whole sizeof(ref) is that if
-         * ChaCha20_ctr32 oversteps i-th byte, then we'd know
-         */
-        for (j = 0; j < sizeof(ref); j++) {
-            if (buf[j] != ref[j]) {
-                fprintf(stderr, "%u failed at %u (%02x)\n", i, j, buf[j]);
-                ret = 1;
-                break;
-            }
+    /*
+     * Idea behind checking for whole sizeof(ref) is that if
+     * ChaCha20_ctr32 oversteps i-th byte, then we'd know
+     */
+    for (j = 0; j < sizeof(ref); j++)
+        if (!TEST_uchar_eq(buf[j], ref[j])) {
+            TEST_info("%d failed at %u (%02x)\n", i, j, buf[j]);
+            return 0;
         }
-    }
+    return 1;
+}
+
+int setup_tests(void)
+{
+#ifdef CPUID_OBJ
+    OPENSSL_cpuid_setup();
+#endif
 
-    return ret;
+    ADD_ALL_TESTS(test_cha_cha_internal, sizeof(ref));
+    return 1;
 }