vboot_audio_tests.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Tests for vboot_audio
  6. */
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "crc32.h"
  12. #include "gbb_header.h"
  13. #include "host_common.h"
  14. #include "load_kernel_fw.h"
  15. #include "rollback_index.h"
  16. #include "test_common.h"
  17. #include "vboot_audio.h"
  18. #include "vboot_audio_private.h"
  19. #include "vboot_common.h"
  20. #include "vboot_display.h"
  21. #include "vboot_nvstorage.h"
  22. #include "vboot_struct.h"
  23. /* Builtin notes */
  24. extern VbDevMusicNote default_notes_[], short_notes_[];
  25. extern uint32_t default_count_, short_count_;
  26. /* Mock data */
  27. static VbCommonParams cparams;
  28. static GoogleBinaryBlockHeader gbb;
  29. static VbDevMusicNote good_notes[] = { {100, 100},
  30. {100, 0},
  31. {200, 200},
  32. {100, 0},
  33. {300, 300},
  34. {100, 0},
  35. {400, 400},
  36. {30000, 0} };
  37. static VbDevMusic good_header =
  38. { .sig = { '$', 'S', 'N', 'D' },
  39. .count = sizeof(good_notes) / sizeof(VbDevMusicNote),
  40. };
  41. static uint8_t notebuf[sizeof(good_header) +
  42. sizeof(good_notes) - sizeof(VbDevMusicNote)];
  43. static VbDevMusic *use_hdr;
  44. static VbDevMusicNote *use_notes;
  45. static uint32_t use_size;
  46. /* Set correct checksum for custom notes */
  47. void FixChecksum(VbDevMusic *hdr) {
  48. hdr->checksum = Crc32(&(hdr->count), sizeof(hdr->count) +
  49. hdr->count * sizeof(hdr->notes[0]));
  50. }
  51. /* Reset mock data (for use before each test) */
  52. static void ResetMocks(void) {
  53. memset(&cparams, 0, sizeof(cparams));
  54. cparams.gbb_data = &gbb;
  55. cparams.gbb = &gbb;
  56. memset(&gbb, 0, sizeof(gbb));
  57. gbb.major_version = GBB_MAJOR_VER;
  58. gbb.minor_version = GBB_MINOR_VER;
  59. gbb.flags = 0;
  60. use_hdr = (VbDevMusic *)notebuf;
  61. use_notes = use_hdr->notes;
  62. memcpy(use_hdr, &good_header, sizeof(good_header));
  63. memcpy(use_notes, good_notes, sizeof(good_notes));
  64. FixChecksum(use_hdr);
  65. use_size = sizeof(notebuf);
  66. }
  67. /* Compare two sets of notes */
  68. static int NotesMatch(VbDevMusicNote *a, VbDevMusicNote *b, uint32_t count) {
  69. int i;
  70. if (!a || !b)
  71. return 0;
  72. for ( i=0; i<count; i++) {
  73. if ( a[i].msec != b[i].msec || a[i].frequency != b[i].frequency)
  74. return 0;
  75. }
  76. return count;
  77. }
  78. /****************************************************************************/
  79. /* Mocked verification functions */
  80. void *VbExGetMusicPtr(void) {
  81. return use_hdr;
  82. }
  83. uint32_t VbExMaxMusicSize(void) {
  84. return use_size;
  85. }
  86. /****************************************************************************/
  87. static void VbAudioTest(void) {
  88. VbAudioContext* a = 0;
  89. /* default is okay */
  90. ResetMocks();
  91. use_hdr = 0;
  92. a = VbAudioOpen(&cparams);
  93. TEST_TRUE(a->music_notes == default_notes_ &&
  94. a->note_count == default_count_,
  95. "VbAudioTest( default )");
  96. VbAudioClose(a);
  97. /* short is okay */
  98. ResetMocks();
  99. use_hdr = 0;
  100. gbb.flags = 0x00000001;
  101. a = VbAudioOpen(&cparams);
  102. TEST_TRUE(a->music_notes == short_notes_ &&
  103. a->note_count == short_count_,
  104. "VbAudioTest( short )");
  105. VbAudioClose(a);
  106. /* good custom is okay */
  107. ResetMocks();
  108. a = VbAudioOpen(&cparams);
  109. TEST_TRUE(NotesMatch(a->music_notes, good_notes, good_header.count) &&
  110. a->note_count == good_header.count,
  111. "VbAudioTest( custom good )");
  112. VbAudioClose(a);
  113. /* good custom is rejected when short flag is set */
  114. ResetMocks();
  115. gbb.flags = 0x00000001;
  116. a = VbAudioOpen(&cparams);
  117. TEST_TRUE(a->music_notes == short_notes_ &&
  118. a->note_count == short_count_,
  119. "VbAudioTest( short has priority )");
  120. VbAudioClose(a);
  121. /* too short gets extended */
  122. ResetMocks();
  123. use_hdr->count--;
  124. FixChecksum(use_hdr);
  125. a = VbAudioOpen(&cparams);
  126. TEST_TRUE(NotesMatch(a->music_notes, use_notes, use_hdr->count) &&
  127. a->note_count == use_hdr->count + 1 &&
  128. a->music_notes[use_hdr->count].msec == 28700 &&
  129. a->music_notes[use_hdr->count].frequency == 0,
  130. "VbAudioTest( too short )");
  131. VbAudioClose(a);
  132. /* too quiet is rejected */
  133. ResetMocks();
  134. use_notes[6].msec = 10;
  135. FixChecksum(use_hdr);
  136. a = VbAudioOpen(&cparams);
  137. TEST_TRUE(a->music_notes == default_notes_ &&
  138. a->note_count == default_count_,
  139. "VbAudioTest( too quiet )");
  140. VbAudioClose(a);
  141. /* inaudible is rejected */
  142. ResetMocks();
  143. use_notes[0].frequency = 99;
  144. use_notes[2].frequency = 2001;
  145. FixChecksum(use_hdr);
  146. a = VbAudioOpen(&cparams);
  147. TEST_TRUE(a->music_notes == default_notes_ &&
  148. a->note_count == default_count_,
  149. "VbAudioTest( inaudible )");
  150. VbAudioClose(a);
  151. /* bad signature is rejected */
  152. ResetMocks();
  153. use_hdr->sig[0] = 'C';
  154. a = VbAudioOpen(&cparams);
  155. TEST_TRUE(a->music_notes == default_notes_ &&
  156. a->note_count == default_count_,
  157. "VbAudioTest( bad signature )");
  158. VbAudioClose(a);
  159. /* count == 0 is rejected */
  160. ResetMocks();
  161. use_hdr->count = 0;
  162. a = VbAudioOpen(&cparams);
  163. TEST_TRUE(a->music_notes == default_notes_ &&
  164. a->note_count == default_count_,
  165. "VbAudioTest( count == 0 )");
  166. VbAudioClose(a);
  167. /* too big is rejected */
  168. ResetMocks();
  169. use_hdr->count = 999;
  170. a = VbAudioOpen(&cparams);
  171. TEST_TRUE(a->music_notes == default_notes_ &&
  172. a->note_count == default_count_,
  173. "VbAudioTest( count too big )");
  174. VbAudioClose(a);
  175. /* bad checksum is rejected */
  176. ResetMocks();
  177. use_hdr->checksum++;
  178. a = VbAudioOpen(&cparams);
  179. TEST_TRUE(a->music_notes == default_notes_ &&
  180. a->note_count == default_count_,
  181. "VbAudioTest( count too big )");
  182. VbAudioClose(a);
  183. }
  184. int main(int argc, char* argv[])
  185. {
  186. VbAudioTest();
  187. return gTestSuccess ? 0 : 255;
  188. }