208 lines
7.6 KiB
C
208 lines
7.6 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "anode_scale.h"
|
|
#include "anode_plat.h"
|
|
#include "anode_dns_valid.h"
|
|
#include "anode_tz_valid.h"
|
|
#include "anode_audio_parse.h"
|
|
#include "anode_wifi_list.h"
|
|
#include "anode_bt_list.h"
|
|
#include "anode_power_policy.h"
|
|
#include "anode-touch-gesture.h"
|
|
|
|
static void test_scale_math(void) {
|
|
assert(anode_clamp_scale(0.1) == 0.5);
|
|
assert(anode_clamp_scale(9.0) == 4.0);
|
|
assert(anode_clamp_scale(1.25) == 1.25);
|
|
assert(anode_clamp_dpi(12.0) == 48.0);
|
|
assert(anode_clamp_dpi(600.0) == 384.0);
|
|
assert(anode_scale_from_dpi(144.0) == 1.5);
|
|
assert(anode_scale_from_dpi(192.0) == 2.0);
|
|
assert(anode_scale_px(15, 1.0) == 15);
|
|
assert(anode_scale_px(15, 1.5) == 23);
|
|
assert(anode_scale_px(15, 2.0) == 30);
|
|
assert(anode_scale_px(15, 0.75) == 11);
|
|
assert(anode_parse_bool_str("true", 0) == 1);
|
|
assert(anode_parse_bool_str("yes", 0) == 1);
|
|
assert(anode_parse_bool_str("1", 0) == 1);
|
|
assert(anode_parse_bool_str("false", 1) == 0);
|
|
assert(anode_parse_bool_str("0", 1) == 0);
|
|
assert(anode_parse_bool_str("unknown", 1) == 1);
|
|
}
|
|
|
|
static void test_dns_validation(void) {
|
|
char v4[ANODE_DNS_MAX_SERVERS][16];
|
|
char v6[ANODE_DNS_MAX_SERVERS][46];
|
|
int n4 = 0, n6 = 0;
|
|
char csv[256] = {0};
|
|
|
|
int tot = anode_parse_dns_servers("1.1.1.1, 8.8.8.8, 1.1.1.1, 2606:4700:4700::1111",
|
|
v4, &n4, ANODE_DNS_MAX_SERVERS,
|
|
v6, &n6, ANODE_DNS_MAX_SERVERS,
|
|
csv, sizeof(csv));
|
|
assert(tot == 3);
|
|
assert(n4 == 2);
|
|
assert(n6 == 1);
|
|
assert(strcmp(csv, "1.1.1.1,8.8.8.8,2606:4700:4700::1111") == 0);
|
|
|
|
assert(anode_parse_dns_servers("0.0.0.0", v4, &n4, 6, v6, &n6, 6, csv, sizeof(csv)) == -1);
|
|
assert(anode_parse_dns_servers("224.0.0.1", v4, &n4, 6, v6, &n6, 6, csv, sizeof(csv)) == -1);
|
|
assert(anode_parse_dns_servers("1.1.1.1; rm -rf /", v4, &n4, 6, v6, &n6, 6, csv, sizeof(csv)) == -1);
|
|
assert(anode_dns_mode_valid("cloudflare"));
|
|
assert(!anode_dns_mode_valid("evil"));
|
|
|
|
char sum[128] = {0};
|
|
int c = anode_parse_resolv_conf("# comment\nnameserver 9.9.9.9\nnameserver 1.1.1.1\n", sum, sizeof(sum));
|
|
assert(c == 2);
|
|
assert(strcmp(sum, "9.9.9.9, 1.1.1.1") == 0);
|
|
}
|
|
|
|
static void test_tz_validation(void) {
|
|
assert(anode_tz_name_valid("UTC"));
|
|
assert(anode_tz_name_valid("America/New_York"));
|
|
assert(anode_tz_name_valid("Etc/GMT+2"));
|
|
assert(!anode_tz_name_valid(""));
|
|
assert(!anode_tz_name_valid("../etc/passwd"));
|
|
assert(!anode_tz_name_valid("/America/New_York"));
|
|
assert(!anode_tz_name_valid("America//New_York"));
|
|
assert(!anode_tz_name_valid("UTC;id"));
|
|
if (access("/usr/share/zoneinfo/UTC", R_OK) == 0) {
|
|
assert(anode_tz_file_valid("/usr/share/zoneinfo", "UTC"));
|
|
assert(!anode_tz_file_valid("/usr/share/zoneinfo", "No/SuchZone"));
|
|
}
|
|
}
|
|
|
|
static void test_audio_parsers(void) {
|
|
int vol = 0;
|
|
bool muted = false;
|
|
assert(anode_parse_wpctl_volume("Volume: 0.65 [MUTED]", &vol, &muted));
|
|
assert(vol == 65 && muted == true);
|
|
assert(anode_parse_wpctl_volume("Volume: 1.00", &vol, &muted));
|
|
assert(vol == 100 && muted == false);
|
|
|
|
char sink[128], src[128];
|
|
anode_parse_pactl_defaults("Server Name: Pulse\nDefault Sink: alsa_out.pci\nDefault Source: alsa_in.pci\n",
|
|
sink, sizeof(sink), src, sizeof(src));
|
|
assert(strcmp(sink, "alsa_out.pci") == 0);
|
|
assert(strcmp(src, "alsa_in.pci") == 0);
|
|
|
|
const char *sinks_txt =
|
|
"Sink #3\n"
|
|
"\tState: RUNNING\n"
|
|
"\tName: alsa_out.pci\n"
|
|
"\tDescription: Built-in Analog\n"
|
|
"\tMute: no\n"
|
|
"\tVolume: front-left: 52428 / 80%\n";
|
|
anode_audio_node_t nodes[8];
|
|
int nn = anode_parse_pactl_nodes(sinks_txt, "Sink #", "alsa_out.pci", false, nodes, 8);
|
|
assert(nn == 1);
|
|
assert(nodes[0].index == 3);
|
|
assert(nodes[0].is_default == true);
|
|
assert(nodes[0].vol_pct == 80);
|
|
assert(nodes[0].muted == false);
|
|
|
|
const char *am_txt = "Simple mixer control 'Master',0\nSimple mixer control 'Capture',0\n";
|
|
int na = anode_parse_amixer_scontrols(am_txt, nodes, 8);
|
|
assert(na == 2 && strcmp(nodes[0].name, "Master") == 0 && nodes[0].is_default);
|
|
|
|
const char *sn_txt = "output.level=0.72\noutput.mute=0\ninput.level=0.40\n";
|
|
int ns = anode_parse_sndioctl(sn_txt, false, nodes, 8);
|
|
assert(ns == 1 && nodes[0].vol_pct == 72);
|
|
}
|
|
|
|
static void test_wifi_and_bt_parsers(void) {
|
|
const char *nm =
|
|
" :11\\:22\\:33\\:44\\:55\\:66:HomeNet:6:45:WPA2\n"
|
|
"*:AA\\:BB\\:CC\\:DD\\:EE\\:FF:HomeNet:36:85:WPA2\n"
|
|
" :00\\:11\\:22\\:33\\:44\\:55:CafeFree:11:60:--\n";
|
|
anode_wifi_group_t grp[8];
|
|
int ng = anode_parse_wifi_scan(nm, grp, 8);
|
|
assert(ng == 2);
|
|
assert(strcmp(grp[0].ssid, "HomeNet") == 0);
|
|
assert(grp[0].in_use == true);
|
|
assert(grp[0].best_chan == 36);
|
|
assert(grp[0].ap_count == 2);
|
|
assert(strcmp(grp[1].security, "Open") == 0);
|
|
|
|
const char *iw = "Connected to aa:bb:cc:dd:ee:ff\n\tSSID: LabWiFi\n\tfreq: 5180\n\tsignal: -55 dBm\n";
|
|
char bssid[32], ssid[64];
|
|
int ch = 0, sig = 0;
|
|
assert(anode_parse_iw_link(iw, bssid, sizeof(bssid), ssid, sizeof(ssid), &ch, &sig));
|
|
assert(strcmp(ssid, "LabWiFi") == 0 && ch == 36 && sig == 90);
|
|
|
|
anode_bt_dev_t devs[8];
|
|
int nb = anode_parse_bt_devices("Device aa:bb:cc:dd:ee:ff Keyboard\nDevice 11:22:33:44:55:66 Speaker\n",
|
|
"Device 11:22:33:44:55:66 Speaker\n",
|
|
"Device 11:22:33:44:55:66 Speaker\n",
|
|
devs, 8);
|
|
assert(nb == 2);
|
|
assert(devs[0].connected && strcmp(devs[0].name, "Speaker") == 0);
|
|
}
|
|
|
|
static void test_power_policy_and_gestures(void) {
|
|
anode_power_cfg_t cfg = anode_power_cfg_default();
|
|
anode_pwr_state_t st = ANODE_PWR_ACTIVE;
|
|
anode_power_actions_t act;
|
|
|
|
anode_power_policy_step(&cfg, &st, 10000, false, 80, true, false, &act);
|
|
assert(st == ANODE_PWR_ACTIVE && !act.dim_now);
|
|
|
|
anode_power_policy_step(&cfg, &st, 65000, false, 80, true, false, &act);
|
|
assert(st == ANODE_PWR_DIMMED && act.dim_now);
|
|
|
|
anode_power_policy_step(&cfg, &st, 1000, false, 80, true, false, &act);
|
|
assert(st == ANODE_PWR_ACTIVE && act.restore_brightness);
|
|
|
|
anode_power_policy_step(&cfg, &st, 190000, false, 80, true, false, &act);
|
|
assert(st == ANODE_PWR_LOCKED && act.lock_now);
|
|
|
|
anode_power_policy_step(&cfg, &st, 610000, false, 80, true, true, &act);
|
|
assert(st == ANODE_PWR_SUSPENDED && act.suspend_now);
|
|
|
|
/* Critical low battery override on battery */
|
|
st = ANODE_PWR_ACTIVE;
|
|
anode_power_policy_step(&cfg, &st, 1000, false, 2, true, false, &act);
|
|
assert(act.warn_low_batt && act.suspend_now);
|
|
|
|
/* Gestures at 1.0x and 2.0x scales */
|
|
anode_touch_track_t tr = {0};
|
|
anode_touch_begin(&tr, 1, 500, 1070, 1000);
|
|
assert(anode_touch_finish(&tr, 500, 900, 1200, 1920, 1080, 1.0) == ANODE_GEST_VKBD_TOGGLE);
|
|
|
|
anode_touch_begin(&tr, 2, 1000, 15, 2000);
|
|
assert(anode_touch_finish(&tr, 1000, 220, 2250, 3840, 2160, 2.0) == ANODE_GEST_LAUNCHER);
|
|
|
|
anode_touch_begin(&tr, 3, 1910, 500, 3000);
|
|
assert(anode_touch_finish(&tr, 1750, 500, 3200, 1920, 1080, 1.0) == ANODE_GEST_CONTROL);
|
|
}
|
|
|
|
static void test_platform_probe(void) {
|
|
anode_net_iface_t ifs[ANODE_MAX_IFACES];
|
|
int n = anode_list_net_ifaces(ifs, ANODE_MAX_IFACES);
|
|
assert(n >= 0);
|
|
anode_battery_info_t bat;
|
|
anode_read_battery(&bat);
|
|
(void)anode_detect_audio_backend();
|
|
char buf[256];
|
|
anode_runtime_dir(buf, sizeof(buf));
|
|
assert(buf[0] == '/');
|
|
anode_config_dir(buf, sizeof(buf));
|
|
assert(buf[0] == '/');
|
|
}
|
|
|
|
int main(void) {
|
|
test_scale_math();
|
|
test_dns_validation();
|
|
test_tz_validation();
|
|
test_audio_parsers();
|
|
test_wifi_and_bt_parsers();
|
|
test_power_policy_and_gestures();
|
|
test_platform_probe();
|
|
printf("test_units: ALL PASSED\n");
|
|
return 0;
|
|
}
|