aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / test_klog.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* test_klog.c -- core/klog.c: head kept in order, ring keeps the newest |
| 18 | * bytes, the dropped-bytes line, the cap. */ |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include "ck_test.h" |
| 22 | #include "klog.h" |
| 23 | |
| 24 | static char out[CK_KLOG_MAX + 16]; |
| 25 | |
| 26 | int main(void) |
| 27 | { |
| 28 | /* Small log: read back exactly. */ |
| 29 | ck_klog_put("hello\n", 6); |
| 30 | ck_klog_put("world\n", 6); |
| 31 | size_t n = ck_klog_read(out, sizeof out); |
| 32 | CHECK(n == 12 && memcmp(out, "hello\nworld\n", 12) == 0); |
| 33 | CHECK(ck_klog_total() == 12); |
| 34 | /* Cap is respected. */ |
| 35 | CHECK(ck_klog_read(out, 5) == 5 && memcmp(out, "hello", 5) == 0); |
| 36 | CHECK(ck_klog_read(out, 0) == 0); |
| 37 | |
| 38 | /* Fill the head exactly, then less than one ring: nothing dropped. */ |
| 39 | ck_klog_clear(); |
| 40 | char *big = malloc(CK_KLOG_HEAD + 3 * CK_KLOG_TAIL); |
| 41 | for (size_t i = 0; i < CK_KLOG_HEAD + 3 * CK_KLOG_TAIL; i++) |
| 42 | big[i] = (char)('a' + i % 23); |
| 43 | ck_klog_put(big, CK_KLOG_HEAD + 100); |
| 44 | n = ck_klog_read(out, sizeof out); |
| 45 | CHECK(n == CK_KLOG_HEAD + 100 && memcmp(out, big, n) == 0); |
| 46 | |
| 47 | /* Overflow the ring: head intact, dropped line, newest CK_KLOG_TAIL bytes. */ |
| 48 | ck_klog_clear(); |
| 49 | size_t put = CK_KLOG_HEAD + 2 * CK_KLOG_TAIL + 777; |
| 50 | for (size_t i = 0; i < put; i += 1000) /* uneven pieces */ |
| 51 | ck_klog_put(big + i, put - i < 1000 ? put - i : 1000); |
| 52 | CHECK(ck_klog_total() == put); |
| 53 | n = ck_klog_read(out, sizeof out); |
| 54 | CHECK(memcmp(out, big, CK_KLOG_HEAD) == 0); |
| 55 | char line[64]; |
| 56 | snprintf(line, sizeof line, "\n[klog: %zu bytes dropped]\n", put - CK_KLOG_HEAD - CK_KLOG_TAIL); |
| 57 | size_t ll = strlen(line); |
| 58 | CHECK(memcmp(out + CK_KLOG_HEAD, line, ll) == 0); |
| 59 | CHECK(n == CK_KLOG_HEAD + ll + CK_KLOG_TAIL); |
| 60 | CHECK(memcmp(out + CK_KLOG_HEAD + ll, big + put - CK_KLOG_TAIL, CK_KLOG_TAIL) == 0); |
| 61 | CHECK(n <= CK_KLOG_MAX); |
| 62 | |
| 63 | /* Exactly one full ring: nothing dropped, no line. */ |
| 64 | ck_klog_clear(); |
| 65 | ck_klog_put(big, CK_KLOG_HEAD + CK_KLOG_TAIL); |
| 66 | n = ck_klog_read(out, sizeof out); |
| 67 | CHECK(n == CK_KLOG_HEAD + CK_KLOG_TAIL && memcmp(out, big, n) == 0); |
| 68 | free(big); |
| 69 | return ck_t_verdict("test_klog"); |
| 70 | } |