1 // Copyright (c) 2012 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 #include <gtest/gtest.h>
6 #include <stdio.h>
7 
8 extern "C" {
9 #include "cras_device_blocklist.h"
10 }
11 
12 namespace {
13 
14 static const char CONFIG_PATH[] = CRAS_UT_TMPDIR;
15 static const char CONFIG_FILENAME[] = "device_blocklist";
16 
CreateConfigFile(const char * config_text)17 void CreateConfigFile(const char* config_text) {
18   FILE* f;
19   char card_path[128];
20 
21   snprintf(card_path, sizeof(card_path), "%s/%s", CONFIG_PATH, CONFIG_FILENAME);
22   f = fopen(card_path, "w");
23   if (f == NULL)
24     return;
25 
26   fprintf(f, "%s", config_text);
27 
28   fclose(f);
29 }
30 
TEST(Blocklist,EmptyBlocklist)31 TEST(Blocklist, EmptyBlocklist) {
32   static const char empty_config_text[] = "";
33   struct cras_device_blocklist* blocklist;
34 
35   CreateConfigFile(empty_config_text);
36 
37   blocklist = cras_device_blocklist_create(CONFIG_PATH);
38   ASSERT_NE(static_cast<cras_device_blocklist*>(NULL), blocklist);
39   EXPECT_EQ(0, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0008, 0, 0));
40 
41   cras_device_blocklist_destroy(blocklist);
42 }
43 
TEST(Blocklist,BlockListOneUsbOutput)44 TEST(Blocklist, BlockListOneUsbOutput) {
45   static const char usb_output_config_text[] =
46       "[USB_Outputs]\n"
47       "0d8c_0008_00000012_0 = 1\n";
48   struct cras_device_blocklist* blocklist;
49 
50   CreateConfigFile(usb_output_config_text);
51 
52   blocklist = cras_device_blocklist_create(CONFIG_PATH);
53   ASSERT_NE(static_cast<cras_device_blocklist*>(NULL), blocklist);
54 
55   EXPECT_EQ(0, cras_device_blocklist_check(blocklist, 0x0d8d, 0x0008, 0x12, 0));
56   EXPECT_EQ(0, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0009, 0x12, 0));
57   EXPECT_EQ(0, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0008, 0x13, 0));
58   EXPECT_EQ(0, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0008, 0x12, 1));
59   EXPECT_EQ(1, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0008, 0x12, 0));
60 
61   cras_device_blocklist_destroy(blocklist);
62 }
63 
TEST(Blocklist,BlockListTwoUsbOutput)64 TEST(Blocklist, BlockListTwoUsbOutput) {
65   static const char usb_output_config_text[] =
66       "[USB_Outputs]\n"
67       "0d8c_0008_00000000_0 = 1\n"
68       "0d8c_0009_00000000_0 = 1\n";
69   struct cras_device_blocklist* blocklist;
70 
71   CreateConfigFile(usb_output_config_text);
72 
73   blocklist = cras_device_blocklist_create(CONFIG_PATH);
74   ASSERT_NE(static_cast<cras_device_blocklist*>(NULL), blocklist);
75 
76   EXPECT_EQ(1, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0009, 0, 0));
77   EXPECT_EQ(1, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0008, 0, 0));
78   EXPECT_EQ(0, cras_device_blocklist_check(blocklist, 0x0d8c, 0x0008, 0, 1));
79 
80   cras_device_blocklist_destroy(blocklist);
81 }
82 
83 }  //  namespace
84 
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86   ::testing::InitGoogleTest(&argc, argv);
87   return RUN_ALL_TESTS();
88 }
89