Skip to content

Commit

Permalink
Clang-format tests/unit/s2n_s.*\.c and enforce in CI (#3678)
Browse files Browse the repository at this point in the history
* Clang-format s and hand review + add to CI

* Remove empty statment work-around

* Add semicolons at the end of code scopes for clang-format
Remove bug workaround

* Appease cpp check

* Add clang-format off for s2n_safety_macros_test.c

* respond to comments

Co-authored-by: Harrison Kaiser <[email protected]>
  • Loading branch information
harrisonkaiser and uwaces authored Dec 15, 2022
1 parent 14a3e07 commit 6d8741c
Show file tree
Hide file tree
Showing 59 changed files with 1,234 additions and 1,311 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_clang_format_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- check: 'tests/testlib'
exclude: ''
- check: 'tests/unit'
exclude: "s2n_[^3a-cd-kt-z].*\\.c"
exclude: "s2n_[^3a-csd-kt-z].*\\.c"
- check: 'tls'
exclude: ''
- check: 'tls/extensions'
Expand Down
2 changes: 2 additions & 0 deletions scripts/s2n_safety_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ def write(f, contents):
write("utils/s2n_safety_macros.h", header)

test = copyright + '''
/* clang-format off */
#include "s2n_test.h"
#include "utils/s2n_safety.h"
Expand Down
29 changes: 14 additions & 15 deletions tests/unit/s2n_safety_blinding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@
*/

#include "s2n_test.h"

#include "tls/s2n_connection.h"
#include "utils/s2n_safety.h"

#define TEST_ERRNO S2N_ERR_T_INTERNAL_END

#define SETUP_TEST(conn) \
#define SETUP_TEST(conn) \
EXPECT_SUCCESS(s2n_connection_wipe(conn)); \
EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING)); \
EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));

#define EXPECT_BLINDING(conn) \
#define EXPECT_BLINDING(conn) \
EXPECT_NOT_EQUAL(s2n_connection_get_delay(conn), 0); \
EXPECT_TRUE(conn->closed); \
EXPECT_TRUE(conn->closed);

#define EXPECT_NO_BLINDING(conn) \
#define EXPECT_NO_BLINDING(conn) \
EXPECT_EQUAL(s2n_connection_get_delay(conn), 0); \
EXPECT_FALSE(conn->closed); \
EXPECT_FALSE(conn->closed);

S2N_RESULT s2n_result_func(bool success)
{
Expand All @@ -45,7 +44,7 @@ int s2n_posix_func(bool success)
}

int ptr_value = 0;
int* s2n_ptr_func(bool success)
int *s2n_ptr_func(bool success)
{
PTR_ENSURE(success, TEST_ERRNO);
return &ptr_value;
Expand Down Expand Up @@ -87,7 +86,7 @@ int s2n_posix_test(struct s2n_connection *conn)
return S2N_SUCCESS;
}

int* s2n_ptr_test(struct s2n_connection *conn)
int *s2n_ptr_test(struct s2n_connection *conn)
{
WITH_ERROR_BLINDING(conn, PTR_GUARD_RESULT(s2n_result_func(true)));
EXPECT_NO_BLINDING(conn);
Expand Down Expand Up @@ -125,15 +124,15 @@ int main(int argc, char **argv)
s2n_errno = S2N_ERR_OK;
EXPECT_OK(s2n_connection_apply_error_blinding(&conn));
EXPECT_NO_BLINDING(conn);
}
};

/* No-op for retriable errors */
{
SETUP_TEST(conn);
s2n_errno = S2N_ERR_IO_BLOCKED;
EXPECT_OK(s2n_connection_apply_error_blinding(&conn));
EXPECT_NO_BLINDING(conn);
}
};

/* Closes connection but does not blind for non-blinding errors */
{
Expand All @@ -142,18 +141,18 @@ int main(int argc, char **argv)
EXPECT_OK(s2n_connection_apply_error_blinding(&conn));
EXPECT_EQUAL(s2n_connection_get_delay(conn), 0);
EXPECT_TRUE(conn->closed);
}
};

/* Blinds for an average error */
{
SETUP_TEST(conn);
s2n_errno = S2N_ERR_UNIMPLEMENTED;
EXPECT_OK(s2n_connection_apply_error_blinding(&conn));
EXPECT_BLINDING(conn);
}
};

EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: WITH_ERROR_BLINDING macro
* The WITH_ERROR_BLINDING macro relies on the current method exiting early.
Expand All @@ -178,7 +177,7 @@ int main(int argc, char **argv)
EXPECT_BLINDING(conn);

EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

END_TEST();
}
2 changes: 2 additions & 0 deletions tests/unit/s2n_safety_macros_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* permissions and limitations under the License.
*/

/* clang-format off */

#include "s2n_test.h"

#include "utils/s2n_safety.h"
Expand Down
67 changes: 32 additions & 35 deletions tests/unit/s2n_safety_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,34 @@
* permissions and limitations under the License.
*/

#include "s2n_test.h"

#include "utils/s2n_safety.h"

#define CHECK_OVF_0(fn, type, a, b) \
do { \
type result_val; \
EXPECT_FAILURE(fn((a), (b), &result_val)); \
} while (0)


#define CHECK_OVF(fn, type, a, b) \
do { \
CHECK_OVF_0(fn, type, a, b); \
CHECK_OVF_0(fn, type, b, a); \
} while (0)

#define CHECK_NO_OVF_0(fn, type, a, b, r) \
do { \
type result_val; \
EXPECT_SUCCESS(fn((a), (b), &result_val)); \
EXPECT_EQUAL(result_val,(r)); \
} while (0)

#define CHECK_NO_OVF(fn, type, a, b, r) \
do { \
CHECK_NO_OVF_0(fn, type, a, b, r); \
CHECK_NO_OVF_0(fn, type, b, a, r); \
} while (0)
#include "s2n_test.h"

#define CHECK_OVF_0(fn, type, a, b) \
do { \
type result_val; \
EXPECT_FAILURE(fn((a), (b), &result_val)); \
} while (0)

#define CHECK_OVF(fn, type, a, b) \
do { \
CHECK_OVF_0(fn, type, a, b); \
CHECK_OVF_0(fn, type, b, a); \
} while (0)

#define CHECK_NO_OVF_0(fn, type, a, b, r) \
do { \
type result_val; \
EXPECT_SUCCESS(fn((a), (b), &result_val)); \
EXPECT_EQUAL(result_val, (r)); \
} while (0)

#define CHECK_NO_OVF(fn, type, a, b, r) \
do { \
CHECK_NO_OVF_0(fn, type, a, b, r); \
CHECK_NO_OVF_0(fn, type, b, a, r); \
} while (0)

static int failure_gte()
{
Expand Down Expand Up @@ -121,7 +119,7 @@ static int failure_notnull()
static int success_memcpy()
{
char dst[1024];
char src[1024] = {0};
char src[1024] = { 0 };

POSIX_CHECKED_MEMCPY(dst, src, 1024);

Expand Down Expand Up @@ -252,7 +250,6 @@ static int success_ct_pkcs1_negative()
return 0;
}


int main(int argc, char **argv)
{
BEGIN_TEST();
Expand Down Expand Up @@ -314,16 +311,16 @@ int main(int argc, char **argv)

for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
x[0] = i;
y[0] = j;
x[0] = i;
y[0] = j;

int expected = 0;
int expected = 0;

if (i == j) {
if (i == j) {
expected = 1;
}
}

EXPECT_EQUAL(s2n_constant_time_equals(x, y, sizeof(x)), expected);
EXPECT_EQUAL(s2n_constant_time_equals(x, y, sizeof(x)), expected);
}
}

Expand Down
Loading

0 comments on commit 6d8741c

Please sign in to comment.