< Summary - Backend C Tests - Coverage Report (WSL)

Information
Class: test_jwt_standalone_c
Assembly: src.backend.tests.unit
File(s): ./src/backend/tests/unit/test_jwt_standalone.c
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 72
Line coverage: 100%
Branch coverage
50%
Covered branches: 17
Total branches: 34
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/18/2026 - 10:50:55 PM Line coverage: 100% (40/40) Branch coverage: 50% (17/34) Total lines: 72 2/18/2026 - 10:50:55 PM Line coverage: 100% (40/40) Branch coverage: 50% (17/34) Total lines: 72

File(s)

./src/backend/tests/unit/test_jwt_standalone.c

#LineLine coverage
 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
 112void test_jwt_lifecycle() {
 113    printf("Testing JWT Generate/Validate/Extract lifecycle...\n");
 14
 115    int user_id = 42;
 116    const char *user = "testadmin";
 117    const char *role = "superadmin";
 18
 19    // 1. Generate
 120    char *token = generate_auth_token(user_id, user, role);
 121    assert(token != NULL);
 122    assert(strncmp(token, "simple_jwt:", 11) == 0);
 123    printf("Generated Token: %s\n", token);
 24
 25    // 2. Validate
 126    assert(validate_auth_token(token) == true);
 27
 28    // 3. Extract User ID
 129    int extracted_id = get_user_id_from_token(token);
 130    assert(extracted_id == user_id);
 31
 32    // 4. Extract Role
 133    char *extracted_role = get_role_from_token(token);
 134    assert(extracted_role != NULL);
 135    assert(strcmp(extracted_role, role) == 0);
 136    free(extracted_role);
 37
 38    // 5. Cleanup
 139    free(token);
 140    printf("[PASS] test_jwt_lifecycle\n");
 141}
 42
 143void test_jwt_errors() {
 144    printf("Testing JWT error cases...\n");
 45
 46    // Null inputs
 147    assert(generate_auth_token(1, NULL, "admin") == NULL);
 148    assert(generate_auth_token(1, "admin", NULL) == NULL);
 49
 50    // Invalid validation
 151    assert(validate_auth_token(NULL) == false);
 152    assert(validate_auth_token("") == false);
 153    assert(validate_auth_token("not_a_jwt") == false);
 154    assert(validate_auth_token("simple_jwt:invalid_json") == false);
 155    assert(validate_auth_token("simple_jwt:{\"no_exp\":1}") == false);
 56
 57    // Invalid extraction
 158    assert(get_user_id_from_token(NULL) == -1);
 159    assert(get_user_id_from_token("invalid") == -1);
 160    assert(get_role_from_token(NULL) == NULL);
 161    assert(get_role_from_token("invalid") == NULL);
 62
 163    printf("[PASS] test_jwt_errors\n");
 164}
 65
 166int main() {
 167    printf("=== Real JWT Unit Tests ===\n");
 168    test_jwt_lifecycle();
 169    test_jwt_errors();
 170    printf("\n✅ All JWT tests passed!\n");
 171    return 0;
 72}

Methods/Properties