Debian Codegraph @

test/test_stream_crypt.cc

raw symbols SCIP
1 /* test_stream_crypt.cc - Test encryption stream functionality
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6
7 This program is distributed under the terms of the same license as zlib.
8 See the accompanying LICENSE file for the full text of the license.
9 */
10
11 #include "mz.h"
12 #include "mz_strm.h"
13 #include "mz_strm_os.h"
14 #include "mz_strm_pkcrypt.h"
15 #include "mz_strm_wzaes.h"
16
17 #include <gtest/gtest.h>
18
19 #include <stdio.h> /* printf, snprintf */
20
21 #if defined(_MSC_VER) && (_MSC_VER < 1900)
22 # define snprintf _snprintf
23 #endif
24
25 static void test_encrypt(const char *path, const char *method, mz_stream_create_cb crypt_create, const char *password) {
26 char org_buf[4096];
27 char mod_buf[4096];
28 int32_t read = 0;
29 int32_t written = 0;
30 int64_t total_written = 0;
31 void *out_stream = NULL;
32 void *in_stream = NULL;
33 void *crypt_out_stream = NULL;
34 char encrypt_path[256];
35 char decrypt_path[256];
36
37 snprintf(encrypt_path, sizeof(encrypt_path), "%s.enc.%s", path, method);
38 snprintf(decrypt_path, sizeof(decrypt_path), "%s.dec.%s", path, method);
39
40 /* Read file to encrypt into memory buffer */
41 in_stream = mz_stream_os_create();
42 ASSERT_NE(in_stream, nullptr);
43 EXPECT_EQ(mz_stream_os_open(in_stream, path, MZ_OPEN_MODE_READ), MZ_OK);
44 {
45 read = mz_stream_os_read(in_stream, org_buf, sizeof(org_buf));
46 mz_stream_os_close(in_stream);
47 }
48 mz_stream_os_delete(&in_stream);
49 EXPECT_GT(read, 0);
50
51 /* Encrypt data to disk */
52 out_stream = mz_stream_os_create();
53 ASSERT_NE(out_stream, nullptr);
54 EXPECT_EQ(mz_stream_os_open(out_stream, encrypt_path, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE), MZ_OK);
55 {
56 crypt_out_stream = crypt_create();
57 ASSERT_NE(crypt_out_stream, nullptr);
58
59 mz_stream_set_base(crypt_out_stream, out_stream);
60
61 EXPECT_EQ(mz_stream_open(crypt_out_stream, password, MZ_OPEN_MODE_WRITE), MZ_OK);
62 {
63 written = mz_stream_write(crypt_out_stream, org_buf, read);
64 mz_stream_close(crypt_out_stream);
65 mz_stream_get_prop_int64(crypt_out_stream, MZ_STREAM_PROP_TOTAL_OUT, &total_written);
66 }
67
68 mz_stream_delete(&crypt_out_stream);
69 mz_stream_os_close(out_stream);
70 }
71 mz_stream_os_delete(&out_stream);
72 EXPECT_GT(written, 0);
73
74 /* Decrypt data from disk */
75 in_stream = mz_stream_os_create();
76 ASSERT_NE(in_stream, nullptr);
77 EXPECT_EQ(mz_stream_os_open(in_stream, encrypt_path, MZ_OPEN_MODE_READ), MZ_OK);
78 {
79 crypt_out_stream = crypt_create();
80 ASSERT_NE(crypt_out_stream, nullptr);
81
82 mz_stream_set_base(crypt_out_stream, in_stream);
83 mz_stream_set_prop_int64(crypt_out_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, total_written);
84
85 EXPECT_EQ(mz_stream_open(crypt_out_stream, password, MZ_OPEN_MODE_READ), MZ_OK);
86 {
87 ASSERT_LE(read, sizeof(mod_buf));
88 read = mz_stream_read(crypt_out_stream, mod_buf, read);
89 mz_stream_close(crypt_out_stream);
90 }
91
92 mz_stream_delete(&crypt_out_stream);
93 mz_stream_os_close(in_stream);
94 }
95 mz_stream_os_delete(&in_stream);
96 EXPECT_GT(read, 0);
97
98 /* Write out decrypted contents to disk for debugging */
99 out_stream = mz_stream_os_create();
100 ASSERT_NE(out_stream, nullptr);
101 EXPECT_EQ(mz_stream_os_open(out_stream, decrypt_path, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE), MZ_OK);
102 {
103 mz_stream_os_write(out_stream, mod_buf, read);
104 mz_stream_os_close(out_stream);
105 }
106 mz_stream_os_delete(&out_stream);
107
108 /* Compare original and modified buffers */
109 EXPECT_EQ(memcmp(org_buf, mod_buf, read), 0);
110 }
111
112 #ifdef HAVE_PKCRYPT
113 TEST(encrypt, pkcrypt) {
114 test_encrypt("LICENSE", "pkcrypt", mz_stream_pkcrypt_create, "hello");
115 }
116 #endif
117
118 #ifdef HAVE_WZAES
119 TEST(encrypt, aes) {
120 test_encrypt("LICENSE", "aes", mz_stream_wzaes_create, "hello");
121 }
122 #endif