| | | 1 | | #include "../../security/jwt.h" |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <assert.h> |
| | | 4 | | #include <string.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | #include <time.h> |
| | | 7 | | #include <stdbool.h> |
| | | 8 | | |
| | | 9 | | // Note: This test links against the REAL jwt.c and db_connection.c |
| | | 10 | | // We focus on the JSON/Token logic which doesn't require a live DB for the most part. |
| | | 11 | | |
| | 1 | 12 | | void test_jwt_lifecycle() { |
| | 1 | 13 | | printf("Testing JWT Generate/Validate/Extract lifecycle...\n"); |
| | | 14 | | |
| | 1 | 15 | | int user_id = 42; |
| | 1 | 16 | | const char *user = "testadmin"; |
| | 1 | 17 | | const char *role = "superadmin"; |
| | | 18 | | |
| | | 19 | | // 1. Generate |
| | 1 | 20 | | char *token = generate_auth_token(user_id, user, role); |
| | 1 | 21 | | assert(token != NULL); |
| | 1 | 22 | | assert(strncmp(token, "simple_jwt:", 11) == 0); |
| | 1 | 23 | | printf("Generated Token: %s\n", token); |
| | | 24 | | |
| | | 25 | | // 2. Validate |
| | 1 | 26 | | assert(validate_auth_token(token) == true); |
| | | 27 | | |
| | | 28 | | // 3. Extract User ID |
| | 1 | 29 | | int extracted_id = get_user_id_from_token(token); |
| | 1 | 30 | | assert(extracted_id == user_id); |
| | | 31 | | |
| | | 32 | | // 4. Extract Role |
| | 1 | 33 | | char *extracted_role = get_role_from_token(token); |
| | 1 | 34 | | assert(extracted_role != NULL); |
| | 1 | 35 | | assert(strcmp(extracted_role, role) == 0); |
| | 1 | 36 | | free(extracted_role); |
| | | 37 | | |
| | | 38 | | // 5. Cleanup |
| | 1 | 39 | | free(token); |
| | 1 | 40 | | printf("[PASS] test_jwt_lifecycle\n"); |
| | 1 | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | void test_jwt_errors() { |
| | 1 | 44 | | printf("Testing JWT error cases...\n"); |
| | | 45 | | |
| | | 46 | | // Null inputs |
| | 1 | 47 | | assert(generate_auth_token(1, NULL, "admin") == NULL); |
| | 1 | 48 | | assert(generate_auth_token(1, "admin", NULL) == NULL); |
| | | 49 | | |
| | | 50 | | // Invalid validation |
| | 1 | 51 | | assert(validate_auth_token(NULL) == false); |
| | 1 | 52 | | assert(validate_auth_token("") == false); |
| | 1 | 53 | | assert(validate_auth_token("not_a_jwt") == false); |
| | 1 | 54 | | assert(validate_auth_token("simple_jwt:invalid_json") == false); |
| | 1 | 55 | | assert(validate_auth_token("simple_jwt:{\"no_exp\":1}") == false); |
| | | 56 | | |
| | | 57 | | // Invalid extraction |
| | 1 | 58 | | assert(get_user_id_from_token(NULL) == -1); |
| | 1 | 59 | | assert(get_user_id_from_token("invalid") == -1); |
| | 1 | 60 | | assert(get_role_from_token(NULL) == NULL); |
| | 1 | 61 | | assert(get_role_from_token("invalid") == NULL); |
| | | 62 | | |
| | 1 | 63 | | printf("[PASS] test_jwt_errors\n"); |
| | 1 | 64 | | } |
| | | 65 | | |
| | 1 | 66 | | int main() { |
| | 1 | 67 | | printf("=== Real JWT Unit Tests ===\n"); |
| | 1 | 68 | | test_jwt_lifecycle(); |
| | 1 | 69 | | test_jwt_errors(); |
| | 1 | 70 | | printf("\n✅ All JWT tests passed!\n"); |
| | 1 | 71 | | return 0; |
| | | 72 | | } |