Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Merge "Add unit tests for some text-message functions" into studio-1.…
Browse files Browse the repository at this point in the history
…4-dev
  • Loading branch information
Jim Kaye authored and Gerrit Code Review committed Jun 30, 2015
2 parents 9f42810 + dcd78da commit c01669c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.tests
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ EMULATOR_UNITTESTS_SOURCES := \
android/wear-agent/PairUpWearPhone_unittest.cpp \
android/wear-agent/testing/WearAgentTestUtils.cpp \
android/wear-agent/WearAgent_unittest.cpp \
telephony/gsm_unittest.cpp \
telephony/gsm.c \

ifeq (windows,$(HOST_OS))
EMULATOR_UNITTESTS_SOURCES += \
Expand Down
8 changes: 8 additions & 0 deletions telephony/gsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ typedef unsigned char byte_t;
typedef byte_t* bytes_t;
typedef const byte_t* cbytes_t;

#ifdef __cplusplus
extern "C" {
#endif

/** BCD
**/

Expand Down Expand Up @@ -198,4 +202,8 @@ extern void* gsm_rope_reserve( GsmRope rope, int len );

#define PHONE_PREFIX "1555521"

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* _android_gsm_h */
80 changes: 80 additions & 0 deletions telephony/gsm_unittest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2015 The Android Open Source Project
//
// This software is licensed under the terms of the GNU General Public
// License version 2, as published by the Free Software Foundation, and
// may be copied, distributed, and modified under those terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

#include <gtest/gtest.h>
#include "telephony/gsm.h"

namespace gsm {

TEST(Gsm, Utf8CheckGsm7) {
char utf8In[] = "01234567890123456789012345678901234567890123456789" // [ 0] .. [ 49]
"0123456789012345678901234***8901234567890123456789" // [ 50] .. [ 99]
"01234567890123456789012345678901234567890123456789" // [100] .. [149]
"01234567890123456789012345678901234567890123456789"; // [150] .. [199]

utf8In[75] = 0xE2; // Insert a UTF-8 'Euro currency' symbol
utf8In[76] = 0x82;
utf8In[77] = 0xAC;

// The Euro symbol is a three-byte UTF-8 symbol and a
// two-byte GSM-7 symbol.

cbytes_t start = (cbytes_t)&utf8In[0];

int isGsm7 = utf8_check_gsm7(start, strlen(utf8In));
EXPECT_TRUE(isGsm7);

cbytes_t end = (cbytes_t)(start + strlen(utf8In));
int gsm7len = 154;

// The "Euro" generates 2 output GSM-7 bytes from 3 input
// bytes.
// The other ("regular") input bytes give 1 GSM-7 byte
// each, so 152 input bytes are be needed to generate 152
// output bytes.
// In total, this test case needs 155 input bytes to
// generate 154 output bytes.
cbytes_t result = utf8_skip_gsm7(start, end, gsm7len);
EXPECT_EQ((result - start), 155);
}

TEST(Gsm, Utf8CheckUcs2) {
char utf8In[] = "01234567890123456789012345678901234567890123456789" // [ 0] .. [ 49]
"0**34567890123456789012345678901234567890123456789" // [ 50] .. [ 99]
"01234567890123456789012345678901234567890123456789" // [100] .. [149]
"01234567890123456789012345678901234567890123456789"; // [150] .. [199]

utf8In[51] = 0xC2; // Insert a UTF-8 'cent' symbol
utf8In[52] = 0xA2;

// The cent symbol is not in GSM-7. Its presence forces
// the SMS message to use UCS-2.

cbytes_t start = (cbytes_t)&utf8In[0];

int isGsm7 = utf8_check_gsm7(start, strlen(utf8In));
EXPECT_FALSE(isGsm7);

cbytes_t end = (cbytes_t)(start + strlen(utf8In));
int ucs2len = 124;

// The "cent" generates 2 output UCS-2 bytes from 2 input
// bytes.
// The other ("regular") input bytes give 2 UCS-2 bytes
// each, so 61 input bytes are be needed to generate 122
// output bytes.
// In total, this test case needs 63 input bytes to
// generate 124 output bytes.
cbytes_t result = utf8_skip_ucs2(start, end, ucs2len);
EXPECT_EQ((result - start), 63);
}

} // namespace gsm

0 comments on commit c01669c

Please sign in to comment.