1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "mms_msg_test.h"
16 
17 #include <iostream>
18 #include <sstream>
19 
20 #include "mms_codec_type.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 static constexpr char HEX_TABLE[] = "0123456789ABCDEF";
25 static constexpr uint8_t HEX_OFFSET = 4;
26 static constexpr uint8_t MAX_LINE_NUM = 16;
27 
MmsMsgTest()28 MmsMsgTest::MmsMsgTest() {}
29 
~MmsMsgTest()30 MmsMsgTest::~MmsMsgTest() {}
31 
ProcessDecodeInput(int inputCMD) const32 void MmsMsgTest::ProcessDecodeInput(int inputCMD) const
33 {
34     switch (inputCMD) {
35         case 0x00:
36             MmsDecodeTest("/data/app/deSrc/SendReq.mms");
37             break;
38         case 0x01:
39             MmsDecodeTest("/data/app/deSrc/SendConf.mms");
40             break;
41         case 0x02:
42             MmsDecodeTest("/data/app/deSrc/NotificationInd.mms");
43             break;
44         case 0x03:
45             MmsDecodeTest("/data/app/deSrc/NotifyRespInd.mms");
46             break;
47         case 0x04:
48             MmsDecodeTest("/data/app/deSrc/RetrieveConf.mms");
49             break;
50         case 0x05:
51             MmsDecodeTest("/data/app/deSrc/AcknowledgeInd.mms");
52             break;
53         case 0x06:
54             MmsDecodeTest("/data/app/deSrc/DeliveryInd.mms");
55             break;
56         case 0x07:
57             MmsDecodeTest("/data/app/deSrc/ReadRecInd.mms");
58             break;
59         case 0x08:
60             MmsDecodeTest("/data/app/deSrc/ReadOrigInd.mms");
61             break;
62         default:
63             break;
64     }
65 }
66 
ProcessEncodeInput(int inputCMD)67 void MmsMsgTest::ProcessEncodeInput(int inputCMD)
68 {
69     switch (inputCMD) {
70         case 0x09:
71             MmsSendReqEncodeTest();
72             break;
73         case 0x0a:
74             MmsSendConfEncodeTest();
75             break;
76         case 0x0b:
77             MmsNotificationIndEncodeTest();
78             break;
79         case 0x0c:
80             MmsNotifyRespIndEncodeTest();
81             break;
82         case 0x0d:
83             MmsRetrieveConfEncodeTest();
84             break;
85         case 0x0e:
86             MmsAcknowledgeIndEncodeTest();
87             break;
88         case 0x0f:
89             MmsDeliveryIndEncodeTest();
90             break;
91         case 0x10:
92             MmsReadRecIndEncodeTest();
93             break;
94         case 0x11:
95             MmsReadOrigIndEncodeTest();
96             break;
97         default:
98             break;
99     }
100 }
101 
ProcessTest()102 void MmsMsgTest::ProcessTest()
103 {
104     bool loopFlag = true;
105     const int exitKey = 100;
106     while (loopFlag) {
107         std::cout << "\nusage:please input a cmd num:\n"
108                      "0:TestDecodeMmsSendReq\n"
109                      "1:TestDecodeMmsSendConf\n"
110                      "2:TestDecodeMmsNotificationInd\r\n"
111                      "3:TestDecodeMmsNotifyRespInd\r\n"
112                      "4:TestDecodeMmsRetrieveConf\r\n"
113                      "5:TestDecodeMmsAcknowledgeInd\r\n"
114                      "6:TestDecodeMmsDeliveryInd\r\n"
115                      "7:TestDecodeMmsReadRecInd\r\n"
116                      "8:TestDecodeMmsReadOrigInd\r\n"
117                      "9:TestEncodeMmsSendReq\r\n"
118                      "10:TestEncodeMmsSendConf\r\n"
119                      "11:TestEncodeMmsNotificationInd\r\n"
120                      "12:TestEncodeMmsNotifyRespInd\r\n"
121                      "13:TestEncodeMmsRetrieveConf\r\n"
122                      "14:TestEncodeMmsAcknowledgeInd\r\n"
123                      "15:TestEncodeMmsDeliveryInd\r\n"
124                      "16:TestEncodeMmsReadRecInd\r\n"
125                      "17:TestEncodeMmsReadOrigInd\r\n"
126                      "100:exit test mms msg\n"
127                   << std::endl;
128 
129         int inputCMD = 0;
130         std::cin >> inputCMD;
131         while (std::cin.fail()) {
132             std::cin.clear();
133             std::cin.ignore();
134             std::cin >> inputCMD;
135         }
136         if (inputCMD == exitKey) {
137             return;
138         }
139         std::cout << "inputCMD is:" << inputCMD << std::endl;
140         ProcessDecodeInput(inputCMD);
141         ProcessEncodeInput(inputCMD);
142     }
143 }
144 
MmsDecodeTest(std::string strPath) const145 void MmsMsgTest::MmsDecodeTest(std::string strPath) const
146 {
147     MmsMsg decodeMsg;
148     if (!decodeMsg.DecodeMsg(strPath)) {
149         std::cout << "mms decode message fail." << std::endl;
150         std::cout << "mms file path name:" << strPath << std::endl;
151         return;
152     }
153     decodeMsg.DumpMms();
154     uint8_t messageType = decodeMsg.GetMmsMessageType();
155     switch (messageType) {
156         case MMS_MSGTYPE_SEND_REQ:
157             MmsSendReqDecodeTest(decodeMsg);
158             break;
159         case MMS_MSGTYPE_SEND_CONF:
160             MmsSendConfDecodeTest(decodeMsg);
161             break;
162         case MMS_MSGTYPE_NOTIFICATION_IND:
163             MmsNotificationIndDecodeTest(decodeMsg);
164             break;
165         case MMS_MSGTYPE_NOTIFYRESP_IND:
166             MmsNotifyRespIndDecodeTest(decodeMsg);
167             break;
168         case MMS_MSGTYPE_RETRIEVE_CONF:
169             MmsRetrieveConfDecodeTest(decodeMsg);
170             break;
171         case MMS_MSGTYPE_ACKNOWLEDGE_IND:
172             MmsAcknowledgeIndDecodeTest(decodeMsg);
173             break;
174         case MMS_MSGTYPE_DELIVERY_IND:
175             MmsDeliveryIndDecodeTest(decodeMsg);
176             break;
177         case MMS_MSGTYPE_READ_REC_IND:
178             MmsReadRecIndDecodeTest(decodeMsg);
179             break;
180         case MMS_MSGTYPE_READ_ORIG_IND:
181             MmsReadOrigIndDecodeTest(decodeMsg);
182             break;
183         default:
184             break;
185     }
186 }
187 
GetSendReqDataTest(MmsMsg & encodeMsg)188 void MmsMsgTest::GetSendReqDataTest(MmsMsg &encodeMsg)
189 {
190     // Mandatory
191     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_SEND_REQ)) {
192         std::cout << "SetMmsMessageType fail" << std::endl;
193         return;
194     }
195     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
196         std::cout << "SetMmsTransactionId fail" << std::endl;
197         return;
198     }
199     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
200         std::cout << "SetMmsVersion fail" << std::endl;
201         return;
202     }
203     MmsAddress address;
204     address.SetMmsAddressString("+8613812345678/TYPE=PLMN");
205     if (!encodeMsg.SetMmsFrom(address)) {
206         std::cout << "SetMmsFrom fail" << std::endl;
207         return;
208     }
209     if (!encodeMsg.SetHeaderContentType("application/vnd.wap.multipart.related")) {
210         std::cout << "SetHeaderContentType fail" << std::endl;
211         return;
212     }
213     // Optional
214     if (!encodeMsg.SetMmsSubject("Test mms")) {
215         std::cout << "SetMmsSubject fail" << std::endl;
216         return;
217     }
218     if (!encodeMsg.SetHeaderOctetValue(MMS_CONTENT_CLASS, static_cast<uint8_t>(MmsContentClass::MMS_TEXT))) {
219         std::cout << "SetHeaderOctetValue MMS_CONTENT_CLASS fail" << std::endl;
220         return;
221     }
222 }
223 
MmsSendReqEncodeTest()224 void MmsMsgTest::MmsSendReqEncodeTest()
225 {
226     std::cout << "Start MmsSendReqEncodeTest" << std::endl;
227     MmsMsg encodeMsg;
228     GetSendReqDataTest(encodeMsg);
229     std::vector<MmsAddress> vecAddrs;
230     MmsAddress toAddrs("+8613888888888/TYPE=PLMN");
231     vecAddrs.push_back(toAddrs);
232     MmsAddress toAddrs2("+8613812345678/TYPE=PLMN");
233     vecAddrs.push_back(toAddrs2);
234     if (!encodeMsg.SetMmsTo(vecAddrs)) {
235         std::cout << "SetMmsTo fail" << std::endl;
236         return;
237     }
238     // add smil file
239     const std::string filePathNameSmil = "/data/app/enSrc/618C0A89.smil";
240     if (!MmsAddAttachment(encodeMsg, filePathNameSmil, "<0000>", "application/smil", true)) {
241         std::cout << "MmsAddAttachment smil fail" << std::endl;
242         return;
243     }
244     // add text file
245     const std::string filePathNameText = "/data/app/enSrc/content.text";
246     if (!MmsAddAttachment(encodeMsg, filePathNameText, "<content.text>", "text/plain", false)) {
247         std::cout << "MmsAddAttachment text fail" << std::endl;
248         return;
249     }
250     // add image file
251     const std::string filePathNameGif = "/data/app/enSrc/picture.gif";
252     if (!MmsAddAttachment(encodeMsg, filePathNameGif, "<picture.gif>", "image/gif", false)) {
253         std::cout << "MmsAddAttachment gif fail" << std::endl;
254         return;
255     }
256     uint32_t len = 0;
257     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
258     if (result == nullptr) {
259         std::cout << "encode fail result nullptr error." << std::endl;
260         return;
261     }
262     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/SendReq.mms")) {
263         std::cout << "Encode write to file error." << std::endl;
264         return;
265     }
266     std::cout << "MmsSendReqEncodeTest encode success, data len = " << len << std::endl;
267 }
268 
MmsSendConfEncodeTest() const269 void MmsMsgTest::MmsSendConfEncodeTest() const
270 {
271     std::cout << "Start MmsSendConfEncodeTest" << std::endl;
272     MmsMsg encodeMsg;
273     // Mandatory
274     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_SEND_CONF)) {
275         std::cout << "SetMmsMessageType fail" << std::endl;
276         return;
277     }
278     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
279         std::cout << "SetMmsTransactionId fail" << std::endl;
280         return;
281     }
282     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
283         std::cout << "SetMmsVersion fail" << std::endl;
284         return;
285     }
286     if (!encodeMsg.SetHeaderOctetValue(MMS_RESPONSE_STATUS, static_cast<uint8_t>(MmsResponseStatus::MMS_OK))) {
287         std::cout << "SetHeaderOctetValue MMS_RESPONSE_STATUS fail" << std::endl;
288         return;
289     }
290     uint32_t len = 0;
291     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
292     if (result == nullptr) {
293         std::cout << "encode fail result nullptr error." << std::endl;
294         return;
295     }
296     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/SendConf.mms")) {
297         std::cout << "Encode write to file error." << std::endl;
298         return;
299     }
300     std::cout << "MmsSendConfEncodeTest encode success, data len =" << len << std::endl;
301 }
302 
GetMmsNotificationIndDataTest(MmsMsg & encodeMsg)303 void MmsMsgTest::GetMmsNotificationIndDataTest(MmsMsg &encodeMsg)
304 {
305     const long expiryTemp = 1637141707;
306     const long messageSize = 12345678;
307     // Mandatory
308     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_NOTIFICATION_IND)) {
309         std::cout << "SetMmsMessageType fail" << std::endl;
310         return;
311     }
312     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
313         std::cout << "SetMmsTransactionId fail" << std::endl;
314         return;
315     }
316     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
317         std::cout << "SetMmsVersion fail" << std::endl;
318         return;
319     }
320     if (!encodeMsg.SetHeaderOctetValue(MMS_MESSAGE_CLASS, static_cast<uint8_t>(MmsMessageClass::PERSONAL))) {
321         std::cout << "SetHeaderOctetValue fail" << std::endl;
322         return;
323     }
324     if (!encodeMsg.SetHeaderLongValue(MMS_EXPIRY, expiryTemp)) {
325         std::cout << "SetHeaderLongValue MMS_EXPIRY fail" << std::endl;
326         return;
327     }
328     if (!encodeMsg.SetHeaderLongValue(MMS_MESSAGE_SIZE, messageSize)) {
329         std::cout << "SetHeaderLongValue MMS_MESSAGE_SIZE fail" << std::endl;
330         return;
331     }
332     std::string strContentLocation = "Test";
333     if (!encodeMsg.SetHeaderStringValue(MMS_CONTENT_LOCATION, strContentLocation)) {
334         std::cout << "SetHeaderStringValue fail" << std::endl;
335         return;
336     } // need to confirmation
337 }
338 
MmsNotificationIndEncodeTest()339 void MmsMsgTest::MmsNotificationIndEncodeTest()
340 {
341     std::cout << "Start MmsNotificationIndEncodeTest" << std::endl;
342     MmsMsg encodeMsg;
343     GetMmsNotificationIndDataTest(encodeMsg);
344     // Optional
345     MmsAddress address;
346     address.SetMmsAddressString("+8613812345678/TYPE=PLMN");
347     if (!encodeMsg.SetMmsFrom(address)) {
348         std::cout << "SetMmsFrom fail" << std::endl;
349         return;
350     }
351     if (!encodeMsg.SetMmsSubject("Test mms")) {
352         std::cout << "SetMmsSubject fail" << std::endl;
353         return;
354     }
355     uint32_t len = 0;
356     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
357     if (result == nullptr) {
358         std::cout << "encode fail result nullptr error." << std::endl;
359         return;
360     }
361     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/NotificationInd.mms")) {
362         std::cout << "Encode write to file error." << std::endl;
363         return;
364     }
365     std::cout << "MmsNotificationIndEncodeTest encode success, data len =" << len << std::endl;
366 }
367 
MmsNotifyRespIndEncodeTest() const368 void MmsMsgTest::MmsNotifyRespIndEncodeTest() const
369 {
370     std::cout << "Start MmsNotifyRespIndEncodeTest" << std::endl;
371     MmsMsg encodeMsg;
372     // Mandatory
373     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_NOTIFYRESP_IND)) {
374         std::cout << "SetMmsMessageType fail" << std::endl;
375         return;
376     }
377     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
378         std::cout << "SetMmsTransactionId fail" << std::endl;
379         return;
380     }
381     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
382         std::cout << "SetMmsVersion fail" << std::endl;
383         return;
384     }
385     if (!encodeMsg.SetHeaderOctetValue(MMS_STATUS, static_cast<uint8_t>(MmsStatus::MMS_EXPIRED))) {
386         std::cout << "SetHeaderOctetValue MMS_STATUS fail" << std::endl;
387         return;
388     }
389     uint32_t len = 0;
390     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
391     if (result == nullptr) {
392         std::cout << "encode fail result nullptr error." << std::endl;
393         return;
394     }
395     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/NotifyRespInd.mms")) {
396         std::cout << "Encode write to file error." << std::endl;
397         return;
398     }
399     std::cout << "MmsNotifyRespIndEncodeTest encode success, data len =" << len << std::endl;
400 }
401 
MmsRetrieveConfDataTest(MmsMsg & encodeMsg)402 void MmsMsgTest::MmsRetrieveConfDataTest(MmsMsg &encodeMsg)
403 {
404     // Mandatory
405     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_RETRIEVE_CONF)) {
406         std::cout << "SetMmsMessageType fail" << std::endl;
407         return;
408     }
409     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
410         std::cout << "SetMmsTransactionId fail" << std::endl;
411         return;
412     }
413     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
414         std::cout << "SetMmsVersion fail" << std::endl;
415         return;
416     }
417     const long dateTemp = 1637141707;
418     if (!encodeMsg.SetMmsDate(dateTemp)) {
419         std::cout << "SetMmsDate fail" << std::endl;
420         return;
421     }
422     if (!encodeMsg.SetHeaderContentType("application/vnd.wap.multipart.related")) {
423         std::cout << "SetHeaderContentType fail" << std::endl;
424         return;
425     }
426     // Optional
427     MmsAddress address;
428     address.SetMmsAddressString("+8613812345678/TYPE=PLMN");
429     if (!encodeMsg.SetMmsFrom(address)) {
430         std::cout << "SetMmsFrom fail" << std::endl;
431         return;
432     }
433     if (!encodeMsg.SetMmsSubject("Test mms")) {
434         std::cout << "SetMmsSubject fail" << std::endl;
435         return;
436     }
437 }
438 
MmsRetrieveConfEncodeTest()439 void MmsMsgTest::MmsRetrieveConfEncodeTest()
440 {
441     std::cout << "Start MmsRetrieveConfEncodeTest" << std::endl;
442     MmsMsg encodeMsg;
443     MmsRetrieveConfDataTest(encodeMsg);
444     // add smil file
445     const std::string filePathNameSmil = "/data/app/enSrc/618C0A89.smil";
446     if (!MmsAddAttachment(encodeMsg, filePathNameSmil, "<0000>", "application/smil", true)) {
447         std::cout << "MmsAddAttachment smil fail" << std::endl;
448         return;
449     }
450     // add text file
451     const std::string filePathNameText = "/data/app/enSrc/content.text";
452     if (!MmsAddAttachment(encodeMsg, filePathNameText, "<content.text>", "text/plain", false)) {
453         std::cout << "MmsAddAttachment text fail" << std::endl;
454         return;
455     }
456     // add image file
457     const std::string filePathNameGif("/data/app/enSrc/picture.gif");
458     if (!MmsAddAttachment(encodeMsg, filePathNameGif, "picture.gif", "image/gif", false)) {
459         std::cout << "MmsAddAttachment gif fail" << std::endl;
460         return;
461     }
462     uint32_t len = 0;
463     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
464     if (result == nullptr) {
465         std::cout << "encode fail result nullptr error." << std::endl;
466         return;
467     }
468     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/RetrieveConf.mms")) {
469         std::cout << "Encode write to file error." << std::endl;
470         return;
471     }
472     std::cout << "MmsRetrieveConfEncodeTest encode success, data len =" << len << std::endl;
473 }
474 
MmsAcknowledgeIndEncodeTest() const475 void MmsMsgTest::MmsAcknowledgeIndEncodeTest() const
476 {
477     std::cout << "Start MmsAcknowledgeIndEncodeTest" << std::endl;
478     MmsMsg encodeMsg;
479     // Mandatory
480     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_ACKNOWLEDGE_IND)) {
481         std::cout << "SetMmsMessageType fail" << std::endl;
482         return;
483     }
484     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
485         std::cout << "SetMmsTransactionId fail" << std::endl;
486         return;
487     }
488     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
489         std::cout << "SetMmsVersion fail" << std::endl;
490         return;
491     }
492     uint32_t len = 0;
493     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
494     if (result == nullptr) {
495         std::cout << "encode fail result nullptr error." << std::endl;
496         return;
497     }
498     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/AcknowledgeInd.mms")) {
499         std::cout << "Encode write to file error." << std::endl;
500         return;
501     }
502     std::cout << "MmsAcknowledgeIndEncodeTest encode success, data len =" << len << std::endl;
503 }
504 
MmsDeliveryIndEncodeTest() const505 void MmsMsgTest::MmsDeliveryIndEncodeTest() const
506 {
507     std::cout << "Start MmsDeliveryIndEncodeTest" << std::endl;
508     MmsMsg encodeMsg;
509     // Mandatory
510     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_DELIVERY_IND)) {
511         std::cout << "SetMmsMessageType fail" << std::endl;
512         return;
513     }
514     if (!encodeMsg.SetMmsTransactionId("2077.1427358451410")) {
515         std::cout << "SetMmsTransactionId fail" << std::endl;
516         return;
517     }
518     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
519         std::cout << "SetMmsVersion fail" << std::endl;
520         return;
521     }
522     std::string strMessageID = "0001";
523     if (!encodeMsg.SetHeaderStringValue(MMS_MESSAGE_ID, strMessageID)) {
524         std::cout << "SetHeaderStringValue fail" << std::endl;
525         return;
526     }
527     const long dateTemp = 1637141707;
528     if (!encodeMsg.SetMmsDate(dateTemp)) {
529         std::cout << "SetMmsDate fail" << std::endl;
530         return;
531     }
532     std::vector<MmsAddress> vecAddrs;
533     MmsAddress toAddrs("+8613888888888/TYPE=PLMN");
534     vecAddrs.push_back(toAddrs);
535     if (!encodeMsg.SetMmsTo(vecAddrs)) {
536         std::cout << "SetMmsTo fail" << std::endl;
537         return;
538     }
539     if (!encodeMsg.SetHeaderOctetValue(MMS_STATUS, static_cast<uint8_t>(MmsStatus::MMS_EXPIRED))) {
540         std::cout << "SetHeaderOctetValue MMS_STATUS fail" << std::endl;
541         return;
542     }
543     uint32_t len = 0;
544     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
545     if (result == nullptr) {
546         std::cout << "encode fail result nullptr error." << std::endl;
547         return;
548     }
549     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/DeliveryInd.mms")) {
550         std::cout << "Encode write to file error." << std::endl;
551         return;
552     }
553     std::cout << "MmsDeliveryIndEncodeTest encode success, data len =" << len << std::endl;
554 }
555 
MmsReadRecIndEncodeTest() const556 void MmsMsgTest::MmsReadRecIndEncodeTest() const
557 {
558     std::cout << "Start MmsReadRecIndEncodeTest" << std::endl;
559     MmsMsg encodeMsg;
560     // Mandatory
561     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_READ_REC_IND)) {
562         std::cout << "SetMmsMessageType fail" << std::endl;
563         return;
564     }
565     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
566         std::cout << "SetMmsVersion fail" << std::endl;
567         return;
568     }
569     std::string strMessageID = "0001";
570     if (!encodeMsg.SetHeaderStringValue(MMS_MESSAGE_ID, strMessageID)) {
571         std::cout << "SetHeaderStringValue fail" << std::endl;
572         return;
573     }
574     std::vector<MmsAddress> vecAddrs;
575     MmsAddress toAddrs("+8613888888888/TYPE=PLMN");
576     vecAddrs.push_back(toAddrs);
577     if (!encodeMsg.SetMmsTo(vecAddrs)) {
578         std::cout << "SetMmsTo fail" << std::endl;
579         return;
580     }
581     MmsAddress address;
582     address.SetMmsAddressString("+8613812345678/TYPE=PLMN");
583     if (!encodeMsg.SetMmsFrom(address)) {
584         std::cout << "SetMmsFrom fail" << std::endl;
585         return;
586     }
587     if (!encodeMsg.SetHeaderOctetValue(MMS_READ_STATUS, static_cast<uint8_t>(MmsReadStatus::MMS_READ))) {
588         std::cout << "SetHeaderOctetValue MMS_READ_STATUS fail" << std::endl;
589         return;
590     }
591     // Optional
592     const long dateTemp = 1637141707;
593     encodeMsg.SetMmsDate(dateTemp);
594     uint32_t len = 0;
595     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
596     if (result == nullptr) {
597         std::cout << "encode fail result nullptr error." << std::endl;
598         return;
599     }
600     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/ReadRecInd.mms")) {
601         std::cout << "Encode write to file error." << std::endl;
602         return;
603     }
604     std::cout << "MmsReadRecIndEncodeTest encode success data len =" << len << std::endl;
605 }
606 
MmsReadOrigIndEncodeTest() const607 void MmsMsgTest::MmsReadOrigIndEncodeTest() const
608 {
609     std::cout << "Start MmsReadOrigIndEncodeTest" << std::endl;
610     MmsMsg encodeMsg;
611     // Mandatory
612     if (!encodeMsg.SetMmsMessageType(MMS_MSGTYPE_READ_ORIG_IND)) {
613         std::cout << "SetMmsMessageType fail" << std::endl;
614         return;
615     }
616     if (!encodeMsg.SetMmsVersion(static_cast<uint16_t>(MmsVersionType::MMS_VERSION_1_2))) {
617         std::cout << "SetMmsVersion fail" << std::endl;
618         return;
619     }
620     std::string strMessageID = "0001";
621     if (!encodeMsg.SetHeaderStringValue(MMS_MESSAGE_ID, strMessageID)) {
622         std::cout << "SetHeaderStringValue fail" << std::endl;
623         return;
624     }
625     std::vector<MmsAddress> vecAddrs;
626     MmsAddress toAddrs("+8613888888888/TYPE=PLMN");
627     vecAddrs.push_back(toAddrs);
628     if (!encodeMsg.SetMmsTo(vecAddrs)) {
629         std::cout << "SetMmsTo fail" << std::endl;
630         return;
631     }
632     MmsAddress address;
633     address.SetMmsAddressString("+8613812345678/TYPE=PLMN");
634     if (!encodeMsg.SetMmsFrom(address)) {
635         std::cout << "SetMmsFrom fail" << std::endl;
636         return;
637     }
638     const long dateTemp = 1637141707;
639     if (!encodeMsg.SetMmsDate(dateTemp)) {
640         std::cout << "SetMmsDate fail" << std::endl;
641         return;
642     }
643     if (!encodeMsg.SetHeaderOctetValue(MMS_READ_STATUS, static_cast<uint8_t>(MmsReadStatus::MMS_READ))) {
644         std::cout << "SetHeaderOctetValue MMS_READ_STATUS fail" << std::endl;
645         return;
646     }
647     // Optional
648     uint32_t len = 0;
649     std::unique_ptr<char[]> result = encodeMsg.EncodeMsg(len);
650     if (result == nullptr) {
651         std::cout << "encode fail result nullptr error." << std::endl;
652         return;
653     }
654     if (!WriteBufferToFile(std::move(result), len, "/data/app/deSrc/ReadOrigInd.mms")) {
655         std::cout << "Encode write to file error." << std::endl;
656         return;
657     }
658     std::cout << "MmsReadOrigIndEncodeTest encode success, data len =" << len << std::endl;
659 }
660 
MmsAddAttachment(MmsMsg & msg,std::string pathName,std::string contentId,std::string contenType,bool isSmil) const661 bool MmsMsgTest::MmsAddAttachment(
662     MmsMsg &msg, std::string pathName, std::string contentId, std::string contenType, bool isSmil) const
663 {
664     MmsAttachment imageAttachment;
665     std::size_t pos = pathName.find_last_of('/');
666     std::string fileName(pathName.substr(pos + 1));
667 
668     if (!imageAttachment.SetAttachmentFilePath(pathName, isSmil)) {
669         std::cout << "MmsAddAttachment SetAttachmentFilePath fail" << std::endl;
670         return false;
671     }
672     if (!imageAttachment.SetFileName(fileName)) {
673         std::cout << "MmsAddAttachment SetFileName fail" << std::endl;
674         return false;
675     }
676     if (!imageAttachment.SetContentId(contentId)) {
677         std::cout << "MmsAddAttachment SetContentId fail" << std::endl;
678         return false;
679     }
680     if (!imageAttachment.SetContentLocation(fileName)) {
681         std::cout << "MmsAddAttachment SetContentLocation fail" << std::endl;
682         return false;
683     }
684     if (!imageAttachment.SetContentType(contenType)) {
685         std::cout << "MmsAddAttachment SetContentType fail" << std::endl;
686         return false;
687     }
688     imageAttachment.SetContentDisposition("attachment");
689     if (!msg.AddAttachment(imageAttachment)) {
690         std::cout << "MmsAddAttachment AddAttachment fail" << std::endl;
691         return false;
692     }
693     return true;
694 }
695 
MmsSendReqDecodeTest(MmsMsg & decodeMsg) const696 void MmsMsgTest::MmsSendReqDecodeTest(MmsMsg &decodeMsg) const
697 {
698     std::cout << "======= Start SendReqDecodeTest ===========" << std::endl;
699     // Mandatory
700     std::cout << "TransactionId: " << decodeMsg.GetMmsTransactionId() << std::endl;
701     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
702     std::cout << "Date: " << decodeMsg.GetMmsDate() << std::endl;
703     std::cout << "From: " << decodeMsg.GetMmsFrom().GetAddressString() << std::endl;
704     std::vector<MmsAddress> toAddress;
705     decodeMsg.GetMmsTo(toAddress);
706     for (auto itTo : toAddress) {
707         std::cout << "To: " << itTo.GetAddressString() << std::endl;
708     }
709     std::string deliverReport = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_DELIVERY_REPORT));
710     std::string sendVisbility = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_SENDER_VISIBILITY));
711     std::string readResport = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_READ_REPORT));
712     std::string messageClass = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_MESSAGE_CLASS));
713     std::string priority = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_PRIORITY));
714 
715     std::cout << "Subject: " << decodeMsg.GetMmsSubject() << std::endl;
716     std::cout << "DeliveryReport: " << deliverReport << std::endl;
717     std::cout << "SenderVisibility: " << sendVisbility << std::endl;
718     std::cout << "ReadReport: " << readResport << std::endl;
719     std::cout << "MessageClass: " << messageClass << std::endl;
720     std::cout << "Prioity: " << priority << std::endl;
721 
722     std::vector<MmsAttachment> attachments;
723     decodeMsg.GetAllAttachment(attachments);
724     int attachmentCnt = 0;
725     for (auto it : attachments) {
726         std::cout << "=======part:" << attachmentCnt << " attachments infos ======" << std::endl;
727         std::cout << "ContentDisposition: " << it.GetContentDisposition() << std::endl;
728         std::cout << "ContentLocation: " << it.GetContentLocation() << std::endl;
729         std::cout << "ContentId: " << it.GetContentId() << std::endl;
730         std::cout << "FileName: " << it.GetFileName() << std::endl;
731         uint32_t len = 0;
732         std::unique_ptr<char[]> buff = it.GetDataBuffer(len);
733         if (buff != nullptr) {
734             std::cout << "attachments buffer size = " << len << std::endl;
735         }
736         std::cout << "=======part:" << attachmentCnt << " attachments infos ======" << std::endl;
737         attachmentCnt++;
738     }
739     std::cout << "======= End SendReqDecodeTest ===========" << std::endl;
740 }
741 
MmsSendConfDecodeTest(MmsMsg & decodeMsg) const742 void MmsMsgTest::MmsSendConfDecodeTest(MmsMsg &decodeMsg) const
743 {
744     std::cout << "======== Start SendConfEncodeTest =============" << std::endl;
745     // Mandatory
746     std::cout << "TransactionId: " << decodeMsg.GetMmsTransactionId() << std::endl;
747     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
748     std::string responseStatus = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_RESPONSE_STATUS));
749     std::cout << "ResponseStatus: " << responseStatus << std::endl;
750     std::cout << "======== End SendConfEncodeTest =============" << std::endl;
751 }
752 
MmsNotificationIndDecodeTest(MmsMsg & decodeMsg) const753 void MmsMsgTest::MmsNotificationIndDecodeTest(MmsMsg &decodeMsg) const
754 {
755     std::cout << "========== Start NotificationIndDecodeTest ========" << std::endl;
756     // Mandatory
757     std::cout << "TransactionId: " << decodeMsg.GetMmsTransactionId() << std::endl;
758     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
759     std::cout << "MessageSize: " << decodeMsg.GetHeaderLongValue(MMS_MESSAGE_SIZE) << std::endl;
760     std::cout << "Expiry: " << decodeMsg.GetHeaderLongValue(MMS_EXPIRY) << std::endl;
761     std::cout << "ContentLocation: " << decodeMsg.GetHeaderStringValue(MMS_CONTENT_LOCATION) << std::endl;
762     std::cout << "========== End NotificationIndDecodeTest ========" << std::endl;
763 }
764 
MmsNotifyRespIndDecodeTest(MmsMsg & decodeMsg) const765 void MmsMsgTest::MmsNotifyRespIndDecodeTest(MmsMsg &decodeMsg) const
766 {
767     std::cout << "========= Start NotifyRespIndDecodeTest ======" << std::endl;
768     // Mandatory
769     std::cout << "TransactionId: " << decodeMsg.GetMmsTransactionId() << std::endl;
770     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
771     std::string status = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_STATUS));
772     std::cout << "Status: " << status << std::endl;
773     std::cout << "========= End NotifyRespIndDecodeTest ======" << std::endl;
774 }
775 
MmsRetrieveConfDecodeTest(MmsMsg & decodeMsg) const776 void MmsMsgTest::MmsRetrieveConfDecodeTest(MmsMsg &decodeMsg) const
777 {
778     std::cout << "======== Start RetrieveConfDecodeTest =========" << std::endl;
779     // Mandatory
780     std::cout << "TransactionId: " << decodeMsg.GetMmsTransactionId() << std::endl;
781     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
782     std::cout << "Date: " << decodeMsg.GetMmsDate() << std::endl;
783 
784     std::vector<MmsAttachment> attachments;
785     decodeMsg.GetAllAttachment(attachments);
786     int attachmentCnt = 0;
787     for (auto it : attachments) {
788         std::cout << "======= part:" << attachmentCnt << " attachments infos ======" << std::endl;
789         std::cout << "ContentDisposition: " << it.GetContentDisposition() << std::endl;
790         std::cout << "ContentLocation: " << it.GetContentLocation() << std::endl;
791         std::cout << "ContentId: " << it.GetContentId() << std::endl;
792         std::cout << "FileName: " << it.GetFileName() << std::endl;
793         uint32_t len = 0;
794         std::unique_ptr<char[]> buff = it.GetDataBuffer(len);
795         if (buff != nullptr) {
796             std::cout << "attachments buffer size = " << len << std::endl;
797         }
798 
799         std::string strPathName = "/data/app/enSrc/";
800         strPathName.append(it.GetFileName());
801         std::cout << "======= strPathName:" << strPathName << std::endl;
802         if (!WriteBufferToFile(std::move(buff), len, strPathName)) {
803             std::cout << "Encode write to file error." << std::endl;
804             return;
805         }
806 
807         std::cout << "======= part:" << attachmentCnt << " attachments infos ======" << std::endl;
808         attachmentCnt++;
809     }
810     std::cout << "======== End RetrieveConfDecodeTest =========" << std::endl;
811 }
812 
MmsAcknowledgeIndDecodeTest(MmsMsg & decodeMsg) const813 void MmsMsgTest::MmsAcknowledgeIndDecodeTest(MmsMsg &decodeMsg) const
814 {
815     std::cout << "======= Start AcknowledgeIndDecodeTest ========" << std::endl;
816     // Mandatory
817     std::cout << "TransactionId: " << decodeMsg.GetMmsTransactionId() << std::endl;
818     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
819     std::cout << "======= End AcknowledgeIndDecodeTest ========" << std::endl;
820 }
821 
MmsDeliveryIndDecodeTest(MmsMsg & decodeMsg) const822 void MmsMsgTest::MmsDeliveryIndDecodeTest(MmsMsg &decodeMsg) const
823 {
824     std::cout << "======== Start DeliveryIndDecodeTest ==========" << std::endl;
825     // Mandatory
826     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
827     std::cout << "MessageID: " << decodeMsg.GetHeaderStringValue(MMS_MESSAGE_ID) << std::endl;
828     std::vector<MmsAddress> toAddress;
829     decodeMsg.GetMmsTo(toAddress);
830     for (auto itTo : toAddress) {
831         std::cout << "To: " << itTo.GetAddressString() << std::endl;
832     }
833     std::cout << "Date: " << decodeMsg.GetMmsDate() << std::endl;
834     std::string status = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_STATUS));
835     std::cout << "Status: " << status << std::endl;
836     std::cout << "======== End DeliveryIndDecodeTest ==========" << std::endl;
837 }
838 
MmsReadRecIndDecodeTest(MmsMsg & decodeMsg) const839 void MmsMsgTest::MmsReadRecIndDecodeTest(MmsMsg &decodeMsg) const
840 {
841     std::cout << "======= Start ReadRecIndDecodeTest =========" << std::endl;
842     // Mandatory
843     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
844     std::cout << "MessageID: " << decodeMsg.GetHeaderStringValue(MMS_MESSAGE_ID) << std::endl;
845     std::vector<MmsAddress> toAddress;
846     decodeMsg.GetMmsTo(toAddress);
847     for (auto itTo : toAddress) {
848         std::cout << "To: " << itTo.GetAddressString() << std::endl;
849     }
850     MmsAddress fromAddress = decodeMsg.GetMmsFrom();
851     std::cout << "From: " << fromAddress.GetAddressString() << std::endl;
852     std::cout << "Date: " << decodeMsg.GetMmsDate() << std::endl;
853     std::string status = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_STATUS));
854     std::cout << "Status: " << status << std::endl;
855     std::cout << "======= End ReadRecIndDecodeTest =========" << std::endl;
856 }
857 
MmsReadOrigIndDecodeTest(MmsMsg & decodeMsg) const858 void MmsMsgTest::MmsReadOrigIndDecodeTest(MmsMsg &decodeMsg) const
859 {
860     std::cout << "======== Start MmsReadOrigIndDecodeTest ========" << std::endl;
861     // Mandatory
862     std::cout << "Version: " << decodeMsg.GetMmsVersion() << std::endl;
863     std::cout << "MessageID: " << decodeMsg.GetHeaderStringValue(MMS_MESSAGE_ID) << std::endl;
864     std::vector<MmsAddress> toAddress;
865     decodeMsg.GetMmsTo(toAddress);
866     for (auto itTo : toAddress) {
867         std::cout << "To: " << itTo.GetAddressString() << std::endl;
868     }
869     MmsAddress fromAddress = decodeMsg.GetMmsFrom();
870     std::cout << "From: " << fromAddress.GetAddressString() << std::endl;
871     std::cout << "Date: " << decodeMsg.GetMmsDate() << std::endl;
872     std::string readStatus = std::to_string(decodeMsg.GetHeaderOctetValue(MMS_READ_STATUS));
873     std::cout << "ReadStatus: " << readStatus << std::endl;
874     std::cout << "======== End MmsReadOrigIndDecodeTest ========" << std::endl;
875 }
876 
BuffToHex(const std::unique_ptr<char[]> & buff,uint32_t len) const877 std::string MmsMsgTest::BuffToHex(const std::unique_ptr<char[]> &buff, uint32_t len) const
878 {
879     std::stringstream ss;
880     for (std::size_t i = 0; i < len; ++i) {
881         unsigned char temp = static_cast<unsigned char>(buff[i]) >> HEX_OFFSET;
882         ss << "0x" << HEX_TABLE[temp] << HEX_TABLE[buff[i] & 0xf] << " ";
883         if ((i % MAX_LINE_NUM == 0) && (i != 0)) {
884             ss << "\r\n";
885         }
886     }
887     return ss.str();
888 }
889 
WriteBufferToFile(const std::unique_ptr<char[]> & buff,uint32_t len,const std::string & strPathName) const890 bool MmsMsgTest::WriteBufferToFile(
891     const std::unique_ptr<char[]> &buff, uint32_t len, const std::string &strPathName) const
892 {
893     FILE *pFile = nullptr;
894     pFile = fopen(strPathName.c_str(), "wb");
895     if (!pFile || buff == nullptr) {
896         std::cout << "open file: " << strPathName << "fail!" << std::endl;
897         return false;
898     }
899     uint32_t fileLen = fwrite(buff.get(), len, 1, pFile);
900     if (fileLen > 0) {
901         std::cout << "write mms buffer to file success name:" << strPathName << std::endl;
902     } else {
903         std::cout << "write mms buffer to file error name:" << strPathName << std::endl;
904     }
905     (void)fclose(pFile);
906     return true;
907 }
908 } // namespace Telephony
909 } // namespace OHOS
910