/* * 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 #include #include #include #include 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()); } { char vols[8][MCDE_SYS_CGD_ENTRY_LEN], conf[128]; int on = -1, n, k; if (mcde_sys_swap_encrypt_get(&on) == MCDE_SYS_OK) printf("swap encrypted in swap: %s\n", on ? "yes" : "no"); else printf("swap (%s)\n", mcde_sys_error()); if (mcde_sys_firewall_get(&on, conf, sizeof conf) == MCDE_SYS_OK) printf("firewall %s, %s\n", on ? "on" : "off", conf); else printf("firewall (%s)\n", mcde_sys_error()); n = mcde_sys_cgd_volumes(vols, 8); if (n >= 0) { printf("cgd %d volume%s\n", n, n == 1 ? "" : "s"); for (k = 0; k < n; k++) printf(" %s\n", vols[k]); } else printf("cgd (%s)\n", mcde_sys_error()); } { McdeSysPrinter pr[16]; char names[8][MCDE_SYS_PRINTER_TEXT]; char uris[8][MCDE_SYS_PRINTER_URI]; int n, k; n = mcde_sys_printers(pr, 16); if (n >= 0) { for (k = 0; k < n; k++) printf("printer %s [%s]%s%s%s\n", pr[k].name, pr[k].state, pr[k].is_default ? " default" : "", pr[k].shared ? " shared" : "", pr[k].location[0] != '\0' ? " (see location)" : ""); } else printf("printer (%s)\n", mcde_sys_error()); n = mcde_sys_printers_discovered(names, uris, 8); if (n >= 0) { for (k = 0; k < n; k++) printf("found %s -> %s\n", names[k], uris[k]); } else printf("found (%s)\n", mcde_sys_error()); } { McdeSysMount mounts[64]; int n, k; n = mcde_sys_mounts(mounts, 64); if (n < 0) { printf("FAIL mcde_sys_mounts: %s\n", mcde_sys_error()); failures++; } else { int root = 0; for (k = 0; k < n; k++) { printf("mount %-24s %s %s%s%s%s\n", mounts[k].mountpoint, mounts[k].fstype, mounts[k].network ? "network" : mounts[k].volume ? "local" : "pseudo", mounts[k].removable ? " removable" : "", mounts[k].system ? " system" : "", mounts[k].label[0] != '\0' ? " label" : ""); if (strcmp(mounts[k].mountpoint, "/") == 0) root++; } /* every system has a root, mounted once */ if (root != 1) { printf("FAIL %d mounts of /\n", root); failures++; } } } { int fd = mcde_sys_disk_events(); char buf[256]; ssize_t r; if (fd >= 0) { r = mcde_sys_disk_event_read(fd, buf, sizeof buf); printf("events fd %d, no event queued: %s\n", fd, r == 0 ? "yes" : "no"); (void)close(fd); } else printf("events (%s)\n", mcde_sys_error()); } if (failures == 0) printf("PASS test_sysinfo\n"); return failures != 0; }