| | | 1 | | #include "../../security/rbac.h" |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <assert.h> |
| | | 4 | | #include <string.h> |
| | | 5 | | |
| | 1 | 6 | | void test_string_to_role_admin() { |
| | 1 | 7 | | UserRole role = string_to_role("admin"); |
| | 1 | 8 | | assert(role == ROLE_ADMIN); |
| | 1 | 9 | | printf("[PASS] string_to_role: admin\n"); |
| | 1 | 10 | | } |
| | | 11 | | |
| | 1 | 12 | | void test_string_to_role_teknisyen() { |
| | 1 | 13 | | UserRole role = string_to_role("teknisyen"); |
| | 1 | 14 | | assert(role == ROLE_TEKNISYEN); |
| | 1 | 15 | | printf("[PASS] string_to_role: teknisyen\n"); |
| | 1 | 16 | | } |
| | | 17 | | |
| | 1 | 18 | | void test_string_to_role_operator() { |
| | 1 | 19 | | UserRole role = string_to_role("operator"); |
| | 1 | 20 | | assert(role == ROLE_OPERATOR); |
| | 1 | 21 | | printf("[PASS] string_to_role: operator\n"); |
| | 1 | 22 | | } |
| | | 23 | | |
| | 1 | 24 | | void test_string_to_role_unknown() { |
| | 1 | 25 | | UserRole role = string_to_role("invalid_role"); |
| | 1 | 26 | | assert(role == ROLE_UNKNOWN); |
| | 1 | 27 | | printf("[PASS] string_to_role: unknown role\n"); |
| | 1 | 28 | | } |
| | | 29 | | |
| | 1 | 30 | | void test_string_to_role_null() { |
| | 1 | 31 | | UserRole role = string_to_role(NULL); |
| | 1 | 32 | | assert(role == ROLE_UNKNOWN); |
| | 1 | 33 | | printf("[PASS] string_to_role: NULL input\n"); |
| | 1 | 34 | | } |
| | | 35 | | |
| | 1 | 36 | | void test_can_delete_assets_admin() { |
| | 1 | 37 | | assert(can_delete_assets(ROLE_ADMIN) == 1); |
| | 1 | 38 | | printf("[PASS] can_delete_assets: admin allowed\n"); |
| | 1 | 39 | | } |
| | | 40 | | |
| | 1 | 41 | | void test_can_delete_assets_teknisyen() { |
| | 1 | 42 | | assert(can_delete_assets(ROLE_TEKNISYEN) == 0); |
| | 1 | 43 | | printf("[PASS] can_delete_assets: teknisyen denied\n"); |
| | 1 | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | void test_can_delete_assets_operator() { |
| | 1 | 47 | | assert(can_delete_assets(ROLE_OPERATOR) == 0); |
| | 1 | 48 | | printf("[PASS] can_delete_assets: operator denied\n"); |
| | 1 | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | void test_can_onboard_assets_admin() { |
| | 1 | 52 | | assert(can_onboard_assets(ROLE_ADMIN) == 1); |
| | 1 | 53 | | printf("[PASS] can_onboard_assets: admin allowed\n"); |
| | 1 | 54 | | } |
| | | 55 | | |
| | 1 | 56 | | void test_can_onboard_assets_teknisyen() { |
| | 1 | 57 | | assert(can_onboard_assets(ROLE_TEKNISYEN) == 1); |
| | 1 | 58 | | printf("[PASS] can_onboard_assets: teknisyen allowed\n"); |
| | 1 | 59 | | } |
| | | 60 | | |
| | 1 | 61 | | void test_can_onboard_assets_operator() { |
| | 1 | 62 | | assert(can_onboard_assets(ROLE_OPERATOR) == 0); |
| | 1 | 63 | | printf("[PASS] can_onboard_assets: operator denied\n"); |
| | 1 | 64 | | } |
| | | 65 | | |
| | 1 | 66 | | void test_can_view_system_all_roles() { |
| | 1 | 67 | | assert(can_view_system(ROLE_ADMIN) == 1); |
| | 1 | 68 | | assert(can_view_system(ROLE_TEKNISYEN) == 1); |
| | 1 | 69 | | assert(can_view_system(ROLE_OPERATOR) == 1); |
| | 1 | 70 | | printf("[PASS] can_view_system: all roles allowed\n"); |
| | 1 | 71 | | } |
| | | 72 | | |
| | 1 | 73 | | void test_can_view_system_unknown() { |
| | 1 | 74 | | assert(can_view_system(ROLE_UNKNOWN) == 0); |
| | 1 | 75 | | printf("[PASS] can_view_system: unknown role denied\n"); |
| | 1 | 76 | | } |
| | | 77 | | |
| | 1 | 78 | | int main() { |
| | 1 | 79 | | printf("=== RBAC Unit Tests ===\n"); |
| | 1 | 80 | | test_string_to_role_admin(); |
| | 1 | 81 | | test_string_to_role_teknisyen(); |
| | 1 | 82 | | test_string_to_role_operator(); |
| | 1 | 83 | | test_string_to_role_unknown(); |
| | 1 | 84 | | test_string_to_role_null(); |
| | 1 | 85 | | test_can_delete_assets_admin(); |
| | 1 | 86 | | test_can_delete_assets_teknisyen(); |
| | 1 | 87 | | test_can_delete_assets_operator(); |
| | 1 | 88 | | test_can_onboard_assets_admin(); |
| | 1 | 89 | | test_can_onboard_assets_teknisyen(); |
| | 1 | 90 | | test_can_onboard_assets_operator(); |
| | 1 | 91 | | test_can_view_system_all_roles(); |
| | 1 | 92 | | test_can_view_system_unknown(); |
| | 1 | 93 | | printf("\n✅ All RBAC tests passed!\n"); |
| | 1 | 94 | | return 0; |
| | | 95 | | } |