| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#ifdef __cplusplus |
| 5 |
#include <cstdint> |
| 6 |
#include <cstddef> |
| 7 |
#else |
| 8 |
#include <stdint.h> |
| 9 |
#include <stddef.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
#include "azure_macro_utils/macro_utils.h" |
| 13 |
#include "testrunnerswitcher.h" |
| 14 |
#include "umock_c/umock_c.h" |
| 15 |
#include "umock_c/umocktypes_charptr.h" |
| 16 |
|
| 17 |
#if defined _MSC_VER |
| 18 |
#pragma warning(disable: 4054) /* MSC incorrectly fires this */ |
| 19 |
#endif |
| 20 |
|
| 21 |
static void* my_gballoc_malloc(size_t size) |
| 22 |
{ |
| 23 |
return malloc(size); |
| 24 |
} |
| 25 |
|
| 26 |
static void* my_gballoc_calloc(size_t nmemb, size_t size) |
| 27 |
{ |
| 28 |
return calloc(nmemb, size); |
| 29 |
} |
| 30 |
|
| 31 |
static void my_gballoc_free(void* ptr) |
| 32 |
{ |
| 33 |
free(ptr); |
| 34 |
} |
| 35 |
|
| 36 |
#define ENABLE_MOCKS |
| 37 |
|
| 38 |
#include "azure_c_shared_utility/gballoc.h" |
| 39 |
|
| 40 |
#undef ENABLE_MOCKS |
| 41 |
|
| 42 |
#include "azure_uamqp_c/sasl_plain.h" |
| 43 |
|
| 44 |
#define MAX_AUTHZID "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345" |
| 45 |
#define MAX_AUTHCID MAX_AUTHZID |
| 46 |
#define MAX_PASSWD MAX_AUTHZID |
| 47 |
|
| 48 |
static TEST_MUTEX_HANDLE g_testByTest; |
| 49 |
|
| 50 |
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES) |
| 51 |
|
| 52 |
static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code) |
| 53 |
{ |
| 54 |
ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code)); |
| 55 |
} |
| 56 |
|
| 57 |
BEGIN_TEST_SUITE(sasl_plain_ut) |
| 58 |
|
| 59 |
TEST_SUITE_INITIALIZE(suite_init) |
| 60 |
{ |
| 61 |
int result; |
| 62 |
|
| 63 |
g_testByTest = TEST_MUTEX_CREATE(); |
| 64 |
ASSERT_IS_NOT_NULL(g_testByTest); |
| 65 |
|
| 66 |
umock_c_init(on_umock_c_error); |
| 67 |
|
| 68 |
result = umocktypes_charptr_register_types(); |
| 69 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 70 |
|
| 71 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc); |
| 72 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc); |
| 73 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free); |
| 74 |
} |
| 75 |
|
| 76 |
TEST_SUITE_CLEANUP(suite_cleanup) |
| 77 |
{ |
| 78 |
umock_c_deinit(); |
| 79 |
|
| 80 |
TEST_MUTEX_DESTROY(g_testByTest); |
| 81 |
} |
| 82 |
|
| 83 |
TEST_FUNCTION_INITIALIZE(method_init) |
| 84 |
{ |
| 85 |
if (TEST_MUTEX_ACQUIRE(g_testByTest)) |
| 86 |
{ |
| 87 |
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework"); |
| 88 |
} |
| 89 |
|
| 90 |
umock_c_reset_all_calls(); |
| 91 |
} |
| 92 |
|
| 93 |
TEST_FUNCTION_CLEANUP(method_cleanup) |
| 94 |
{ |
| 95 |
TEST_MUTEX_RELEASE(g_testByTest); |
| 96 |
} |
| 97 |
|
| 98 |
/* saslplain_create */ |
| 99 |
|
| 100 |
/* Tests_SRS_SASL_PLAIN_01_001: [`saslplain_create` shall return on success a non-NULL handle to a new SASL plain mechanism.] */ |
| 101 |
TEST_FUNCTION(saslplain_create_with_valid_args_succeeds) |
| 102 |
{ |
| 103 |
// arrange |
| 104 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", "test_authzid" }; |
| 105 |
CONCRETE_SASL_MECHANISM_HANDLE result; |
| 106 |
|
| 107 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)); |
| 108 |
STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)); |
| 109 |
|
| 110 |
// act |
| 111 |
result = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 112 |
|
| 113 |
// assert |
| 114 |
ASSERT_IS_NOT_NULL(result); |
| 115 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 116 |
|
| 117 |
// cleanup |
| 118 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(result); |
| 119 |
} |
| 120 |
|
| 121 |
/* Tests_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then `saslplain_create` shall return NULL.] */ |
| 122 |
TEST_FUNCTION(when_allocating_memory_fails_then_saslplain_create_fails) |
| 123 |
{ |
| 124 |
// arrange |
| 125 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", "test_authzid" }; |
| 126 |
CONCRETE_SASL_MECHANISM_HANDLE result; |
| 127 |
|
| 128 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)) |
| 129 |
.SetReturn(NULL); |
| 130 |
|
| 131 |
// act |
| 132 |
result = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 133 |
|
| 134 |
// assert |
| 135 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 136 |
ASSERT_IS_NULL(result); |
| 137 |
} |
| 138 |
|
| 139 |
/* Tests_SRS_SASL_PLAIN_01_002: [If allocating the memory needed for the saslplain instance fails then `saslplain_create` shall return NULL.] */ |
| 140 |
TEST_FUNCTION(when_allocating_memory_for_the_config_fails_then_saslplain_create_fails) |
| 141 |
{ |
| 142 |
// arrange |
| 143 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", "test_authzid" }; |
| 144 |
CONCRETE_SASL_MECHANISM_HANDLE result; |
| 145 |
|
| 146 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)); |
| 147 |
STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)) |
| 148 |
.SetReturn(NULL); |
| 149 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 150 |
|
| 151 |
// act |
| 152 |
result = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 153 |
|
| 154 |
// assert |
| 155 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 156 |
ASSERT_IS_NULL(result); |
| 157 |
} |
| 158 |
|
| 159 |
/* Tests_SRS_SASL_PLAIN_01_003: [If the `config` argument is NULL, then `saslplain_create` shall fail and return NULL.] */ |
| 160 |
TEST_FUNCTION(saslplain_create_with_NULL_config_fails) |
| 161 |
{ |
| 162 |
// arrange |
| 163 |
|
| 164 |
// act |
| 165 |
CONCRETE_SASL_MECHANISM_HANDLE result = saslplain_get_interface()->concrete_sasl_mechanism_create(NULL); |
| 166 |
|
| 167 |
// assert |
| 168 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 169 |
ASSERT_IS_NULL(result); |
| 170 |
} |
| 171 |
|
| 172 |
/* Tests_SRS_SASL_PLAIN_01_004: [If either the `authcid` or `passwd` member of the `config` structure is NULL, then `saslplain_create` shall fail and return NULL.] */ |
| 173 |
TEST_FUNCTION(saslplain_create_with_NULL_authcid_fails) |
| 174 |
{ |
| 175 |
// arrange |
| 176 |
SASL_PLAIN_CONFIG sasl_plain_config = { NULL, "test_pwd", "test_authzid" }; |
| 177 |
|
| 178 |
// act |
| 179 |
CONCRETE_SASL_MECHANISM_HANDLE result = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 180 |
|
| 181 |
// assert |
| 182 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 183 |
ASSERT_IS_NULL(result); |
| 184 |
} |
| 185 |
|
| 186 |
/* Tests_SRS_SASL_PLAIN_01_004: [If either the `authcid` or `passwd` member of the `config` structure is NULL, then `saslplain_create` shall fail and return NULL.] */ |
| 187 |
TEST_FUNCTION(saslplain_create_with_NULL_passwd_fails) |
| 188 |
{ |
| 189 |
// arrange |
| 190 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", NULL, "test_authzid" }; |
| 191 |
|
| 192 |
// act |
| 193 |
CONCRETE_SASL_MECHANISM_HANDLE result = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 194 |
|
| 195 |
// assert |
| 196 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 197 |
ASSERT_IS_NULL(result); |
| 198 |
} |
| 199 |
|
| 200 |
/* Tests_SRS_SASL_PLAIN_01_004: [If either the `authcid` or `passwd` member of the `config` structure is NULL, then `saslplain_create` shall fail and return NULL.] */ |
| 201 |
/* Tests_SRS_SASL_PLAIN_01_025: [ `authzid` shall be optional. ]*/ |
| 202 |
TEST_FUNCTION(saslplain_create_with_NULL_authzid_succeeds) |
| 203 |
{ |
| 204 |
// arrange |
| 205 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 206 |
CONCRETE_SASL_MECHANISM_HANDLE result; |
| 207 |
|
| 208 |
STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)); |
| 209 |
STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)); |
| 210 |
|
| 211 |
// act |
| 212 |
result = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 213 |
|
| 214 |
// assert |
| 215 |
ASSERT_IS_NOT_NULL(result); |
| 216 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 217 |
|
| 218 |
// cleanup |
| 219 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(result); |
| 220 |
} |
| 221 |
|
| 222 |
/* saslplain_destroy */ |
| 223 |
|
| 224 |
/* Tests_SRS_SASL_PLAIN_01_005: [`saslplain_destroy` shall free all resources associated with the SASL mechanism.] */ |
| 225 |
TEST_FUNCTION(saslplain_destroy_frees_the_allocated_memory) |
| 226 |
{ |
| 227 |
// arrange |
| 228 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 229 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 230 |
umock_c_reset_all_calls(); |
| 231 |
|
| 232 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 233 |
STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); |
| 234 |
|
| 235 |
// act |
| 236 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 237 |
|
| 238 |
// assert |
| 239 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 240 |
} |
| 241 |
|
| 242 |
/* Tests_SRS_SASL_PLAIN_01_006: [If the argument `concrete_sasl_mechanism` is NULL, `saslplain_destroy` shall do nothing.] */ |
| 243 |
TEST_FUNCTION(saslplain_destroy_with_NULL_handle_does_nothing) |
| 244 |
{ |
| 245 |
// arrange |
| 246 |
|
| 247 |
// act |
| 248 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(NULL); |
| 249 |
|
| 250 |
// assert |
| 251 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 252 |
} |
| 253 |
|
| 254 |
/* saslplain_get_init_bytes */ |
| 255 |
|
| 256 |
/* Tests_SRS_SASL_PLAIN_01_007: [`saslplain_get_init_bytes` shall construct the initial bytes per the RFC 4616.] */ |
| 257 |
/* Tests_SRS_SASL_PLAIN_01_008: [On success `saslplain_get_init_bytes` shall return zero.] */ |
| 258 |
/* Tests_SRS_SASL_PLAIN_01_016: [The mechanism consists of a single message, a string of [UTF-8] encoded [Unicode] characters, from the client to the server.] */ |
| 259 |
/* Tests_SRS_SASL_PLAIN_01_017: [The client presents the authorization identity (identity to act as), followed by a NUL (U+0000) character, followed by the authentication identity (identity whose password will be used), followed by a NUL (U+0000) character, followed by the clear-text password.] */ |
| 260 |
/* Tests_SRS_SASL_PLAIN_01_019: [ message = [authzid] UTF8NUL authcid UTF8NUL passwd] */ |
| 261 |
/* Tests_SRS_SASL_PLAIN_01_023: [The authorization identity (authzid), authentication identity (authcid), password (passwd), and NUL character deliminators SHALL be transferred as [UTF-8] encoded strings of [Unicode] characters.] */ |
| 262 |
/* Tests_SRS_SASL_PLAIN_01_024: [As the NUL (U+0000) character is used as a deliminator, the NUL (U+0000) character MUST NOT appear in authzid, authcid, or passwd productions.] */ |
| 263 |
TEST_FUNCTION(saslplain_get_init_bytes_returns_the_correct_concateneted_bytes) |
| 264 |
{ |
| 265 |
// arrange |
| 266 |
int result; |
| 267 |
unsigned char expected_bytes[] = "test_authzid" "\0" "test_authcid" "\0" "test_pwd"; |
| 268 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", "test_authzid" }; |
| 269 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 270 |
SASL_MECHANISM_BYTES init_bytes; |
| 271 |
|
| 272 |
umock_c_reset_all_calls(); |
| 273 |
|
| 274 |
// act |
| 275 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(sasl_plain, &init_bytes); |
| 276 |
|
| 277 |
// assert |
| 278 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 279 |
ASSERT_ARE_EQUAL(size_t, sizeof(expected_bytes) - 1, init_bytes.length); |
| 280 |
ASSERT_ARE_EQUAL(int, 0, memcmp(expected_bytes, init_bytes.bytes, sizeof(expected_bytes) - 1)); |
| 281 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 282 |
|
| 283 |
// cleanup |
| 284 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 285 |
} |
| 286 |
|
| 287 |
/* Tests_SRS_SASL_PLAIN_01_007: [`saslplain_get_init_bytes` shall construct the initial bytes per the RFC 4616.] */ |
| 288 |
/* Tests_SRS_SASL_PLAIN_01_008: [On success `saslplain_get_init_bytes` shall return zero.] */ |
| 289 |
/* Tests_SRS_SASL_PLAIN_01_018: [As with other SASL mechanisms, the client does not provide an authorization identity when it wishes the server to derive an identity from the credentials and use that as the authorization identity.] */ |
| 290 |
TEST_FUNCTION(saslplain_get_init_bytes_with_NULL_authzid_returns_the_correct_concateneted_bytes) |
| 291 |
{ |
| 292 |
// arrange |
| 293 |
int result; |
| 294 |
unsigned char expected_bytes[] = "\0" "test_authcid" "\0" "test_pwd"; |
| 295 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 296 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 297 |
SASL_MECHANISM_BYTES init_bytes; |
| 298 |
umock_c_reset_all_calls(); |
| 299 |
|
| 300 |
// act |
| 301 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(sasl_plain, &init_bytes); |
| 302 |
|
| 303 |
// assert |
| 304 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 305 |
ASSERT_ARE_EQUAL(size_t, sizeof(expected_bytes) - 1, init_bytes.length); |
| 306 |
ASSERT_ARE_EQUAL(int, 0, memcmp(expected_bytes, init_bytes.bytes, sizeof(expected_bytes) - 1)); |
| 307 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 308 |
|
| 309 |
// cleanup |
| 310 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 311 |
} |
| 312 |
|
| 313 |
/* Tests_SRS_SASL_PLAIN_01_018: [As with other SASL mechanisms, the client does not provide an authorization identity when it wishes the server to derive an identity from the credentials and use that as the authorization identity.] */ |
| 314 |
TEST_FUNCTION(saslplain_get_init_bytes_with_authzid_zero_length_succeeds) |
| 315 |
{ |
| 316 |
// arrange |
| 317 |
int result; |
| 318 |
unsigned char expected_bytes[] = "\0" "test_authcid" "\0" "test_pwd"; |
| 319 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", "" }; |
| 320 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 321 |
SASL_MECHANISM_BYTES init_bytes; |
| 322 |
umock_c_reset_all_calls(); |
| 323 |
|
| 324 |
// act |
| 325 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(sasl_plain, &init_bytes); |
| 326 |
|
| 327 |
// assert |
| 328 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 329 |
ASSERT_ARE_EQUAL(size_t, sizeof(expected_bytes) - 1, init_bytes.length); |
| 330 |
ASSERT_ARE_EQUAL(int, 0, memcmp(expected_bytes, init_bytes.bytes, sizeof(expected_bytes) - 1)); |
| 331 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 332 |
|
| 333 |
// cleanup |
| 334 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 335 |
} |
| 336 |
|
| 337 |
/* Tests_SRS_SASL_PLAIN_01_020: [ authcid = 1*SAFE ; MUST accept up to 255 octets] */ |
| 338 |
TEST_FUNCTION(saslplain_get_init_bytes_with_1_byte_for_each_field_succeeds) |
| 339 |
{ |
| 340 |
// arrange |
| 341 |
int result; |
| 342 |
unsigned char expected_bytes[] = "1" "\0" "b" "\0" "c"; |
| 343 |
SASL_PLAIN_CONFIG sasl_plain_config = { "b", "c", "1" }; |
| 344 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 345 |
SASL_MECHANISM_BYTES init_bytes; |
| 346 |
umock_c_reset_all_calls(); |
| 347 |
|
| 348 |
// act |
| 349 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(sasl_plain, &init_bytes); |
| 350 |
|
| 351 |
// assert |
| 352 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 353 |
ASSERT_ARE_EQUAL(size_t, sizeof(expected_bytes) - 1, init_bytes.length); |
| 354 |
ASSERT_ARE_EQUAL(int, 0, memcmp(expected_bytes, init_bytes.bytes, sizeof(expected_bytes) - 1)); |
| 355 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 356 |
|
| 357 |
// cleanup |
| 358 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 359 |
} |
| 360 |
|
| 361 |
/* Tests_SRS_SASL_PLAIN_01_020: [ authcid = 1*SAFE ; MUST accept up to 255 octets] */ |
| 362 |
/* Tests_SRS_SASL_PLAIN_01_021: [ authzid = 1*SAFE ; MUST accept up to 255 octets] */ |
| 363 |
/* Tests_SRS_SASL_PLAIN_01_022: [ passwd = 1*SAFE ; MUST accept up to 255 octets] */ |
| 364 |
TEST_FUNCTION(saslplain_get_init_bytes_with_max_bytes_for_each_field_succeeds) |
| 365 |
{ |
| 366 |
// arrange |
| 367 |
int result; |
| 368 |
unsigned char expected_bytes[] = MAX_AUTHZID "\0" MAX_AUTHCID "\0" MAX_PASSWD; |
| 369 |
SASL_PLAIN_CONFIG sasl_plain_config = { MAX_AUTHCID, MAX_PASSWD, MAX_AUTHZID }; |
| 370 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 371 |
SASL_MECHANISM_BYTES init_bytes; |
| 372 |
umock_c_reset_all_calls(); |
| 373 |
|
| 374 |
// act |
| 375 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(sasl_plain, &init_bytes); |
| 376 |
|
| 377 |
// assert |
| 378 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 379 |
ASSERT_ARE_EQUAL(size_t, sizeof(expected_bytes) - 1, init_bytes.length); |
| 380 |
ASSERT_ARE_EQUAL(int, 0, memcmp(expected_bytes, init_bytes.bytes, sizeof(expected_bytes) - 1)); |
| 381 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 382 |
|
| 383 |
// cleanup |
| 384 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 385 |
} |
| 386 |
|
| 387 |
/* Tests_SRS_SASL_PLAIN_01_020: [ authcid = 1*SAFE ; MUST accept up to 255 octets] */ |
| 388 |
TEST_FUNCTION(saslplain_get_init_bytes_with_authcid_over_max_bytes_fails) |
| 389 |
{ |
| 390 |
// arrange |
| 391 |
SASL_PLAIN_CONFIG sasl_plain_config = { MAX_AUTHCID "x", MAX_PASSWD, MAX_AUTHZID }; |
| 392 |
|
| 393 |
// act |
| 394 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 395 |
|
| 396 |
// assert |
| 397 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 398 |
ASSERT_IS_NULL(sasl_plain); |
| 399 |
} |
| 400 |
|
| 401 |
/* Tests_SRS_SASL_PLAIN_01_021: [ authzid = 1*SAFE ; MUST accept up to 255 octets] */ |
| 402 |
TEST_FUNCTION(saslplain_get_init_bytes_with_authzid_over_max_bytes_fails) |
| 403 |
{ |
| 404 |
// arrange |
| 405 |
SASL_PLAIN_CONFIG sasl_plain_config = { MAX_AUTHCID, MAX_PASSWD, MAX_AUTHZID "x" }; |
| 406 |
|
| 407 |
// act |
| 408 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 409 |
|
| 410 |
// assert |
| 411 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 412 |
ASSERT_IS_NULL(sasl_plain); |
| 413 |
} |
| 414 |
|
| 415 |
/* Tests_SRS_SASL_PLAIN_01_022: [ passwd = 1*SAFE ; MUST accept up to 255 octets] */ |
| 416 |
TEST_FUNCTION(saslplain_get_init_bytes_with_passwd_over_max_bytes_fails) |
| 417 |
{ |
| 418 |
// arrange |
| 419 |
SASL_PLAIN_CONFIG sasl_plain_config = { MAX_AUTHCID, MAX_PASSWD "x", MAX_AUTHZID }; |
| 420 |
|
| 421 |
// act |
| 422 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 423 |
|
| 424 |
// assert |
| 425 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 426 |
ASSERT_IS_NULL(sasl_plain); |
| 427 |
} |
| 428 |
|
| 429 |
/* Tests_SRS_SASL_PLAIN_01_020: [ authcid = 1*SAFE ; MUST accept up to 255 octets] */ |
| 430 |
TEST_FUNCTION(saslplain_get_init_bytes_with_authcid_zero_length_fails) |
| 431 |
{ |
| 432 |
// arrange |
| 433 |
SASL_PLAIN_CONFIG sasl_plain_config = { "", "passwd", "authzid" }; |
| 434 |
|
| 435 |
// act |
| 436 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 437 |
|
| 438 |
// assert |
| 439 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 440 |
ASSERT_IS_NULL(sasl_plain); |
| 441 |
} |
| 442 |
|
| 443 |
/* Tests_SRS_SASL_PLAIN_01_022: [ passwd = 1*SAFE ; MUST accept up to 255 octets] */ |
| 444 |
TEST_FUNCTION(saslplain_get_init_bytes_with_passwd_zero_length_fails) |
| 445 |
{ |
| 446 |
// arrange |
| 447 |
SASL_PLAIN_CONFIG sasl_plain_config = { "authcid", "", "authzid" }; |
| 448 |
|
| 449 |
// act |
| 450 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 451 |
|
| 452 |
// assert |
| 453 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 454 |
ASSERT_IS_NULL(sasl_plain); |
| 455 |
} |
| 456 |
|
| 457 |
/* Tests_SRS_SASL_PLAIN_01_009: [If any argument is NULL, `saslplain_get_init_bytes` shall return a non-zero value.] */ |
| 458 |
TEST_FUNCTION(saslplain_get_init_bytes_with_NULL_sasl_plain_handle_fails) |
| 459 |
{ |
| 460 |
// arrange |
| 461 |
SASL_MECHANISM_BYTES init_bytes; |
| 462 |
|
| 463 |
// act |
| 464 |
int result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(NULL, &init_bytes); |
| 465 |
|
| 466 |
// assert |
| 467 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 468 |
ASSERT_ARE_NOT_EQUAL(int, 0, result); |
| 469 |
} |
| 470 |
|
| 471 |
/* Tests_SRS_SASL_PLAIN_01_009: [If any argument is NULL, `saslplain_get_init_bytes` shall return a non-zero value.] */ |
| 472 |
TEST_FUNCTION(saslplain_get_init_bytes_with_NULL_init_bytes_fails) |
| 473 |
{ |
| 474 |
// arrange |
| 475 |
int result; |
| 476 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 477 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 478 |
umock_c_reset_all_calls(); |
| 479 |
|
| 480 |
// act |
| 481 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_init_bytes(sasl_plain, NULL); |
| 482 |
|
| 483 |
// assert |
| 484 |
ASSERT_ARE_NOT_EQUAL(int, 0, result); |
| 485 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 486 |
|
| 487 |
// cleanup |
| 488 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 489 |
} |
| 490 |
|
| 491 |
/* saslplain_get_mechanism_name */ |
| 492 |
|
| 493 |
/* Tests_SRS_SASL_PLAIN_01_010: [`saslplain_get_mechanism_name` shall validate the argument `concrete_sasl_mechanism` and on success it shall return a pointer to the string "PLAIN".] */ |
| 494 |
TEST_FUNCTION(saslplain_get_mechanism_name_with_non_NULL_concrete_sasl_mechanism_succeeds) |
| 495 |
{ |
| 496 |
// arrange |
| 497 |
const char* result; |
| 498 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 499 |
CONCRETE_SASL_MECHANISM_HANDLE sasl_plain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 500 |
umock_c_reset_all_calls(); |
| 501 |
|
| 502 |
// act |
| 503 |
result = saslplain_get_interface()->concrete_sasl_mechanism_get_mechanism_name(sasl_plain); |
| 504 |
|
| 505 |
// assert |
| 506 |
ASSERT_ARE_EQUAL(char_ptr, "PLAIN", result); |
| 507 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 508 |
|
| 509 |
// cleanup |
| 510 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(sasl_plain); |
| 511 |
} |
| 512 |
|
| 513 |
/* Tests_SRS_SASL_PLAIN_01_011: [If the argument `concrete_sasl_mechanism` is NULL, `saslplain_get_mechanism_name` shall return NULL.] */ |
| 514 |
TEST_FUNCTION(saslplain_get_mechanism_name_with_NULL_concrete_sasl_mechanism_fails) |
| 515 |
{ |
| 516 |
// arrange |
| 517 |
|
| 518 |
// act |
| 519 |
const char* result = saslplain_get_interface()->concrete_sasl_mechanism_get_mechanism_name(NULL); |
| 520 |
|
| 521 |
// assert |
| 522 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 523 |
ASSERT_IS_NULL(result); |
| 524 |
} |
| 525 |
|
| 526 |
/* saslplain_challenge */ |
| 527 |
|
| 528 |
/* Tests_SRS_SASL_PLAIN_01_012: [`saslplain_challenge` shall set the `response_bytes` buffer to NULL and 0 size as the PLAIN SASL mechanism does not implement challenge/response.] */ |
| 529 |
/* Tests_SRS_SASL_PLAIN_01_013: [On success, `saslplain_challenge` shall return 0.] */ |
| 530 |
TEST_FUNCTION(saslplain_challenge_returns_a_NULL_response_bytes_buffer) |
| 531 |
{ |
| 532 |
// arrange |
| 533 |
int result; |
| 534 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 535 |
CONCRETE_SASL_MECHANISM_HANDLE saslplain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 536 |
SASL_MECHANISM_BYTES challenge_bytes; |
| 537 |
SASL_MECHANISM_BYTES response_bytes; |
| 538 |
umock_c_reset_all_calls(); |
| 539 |
|
| 540 |
// act |
| 541 |
result = saslplain_get_interface()->concrete_sasl_mechanism_challenge(saslplain, &challenge_bytes, &response_bytes); |
| 542 |
|
| 543 |
// assert |
| 544 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 545 |
ASSERT_IS_NULL(response_bytes.bytes); |
| 546 |
ASSERT_ARE_EQUAL(size_t, 0, response_bytes.length); |
| 547 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 548 |
|
| 549 |
// cleanup |
| 550 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(saslplain); |
| 551 |
} |
| 552 |
|
| 553 |
/* Tests_SRS_SASL_PLAIN_01_013: [On success, `saslplain_challenge` shall return 0.] */ |
| 554 |
TEST_FUNCTION(saslplain_with_NULL_challenge_bytes_returns_a_NULL_response_bytes_buffer) |
| 555 |
{ |
| 556 |
// arrange |
| 557 |
int result; |
| 558 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 559 |
CONCRETE_SASL_MECHANISM_HANDLE saslplain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 560 |
SASL_MECHANISM_BYTES response_bytes; |
| 561 |
umock_c_reset_all_calls(); |
| 562 |
|
| 563 |
// act |
| 564 |
result = saslplain_get_interface()->concrete_sasl_mechanism_challenge(saslplain, NULL, &response_bytes); |
| 565 |
|
| 566 |
// assert |
| 567 |
ASSERT_ARE_EQUAL(int, 0, result); |
| 568 |
ASSERT_IS_NULL(response_bytes.bytes); |
| 569 |
ASSERT_ARE_EQUAL(size_t, 0, response_bytes.length); |
| 570 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 571 |
|
| 572 |
// cleanup |
| 573 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(saslplain); |
| 574 |
} |
| 575 |
|
| 576 |
/* Tests_SRS_SASL_PLAIN_01_014: [If the `concrete_sasl_mechanism` or `response_bytes` argument is NULL then `saslplain_challenge` shall fail and return a non-zero value.] */ |
| 577 |
TEST_FUNCTION(saslplain_challenge_with_NULL_handle_fails) |
| 578 |
{ |
| 579 |
// arrange |
| 580 |
SASL_MECHANISM_BYTES challenge_bytes; |
| 581 |
SASL_MECHANISM_BYTES response_bytes; |
| 582 |
|
| 583 |
// act |
| 584 |
int result = saslplain_get_interface()->concrete_sasl_mechanism_challenge(NULL, &challenge_bytes, &response_bytes); |
| 585 |
|
| 586 |
// assert |
| 587 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 588 |
ASSERT_ARE_NOT_EQUAL(int, 0, result); |
| 589 |
} |
| 590 |
|
| 591 |
/* Tests_SRS_SASL_PLAIN_01_014: [If the `concrete_sasl_mechanism` or `response_bytes` argument is NULL then `saslplain_challenge` shall fail and return a non-zero value.] */ |
| 592 |
TEST_FUNCTION(saslplain_challenge_with_NULL_response_bytes_fails) |
| 593 |
{ |
| 594 |
// arrange |
| 595 |
int result; |
| 596 |
SASL_PLAIN_CONFIG sasl_plain_config = { "test_authcid", "test_pwd", NULL }; |
| 597 |
CONCRETE_SASL_MECHANISM_HANDLE saslplain = saslplain_get_interface()->concrete_sasl_mechanism_create(&sasl_plain_config); |
| 598 |
SASL_MECHANISM_BYTES challenge_bytes; |
| 599 |
umock_c_reset_all_calls(); |
| 600 |
|
| 601 |
// act |
| 602 |
result = saslplain_get_interface()->concrete_sasl_mechanism_challenge(saslplain, &challenge_bytes, NULL); |
| 603 |
|
| 604 |
// assert |
| 605 |
ASSERT_ARE_NOT_EQUAL(int, 0, result); |
| 606 |
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); |
| 607 |
|
| 608 |
// cleanup |
| 609 |
saslplain_get_interface()->concrete_sasl_mechanism_destroy(saslplain); |
| 610 |
} |
| 611 |
|
| 612 |
/* saslplain_get_interface */ |
| 613 |
|
| 614 |
/* Tests_SRS_SASL_PLAIN_01_015: [**`saslplain_get_interface` shall return a pointer to a `SASL_MECHANISM_INTERFACE_DESCRIPTION` structure that contains pointers to the functions: `saslplain_create`, `saslplain_destroy`, `saslplain_get_init_bytes`, `saslplain_get_mechanism_name`, `saslplain_challenge`.] */ |
| 615 |
TEST_FUNCTION(saslplain_get_interface_returns_the_sasl_plain_mechanism_interface) |
| 616 |
{ |
| 617 |
// arrange |
| 618 |
|
| 619 |
// act |
| 620 |
const SASL_MECHANISM_INTERFACE_DESCRIPTION* result = saslplain_get_interface(); |
| 621 |
|
| 622 |
// assert |
| 623 |
ASSERT_IS_NOT_NULL(result->concrete_sasl_mechanism_create); |
| 624 |
ASSERT_IS_NOT_NULL(result->concrete_sasl_mechanism_destroy); |
| 625 |
ASSERT_IS_NOT_NULL(result->concrete_sasl_mechanism_get_init_bytes); |
| 626 |
ASSERT_IS_NOT_NULL(result->concrete_sasl_mechanism_get_mechanism_name); |
| 627 |
ASSERT_IS_NOT_NULL(result->concrete_sasl_mechanism_challenge); |
| 628 |
} |
| 629 |
|
| 630 |
END_TEST_SUITE(sasl_plain_ut) |