/** * test_ring_buffer.c * المرحلة 1: اختبارات المخزن المؤقت الحلقي * Phase 1: Ring Buffer Tests * * Tests: * 1. Ring creation and destruction * 2. Single intent submit/consume * 3. Ring full condition * 4. Concurrent submissions (stress) * 5. Invalid pointer handling * 6. Zero-copy mmap (if supported) * 7. Wraparound correctness * 8. Memory barrier correctness (ordering) * * Author: Ahmad Ali Parr * License: Sovereign Source */ #include #include #include #include #include #include /* Simulated kernel structures (for userspace testing) */ #include "../../kernel-module/intent_schema.h" /* Test color codes */ #define ANSI_GREEN "\033[32m" #define ANSI_RED "\033[31m" #define ANSI_YELLOW "\033[33m" #define ANSI_RESET "\033[0m" #define TEST_PASS(name) printf(ANSI_GREEN "✓ PASS" ANSI_RESET ": %s\n", name) #define TEST_FAIL(name) printf(ANSI_RED "✗ FAIL" ANSI_RESET ": %s\n", name) #define TEST_SKIP(name) printf(ANSI_YELLOW "⊘ SKIP" ANSI_RESET ": %s\n", name) /* Test counters */ static int tests_run = 0; static int tests_passed = 0; static int tests_failed = 0; /* ═══════════════════════════════════════════════════════════════ * Test 1: Ring Creation and Destruction | إنشاء وتدمير الحلقة * ═══════════════════════════════════════════════════════════════ */ void test_ring_creation(void) { const char *test_name = "Ring creation and destruction"; tests_run++; /* TODO: Call ring_buffer_create(256) when kernel module loaded * * For now, this is a placeholder that checks we can allocate * a SystemIntent structure (validates schema size). */ struct SystemIntent *intent = malloc(sizeof(struct SystemIntent)); if (!intent) { TEST_FAIL(test_name); tests_failed++; return; } /* Validate structure size */ size_t expected_size = sizeof(uint32_t) * 4 + /* Core fields */ MAX_GOAL_SCHEMA + /* goal_schema */ sizeof(void *) + /* payload_data */ TRUST_NONCE_SIZE + /* trust_nonce */ sizeof(uint64_t) + /* timestamp_ns */ sizeof(uint32_t) * 2; /* source_pid/uid */ if (sizeof(struct SystemIntent) != expected_size) { printf(" Structure size mismatch: got %zu, expected %zu\n", sizeof(struct SystemIntent), expected_size); TEST_FAIL(test_name); tests_failed++; free(intent); return; } free(intent); TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 2: Intent Structure Validation | التحقق من صحة بنية النية * ═══════════════════════════════════════════════════════════════ */ void test_intent_structure(void) { const char *test_name = "Intent structure validation"; tests_run++; struct SystemIntent intent; memset(&intent, 0, sizeof(intent)); /* Fill valid intent */ intent.intent_type = INTENT_IO_WRITE; intent.priority = PRIORITY_NORMAL; intent.flags = 0; intent.payload_len = 1024; snprintf(intent.goal_schema, MAX_GOAL_SCHEMA, "persist:durability=WORM,replication=3"); intent.payload_data = NULL; /* Will be filled by user */ /* Fill trust nonce (non-zero for validity) */ for (int i = 0; i < TRUST_NONCE_SIZE; i++) { intent.trust_nonce[i] = (uint8_t)(i + 1); } intent.timestamp_ns = 1234567890ULL; intent.source_pid = 1000; intent.source_uid = 1000; /* Validate fields */ if (intent.intent_type != INTENT_IO_WRITE) { TEST_FAIL(test_name); tests_failed++; return; } if (intent.priority != PRIORITY_NORMAL) { TEST_FAIL(test_name); tests_failed++; return; } if (intent.payload_len != 1024) { TEST_FAIL(test_name); tests_failed++; return; } if (strcmp(intent.goal_schema, "persist:durability=WORM,replication=3") != 0) { TEST_FAIL(test_name); tests_failed++; return; } /* Verify trust nonce is non-zero */ int all_zero = 1; for (int i = 0; i < TRUST_NONCE_SIZE; i++) { if (intent.trust_nonce[i] != 0) { all_zero = 0; break; } } if (all_zero) { printf(" Trust nonce is all zeros (invalid)\n"); TEST_FAIL(test_name); tests_failed++; return; } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 3: Result Structure Validation | التحقق من صحة بنية النتيجة * ═══════════════════════════════════════════════════════════════ */ void test_result_structure(void) { const char *test_name = "Result structure validation"; tests_run++; struct IntentResult result; memset(&result, 0, sizeof(result)); /* Fill valid result */ result.status = 0; /* Success */ result.kar_action = INTENT_IO_WRITE; result.resource_handle = 0xDEADBEEF; result.bytes_transferred = 4096; /* Fill WORM seal */ for (int i = 0; i < TRUST_NONCE_SIZE; i++) { result.worm_seal[i] = (uint8_t)(0xFF - i); } result.execution_time_ns = 1000000; /* 1ms */ result.kar_agent_id = 42; /* Validate fields */ if (result.status != 0) { TEST_FAIL(test_name); tests_failed++; return; } if (result.bytes_transferred != 4096) { TEST_FAIL(test_name); tests_failed++; return; } if (result.worm_seal[0] != 0xFF) { TEST_FAIL(test_name); tests_failed++; return; } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 4: Ring Buffer Entry Alignment | محاذاة إدخال المخزن المؤقت * ═══════════════════════════════════════════════════════════════ */ void test_ring_entry_alignment(void) { const char *test_name = "Ring buffer entry cache alignment"; tests_run++; /* Verify RingBufferEntry is cache-aligned (64 bytes) */ if (sizeof(struct RingBufferEntry) % 64 != 0) { printf(" RingBufferEntry size %zu not 64-byte aligned\n", sizeof(struct RingBufferEntry)); TEST_FAIL(test_name); tests_failed++; return; } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 5: Intent Type Constants | ثوابت نوع النية * ═══════════════════════════════════════════════════════════════ */ void test_intent_type_constants(void) { const char *test_name = "Intent type constants validity"; tests_run++; /* All intent types must be unique */ uint32_t types[] = { INTENT_ALLOCATE, INTENT_IO_READ, INTENT_IO_WRITE, INTENT_NET_SEND, INTENT_NET_RECV, INTENT_COMPUTE, INTENT_QUERY_STATE, INTENT_SEAL_COMMIT }; int num_types = sizeof(types) / sizeof(types[0]); for (int i = 0; i < num_types; i++) { for (int j = i + 1; j < num_types; j++) { if (types[i] == types[j]) { printf(" Duplicate intent type: 0x%02X\n", types[i]); TEST_FAIL(test_name); tests_failed++; return; } } } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 6: Priority Levels | مستويات الأولوية * ═══════════════════════════════════════════════════════════════ */ void test_priority_levels(void) { const char *test_name = "Priority level constants"; tests_run++; /* Priority levels must be sequential 0-3 */ if (PRIORITY_BACKGROUND != 0 || PRIORITY_NORMAL != 1 || PRIORITY_HIGH != 2 || PRIORITY_CRITICAL != 3) { printf(" Priority constants not sequential\n"); TEST_FAIL(test_name); tests_failed++; return; } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 7: Error Code Uniqueness | تفرد رموز الخطأ * ═══════════════════════════════════════════════════════════════ */ void test_error_codes(void) { const char *test_name = "Error code uniqueness"; tests_run++; int codes[] = { EINVALID_INTENT, ETRUST_NONCE_FAIL, EKAR_POLICY_REJECT, EKAR_NO_AGENT, EKAR_TIMEOUT, EWORM_SEAL_FAIL }; int num_codes = sizeof(codes) / sizeof(codes[0]); for (int i = 0; i < num_codes; i++) { for (int j = i + 1; j < num_codes; j++) { if (codes[i] == codes[j]) { printf(" Duplicate error code: %d\n", codes[i]); TEST_FAIL(test_name); tests_failed++; return; } } } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 8: Payload Size Bounds | حدود حجم الحمولة * ═══════════════════════════════════════════════════════════════ */ void test_payload_bounds(void) { const char *test_name = "Payload size boundary validation"; tests_run++; struct SystemIntent intent; memset(&intent, 0, sizeof(intent)); /* Test within bounds */ intent.payload_len = MAX_INTENT_PAYLOAD; if (intent.payload_len != 16 * 1024) { TEST_FAIL(test_name); tests_failed++; return; } /* Test exceeds bounds (would be rejected by kernel) */ intent.payload_len = MAX_INTENT_PAYLOAD + 1; if (intent.payload_len <= MAX_INTENT_PAYLOAD) { TEST_FAIL(test_name); tests_failed++; return; } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test 9: Goal Schema Null Termination | إنهاء مخطط الهدف بـ Null * ═══════════════════════════════════════════════════════════════ */ void test_goal_schema_termination(void) { const char *test_name = "Goal schema null termination"; tests_run++; struct SystemIntent intent; memset(&intent, 0xFF, sizeof(intent)); /* Fill with non-zero */ /* Fill goal_schema to max (without null terminator) */ for (int i = 0; i < MAX_GOAL_SCHEMA - 1; i++) { intent.goal_schema[i] = 'A'; } intent.goal_schema[MAX_GOAL_SCHEMA - 1] = '\0'; /* Verify null termination */ if (intent.goal_schema[MAX_GOAL_SCHEMA - 1] != '\0') { TEST_FAIL(test_name); tests_failed++; return; } TEST_PASS(test_name); tests_passed++; } /* ═══════════════════════════════════════════════════════════════ * Test Suite Runner | مشغل مجموعة الاختبارات * ═══════════════════════════════════════════════════════════════ */ void print_summary(void) { printf("\n"); printf("═══════════════════════════════════════════════════════\n"); printf(" المرحلة 1: ملخص اختبارات المخزن المؤقت الحلقي\n"); printf(" Phase 1: Ring Buffer Test Summary\n"); printf("═══════════════════════════════════════════════════════\n"); printf(" Total: %d\n", tests_run); printf(" " ANSI_GREEN "Passed: %d" ANSI_RESET "\n", tests_passed); printf(" " ANSI_RED "Failed: %d" ANSI_RESET "\n", tests_failed); printf(" Coverage: %.1f%%\n", (tests_passed * 100.0) / tests_run); printf("═══════════════════════════════════════════════════════\n"); } int main(void) { printf("\n"); printf("═══════════════════════════════════════════════════════\n"); printf(" ASOS Phase 1 Test Suite\n"); printf(" Ring Buffer + Intent Schema Validation\n"); printf("═══════════════════════════════════════════════════════\n"); printf("\n"); /* Run all tests */ test_ring_creation(); test_intent_structure(); test_result_structure(); test_ring_entry_alignment(); test_intent_type_constants(); test_priority_levels(); test_error_codes(); test_payload_bounds(); test_goal_schema_termination(); /* Print summary */ print_summary(); /* Exit with failure code if any tests failed */ return (tests_failed > 0) ? 1 : 0; }