Skip to content

Commit

Permalink
Clang-format tests/unit/s2n_[t-z].*\.c and enforce in CI (aws#3679)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisonkaiser authored Dec 9, 2022
1 parent 06e9e8c commit 0bb6146
Show file tree
Hide file tree
Showing 35 changed files with 995 additions and 1,005 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@ Standard: Auto
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
- test_stack_blob_success
TabWidth: 8
UseTab: Never
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 @@ -18,7 +18,7 @@ jobs:
- check: 'tests/testlib'
exclude: ''
- check: 'tests/unit'
exclude: "s2n_[^3a-cd-k].*\\.c"
exclude: "s2n_[^3a-cd-kt-z].*\\.c"
- check: 'tls'
exclude: "(tls\\/extensions)|(s2n_[a-hs-z].*\\.[ch])"
- check: 'tls/extensions'
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/s2n_testlib_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* permissions and limitations under the License.
*/

#include "s2n_test.h"

#include "testlib/s2n_testlib.h"

#include "s2n_test.h"

int main(int argc, char **argv)
{
BEGIN_TEST();
Expand All @@ -41,7 +41,7 @@ int main(int argc, char **argv)
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, NULL), S2N_ERR_NULL);

EXPECT_SUCCESS(s2n_connection_free(server_conn));
}
};

END_TEST();
}
7 changes: 2 additions & 5 deletions tests/unit/s2n_timer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "s2n_test.h"

#include "utils/s2n_timer.h"

#include "s2n_test.h"
#include "tls/s2n_config.h"



int mock_clock(void *in, uint64_t *out)
{
*out = *(uint64_t *)in;
*out = *(uint64_t *) in;

return 0;
}
Expand Down
68 changes: 31 additions & 37 deletions tests/unit/s2n_tls12_handshake_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
*/

#include "api/s2n.h"

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

#include "tls/s2n_connection.h"
#include "tls/s2n_handshake.h"
#include "utils/s2n_safety.h"
Expand All @@ -30,19 +28,20 @@ static message_type_t invalid_handshake[S2N_MAX_HANDSHAKE_LENGTH] = { 0 };
static int expected_handler_called;
static int unexpected_handler_called;

static int s2n_test_handler(struct s2n_connection* conn)
static int s2n_test_handler(struct s2n_connection *conn)
{
unexpected_handler_called = 1;
return 0;
}

static int s2n_test_expected_handler(struct s2n_connection* conn)
static int s2n_test_expected_handler(struct s2n_connection *conn)
{
expected_handler_called = 1;
return 0;
}

static int s2n_setup_handler_to_expect(message_type_t expected, uint8_t direction) {
static int s2n_setup_handler_to_expect(message_type_t expected, uint8_t direction)
{
for (int i = 0; i < s2n_array_len(state_machine); i++) {
state_machine[i].handler[0] = s2n_test_handler;
state_machine[i].handler[1] = s2n_test_handler;
Expand Down Expand Up @@ -96,7 +95,7 @@ int main(int argc, char **argv)
uint16_t valid_tls12_handshakes[S2N_HANDSHAKES_COUNT];
int valid_tls12_handshakes_size = 0;
for (int i = 0; i < S2N_HANDSHAKES_COUNT; i++) {
if(memcmp(handshakes[i], invalid_handshake, S2N_MAX_HANDSHAKE_LENGTH) != 0) {
if (memcmp(handshakes[i], invalid_handshake, S2N_MAX_HANDSHAKE_LENGTH) != 0) {
valid_tls12_handshakes[valid_tls12_handshakes_size] = i;
valid_tls12_handshakes_size++;
}
Expand All @@ -111,7 +110,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(ACTIVE_STATE_MACHINE(conn), state_machine);
EXPECT_EQUAL(ACTIVE_HANDSHAKES(conn), handshakes);
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: TLS1.2 server waits for expected CCS messages */
{
Expand All @@ -138,7 +137,7 @@ int main(int argc, char **argv)
}

EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: Client CCS messages always come before Client Finished messages */
{
Expand All @@ -152,7 +151,6 @@ int main(int argc, char **argv)
bool ccs_encountered = false;

for (int j = 0; j < S2N_MAX_HANDSHAKE_LENGTH; j++) {

if (handshakes[handshake][j] == CLIENT_CHANGE_CIPHER_SPEC) {
ccs_encountered = true;
}
Expand All @@ -164,7 +162,7 @@ int main(int argc, char **argv)
/* Every valid handshake includes a CCS message */
EXPECT_TRUE(ccs_encountered);
}
}
};

/* Test: TLS1.2 client waits for expected CCS messages */
{
Expand All @@ -191,7 +189,7 @@ int main(int argc, char **argv)
}

EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: TLS1.2 client handles expected server CCS messages
* but errors on unexpected CCS messages */
Expand Down Expand Up @@ -231,7 +229,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: TLS1.2 server handles expected client CCS messages
* but errors on unexpected CCS messages */
Expand Down Expand Up @@ -271,7 +269,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: TLS1.2 client can receive a hello request message at any time. */
{
Expand Down Expand Up @@ -305,7 +303,7 @@ int main(int argc, char **argv)
EXPECT_FALSE(unexpected_handler_called);
EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: TLS1.2 s2n_handshake_read_io should accept only the expected message */
{
Expand All @@ -332,7 +330,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* TLS1.2 should error for an unexpected message */
{
Expand All @@ -357,7 +355,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* TLS1.2 should error for an expected message from the wrong writer */
{
Expand All @@ -382,7 +380,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* TLS1.2 should error for an expected message from the wrong record type */
{
Expand Down Expand Up @@ -419,8 +417,8 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_stuffer_free(&input));
EXPECT_SUCCESS(s2n_connection_free(conn));
state_machine[SERVER_CHANGE_CIPHER_SPEC].message_type = old_message_type;
}
}
};
};

/* Test: TLS1.2 handshake type name maximum size is set correctly.
* The maximum size is the size of a name with all flags set. */
Expand All @@ -433,7 +431,7 @@ int main(int argc, char **argv)
fprintf(stderr, "\nMAX_HANDSHAKE_TYPE_LEN should be at least %lu\n", (unsigned long) correct_size);
FAIL_MSG("MAX_HANDSHAKE_TYPE_LEN wrong for TLS1.2 handshakes");
}
}
};

/* Test: TLS 1.2 handshake types are all properly printed */
{
Expand All @@ -446,10 +444,9 @@ int main(int argc, char **argv)
conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE;
EXPECT_STRING_EQUAL("NEGOTIATED|FULL_HANDSHAKE", s2n_connection_get_handshake_type_name(conn));

const char* all_flags_handshake_type_name = "NEGOTIATED|FULL_HANDSHAKE|CLIENT_AUTH|NO_CLIENT_CERT|"
"TLS12_PERFECT_FORWARD_SECRECY|OCSP_STATUS|WITH_SESSION_TICKET|WITH_NPN";
conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | \
TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | WITH_SESSION_TICKET | WITH_NPN;
const char *all_flags_handshake_type_name = "NEGOTIATED|FULL_HANDSHAKE|CLIENT_AUTH|NO_CLIENT_CERT|"
"TLS12_PERFECT_FORWARD_SECRECY|OCSP_STATUS|WITH_SESSION_TICKET|WITH_NPN";
conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | WITH_SESSION_TICKET | WITH_NPN;
EXPECT_STRING_EQUAL(all_flags_handshake_type_name, s2n_connection_get_handshake_type_name(conn));

const char *handshake_type_name;
Expand All @@ -470,37 +467,35 @@ int main(int argc, char **argv)
}

EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: TLS 1.2 message types are all properly printed */
{
uint32_t test_handshake_type = NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | \
OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN;
const char* expected[] = { "CLIENT_HELLO",
"SERVER_HELLO", "SERVER_CERT", "SERVER_CERT_STATUS", "SERVER_KEY", "SERVER_CERT_REQ", "SERVER_HELLO_DONE",
"CLIENT_CERT", "CLIENT_KEY", "CLIENT_CERT_VERIFY", "CLIENT_CHANGE_CIPHER_SPEC", "CLIENT_NPN",
"CLIENT_FINISHED", "SERVER_NEW_SESSION_TICKET", "SERVER_CHANGE_CIPHER_SPEC", "SERVER_FINISHED",
"APPLICATION_DATA" };
uint32_t test_handshake_type = NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN;
const char *expected[] = { "CLIENT_HELLO",
"SERVER_HELLO", "SERVER_CERT", "SERVER_CERT_STATUS", "SERVER_KEY", "SERVER_CERT_REQ", "SERVER_HELLO_DONE",
"CLIENT_CERT", "CLIENT_KEY", "CLIENT_CERT_VERIFY", "CLIENT_CHANGE_CIPHER_SPEC", "CLIENT_NPN",
"CLIENT_FINISHED", "SERVER_NEW_SESSION_TICKET", "SERVER_CHANGE_CIPHER_SPEC", "SERVER_FINISHED",
"APPLICATION_DATA" };

struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);

conn->handshake.handshake_type = test_handshake_type;

for (int i=0; i < sizeof(expected) / sizeof(char *); i++) {
for (int i = 0; i < sizeof(expected) / sizeof(char *); i++) {
conn->handshake.message_number = i;
EXPECT_STRING_EQUAL(expected[i], s2n_connection_get_last_message_name(conn));
}

EXPECT_SUCCESS(s2n_connection_free(conn));
}
};

/* Test: A WITH_NPN form of every valid, negotiated handshake exists */
{
uint32_t handshake_type_original, handshake_type_npn;
message_type_t *messages_original, *messages_npn;

for (size_t i = 0; i < valid_tls12_handshakes_size; i++) {

handshake_type_original = valid_tls12_handshakes[i];
messages_original = handshakes[handshake_type_original];

Expand All @@ -514,7 +509,6 @@ int main(int argc, char **argv)
messages_npn = handshakes[handshake_type_npn];

for (size_t j = 0, j_npn = 0; j < S2N_MAX_HANDSHAKE_LENGTH && j_npn < S2N_MAX_HANDSHAKE_LENGTH; j++, j_npn++) {

/* The original handshake cannot contain the Next Protocol message */
EXPECT_NOT_EQUAL(messages_original[j], CLIENT_NPN);

Expand All @@ -527,7 +521,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(messages_original[j], messages_npn[j_npn]);
}
}
}
};

END_TEST();
return 0;
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/s2n_tls13_cert_request_extensions_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
* permissions and limitations under the License.
*/

#include "s2n_test.h"

#include <string.h>
#include <stdio.h>
#include "api/s2n.h"
#include <string.h>

#include "tls/extensions/s2n_server_supported_versions.h"
#include "tls/extensions/s2n_server_signature_algorithms.h"
#include "api/s2n.h"
#include "s2n_test.h"
#include "stuffer/s2n_stuffer.h"
#include "testlib/s2n_testlib.h"
#include "tls/extensions/s2n_server_signature_algorithms.h"
#include "tls/extensions/s2n_server_supported_versions.h"
#include "tls/s2n_tls.h"
#include "tls/s2n_tls13.h"
#include "utils/s2n_safety.h"
Expand Down Expand Up @@ -59,7 +58,7 @@ int main(int argc, char **argv)
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_req_recv(client_conn), S2N_ERR_MISSING_EXTENSION);

EXPECT_SUCCESS(s2n_connection_free(client_conn));
}
};

EXPECT_SUCCESS(s2n_disable_tls13_in_test());
END_TEST();
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/s2n_tls13_cert_request_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
* permissions and limitations under the License.
*/

#include "s2n_test.h"

#include <string.h>
#include <stdio.h>
#include "api/s2n.h"
#include <string.h>

#include "tls/extensions/s2n_server_signature_algorithms.h"
#include "api/s2n.h"
#include "s2n_test.h"
#include "stuffer/s2n_stuffer.h"
#include "testlib/s2n_testlib.h"
#include "tls/extensions/s2n_server_signature_algorithms.h"
#include "tls/s2n_tls.h"
#include "tls/s2n_tls13.h"
#include "utils/s2n_safety.h"
Expand Down Expand Up @@ -74,7 +73,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_connection_free(client_conn));
EXPECT_SUCCESS(s2n_connection_free(server_conn));
}
};

/* Test request context length other than 0 fails */
{
Expand All @@ -91,7 +90,7 @@ int main(int argc, char **argv)
EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_req_recv(client_conn), S2N_ERR_BAD_MESSAGE);

EXPECT_SUCCESS(s2n_connection_free(client_conn));
}
};

EXPECT_SUCCESS(s2n_disable_tls13_in_test());
END_TEST();
Expand Down
Loading

0 comments on commit 0bb6146

Please sign in to comment.