Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: make CMake test flags more consistent with make #4392

Merged
merged 11 commits into from
Feb 9, 2024
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ otherwise a crypto target needs to be defined." ON)
option(UNSAFE_TREAT_WARNINGS_AS_ERRORS "Compiler warnings are treated as errors. Warnings may
indicate danger points where you should verify with the S2N-TLS developers that the security of
the library is not compromised. Turn this OFF to ignore warnings." ON)
option(S2N_WERROR_ALL "This option will cause all artifacts linked to libs2n to use the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be set in s2n_codebuild.sh for unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that is a much larger project unfortunately. That was my initial approach, but I encountered some of the following problems

  • 32 bit build has a rollover warning in the 2050 detection function (which will never be called, but the time_t definition has that problem regardless
  • individual librypto builds have problems with unused variable/unused function warnings, because there are some functions that never get called under certain if/def conditions.

-Werror setting." OFF)
option(S2N_INTERN_LIBCRYPTO "This ensures that s2n-tls is compiled and deployed with a specific
version of libcrypto by interning the code and hiding symbols. This also enables s2n-tls to be
loaded in an application with an otherwise conflicting libcrypto version." OFF)
Expand Down Expand Up @@ -136,7 +138,9 @@ target_compile_options(${PROJECT_NAME} PRIVATE -pedantic -std=gnu99 -Wall -Wimpl
-Wno-missing-braces -Wsign-compare -Wno-strict-prototypes -Wa,--noexecstack
)

if (UNSAFE_TREAT_WARNINGS_AS_ERRORS)
if (S2N_WERROR_ALL)
target_compile_options(${PROJECT_NAME} PUBLIC -Werror)
elseif (UNSAFE_TREAT_WARNINGS_AS_ERRORS)
target_compile_options(${PROJECT_NAME} PRIVATE -Werror )
endif ()

Expand Down Expand Up @@ -500,7 +504,11 @@ if (BUILD_TESTING)
find . -name '${test_case_name}.c.o' -exec objcopy --redefine-syms libcrypto.symbols {} \\\;
)
endif()
target_compile_options(${test_case_name} PRIVATE -Wno-implicit-function-declaration -Wno-deprecated -Wunused-result -D_POSIX_C_SOURCE=200809L -std=gnu99)
target_compile_options(${test_case_name} PRIVATE
-Wall -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized
-Wshadow -Wcast-align -Wwrite-strings -Wformat-security
-Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-deprecated
-fPIC -D_POSIX_C_SOURCE=200809L -std=gnu99)
if (S2N_LTO)
target_compile_options(${test_case_name} PRIVATE -flto)
endif()
Expand Down
25 changes: 21 additions & 4 deletions tests/unit/s2n_build_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "crypto/s2n_openssl.h"
#include "s2n_test.h"

#define MAX_LIBCRYPTO_NAME_LEN 100

int tokenize_s2n_libcrypto(char *s2n_libcrypto, char **name, char **version)
{
if (name == NULL || version == NULL || s2n_libcrypto == NULL) {
Expand All @@ -44,6 +46,19 @@ int tokenize_s2n_libcrypto(char *s2n_libcrypto, char **name, char **version)
return S2N_SUCCESS;
}

S2N_RESULT s2n_test_lowercase_copy(const char *input, char *destination, size_t max_len)
{
RESULT_ENSURE_REF(input);
RESULT_ENSURE_REF(destination);

for (size_t i = 0; i < strlen(input); i++) {
RESULT_ENSURE_LT(i, max_len);
destination[i] = tolower(input[i]);
}

return S2N_RESULT_OK;
}

int main()
{
BEGIN_TEST();
Expand All @@ -69,8 +84,9 @@ int main()
END_TEST();
}

char s2n_libcrypto_copy[100] = { 0 };
strncpy(s2n_libcrypto_copy, s2n_libcrypto, 99);
char s2n_libcrypto_copy[MAX_LIBCRYPTO_NAME_LEN] = { 0 };
EXPECT_TRUE(strlen(s2n_libcrypto) < MAX_LIBCRYPTO_NAME_LEN);
EXPECT_OK(s2n_test_lowercase_copy(s2n_libcrypto, &s2n_libcrypto_copy[0], s2n_array_len(s2n_libcrypto_copy)));
char *name = NULL;
char *version = NULL;
EXPECT_SUCCESS(tokenize_s2n_libcrypto(s2n_libcrypto_copy, &name, &version));
Expand All @@ -83,8 +99,9 @@ int main()
EXPECT_TRUE(s2n_libcrypto_is_awslc());
} else {
/* Any other library should have the name of the library (modulo case) in its version string. */
const char *ssleay_version_text = SSLeay_version(SSLEAY_VERSION);
EXPECT_NOT_NULL(strcasestr(ssleay_version_text, name));
char ssleay_version_text[MAX_LIBCRYPTO_NAME_LEN] = { 0 };
EXPECT_OK(s2n_test_lowercase_copy(SSLeay_version(SSLEAY_VERSION), &ssleay_version_text[0], MAX_LIBCRYPTO_NAME_LEN));
EXPECT_NOT_NULL(strstr(ssleay_version_text, name));
}
};

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/s2n_ktls_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(crypto_info.value.size, sizeof(crypto_info.ciphers.aes_gcm_128));
EXPECT_EQUAL(crypto_info.value.data, (uint8_t *) &crypto_info.ciphers.aes_gcm_128);
s2n_ktls_crypto_info_tls12_aes_gcm_128 *value =
(s2n_ktls_crypto_info_tls12_aes_gcm_128 *) crypto_info.value.data;
(s2n_ktls_crypto_info_tls12_aes_gcm_128 *) (void *) crypto_info.value.data;

EXPECT_EQUAL(test_key.size, sizeof(value->key));
EXPECT_BYTEARRAY_EQUAL(test_key.data, value->key, sizeof(value->key));
Expand Down Expand Up @@ -216,7 +216,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(crypto_info.value.size, sizeof(crypto_info.ciphers.aes_gcm_256));
EXPECT_EQUAL(crypto_info.value.data, (uint8_t *) &crypto_info.ciphers.aes_gcm_256);
s2n_ktls_crypto_info_tls12_aes_gcm_256 *value =
(s2n_ktls_crypto_info_tls12_aes_gcm_256 *) crypto_info.value.data;
(s2n_ktls_crypto_info_tls12_aes_gcm_256 *) (void *) crypto_info.value.data;

EXPECT_EQUAL(test_key.size, sizeof(value->key));
EXPECT_BYTEARRAY_EQUAL(test_key.data, value->key, sizeof(value->key));
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/s2n_signature_algorithms_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ int main(int argc, char **argv)
/* Test: ECDSA */
{
const struct s2n_signature_scheme *expected = &s2n_ecdsa_sha1;
conn->handshake_params.client_cert_pkey_type = S2N_AUTHENTICATION_ECDSA;
conn->handshake_params.client_cert_pkey_type = S2N_PKEY_TYPE_ECDSA;
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_ecdsa_config));

/* TLS1.1 selects the default */
Expand All @@ -256,7 +256,7 @@ int main(int argc, char **argv)
/* Test: RSA */
{
const struct s2n_signature_scheme *expected = &s2n_rsa_pkcs1_md5_sha1;
conn->handshake_params.client_cert_pkey_type = S2N_AUTHENTICATION_RSA;
conn->handshake_params.client_cert_pkey_type = S2N_PKEY_TYPE_RSA;
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_rsa_config));

/* TLS1.1 selects the default */
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/s2n_x509_validator_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
* permissions and limitations under the License.
*/

#include "crypto/s2n_openssl_x509.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"

DEFINE_POINTER_CLEANUP_FUNC(X509 *, X509_free);

static int mock_time(void *data, uint64_t *timestamp)
{
*timestamp = *(uint64_t *) data;
Expand Down
Loading