summaryrefslogtreecommitdiff
path: root/cras/src/tests/device_blacklist_unittest.cc
blob: 812ac4cc3c6b5812230251bb6fbfd646daaaafef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <gtest/gtest.h>

extern "C" {
#include "cras_device_blacklist.h"
}

namespace {

static const char CONFIG_PATH[] = CRAS_UT_TMPDIR;
static const char CONFIG_FILENAME[] = "device_blacklist";

void CreateConfigFile(const char* config_text) {
  FILE* f;
  char card_path[128];

  snprintf(card_path, sizeof(card_path), "%s/%s", CONFIG_PATH, CONFIG_FILENAME);
  f = fopen(card_path, "w");
  if (f == NULL)
    return;

  fprintf(f, "%s", config_text);

  fclose(f);
}

TEST(Blacklist, EmptyBlacklist) {
  static const char empty_config_text[] = "";
  struct cras_device_blacklist *blacklist;

  CreateConfigFile(empty_config_text);

  blacklist = cras_device_blacklist_create(CONFIG_PATH);
  ASSERT_NE(static_cast<cras_device_blacklist*>(NULL), blacklist);
  EXPECT_EQ(0, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0008, 0, 0));

  cras_device_blacklist_destroy(blacklist);
}

TEST(Blacklist, BlackListOneUsbOutput) {
  static const char usb_output_config_text[] =
      "[USB_Outputs]\n"
      "0d8c_0008_00000012_0 = 1\n";
  struct cras_device_blacklist *blacklist;

  CreateConfigFile(usb_output_config_text);

  blacklist = cras_device_blacklist_create(CONFIG_PATH);
  ASSERT_NE(static_cast<cras_device_blacklist*>(NULL), blacklist);

  EXPECT_EQ(0, cras_device_blacklist_check(blacklist, 0x0d8d, 0x0008, 0x12, 0));
  EXPECT_EQ(0, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0009, 0x12, 0));
  EXPECT_EQ(0, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0008, 0x13, 0));
  EXPECT_EQ(0, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0008, 0x12, 1));
  EXPECT_EQ(1, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0008, 0x12, 0));

  cras_device_blacklist_destroy(blacklist);
}

TEST(Blacklist, BlackListTwoUsbOutput) {
  static const char usb_output_config_text[] =
      "[USB_Outputs]\n"
      "0d8c_0008_00000000_0 = 1\n"
      "0d8c_0009_00000000_0 = 1\n";
  struct cras_device_blacklist *blacklist;

  CreateConfigFile(usb_output_config_text);

  blacklist = cras_device_blacklist_create(CONFIG_PATH);
  ASSERT_NE(static_cast<cras_device_blacklist*>(NULL), blacklist);

  EXPECT_EQ(1, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0009, 0, 0));
  EXPECT_EQ(1, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0008, 0, 0));
  EXPECT_EQ(0, cras_device_blacklist_check(blacklist, 0x0d8c, 0x0008, 0, 1));

  cras_device_blacklist_destroy(blacklist);
}

}  //  namespace

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}