diff --git a/include/spdk/base64.h b/include/spdk/base64.h new file mode 100644 index 000000000..c158c2316 --- /dev/null +++ b/include/spdk/base64.h @@ -0,0 +1,138 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS 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 COPYRIGHT + * OWNER OR 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. + */ + +/** + * \file + * Base64 utility functions + */ + +#ifndef SPDK_BASE64_H +#define SPDK_BASE64_H + +#include "spdk/stdinc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Following the Base64 part in RFC4648: + * https://tools.ietf.org/html/rfc4648.html + */ + +/** + * Calculate strlen of encoded Base64 string based on raw buffer length. + * + * \param raw_len Length of raw buffer. + * \return Encoded Base64 string length, excluding the terminating null byte ('\0'). + */ +static inline size_t spdk_base64_get_encoded_strlen(size_t raw_len) +{ + return (raw_len + 2) / 3 * 4; +} + +/** + * Calculate length of raw buffer based on strlen of encoded Base64. + * + * \param encoded_strlen Length of enocded Base64 string, excluding terminating null byte ('\0'). + * \return Length of raw buffer. + */ +static inline size_t spdk_base64_get_decoded_len(size_t encoded_strlen) +{ + /* text_strlen and raw_len should be (4n,3n), (4n+2, 3n+1) or (4n+3, 3n+2) */ + return encoded_strlen / 4 * 3 + ((encoded_strlen % 4 + 1) / 2); +} + +/** + * Base 64 Encoding with Standard Base64 Alphabet defined in RFC4684. + * + * \param dst Buffer address of encoded Base64 string. Its length should be enough + * to contain Base64 string and the terminating null byte ('\0'), so it needs to be at + * least as long as 1 + spdk_base64_get_encoded_strlen(src_len). + * \param src Raw data buffer to be encoded. + * \param src_len Length of raw data buffer. + * + * \return 0 on success. + * \return -EINVAL if dst or src is NULL, or binary_len <= 0. + */ +int spdk_base64_encode(char *dst, const void *src, size_t src_len); + +/** + * Base 64 Encoding with URL and Filename Safe Alphabet. + * + * \param dst Buffer address of encoded Base64 string. Its length should be enough + * to contain Base64 string and the terminating null byte ('\0'), so it needs to be at + * least as long as 1 + spdk_base64_get_encoded_strlen(src_len). + * \param src Raw data buffer to be encoded. + * \param src_len Length of raw data buffer. + * + * \return 0 on success. + * \return -EINVAL if dst or src is NULL, or binary_len <= 0. + */ +int spdk_base64_urlsafe_encode(char *dst, const void *src, size_t src_len); + +/** + * Base 64 Decoding with Standard Base64 Alphabet defined in RFC4684. + * + * \param dst Buffer address of decoded raw data. Its length should be enough + * to contain decoded raw data, so it needs to be at least as long as + * spdk_base64_get_decoded_len(encoded_strlen). + * \param dst_len Output parameter for the length of actual decoded raw data. + * If NULL, the actual decoded length won't be returned. + * \param src Data buffer for base64 string to be decoded. + * + * \return 0 on success. + * \return -EINVAL if dst or src is NULL, or content of src is illegal. + */ +int spdk_base64_decode(void *dst, size_t *dst_len, const char *src); + +/** + * Base 64 Decoding with URL and Filename Safe Alphabet. + * + * \param dst Buffer address of decoded raw data. Its length should be enough + * to contain decoded raw data, so it needs to be at least as long as + * spdk_base64_get_decoded_len(encoded_strlen). + * \param dst_len Output parameter for the length of actual decoded raw data. + * If NULL, the actual decoded length won't be returned. + * \param src Data buffer for base64 string to be decoded. + * + * \return 0 on success. + * \return -EINVAL if dst or src is NULL, or content of src is illegal. + */ +int spdk_base64_urlsafe_decode(void *dst, size_t *dst_len, const char *src); + +#ifdef __cplusplus +} +#endif + +#endif /* SPDK_BASE64_H */ diff --git a/lib/util/Makefile b/lib/util/Makefile index 4b993590f..1dd0f395c 100644 --- a/lib/util/Makefile +++ b/lib/util/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -C_SRCS = bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c fd.c strerror_tls.c string.c uuid.c +C_SRCS = base64.c bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c fd.c strerror_tls.c string.c uuid.c LIBNAME = util include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/lib/util/base64.c b/lib/util/base64.c new file mode 100644 index 000000000..81361263b --- /dev/null +++ b/lib/util/base64.c @@ -0,0 +1,228 @@ +/*- + * BSD LICENSE + * + * Copyright(c) Intel Corporation. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS 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 COPYRIGHT + * OWNER OR 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. + */ + +#include "spdk/stdinc.h" +#include "spdk/endian.h" +#include "spdk/base64.h" + +#define BASE64_ENC_BITMASK 0x3FUL +#define BASE64_PADDING_CHAR '=' + +static const char base64_enc_table[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +static const char base64_urfsafe_enc_table[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789-_"; + +static const uint8_t +base64_dec_table[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255, + 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, + 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +}; + +static const uint8_t +base64_urlsafe_dec_table[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255, + 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 63, + 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +}; + +static int +_spdk_base64_encode(char *dst, const char *enc_table, const void *src, size_t src_len) +{ + uint32_t raw_u32; + + if (!dst || !src || src_len <= 0) { + return -EINVAL; + } + + while (src_len >= 4) { + raw_u32 = from_be32(src); + + *dst++ = enc_table[(raw_u32 >> 26) & BASE64_ENC_BITMASK]; + *dst++ = enc_table[(raw_u32 >> 20) & BASE64_ENC_BITMASK]; + *dst++ = enc_table[(raw_u32 >> 14) & BASE64_ENC_BITMASK]; + *dst++ = enc_table[(raw_u32 >> 8) & BASE64_ENC_BITMASK]; + + src_len -= 3; + src += 3; + } + + if (src_len == 0) { + goto out; + } + + raw_u32 = 0; + memcpy(&raw_u32, src, src_len); + raw_u32 = from_be32(&raw_u32); + + *dst++ = enc_table[(raw_u32 >> 26) & BASE64_ENC_BITMASK]; + *dst++ = enc_table[(raw_u32 >> 20) & BASE64_ENC_BITMASK]; + *dst++ = (src_len >= 2) ? enc_table[(raw_u32 >> 14) & BASE64_ENC_BITMASK] : BASE64_PADDING_CHAR; + *dst++ = (src_len == 3) ? enc_table[(raw_u32 >> 8) & BASE64_ENC_BITMASK] : BASE64_PADDING_CHAR; + +out: + *dst = '\0'; + + return 0; +} + +int +spdk_base64_encode(char *dst, const void *src, size_t src_len) +{ + return _spdk_base64_encode(dst, base64_enc_table, src, src_len); +} + +int +spdk_base64_urlsafe_encode(char *dst, const void *src, size_t src_len) +{ + return _spdk_base64_encode(dst, base64_urfsafe_enc_table, src, src_len); +} + +static int +_spdk_base64_decode(void *dst, size_t *_dst_len, const uint8_t *dec_table, const char *src) +{ + size_t src_strlen, dst_len; + size_t tail_len = 0; + const uint8_t *src_in; + uint32_t tmp[4]; + int i; + + if (!dst || !src) { + return -EINVAL; + } + + src_strlen = strlen(src); + + /* strlen of src should be 4n */ + if (src_strlen == 0 || src_strlen % 4 != 0) { + return -EINVAL; + } + + /* Consider Base64 padding, it at most has 2 padding characters. */ + for (i = 0; i < 2; i++) { + if (src[src_strlen - 1] != BASE64_PADDING_CHAR) { + break; + } + src_strlen--; + } + + /* strlen of src without padding shouldn't be 4n+1 */ + if (src_strlen == 0 || src_strlen % 4 == 1) { + return -EINVAL; + } + + dst_len = spdk_base64_get_decoded_len(src_strlen); + src_in = (const uint8_t *) src; + + /* space of dst can be used by to_be32 */ + while (src_strlen > 4) { + tmp[0] = dec_table[*src_in++]; + tmp[1] = dec_table[*src_in++]; + tmp[2] = dec_table[*src_in++]; + tmp[3] = dec_table[*src_in++]; + + if (tmp[0] == 255 || tmp[1] == 255 || tmp[2] == 255 || tmp[3] == 255) { + return -EINVAL; + } + + to_be32(dst, tmp[3] << 8 | tmp[2] << 14 | tmp[1] << 20 | tmp[0] << 26); + + dst += 3; + src_strlen -= 4; + } + + /* space of dst is not enough to be used by to_be32 */ + tmp[0] = dec_table[src_in[0]]; + tmp[1] = dec_table[src_in[1]]; + tmp[2] = (src_strlen >= 3) ? dec_table[src_in[2]] : 0; + tmp[3] = (src_strlen == 4) ? dec_table[src_in[3]] : 0; + tail_len = src_strlen - 1; + + if (tmp[0] == 255 || tmp[1] == 255 || tmp[2] == 255 || tmp[3] == 255) { + return -EINVAL; + } + + to_be32(&tmp[3], tmp[3] << 8 | tmp[2] << 14 | tmp[1] << 20 | tmp[0] << 26); + memcpy(dst, (uint8_t *)&tmp[3], tail_len); + + /* Assign pointers */ + if (_dst_len) { + *_dst_len = dst_len; + } + + return 0; +} + +int +spdk_base64_decode(void *dst, size_t *dst_len, const char *src) +{ + return _spdk_base64_decode(dst, dst_len, base64_dec_table, src); +} + +int +spdk_base64_urlsafe_decode(void *dst, size_t *dst_len, const char *src) +{ + return _spdk_base64_decode(dst, dst_len, base64_urlsafe_dec_table, src); +} diff --git a/test/unit/lib/util/Makefile b/test/unit/lib/util/Makefile index 00f68d63b..4813e63b7 100644 --- a/test/unit/lib/util/Makefile +++ b/test/unit/lib/util/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -DIRS-y = bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c string.c +DIRS-y = base64.c bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c string.c .PHONY: all clean $(DIRS-y) diff --git a/test/unit/lib/util/base64.c/.gitignore b/test/unit/lib/util/base64.c/.gitignore new file mode 100644 index 000000000..a5b175236 --- /dev/null +++ b/test/unit/lib/util/base64.c/.gitignore @@ -0,0 +1 @@ +base64_ut diff --git a/test/unit/lib/util/base64.c/Makefile b/test/unit/lib/util/base64.c/Makefile new file mode 100644 index 000000000..ff6c92142 --- /dev/null +++ b/test/unit/lib/util/base64.c/Makefile @@ -0,0 +1,40 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * 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. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS 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 COPYRIGHT +# OWNER OR 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. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk +include $(SPDK_ROOT_DIR)/mk/spdk.app.mk + +TEST_FILE = base64_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/test/unit/lib/util/base64.c/base64_ut.c b/test/unit/lib/util/base64.c/base64_ut.c new file mode 100644 index 000000000..0f537132a --- /dev/null +++ b/test/unit/lib/util/base64.c/base64_ut.c @@ -0,0 +1,268 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS 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 COPYRIGHT + * OWNER OR 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. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" + +#include "util/base64.c" + +char text_A[] = "FZB3"; +uint8_t raw_A[] = {0x15, 0x90, 0x77}; +char text_B[] = "AbC/1+c="; +char text_urlsafe_B[] = "AbC_1-c="; +uint8_t raw_B[] = {0x01, 0xB0, 0xBF, 0xD7, 0xE7}; +char text_C[] = "AbC/1+cC"; +char text_urlsafe_C[] = "AbC_1-cC"; +uint8_t raw_C[] = {0x01, 0xB0, 0xBF, 0xD7, 0xE7, 0x02}; +char text_D[] = "AbC/1w=="; +char text_urlsafe_D[] = "AbC_1w=="; +uint8_t raw_D[] = {0x01, 0xB0, 0xBF, 0xD7}; +char text_E[] = "AbC12==="; +char text_F[] = "AbCd112"; +char text_G[] = "AbCd12"; +char text_H[] = "AbC12"; + +static void +test_base64_get_encoded_strlen(void) +{ + uint32_t raw_lens[4] = {8, 9, 10, 11}; + uint32_t text_strlens[4] = {12, 12, 16, 16}; + uint32_t text_strlen; + int i; + + for (i = 0; i < 4; i++) { + text_strlen = spdk_base64_get_encoded_strlen(raw_lens[i]); + CU_ASSERT_EQUAL(text_strlen, text_strlens[i]); + } +} + +static void +test_base64_get_decoded_len(void) +{ + uint32_t text_strlens[4] = {8, 10, 11, 12}; + uint32_t raw_lens[4] = {6, 7, 8, 9}; + uint32_t bin_len; + int i; + + for (i = 0; i < 4; i++) { + bin_len = spdk_base64_get_decoded_len(text_strlens[i]); + CU_ASSERT_EQUAL(bin_len, raw_lens[i]); + } +} + +static void +test_base64_encode(void) +{ + char text[100]; + int ret; + + ret = spdk_base64_encode(text, raw_A, sizeof(raw_A)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_A) == 0); + CU_ASSERT_EQUAL(strlen(text), strlen(text_A)); + + ret = spdk_base64_encode(text, raw_B, sizeof(raw_B)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_B) == 0); + CU_ASSERT_EQUAL(strlen(text), strlen(text_B)); + + ret = spdk_base64_encode(text, raw_C, sizeof(raw_C)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_C) == 0); + + ret = spdk_base64_encode(text, raw_D, sizeof(raw_D)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_D) == 0); + + ret = spdk_base64_encode(NULL, raw_A, sizeof(raw_A)); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_encode(text, NULL, sizeof(raw_A)); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_encode(text, raw_A, 0); + CU_ASSERT_EQUAL(ret, -EINVAL); +} + +static void +test_base64_decode(void) +{ + char raw_buf[100]; + void *raw = (void *)raw_buf; + size_t raw_len; + int ret; + + ret = spdk_base64_decode(raw, &raw_len, text_A); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_A)); + CU_ASSERT(memcmp(raw, raw_A, sizeof(raw_A)) == 0); + + ret = spdk_base64_decode(raw, &raw_len, text_B); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_B)); + CU_ASSERT(memcmp(raw, raw_B, sizeof(raw_B)) == 0); + + ret = spdk_base64_decode(raw, &raw_len, text_C); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_C)); + CU_ASSERT(memcmp(raw, raw_C, sizeof(raw_C)) == 0); + + ret = spdk_base64_decode(raw, &raw_len, text_D); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_D)); + CU_ASSERT(memcmp(raw, raw_D, sizeof(raw_D)) == 0); + + ret = spdk_base64_decode(raw, &raw_len, text_E); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_decode(raw, &raw_len, text_F); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_decode(raw, &raw_len, text_G); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_decode(raw, &raw_len, text_H); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_decode(NULL, &raw_len, text_H); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_decode(raw, &raw_len, NULL); + CU_ASSERT_EQUAL(ret, -EINVAL); +} + +static void +test_base64_urlsafe_encode(void) +{ + char text[100]; + int ret; + + ret = spdk_base64_urlsafe_encode(text, raw_A, sizeof(raw_A)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_A) == 0); + CU_ASSERT_EQUAL(strlen(text), strlen(text_A)); + + ret = spdk_base64_urlsafe_encode(text, raw_B, sizeof(raw_B)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_urlsafe_B) == 0); + CU_ASSERT_EQUAL(strlen(text), strlen(text_urlsafe_B)); + + ret = spdk_base64_urlsafe_encode(text, raw_C, sizeof(raw_C)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_urlsafe_C) == 0); + + ret = spdk_base64_urlsafe_encode(text, raw_D, sizeof(raw_D)); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT(strcmp(text, text_urlsafe_D) == 0); + + ret = spdk_base64_urlsafe_encode(NULL, raw_A, sizeof(raw_A)); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_encode(text, NULL, sizeof(raw_A)); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_encode(text, raw_A, 0); + CU_ASSERT_EQUAL(ret, -EINVAL); +} + +static void +test_base64_urlsafe_decode(void) +{ + char raw_buf[100]; + void *raw = (void *)raw_buf; + size_t raw_len; + int ret; + + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_A); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_A)); + CU_ASSERT(memcmp(raw, raw_A, sizeof(raw_A)) == 0); + + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_B); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_B)); + CU_ASSERT(memcmp(raw, raw_B, sizeof(raw_B)) == 0); + + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_C); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_C)); + CU_ASSERT(memcmp(raw, raw_C, sizeof(raw_C)) == 0); + + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_D); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(raw_len, sizeof(raw_D)); + CU_ASSERT(memcmp(raw, raw_D, sizeof(raw_D)) == 0); + + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_E); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_F); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_G); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_H); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_H); + CU_ASSERT_EQUAL(ret, -EINVAL); + ret = spdk_base64_urlsafe_decode(raw, &raw_len, NULL); + CU_ASSERT_EQUAL(ret, -EINVAL); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("base64", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "test_base64_get_encoded_strlen", test_base64_get_encoded_strlen) == NULL || + CU_add_test(suite, "test_base64_get_decoded_len", + test_base64_get_decoded_len) == NULL || + CU_add_test(suite, "test_base64_encode", test_base64_encode) == NULL || + CU_add_test(suite, "test_base64_decode", test_base64_decode) == NULL || + CU_add_test(suite, "test_base64_urlsafe_encode", test_base64_urlsafe_encode) == NULL || + CU_add_test(suite, "test_base64_urlsafe_decode", test_base64_urlsafe_decode) == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + CU_basic_set_mode(CU_BRM_VERBOSE); + + CU_basic_run_tests(); + + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/test/unit/unittest.sh b/test/unit/unittest.sh index ef003e7b6..b88f8d1fa 100755 --- a/test/unit/unittest.sh +++ b/test/unit/unittest.sh @@ -114,6 +114,7 @@ $valgrind $testdir/lib/iscsi/portal_grp.c/portal_grp_ut $testdir/lib/iscsi/porta $valgrind $testdir/lib/thread/thread.c/thread_ut +$valgrind $testdir/lib/util/base64.c/base64_ut $valgrind $testdir/lib/util/bit_array.c/bit_array_ut $valgrind $testdir/lib/util/crc16.c/crc16_ut $valgrind $testdir/lib/util/crc32_ieee.c/crc32_ieee_ut