Files
makronix/ext/src/mcde/lib/McdeSys/test_sysinfo.c
T
lotherkandClaude Sonnet 5.5 abd4c853de licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

214 lines
6.5 KiB
C

/*
* test_sysinfo.c -- McdeSys: mcde_sys_info on this computer (make check)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The values differ from computer to computer; what must hold everywhere is
* checked, and everything is printed for a look by hand.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <Mcde/McdeSys.h>
int
main(void)
{
McdeSysInfo i;
int failures = 0;
if (mcde_sys_info(&i) != MCDE_SYS_OK) {
printf("FAIL mcde_sys_info: %s\n", mcde_sys_error());
return 1;
}
printf("backend %s\nsystem %s %s\nkernel %s\nmodel %s\n"
"cpu %s (%d)\nmemory %llu MB\ndisk %s, %llu of %llu MB free\n"
"hostname %s\n", mcde_sys_backend(), i.system, i.version, i.kernel,
i.model, i.cpu, i.cpus, i.memory >> 20, i.disk, i.diskFree >> 20,
i.diskSize >> 20, i.hostname);
if (i.system[0] == '\0' || i.kernel[0] == '\0') {
printf("FAIL no system name\n");
failures++;
}
if (i.cpus < 1 || i.memory == 0 || i.diskSize == 0) {
printf("FAIL no processors, memory or disk\n");
failures++;
}
if (i.diskFree > i.diskSize) {
printf("FAIL more free than there is\n");
failures++;
}
{
McdeSysInterface ifs[16];
McdeSysBluetooth bt;
int n, k, vol = -1, muted = -1;
/* only read here: a test does not change the computer */
n = mcde_sys_interfaces(ifs, 16);
if (n < 0) {
printf("FAIL mcde_sys_interfaces: %s\n", mcde_sys_error());
failures++;
}
for (k = 0; k < n; k++)
printf("net %s %s %s %s%s%s\n", ifs[k].name,
ifs[k].kind == MCDE_NET_WIFI ? "wifi" :
ifs[k].kind == MCDE_NET_WIRED ? "wired" : "other",
ifs[k].up ? "up" : "down", ifs[k].address,
ifs[k].ssid[0] != '\0' ? " ssid " : "", ifs[k].ssid);
/* the settings of each card, read; and a nonsense address that
* must be refused before anything is written */
for (k = 0; k < n; k++) {
static const char *const m4[] = { "dhcp",
"dhcp+manual", "manual", "off" };
McdeSysNetConfig c, bad;
int r;
if (ifs[k].kind == MCDE_NET_OTHER)
continue;
r = mcde_sys_net_config(ifs[k].name, &c);
printf("netcfg %s %s %s/%s router %s now %s/%s via %s "
"dns %d mac %s mtu %d%s%s\n", ifs[k].name,
(unsigned) c.ipv4 < 4 ? m4[c.ipv4] : "?",
c.address, c.netmask, c.router, c.cur_address,
c.cur_netmask, c.cur_router, c.ncur_dns, c.mac,
c.mtu, r == MCDE_SYS_OK ? "" : " -- ",
r == MCDE_SYS_OK ? "" : mcde_sys_error());
if ((unsigned) c.ipv4 > MCDE_IPV4_OFF ||
(c.cur_address[0] != '\0' && c.cur_netmask[0] ==
'\0')) {
printf("FAIL netcfg %s\n", ifs[k].name);
failures++;
}
bad = c;
bad.ipv4 = MCDE_IPV4_MANUAL;
snprintf(bad.address, sizeof bad.address, "300.1.2.3");
if (mcde_sys_net_configure(ifs[k].name, &bad) ==
MCDE_SYS_OK) {
printf("FAIL 300.1.2.3 taken\n");
failures++;
}
}
if (mcde_sys_volume_get(&vol, &muted) == MCDE_SYS_OK) {
printf("volume %d%%%s\n", vol, muted ? " muted" : "");
if (vol < 0 || vol > 150) {
printf("FAIL volume out of range\n");
failures++;
}
} else
printf("volume (%s)\n", mcde_sys_error());
if (mcde_sys_bluetooth(&bt) == MCDE_SYS_OK)
printf("bluetooth %s%s%s\n", bt.present ? bt.name : "none",
bt.present ? " " : "", bt.present ?
(bt.on ? "on" : "off") : "");
/* the Wi-Fi networks in reach, read only */
for (k = 0; k < n; k++)
if (ifs[k].kind == MCDE_NET_WIFI) {
McdeSysWifi nets[64];
int nn = mcde_sys_wifi_scan(ifs[k].name, nets, 64);
printf("wifi %s: %d networks%s%s\n",
ifs[k].name, nn, nn < 0 ? ", " : "",
nn < 0 ? mcde_sys_error() : "");
if (nn > 0)
printf(" first: %s, %d%%%s\n",
nets[0].ssid, nets[0].signal,
nets[0].secured ? ", secured" : "");
}
/* the sharing services, read only */
{
static const char *const names[MCDE_SHARE_COUNT] = {
"ssh", "smb", "nfs", "afp", "appletalk" };
int s;
for (s = 0; s < MCDE_SHARE_COUNT; s++) {
McdeSysServiceState st;
if (mcde_sys_service_state((McdeSysShareService) s,
&st) != MCDE_SYS_OK) {
printf("FAIL service %s: %s\n", names[s],
mcde_sys_error());
failures++;
continue;
}
printf("share %-9s installed %d enabled %d "
"running %d\n", names[s], st.installed,
st.enabled, st.running);
}
}
{
static char zones[600][MCDE_SYS_ZONE_LEN];
char zone[MCDE_SYS_ZONE_LEN];
int nz = mcde_sys_timezones(zones, 600);
(void) mcde_sys_timezone_get(zone, sizeof zone);
printf("zone %s, %d zones offered\n", zone, nz);
if (nz < 2) {
printf("FAIL no list of time zones\n");
failures++;
}
/* a bad name is refused before anything is touched */
if (mcde_sys_timezone_set("../../etc/passwd") !=
MCDE_SYS_EINVAL) {
printf("FAIL a bad zone name was taken\n");
failures++;
}
}
{
McdeSysBattery b;
if (mcde_sys_battery(&b) == MCDE_SYS_OK) {
if (b.present)
printf("battery %d%%%s%s, %d min\n",
b.percent, b.charging ?
" charging" : "", b.ac ? " ac" : "",
b.minutes);
else
printf("battery none%s\n",
b.ac ? " ac" : "");
if (b.percent < 0 || b.percent > 100) {
printf("FAIL battery out of range\n");
failures++;
}
}
}
}
{
McdeSysUser users[64], bad;
McdeSysUpdate up;
int n, k;
n = mcde_sys_users(users, 64);
for (k = 0; k < n; k++)
printf("user %s (%s) uid %d%s\n", users[k].login,
users[k].fullname, (int) users[k].uid,
users[k].admin ? " admin" : "");
/* refused before anything is run: a bad name, and -- not as
* root -- a good one */
memset(&bad, 0, sizeof bad);
snprintf(bad.login, sizeof bad.login, "Bad Name");
if (mcde_sys_user_add(&bad, NULL) != MCDE_SYS_EINVAL) {
printf("FAIL a bad login was not refused\n");
failures++;
}
snprintf(bad.login, sizeof bad.login, "mcdetest");
if (geteuid() != 0 &&
mcde_sys_user_add(&bad, NULL) != MCDE_SYS_EPERM) {
printf("FAIL adding without root was not refused\n");
failures++;
}
if (mcde_sys_update_check(&up) == MCDE_SYS_OK)
printf("update running %s, available %s, packages %d, "
"source %s%s%s\n", up.current, up.available[0] != '\0'
? up.available : "-", up.packages, up.source[0] != '\0'
? up.source : "-", up.note[0] != '\0' ? "; " : "",
up.note);
else
printf("update (%s)\n", mcde_sys_error());
}
if (failures == 0)
printf("PASS test_sysinfo\n");
return failures != 0;
}