1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "mmc/codec_server/hfp_lc3_mmc_decoder.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <cerrno>
23 #include <cstdint>
24 
25 #include "mmc/codec_server/lc3_utils.h"
26 #include "mmc/proto/mmc_config.pb.h"
27 #include "mmc/test/mock/mock_embdrv_lc3.h"
28 #include "test/common/mock_functions.h"
29 #include "test/mock/mock_osi_allocator.h"
30 
31 namespace {
32 
33 using ::testing::Contains;
34 using ::testing::Each;
35 using ::testing::Ne;
36 using ::testing::Test;
37 
38 const int kInputLen = mmc::HFP_LC3_PKT_FRAME_LEN;
39 const int kOutputLen = mmc::HFP_LC3_PCM_BYTES + 1;
40 const uint8_t kInputBuf[kInputLen] = {0};
41 static uint8_t kOutputBuf[kOutputLen] = {0};
42 
43 class HfpLc3DecoderTest : public Test {
44 public:
45 protected:
SetUp()46   void SetUp() override {
47     reset_mock_function_count_map();
48     decoder_ = std::make_unique<mmc::HfpLc3Decoder>();
49   }
TearDown()50   void TearDown() override { decoder_.release(); }
51   std::unique_ptr<mmc::HfpLc3Decoder> decoder_ = nullptr;
52 };
53 
54 class HfpLc3DecoderWithInitTest : public HfpLc3DecoderTest {
55 public:
56 protected:
SetUp()57   void SetUp() override {
58     test::mock::osi_allocator::osi_malloc.body = [&](size_t size) {
59       this->lc3_decoder_ = new struct lc3_decoder;
60       return (void*)this->lc3_decoder_;
61     };
62     test::mock::embdrv_lc3::lc3_setup_decoder.body =
63             [this](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) { return this->lc3_decoder_; };
64     test::mock::osi_allocator::osi_free_and_reset.body = [&](void** p_ptr) {
65       delete this->lc3_decoder_;
66       lc3_decoder_ = nullptr;
67       *p_ptr = nullptr;
68       return;
69     };
70     std::fill(kOutputBuf, kOutputBuf + kOutputLen, 1);
71 
72     HfpLc3DecoderTest::SetUp();
73     mmc::ConfigParam config;
74     *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
75     ASSERT_EQ(decoder_->init(config), mmc::HFP_LC3_PKT_FRAME_LEN);
76   }
TearDown()77   void TearDown() override {
78     HfpLc3DecoderTest::TearDown();
79     test::mock::embdrv_lc3::lc3_setup_decoder = {};
80     test::mock::osi_allocator::osi_malloc = {};
81     test::mock::osi_allocator::osi_free_and_reset = {};
82     std::fill(kOutputBuf, kOutputBuf + kOutputLen, 0);
83   }
84   struct lc3_decoder* lc3_decoder_ = nullptr;
85 };
86 
TEST_F(HfpLc3DecoderTest,InitWrongCodec)87 TEST_F(HfpLc3DecoderTest, InitWrongCodec) {
88   mmc::ConfigParam config;
89   *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
90 
91   int ret = decoder_->init(config);
92   EXPECT_EQ(ret, -EINVAL);
93   EXPECT_EQ(get_func_call_count("lc3_setup_decoder"), 0);
94 }
95 
TEST_F(HfpLc3DecoderTest,InitWrongConfig)96 TEST_F(HfpLc3DecoderTest, InitWrongConfig) {
97   mmc::ConfigParam config;
98   *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
99 
100   // lc3_setup_decoder failed due to wrong parameters (returned nullptr).
101   test::mock::embdrv_lc3::lc3_setup_decoder.body = [](int dt_us, int sr_hz, int sr_pcm_hz,
102                                                       void* mem) { return nullptr; };
103 
104   int ret = decoder_->init(config);
105   EXPECT_EQ(ret, -EINVAL);
106   EXPECT_EQ(get_func_call_count("lc3_setup_decoder"), 1);
107 
108   test::mock::embdrv_lc3::lc3_setup_decoder = {};
109 }
110 
TEST_F(HfpLc3DecoderTest,InitSuccess)111 TEST_F(HfpLc3DecoderTest, InitSuccess) {
112   mmc::ConfigParam config;
113   *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
114 
115   // lc3_setup_decoder returns decoder instance pointer.
116   struct lc3_decoder lc3_decoder;
117   test::mock::embdrv_lc3::lc3_setup_decoder.body =
118           [&lc3_decoder](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) { return &lc3_decoder; };
119 
120   int ret = decoder_->init(config);
121   EXPECT_EQ(ret, mmc::HFP_LC3_PKT_FRAME_LEN);
122   EXPECT_EQ(get_func_call_count("lc3_setup_decoder"), 1);
123 
124   test::mock::embdrv_lc3::lc3_setup_decoder = {};
125 }
126 
TEST_F(HfpLc3DecoderWithInitTest,CleanUp)127 TEST_F(HfpLc3DecoderWithInitTest, CleanUp) {
128   decoder_->cleanup();
129   EXPECT_EQ(get_func_call_count("osi_free_and_reset"), 1);
130 }
131 
TEST_F(HfpLc3DecoderTest,TranscodeNullBuffer)132 TEST_F(HfpLc3DecoderTest, TranscodeNullBuffer) {
133   // Null output buffer.
134   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, nullptr, 0);
135   EXPECT_EQ(ret, -EINVAL);
136   EXPECT_EQ(get_func_call_count("lc3_decode"), 0);
137 }
138 
TEST_F(HfpLc3DecoderWithInitTest,TranscodeWrongParam)139 TEST_F(HfpLc3DecoderWithInitTest, TranscodeWrongParam) {
140   // lc3_decode failed (returned value neither zero nor one).
141   test::mock::embdrv_lc3::lc3_decode.return_value = -1;
142 
143   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf, kOutputLen);
144   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES + 1);
145   EXPECT_THAT(kOutputBuf, Each(0));
146   EXPECT_EQ(get_func_call_count("lc3_decode"), 1);
147 
148   test::mock::embdrv_lc3::lc3_decode = {};
149 }
150 
TEST_F(HfpLc3DecoderWithInitTest,TranscodePLC)151 TEST_F(HfpLc3DecoderWithInitTest, TranscodePLC) {
152   // lc3_decode conducted PLC (return one).
153   test::mock::embdrv_lc3::lc3_decode.return_value = 1;
154 
155   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf, kOutputLen);
156   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES + 1);
157   EXPECT_EQ(kOutputBuf[0], 1);
158   EXPECT_EQ(get_func_call_count("lc3_decode"), 1);
159 
160   test::mock::embdrv_lc3::lc3_decode = {};
161 }
162 
TEST_F(HfpLc3DecoderWithInitTest,TranscodeSuccess)163 TEST_F(HfpLc3DecoderWithInitTest, TranscodeSuccess) {
164   // lc3_decode succeeded (return zero value).
165   test::mock::embdrv_lc3::lc3_decode.return_value = 0;
166 
167   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf, kOutputLen);
168   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES + 1);
169   EXPECT_EQ(kOutputBuf[0], 0);
170   EXPECT_THAT(kOutputBuf, Contains(Ne(0)));
171   EXPECT_EQ(get_func_call_count("lc3_decode"), 1);
172 
173   test::mock::embdrv_lc3::lc3_decode = {};
174 }
175 
176 }  // namespace
177