Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/tests/usb_audio_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / tests / usb_audio_test.c` 224 lines ยท 9193 bytes Raw Blame History 1 /* usb_audio_test.c -- host tests for the audio.output pure logic 2 * (dev/usb_audio.c, issue #85): the Rust reference vectors and regressions of 3 * crates/aienos-kernel/src/usb/audio.rs, plus hostile descriptors. Hosted 4 * test code only. Run by make usb-audio-test (and stage-test). */ 5 #include 6 #include 7 #include 8 #include "usb_audio.h" 9 10 static int checks, failures; 11 #define CHECK(c) \ 12 do { \ 13 checks++; \ 14 if (!(c)) { \ 15 failures++; \ 16 printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ 17 } \ 18 } while (0) 19 20 /* Real UAC1 shape: AC, AS alt zero, stereo Type I PCM 16-bit/48 kHz, OUT. */ 21 static const uint8_t UAC1[] = { 22 9, 2, 73, 0, 2, 1, 0, 0x80, 50, 9, 4, 0, 0, 0, 1, 1, 0, 0, 9, 0x24, 1, 0, 1, 0x26, 0, 1, 1, 23 9, 4, 1, 0, 0, 1, 2, 0, 0, 9, 4, 1, 1, 1, 1, 2, 0, 0, 7, 0x24, 1, 1, 2, 3, 0, 11, 0x24, 2, 24 1, 2, 2, 16, 1, 0x80, 0xbb, 0, 9, 5, 1, 1, 192, 0, 1, 0, 0, 25 }; 26 /* UAC2 clock source and Type I 16-bit stereo format, one 48 kHz clock. */ 27 static const uint8_t UAC2[] = { 28 9, 2, 85, 0, 2, 1, 0, 0x80, 50, 9, 4, 0, 0, 0, 1, 1, 0, 0, 9, 0x24, 1, 0, 2, 9, 0, 1, 1, 8, 29 0x24, 0x0a, 3, 7, 0, 0, 0, 9, 4, 1, 0, 0, 1, 2, 0, 0, 9, 4, 1, 1, 1, 1, 2, 0, 0, 16, 0x24, 30 1, 1, 0, 1, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 6, 0x24, 2, 1, 2, 16, 9, 5, 1, 1, 192, 0, 1, 0, 31 0, 32 }; 33 34 static size_t find(const uint8_t *v, size_t n, const uint8_t *pat, size_t pn) 35 { 36 for (size_t i = 0; i + pn <= n; i++) 37 if (!memcmp(v + i, pat, pn)) return i; 38 return (size_t)-1; 39 } 40 41 static void test_parse(void) 42 { 43 ck_uac_stream s; 44 CHECK(ck_uac_select_output(UAC1, sizeof UAC1, &s) == 0); 45 CHECK(s.version == CK_UAC1 && s.interface == 1 && s.alternate == 1 && s.channels == 2 && s.bit_resolution == 16); 46 CHECK(s.control_interface == 0 && s.endpoint_address == 1 && s.max_packet == 192 && s.interval == 1); 47 CHECK(s.rate_count == 1 && s.sample_rates[0] == 48000u && s.clock_source == CK_UAC_NO_CLOCK); 48 CHECK(ck_uac_select_output(UAC2, sizeof UAC2, &s) == 0); 49 CHECK(s.version == CK_UAC2 && s.clock_source == 3 && s.channels == 2 && s.subframe_bytes == 2); 50 } 51 52 static void test_uac1_channels_from_format_type(void) 53 { 54 /* UAC1 AS_GENERAL offset 4 is bDelay, not a channel count. */ 55 uint8_t v[sizeof UAC1]; 56 memcpy(v, UAC1, sizeof v); 57 static const uint8_t asg[] = {7, 0x24, 1, 1, 2, 3, 0}; 58 size_t at = find(v, sizeof v, asg, sizeof asg); 59 CHECK(at != (size_t)-1); 60 v[at + 4] = 7; 61 ck_uac_stream s; 62 CHECK(ck_uac_select_output(v, sizeof v, &s) == 0 && s.channels == 2); 63 } 64 65 static void test_hostile(void) 66 { 67 ck_uac_stream s; 68 uint8_t v[sizeof UAC1]; 69 static const uint8_t ft[] = {11, 0x24, 2}; 70 size_t at = find(UAC1, sizeof UAC1, ft, sizeof ft); 71 CHECK(at != (size_t)-1); 72 /* Short UAC1 FORMAT_TYPE_I (bLength 7 < 8): no read past it, no crash. */ 73 memcpy(v, UAC1, sizeof v); 74 v[at] = 7; 75 (void)ck_uac_select_output(v, sizeof v, &s); 76 /* Every truncation of the descriptor is refused, in an exact-size heap 77 * buffer so ASan catches any read past it. */ 78 for (size_t n = 0; n < sizeof UAC1; n++) { 79 uint8_t *cut = (uint8_t *)malloc(n ? n : 1); 80 if (!cut) continue; 81 memcpy(cut, UAC1, n); 82 CHECK(ck_uac_select_output(cut, n, &s) == -1); 83 free(cut); 84 } 85 /* Zero bLength inside the walk is malformed. */ 86 memcpy(v, UAC1, sizeof v); 87 v[9] = 0; 88 CHECK(ck_uac_select_output(v, sizeof v, &s) == -1); 89 /* An element running past wTotalLength is malformed. */ 90 memcpy(v, UAC1, sizeof v); 91 v[sizeof v - 9] = 40; 92 CHECK(ck_uac_select_output(v, sizeof v, &s) == -1); 93 /* Not a configuration descriptor, NULL arguments. */ 94 memcpy(v, UAC1, sizeof v); 95 v[1] = 1; 96 CHECK(ck_uac_select_output(v, sizeof v, &s) == -1); 97 CHECK(ck_uac_select_output(NULL, 9, &s) == -1); 98 CHECK(ck_uac_select_output(UAC1, sizeof UAC1, NULL) == -1); 99 /* An IN endpoint (a capture stream) is never selected for output. */ 100 memcpy(v, UAC1, sizeof v); 101 v[sizeof v - 7] = 0x81; 102 CHECK(ck_uac_select_output(v, sizeof v, &s) == -1); 103 /* A mono stream is refused. */ 104 memcpy(v, UAC1, sizeof v); 105 v[at + 4] = 1; 106 CHECK(ck_uac_select_output(v, sizeof v, &s) == -1); 107 /* 44.1 kHz only (48 kHz not offered) is refused. */ 108 memcpy(v, UAC1, sizeof v); 109 v[at + 8] = 0x44; 110 v[at + 9] = 0xac; 111 CHECK(ck_uac_select_output(v, sizeof v, &s) == -1); 112 } 113 114 static void test_interval(void) 115 { 116 CHECK(ck_uac_interval_us(0, 1) == 1000u && ck_uac_interval_us(0, 4) == 1000u); 117 CHECK(ck_uac_interval_us(1, 1) == 125u && ck_uac_interval_us(1, 4) == 1000u); 118 CHECK(ck_uac_interval_us(1, 16) == (125u << 15) && ck_uac_interval_us(1, 17) == 0); 119 CHECK(ck_uac_interval_us(0, 0) == 0 && ck_uac_interval_us(1, 0) == 0); 120 } 121 122 static void test_framer(void) 123 { 124 ck_pcm_framer f; 125 CHECK(ck_pcm_framer_init(&f, 44100, 1, 2, 1000) == 0); 126 size_t frames = 0; 127 int saw44 = 0, saw45 = 0; 128 for (int i = 0; i < 1000; i++) { 129 size_t n = ck_pcm_next_bytes(&f) / 2; 130 frames += n; 131 saw44 |= n == 44; 132 saw45 |= n == 45; 133 } 134 CHECK(frames == 44100 && saw44 && saw45); 135 136 CHECK(ck_pcm_framer_init(&f, 44100, 2, 2, 1000) == 0); 137 int16_t src[256], out[90]; 138 for (unsigned i = 0; i < 256; i++) src[i] = 7; 139 size_t cursor = 0, total = 0; 140 for (int i = 0; i < 1000; i++) total += ck_pcm_fill_i16(&f, src, 256, &cursor, out, 90) * 2; 141 CHECK(total == 176400); 142 143 CHECK(ck_pcm_framer_init(&f, 48000, 2, 2, 1000) == 0); 144 const int16_t ring[5] = {1, 2, 3, 4, 5}; 145 int16_t o[192]; 146 cursor = 3; 147 CHECK(ck_pcm_fill_i16(&f, ring, 5, &cursor, o, 192) == 96); 148 CHECK(o[0] == 4 && o[1] == 5 && o[2] == 1 && o[3] == 2 && cursor == 4); 149 /* Empty ring: silence. */ 150 for (unsigned i = 0; i < 192; i++) o[i] = 9; 151 CHECK(ck_pcm_fill_i16(&f, NULL, 0, &cursor, o, 192) == 96 && o[0] == 0 && o[95] == 0 && o[96] == 9); 152 153 CHECK(ck_pcm_framer_init(&f, 0, 2, 2, 1000) == -1); 154 CHECK(ck_pcm_framer_init(&f, 48000, 0, 2, 1000) == -1); 155 CHECK(ck_pcm_framer_init(&f, 48000, 2, 0, 1000) == -1); 156 CHECK(ck_pcm_framer_init(&f, 48000, 2, 2, 0) == -1); 157 CHECK(ck_pcm_next_bytes(&f) == 0); 158 } 159 160 static void test_schedule(void) 161 { 162 ck_pcm_framer f; 163 ck_pcm_framer_init(&f, 48000, 2, 2, 1000); 164 const ck_uac_packet pk[2] = {{0x1000, 192}, {0x2000, 192}}; 165 int16_t pcm[192]; 166 ck_uac_trb t[2]; 167 memset(t, 0xa5, sizeof t); 168 size_t cursor = 0; 169 CHECK(ck_uac_schedule(&f, 2046, 2, pk, 2, NULL, 0, &cursor, pcm, 192, 96, t, 2) == 2); 170 CHECK(((t[0].d[3] >> 20) & 0x7ff) == 0 && ((t[1].d[3] >> 20) & 0x7ff) == 1); 171 CHECK(t[0].d[2] == 192 && t[1].d[2] == 192); 172 CHECK(t[0].d[0] == 0x1000 && t[1].d[0] == 0x2000 && t[0].d[1] == 0); 173 CHECK(((t[0].d[3] >> 10) & 0x3f) == CK_UAC_TRB_ISOCH); 174 CHECK((t[0].d[3] & CK_UAC_TRB_IOC) == 0 && (t[1].d[3] & CK_UAC_TRB_IOC) != 0); 175 /* No SIA, no cycle (the ring writer sets it), no CHAIN, TBC zero. */ 176 CHECK((t[0].d[3] & (CK_UAC_TRB_SIA | 1u | 0x10u | (7u << 7))) == 0); 177 178 /* 96 kHz stereo needs 384 B per 1 ms packet: it does not fit 192 B, so 179 * nothing is scheduled and no sample is consumed. */ 180 ck_pcm_framer_init(&f, 96000, 2, 2, 1000); 181 int16_t ring[512]; 182 for (unsigned i = 0; i < 512; i++) ring[i] = 7; 183 int16_t pcm2[256]; 184 ck_uac_trb t1[1]; 185 const ck_uac_packet small[1] = {{0x1000, 192}}; 186 cursor = 0; 187 CHECK(ck_uac_schedule(&f, 0, 1, small, 1, ring, 512, &cursor, pcm2, 256, 256, t1, 1) == 0); 188 CHECK(cursor == 0); 189 const ck_uac_packet big[1] = {{0x1000, 1024}}; 190 CHECK(ck_uac_schedule(&f, 0, 1, big, 1, ring, 512, &cursor, pcm2, 256, 0, t1, 1) == 0); 191 /* Fits now: one packet, IOC set. */ 192 CHECK(ck_uac_schedule(&f, 0, 1, big, 1, ring, 512, &cursor, pcm2, 256, 256, t1, 1) == 1); 193 CHECK(t1[0].d[2] == 384 && cursor == 192 && (t1[0].d[3] & CK_UAC_TRB_IOC)); 194 } 195 196 static void test_tone(void) 197 { 198 ck_test_tone t; 199 CHECK(ck_test_tone_init(&t, 440, 48000) == 0); 200 int lo = 0, hi = 0, ok = 1; 201 for (int i = 0; i < 48000; i++) { 202 int16_t x = ck_test_tone_next(&t); 203 /* The tone never emits INT16_MIN: it is the one sample whose negation overflows. */ 204 ok &= x != -32768; 205 lo |= x < -30000; 206 hi |= x > 30000; 207 } 208 CHECK(ok && lo && hi); 209 CHECK(ck_test_tone_init(&t, 24000, 48000) == -1); 210 CHECK(ck_test_tone_init(&t, 440, 0) == -1); 211 } 212 213 int main(void) 214 { 215 test_parse(); 216 test_uac1_channels_from_format_type(); 217 test_hostile(); 218 test_interval(); 219 test_framer(); 220 test_schedule(); 221 test_tone(); 222 printf("CK_USB_AUDIO_TEST: %s checks=%d failures=%d\n", failures ? "FAIL" : "PASS", checks, failures); 223 return failures ? 1 : 0; 224 }