Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: utilities_unittest.cc

Issue 6822060: Handle UCS-2 data coding scheme for SMS messsages. (Closed) Base URL: ssh://gitrw.chromium.org:9222/cromo.git@11.1.241.B
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utilities.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // Utilities for the cromo modem manager 4 // Unit tests for utilities for the cromo modem manager
5 5
6 #include "utilities.h" 6 #include "utilities.h"
7 7
8 #include <gflags/gflags.h> 8 #include <gflags/gflags.h>
9 #include <glog/logging.h> 9 #include <glog/logging.h>
10 #include <gtest/gtest.h> 10 #include <gtest/gtest.h>
11 11
12 TEST(Utilities, ExtractString) { 12 TEST(Utilities, ExtractString) {
13 using utilities::ExtractString; 13 using utilities::ExtractString;
14 using utilities::DBusPropertyMap; 14 using utilities::DBusPropertyMap;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 EXPECT_TRUE(HexEsnToDecimal("80000001", &out)); 53 EXPECT_TRUE(HexEsnToDecimal("80000001", &out));
54 EXPECT_STREQ("12800000001", out.c_str()); 54 EXPECT_STREQ("12800000001", out.c_str());
55 55
56 EXPECT_TRUE(HexEsnToDecimal("1", &out)); 56 EXPECT_TRUE(HexEsnToDecimal("1", &out));
57 EXPECT_STREQ("00000000001", out.c_str()); 57 EXPECT_STREQ("00000000001", out.c_str());
58 58
59 EXPECT_FALSE(HexEsnToDecimal("000bogus", &out)); 59 EXPECT_FALSE(HexEsnToDecimal("000bogus", &out));
60 EXPECT_FALSE(HexEsnToDecimal("ffffffff" "f", &out)); 60 EXPECT_FALSE(HexEsnToDecimal("ffffffff" "f", &out));
61 } 61 }
62 62
63 int main(int argc, char *argv[]) { 63 const uint8_t gsm1[] = {
64 10, 0xe8, 0x32, 0x9b, 0xfd, 0x46, 0x97, 0xd9, 0xec, 0x37
65 };
66 const uint8_t gsm2[] = {
67 9, 0xd4, 0xf2, 0x9c, 0x0e, 0x9a, 0x36, 0xa7, 0x2e
68 };
69 const uint8_t gsm3[] = {
70 10, 0xc9, 0x53, 0x1b, 0x24, 0x40, 0xf3, 0xdb, 0x65, 0x17
71 };
72 const uint8_t gsm4[] = { 2, 0x1b, 0x1e };
73 const uint8_t gsm5[] = {
74 0x6a, 0xc8, 0xb2, 0xbc, 0x7c, 0x9a, 0x83, 0xc2,
75 0x20, 0xf6, 0xdb, 0x7d, 0x2e, 0xcb, 0x41, 0xed,
76 0xf2, 0x7c, 0x1e, 0x3e, 0x97, 0x41, 0x1b, 0xde,
77 0x06, 0x75, 0x4f, 0xd3, 0xd1, 0xa0, 0xf9, 0xbb,
78 0x5d, 0x06, 0x95, 0xf1, 0xf4, 0xb2, 0x9b, 0x5c,
79 0x26, 0x83, 0xc6, 0xe8, 0xb0, 0x3c, 0x3c, 0xa6,
80 0x97, 0xe5, 0xf3, 0x4d, 0x6a, 0xe3, 0x03, 0xd1,
81 0xd1, 0xf2, 0xf7, 0xdd, 0x0d, 0x4a, 0xbb, 0x59,
82 0xa0, 0x79, 0x7d, 0x8c, 0x06, 0x85, 0xe7, 0xa0,
83 0x00, 0x28, 0xec, 0x26, 0x83, 0x2a, 0x96, 0x0b,
84 0x28, 0xec, 0x26, 0x83, 0xbe, 0x60, 0x50, 0x78,
85 0x0e, 0xba, 0x97, 0xd9, 0x6c, 0x17
86 };
87 const uint8_t gsm7_alphabet[] = {
88 0x7f, 0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0e,
89 0x88, 0x84, 0x62, 0xc1, 0x68, 0x38, 0x1e, 0x90,
90 0x88, 0x64, 0x42, 0xa9, 0x58, 0x2e, 0x98, 0x8c,
91 0x86, 0xd3, 0xf1, 0x7c, 0x40, 0x21, 0xd1, 0x88,
92 0x54, 0x32, 0x9d, 0x50, 0x29, 0xd5, 0x8a, 0xd5,
93 0x72, 0xbd, 0x60, 0x31, 0xd9, 0x8c, 0x56, 0xb3,
94 0xdd, 0x70, 0x39, 0xdd, 0x8e, 0xd7, 0xf3, 0xfd,
95 0x80, 0x41, 0xe1, 0x90, 0x58, 0x34, 0x1e, 0x91,
96 0x49, 0xe5, 0x92, 0xd9, 0x74, 0x3e, 0xa1, 0x51,
97 0xe9, 0x94, 0x5a, 0xb5, 0x5e, 0xb1, 0x59, 0xed,
98 0x96, 0xdb, 0xf5, 0x7e, 0xc1, 0x61, 0xf1, 0x98,
99 0x5c, 0x36, 0x9f, 0xd1, 0x69, 0xf5, 0x9a, 0xdd,
100 0x76, 0xbf, 0xe1, 0x71, 0xf9, 0x9c, 0x5e, 0xb7,
101 0xdf, 0xf1, 0x79, 0xfd, 0x9e, 0xdf, 0xf7, 0xff,
102 0x01
103 };
104 const uint8_t gsm7_extended_chars[] = {
105 0x14, 0x1b, 0xc5, 0x86, 0xb2, 0x41, 0x6d, 0x52,
106 0x9b, 0xd7, 0x86, 0xb7, 0xe9, 0x6d, 0x7c, 0x1b,
107 0xe0, 0xa6, 0x0c
108 };
109
110 static const struct {
111 const std::string utf8_string;
112 const uint8_t* packed_gsm7;
113 size_t packed_gsm7_size;
114 } gsm7_test_data[] = {
115 {"hellohello", gsm1, sizeof(gsm1)},
116 {"Test SMS.", gsm2, sizeof(gsm2)},
117 {"I'm $höme.", gsm3, sizeof(gsm3)},
118 {"[", gsm4, sizeof(gsm4)},
119 {"Here's a longer message [{with some extended characters}] thrown "
120 "in, such as £ and ΩΠΨ and §¿ as well.", gsm5, sizeof(gsm5)},
121 {"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./"
122 "0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZ"
123 "ÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà",
124 gsm7_alphabet,
125 sizeof(gsm7_alphabet)},
126 {"\xC^{}\\[~]|€", gsm7_extended_chars, sizeof(gsm7_extended_chars)},
127 {"", NULL}
128 };
129
130 TEST(Utilities, Gsm7ToUtf8) {
131 using utilities::Gsm7ToUtf8String;
132 std::string out;
133
134 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) {
135 out = Gsm7ToUtf8String(gsm7_test_data[i].packed_gsm7);
136 EXPECT_EQ(gsm7_test_data[i].utf8_string, out);
137 }
138
139 }
140
141 TEST(Utilities, Utf8ToGsm7) {
142 using utilities::Utf8StringToGsm7;
143 std::vector<uint8_t> out;
144
145 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) {
146 out = Utf8StringToGsm7(gsm7_test_data[i].utf8_string);
147 EXPECT_EQ(gsm7_test_data[i].packed_gsm7_size, out.size());
148 EXPECT_EQ(0, memcmp(&out[0], gsm7_test_data[i].packed_gsm7, out.size()));
149 }
150 }
151
152 TEST(Utilities, Utf8Gsm7RoundTrip) {
153 using utilities::Utf8StringToGsm7;
154 using utilities::Gsm7ToUtf8String;
155 std::vector<uint8_t> gsm7_out;
156 std::string utf8_out;
157
158 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) {
159 gsm7_out = Utf8StringToGsm7(gsm7_test_data[i].utf8_string);
160 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]);
161 EXPECT_EQ(gsm7_test_data[i].utf8_string, utf8_out);
162 }
163 }
164
165 TEST(Utilities, Gsm7Utf8RoundTrip) {
166 using utilities::Utf8StringToGsm7;
167 using utilities::Gsm7ToUtf8String;
168 std::vector<uint8_t> gsm7_out;
169 std::string utf8_out;
170
171 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) {
172 utf8_out = Gsm7ToUtf8String(gsm7_test_data[i].packed_gsm7);
173 gsm7_out = Utf8StringToGsm7(utf8_out);
174 EXPECT_EQ(gsm7_test_data[i].packed_gsm7_size, gsm7_out.size());
175 EXPECT_EQ(
176 0, memcmp(&gsm7_out[0], gsm7_test_data[i].packed_gsm7,
177 gsm7_out.size()));
178 }
179 }
180
181 TEST(Utilities, Gsm7InvalidCharacter) {
182 using utilities::Utf8StringToGsm7;
183 using utilities::Gsm7ToUtf8String;
184 std::string utf8_input;
185 std::vector<uint8_t> gsm7_out;
186 std::string utf8_out;
187
188 utf8_input = "This |±| text '©' has |½| non-GSM7 characters";
189 gsm7_out = Utf8StringToGsm7(utf8_input);
190 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]);
191 // Expect the text to have spaces where the invalid characters were.
192 EXPECT_EQ("This | | text ' ' has | | non-GSM7 characters", utf8_out);
193 }
194
195 uint8_t ucs_sample1[] = {
196 0x3a,
197 0x04, 0x1f, 0x04, 0x40, 0x04, 0x3e, 0x04, 0x41,
198 0x04, 0x42, 0x04, 0x3e, 0x00, 0x20, 0x04, 0x42,
199 0x04, 0x35, 0x04, 0x3a, 0x04, 0x41, 0x04, 0x42,
200 0x00, 0x2e, 0x00, 0x20, 0x00, 0x4a, 0x00, 0x75,
201 0x00, 0x73, 0x00, 0x74, 0x00, 0x20, 0x00, 0x73,
202 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x20,
203 0x00, 0x74, 0x00, 0x65, 0x00, 0x78, 0x00, 0x74,
204 0x00, 0x2e
205 };
206
207 uint8_t ucs_sample2[] = {
208 0x08,
209 0x04, 0x42, 0x04, 0x35, 0x04, 0x41, 0x04, 0x42
210 };
211
212 static const struct {
213 const std::string utf8_string;
214 const uint8_t* ucs2_string;
215 size_t ucs2_size;
216 } ucs2_test_data[] = {
217 {"Просто текст. Just some text.", ucs_sample1, sizeof(ucs_sample1)},
218 {"тест", ucs_sample2, sizeof(ucs_sample2)},
219 {"", NULL}
220 };
221
222 TEST(Utilities, Ucs2ToUtf8) {
223 using utilities::Ucs2ToUtf8String;
224 std::string out;
225
226 for (int i = 0; ucs2_test_data[i].ucs2_string != NULL; ++i) {
227 out = Ucs2ToUtf8String(ucs2_test_data[i].ucs2_string);
228 EXPECT_EQ(ucs2_test_data[i].utf8_string, out);
229 }
230 }
231
232 TEST(Utilities, Utf8ToUcs2) {
233 using utilities::Utf8StringToUcs2;
234 std::vector<uint8_t> out;
235
236 for (int i = 0; ucs2_test_data[i].ucs2_string != NULL; ++i) {
237 out = Utf8StringToUcs2(ucs2_test_data[i].utf8_string);
238 EXPECT_EQ(ucs2_test_data[i].ucs2_size, out.size());
239 EXPECT_EQ(0, memcmp(&out[0], ucs2_test_data[i].ucs2_string, out.size()));
240 }
241 }
242
243 TEST(Utilities, Utf8Ucs2RoundTrip) {
244 using utilities::Utf8StringToUcs2;
245 using utilities::Ucs2ToUtf8String;
246 std::vector<uint8_t> ucs2_out;
247 std::string utf8_out;
248
249 for (int i = 0; ucs2_test_data[i].ucs2_string != NULL; ++i) {
250 ucs2_out = Utf8StringToUcs2(ucs2_test_data[i].utf8_string);
251 utf8_out = Ucs2ToUtf8String(&ucs2_out[0]);
252 EXPECT_EQ(ucs2_test_data[i].utf8_string, utf8_out);
253 }
254 }
255
256 TEST(Utilities, Ucs2Utf8RoundTrip) {
257 using utilities::Utf8StringToUcs2;
258 using utilities::Ucs2ToUtf8String;
259 std::vector<uint8_t> ucs2_out;
260 std::string utf8_out;
261
262 for (int i = 0; ucs2_test_data[i].ucs2_string != NULL; ++i) {
263 utf8_out = Ucs2ToUtf8String(ucs2_test_data[i].ucs2_string);
264 ucs2_out = Utf8StringToUcs2(utf8_out);
265 EXPECT_EQ(ucs2_test_data[i].ucs2_size, ucs2_out.size());
266 EXPECT_EQ(
267 0, memcmp(&ucs2_out[0], ucs2_test_data[i].ucs2_string,
268 ucs2_out.size()));
269 }
270 }
271
272 int main(int argc, char* argv[]) {
64 testing::InitGoogleTest(&argc, argv); 273 testing::InitGoogleTest(&argc, argv);
65 google::InitGoogleLogging(argv[0]); 274 google::InitGoogleLogging(argv[0]);
66 google::ParseCommandLineFlags(&argc, &argv, true); 275 google::ParseCommandLineFlags(&argc, &argv, true);
67 276
68 return RUN_ALL_TESTS(); 277 return RUN_ALL_TESTS();
69 } 278 }
OLDNEW
« no previous file with comments | « utilities.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698