|
| 1 | +/* Host-side unit test for ELFUSE_GUEST_UID / ELFUSE_GUEST_GID environment |
| 2 | + * overrides. |
| 3 | + * |
| 4 | + * Copyright 2026 elfuse contributors |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <assert.h> |
| 11 | + |
| 12 | +#include "syscall/proc.h" |
| 13 | +#include "syscall/proc-identity.h" |
| 14 | +#include "syscall/abi.h" |
| 15 | + |
| 16 | +/* Mock shim_globals_publish_pgsid to avoid linking the entire guest shim |
| 17 | + * subsystem */ |
| 18 | +void shim_globals_publish_pgsid(guest_t *g, int64_t pgid, int64_t sid); |
| 19 | +void shim_globals_publish_pgsid(guest_t *g, int64_t pgid, int64_t sid) |
| 20 | +{ |
| 21 | + (void) g; |
| 22 | + (void) pgid; |
| 23 | + (void) sid; |
| 24 | +} |
| 25 | + |
| 26 | +int main(void) |
| 27 | +{ |
| 28 | + // Test 1: Fallback case (no env vars) |
| 29 | + unsetenv("ELFUSE_GUEST_UID"); |
| 30 | + unsetenv("ELFUSE_GUEST_GID"); |
| 31 | + proc_identity_init(); |
| 32 | + |
| 33 | + assert(proc_get_uid() == GUEST_UID); |
| 34 | + assert(proc_get_euid() == GUEST_UID); |
| 35 | + assert(proc_get_suid() == GUEST_UID); |
| 36 | + assert(proc_get_gid() == GUEST_GID); |
| 37 | + assert(proc_get_egid() == GUEST_GID); |
| 38 | + assert(proc_get_sgid() == GUEST_GID); |
| 39 | + |
| 40 | + // Test 2: Override case |
| 41 | + setenv("ELFUSE_GUEST_UID", "2000", 1); |
| 42 | + setenv("ELFUSE_GUEST_GID", "3000", 1); |
| 43 | + proc_identity_init(); |
| 44 | + |
| 45 | + assert(proc_get_uid() == 2000); |
| 46 | + assert(proc_get_euid() == 2000); |
| 47 | + assert(proc_get_suid() == 2000); |
| 48 | + assert(proc_get_gid() == 3000); |
| 49 | + assert(proc_get_egid() == 3000); |
| 50 | + assert(proc_get_sgid() == 3000); |
| 51 | + |
| 52 | + // Test 3: Override only UID |
| 53 | + setenv("ELFUSE_GUEST_UID", "4000", 1); |
| 54 | + unsetenv("ELFUSE_GUEST_GID"); |
| 55 | + proc_identity_init(); |
| 56 | + |
| 57 | + assert(proc_get_uid() == 4000); |
| 58 | + assert(proc_get_euid() == 4000); |
| 59 | + assert(proc_get_suid() == 4000); |
| 60 | + assert(proc_get_gid() == GUEST_GID); |
| 61 | + assert(proc_get_egid() == GUEST_GID); |
| 62 | + assert(proc_get_sgid() == GUEST_GID); |
| 63 | + |
| 64 | + // Test 4: Override only GID |
| 65 | + unsetenv("ELFUSE_GUEST_UID"); |
| 66 | + setenv("ELFUSE_GUEST_GID", "5000", 1); |
| 67 | + proc_identity_init(); |
| 68 | + |
| 69 | + assert(proc_get_uid() == GUEST_UID); |
| 70 | + assert(proc_get_euid() == GUEST_UID); |
| 71 | + assert(proc_get_suid() == GUEST_UID); |
| 72 | + assert(proc_get_gid() == 5000); |
| 73 | + assert(proc_get_egid() == 5000); |
| 74 | + assert(proc_get_sgid() == 5000); |
| 75 | + |
| 76 | + // Test 5: Invalid values should be ignored (fall back to default) |
| 77 | + setenv("ELFUSE_GUEST_UID", "-10", 1); |
| 78 | + setenv("ELFUSE_GUEST_GID", "abc", 1); |
| 79 | + proc_identity_init(); |
| 80 | + assert(proc_get_uid() == GUEST_UID); |
| 81 | + assert(proc_get_gid() == GUEST_GID); |
| 82 | + |
| 83 | + setenv("ELFUSE_GUEST_UID", "5000000000", 1); // overflows uint32_t |
| 84 | + setenv("ELFUSE_GUEST_GID", "", 1); |
| 85 | + proc_identity_init(); |
| 86 | + assert(proc_get_uid() == GUEST_UID); |
| 87 | + assert(proc_get_gid() == GUEST_GID); |
| 88 | + |
| 89 | + // Test 6: Dynamic values above INT_MAX but <= UINT32_MAX |
| 90 | + setenv("ELFUSE_GUEST_UID", "4294967294", 1); // valid uint32 > INT_MAX |
| 91 | + setenv("ELFUSE_GUEST_GID", "4294967295", 1); // valid uint32 (UINT32_MAX) |
| 92 | + proc_identity_init(); |
| 93 | + assert(proc_get_uid() == 4294967294); |
| 94 | + assert(proc_get_gid() == 4294967295); |
| 95 | + |
| 96 | + printf("test-identity-override-host: PASS\n"); |
| 97 | + return 0; |
| 98 | +} |
0 commit comments