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) {
64           return this->lc3_decoder_;
65         };
66     test::mock::osi_allocator::osi_free_and_reset.body = [&](void** p_ptr) {
67       delete this->lc3_decoder_;
68       lc3_decoder_ = nullptr;
69       *p_ptr = nullptr;
70       return;
71     };
72     std::fill(kOutputBuf, kOutputBuf + kOutputLen, 1);
73 
74     HfpLc3DecoderTest::SetUp();
75     mmc::ConfigParam config;
76     *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
77     ASSERT_EQ(decoder_->init(config), mmc::HFP_LC3_PKT_FRAME_LEN);
78   }
TearDown()79   void TearDown() override {
80     HfpLc3DecoderTest::TearDown();
81     test::mock::embdrv_lc3::lc3_setup_decoder = {};
82     test::mock::osi_allocator::osi_malloc = {};
83     test::mock::osi_allocator::osi_free_and_reset = {};
84     std::fill(kOutputBuf, kOutputBuf + kOutputLen, 0);
85   }
86   struct lc3_decoder* lc3_decoder_ = nullptr;
87 };
88 
TEST_F(HfpLc3DecoderTest,InitWrongCodec)89 TEST_F(HfpLc3DecoderTest, InitWrongCodec) {
90   mmc::ConfigParam config;
91   *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
92 
93   int ret = decoder_->init(config);
94   EXPECT_EQ(ret, -EINVAL);
95   EXPECT_EQ(get_func_call_count("lc3_setup_decoder"), 0);
96 }
97 
TEST_F(HfpLc3DecoderTest,InitWrongConfig)98 TEST_F(HfpLc3DecoderTest, InitWrongConfig) {
99   mmc::ConfigParam config;
100   *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
101 
102   // lc3_setup_decoder failed due to wrong parameters (returned nullptr).
103   test::mock::embdrv_lc3::lc3_setup_decoder.body =
104       [](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) { return nullptr; };
105 
106   int ret = decoder_->init(config);
107   EXPECT_EQ(ret, -EINVAL);
108   EXPECT_EQ(get_func_call_count("lc3_setup_decoder"), 1);
109 
110   test::mock::embdrv_lc3::lc3_setup_decoder = {};
111 }
112 
TEST_F(HfpLc3DecoderTest,InitSuccess)113 TEST_F(HfpLc3DecoderTest, InitSuccess) {
114   mmc::ConfigParam config;
115   *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
116 
117   // lc3_setup_decoder returns decoder instance pointer.
118   struct lc3_decoder lc3_decoder;
119   test::mock::embdrv_lc3::lc3_setup_decoder.body =
120       [&lc3_decoder](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) {
121         return &lc3_decoder;
122       };
123 
124   int ret = decoder_->init(config);
125   EXPECT_EQ(ret, mmc::HFP_LC3_PKT_FRAME_LEN);
126   EXPECT_EQ(get_func_call_count("lc3_setup_decoder"), 1);
127 
128   test::mock::embdrv_lc3::lc3_setup_decoder = {};
129 }
130 
TEST_F(HfpLc3DecoderWithInitTest,CleanUp)131 TEST_F(HfpLc3DecoderWithInitTest, CleanUp) {
132   decoder_->cleanup();
133   EXPECT_EQ(get_func_call_count("osi_free_and_reset"), 1);
134 }
135 
TEST_F(HfpLc3DecoderTest,TranscodeNullBuffer)136 TEST_F(HfpLc3DecoderTest, TranscodeNullBuffer) {
137   // Null output buffer.
138   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, nullptr, 0);
139   EXPECT_EQ(ret, -EINVAL);
140   EXPECT_EQ(get_func_call_count("lc3_decode"), 0);
141 }
142 
TEST_F(HfpLc3DecoderWithInitTest,TranscodeWrongParam)143 TEST_F(HfpLc3DecoderWithInitTest, TranscodeWrongParam) {
144   // lc3_decode failed (returned value neither zero nor one).
145   test::mock::embdrv_lc3::lc3_decode.return_value = -1;
146 
147   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf,
148                                 kOutputLen);
149   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES + 1);
150   EXPECT_THAT(kOutputBuf, Each(0));
151   EXPECT_EQ(get_func_call_count("lc3_decode"), 1);
152 
153   test::mock::embdrv_lc3::lc3_decode = {};
154 }
155 
TEST_F(HfpLc3DecoderWithInitTest,TranscodePLC)156 TEST_F(HfpLc3DecoderWithInitTest, TranscodePLC) {
157   // lc3_decode conducted PLC (return one).
158   test::mock::embdrv_lc3::lc3_decode.return_value = 1;
159 
160   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf,
161                                 kOutputLen);
162   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES + 1);
163   EXPECT_EQ(kOutputBuf[0], 1);
164   EXPECT_EQ(get_func_call_count("lc3_decode"), 1);
165 
166   test::mock::embdrv_lc3::lc3_decode = {};
167 }
168 
TEST_F(HfpLc3DecoderWithInitTest,TranscodeSuccess)169 TEST_F(HfpLc3DecoderWithInitTest, TranscodeSuccess) {
170   // lc3_decode succeeded (return zero value).
171   test::mock::embdrv_lc3::lc3_decode.return_value = 0;
172 
173   int ret = decoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf,
174                                 kOutputLen);
175   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES + 1);
176   EXPECT_EQ(kOutputBuf[0], 0);
177   EXPECT_THAT(kOutputBuf, Contains(Ne(0)));
178   EXPECT_EQ(get_func_call_count("lc3_decode"), 1);
179 
180   test::mock::embdrv_lc3::lc3_decode = {};
181 }
182 
183 }  // namespace
184