Codebase list openssl / a7eeefe
Fix the comment of PEM_read_bio_ex Add one more unit test case Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Tim Hudson <tjh@openssl.org> Reviewed-by: Paul Yang <yang.yang@baishancloud.com> (Merged from https://github.com/openssl/openssl/pull/6892) wzhang authored 5 years ago Paul Yang committed 5 years ago
2 changed file(s) with 23 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
879879 * Read in PEM-formatted data from the given BIO.
880880 *
881881 * By nature of the PEM format, all content must be printable ASCII (except
882 * for line endings). Other characters, or lines that are longer than 80
883 * characters, are malformed input and will be rejected.
882 * for line endings). Other characters are malformed input and will be rejected.
884883 */
885884 int PEM_read_bio_ex(BIO *bp, char **name_out, char **header,
886885 unsigned char **data, long *len_out, unsigned int flags)
1111 #include <openssl/pem.h>
1212
1313 #include "testutil.h"
14 #include "internal/nelem.h"
1415
15 static const char raw[] = "hello world";
16 static const char encoded[] = "aGVsbG8gd29ybGQ=";
17 static const char pemtype[] = "PEMTESTDATA";
16 typedef struct {
17 const char *raw;
18 const char *encoded;
19 } TESTDATA;
1820
19 static int test_b64(void)
21 static TESTDATA b64_pem_data[] = {
22 { "hello world",
23 "aGVsbG8gd29ybGQ=" },
24 { "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input",
25 "YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" }
26 };
27
28 static const char *pemtype = "PEMTESTDATA";
29
30 static int test_b64(int idx)
2031 {
2132 BIO *b = BIO_new(BIO_s_mem());
2233 char *name = NULL, *header = NULL;
2334 unsigned char *data = NULL;
2435 long len;
2536 int ret = 0;
37 const char *raw = b64_pem_data[idx].raw;
38 const char *encoded = b64_pem_data[idx].encoded;
2639
2740 if (!TEST_ptr(b)
2841 || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
3144 || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
3245 PEM_FLAG_ONLY_B64)))
3346 goto err;
34 if (!TEST_int_eq(memcmp(pemtype, name, sizeof(pemtype) - 1), 0)
35 || !TEST_int_eq(len,sizeof(raw) - 1)
36 || !TEST_int_eq(memcmp(data, raw, sizeof(raw) - 1), 0))
47 if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0)
48 || !TEST_int_eq(len, strlen(raw))
49 || !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0))
3750 goto err;
3851 ret = 1;
3952 err:
5063 char *name = NULL, *header = NULL;
5164 unsigned char *data = NULL;
5265 long len;
66 const char *encoded = b64_pem_data[0].encoded;
5367
5468 if (!TEST_ptr(b)
5569 || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
7084
7185 int setup_tests(void)
7286 {
73 ADD_TEST(test_b64);
87 ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
7488 ADD_TEST(test_invalid);
7589 return 1;
7690 }