1 /*
2 *
3 * Copyright 2023 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "bta_rfcomm_scn.h"
20
21 #include <gtest/gtest.h>
22
23 #include "bta/jv/bta_jv_int.h" // tBTA_JV_CB
24 #include "stack/include/rfcdefs.h" // RFCOMM_MAX_SCN
25
26 using testing::Test;
27
28 class BtaRfcommScnTest : public Test {
29 public:
30 protected:
SetUp()31 void SetUp() override {
32 tBTA_JV_DM_CBACK* p_cback = [](tBTA_JV_EVT, tBTA_JV*, uint32_t) {};
33 bta_jv_enable(p_cback);
34 }
35
TearDown()36 void TearDown() override {}
37 };
38
TEST_F(BtaRfcommScnTest,scn_available_after_available_index)39 TEST_F(BtaRfcommScnTest, scn_available_after_available_index) {
40 ASSERT_EQ(BTA_AllocateSCN(), 2);
41 ASSERT_EQ(BTA_AllocateSCN(), 3);
42 ASSERT_TRUE(BTA_TryAllocateSCN(4));
43 ASSERT_TRUE(BTA_TryAllocateSCN(5));
44
45 // Available index should be 3, and the next available scn is 6
46 ASSERT_EQ(BTA_AllocateSCN(), 6);
47 }
48
TEST_F(BtaRfcommScnTest,scn_available_before_available_index)49 TEST_F(BtaRfcommScnTest, scn_available_before_available_index) {
50 for (uint8_t scn = 2; scn <= RFCOMM_MAX_SCN; scn++) {
51 ASSERT_TRUE(BTA_TryAllocateSCN(scn));
52 }
53 ASSERT_TRUE(BTA_FreeSCN(28));
54 ASSERT_EQ(BTA_AllocateSCN(), 28);
55 ASSERT_TRUE(BTA_FreeSCN(2));
56
57 // Available index is 27, and the available scn is 2
58 ASSERT_EQ(BTA_AllocateSCN(), 2);
59 }
60
TEST_F(BtaRfcommScnTest,can_allocate_all_scns)61 TEST_F(BtaRfcommScnTest, can_allocate_all_scns) {
62 for (uint8_t scn = 2; scn <= RFCOMM_MAX_SCN; scn++) {
63 ASSERT_EQ(BTA_AllocateSCN(), scn);
64 }
65 }
66
TEST_F(BtaRfcommScnTest,only_last_scn_available)67 TEST_F(BtaRfcommScnTest, only_last_scn_available) {
68 // Fill all relevant SCN except the last
69 for (uint8_t scn = 2; scn < RFCOMM_MAX_SCN; scn++) {
70 ASSERT_EQ(BTA_AllocateSCN(), scn);
71 }
72
73 ASSERT_EQ(BTA_AllocateSCN(), RFCOMM_MAX_SCN);
74 }
75
TEST_F(BtaRfcommScnTest,no_scn_available)76 TEST_F(BtaRfcommScnTest, no_scn_available) {
77 for (uint8_t scn = 2; scn <= RFCOMM_MAX_SCN; scn++) {
78 ASSERT_EQ(BTA_AllocateSCN(), scn);
79 }
80
81 ASSERT_EQ(BTA_AllocateSCN(), 0);
82 }
83