OLD | NEW |
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 // Unit tests for 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 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 const uint8_t gsm7_extended_chars[] = { | 104 const uint8_t gsm7_extended_chars[] = { |
105 0x14, 0x1b, 0xc5, 0x86, 0xb2, 0x41, 0x6d, 0x52, | 105 0x14, 0x1b, 0xc5, 0x86, 0xb2, 0x41, 0x6d, 0x52, |
106 0x9b, 0xd7, 0x86, 0xb7, 0xe9, 0x6d, 0x7c, 0x1b, | 106 0x9b, 0xd7, 0x86, 0xb7, 0xe9, 0x6d, 0x7c, 0x1b, |
107 0xe0, 0xa6, 0x0c | 107 0xe0, 0xa6, 0x0c |
108 }; | 108 }; |
109 | 109 |
110 static const struct { | 110 static const struct { |
111 const std::string utf8_string; | 111 const std::string utf8_string; |
112 const uint8_t* packed_gsm7; | 112 const uint8_t* packed_gsm7; |
113 size_t packed_gsm7_size; | 113 size_t packed_gsm7_size; |
114 } gsm_test_data[] = { | 114 } gsm7_test_data[] = { |
115 {"hellohello", gsm1, sizeof(gsm1)}, | 115 {"hellohello", gsm1, sizeof(gsm1)}, |
116 {"Test SMS.", gsm2, sizeof(gsm2)}, | 116 {"Test SMS.", gsm2, sizeof(gsm2)}, |
117 {"I'm $höme.", gsm3, sizeof(gsm3)}, | 117 {"I'm $höme.", gsm3, sizeof(gsm3)}, |
118 {"[", gsm4, sizeof(gsm4)}, | 118 {"[", gsm4, sizeof(gsm4)}, |
119 {"Here's a longer message [{with some extended characters}] thrown " | 119 {"Here's a longer message [{with some extended characters}] thrown " |
120 "in, such as £ and ΩΠΨ and §¿ as well.", gsm5, sizeof(gsm5)}, | 120 "in, such as £ and ΩΠΨ and §¿ as well.", gsm5, sizeof(gsm5)}, |
121 {"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./" | 121 {"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./" |
122 "0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 122 "0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
123 "ÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà", | 123 "ÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà", |
124 gsm7_alphabet, | 124 gsm7_alphabet, |
125 sizeof(gsm7_alphabet)}, | 125 sizeof(gsm7_alphabet)}, |
126 {"\xC^{}\\[~]|€", gsm7_extended_chars, sizeof(gsm7_extended_chars)}, | 126 {"\xC^{}\\[~]|€", gsm7_extended_chars, sizeof(gsm7_extended_chars)}, |
127 {"", NULL} | 127 {"", NULL} |
128 }; | 128 }; |
129 | 129 |
130 TEST(Utilities, Gsm7ToUtf8) { | 130 TEST(Utilities, Gsm7ToUtf8) { |
131 using utilities::Gsm7ToUtf8String; | 131 using utilities::Gsm7ToUtf8String; |
132 std::string out; | 132 std::string out; |
133 | 133 |
134 for (int i = 0; gsm_test_data[i].packed_gsm7 != NULL; ++i) { | 134 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) { |
135 out = Gsm7ToUtf8String(gsm_test_data[i].packed_gsm7); | 135 out = Gsm7ToUtf8String(gsm7_test_data[i].packed_gsm7); |
136 EXPECT_EQ(gsm_test_data[i].utf8_string, out); | 136 EXPECT_EQ(gsm7_test_data[i].utf8_string, out); |
137 } | 137 } |
138 | 138 |
139 } | 139 } |
140 | 140 |
141 TEST(Utilities, Utf8ToGsm7) { | 141 TEST(Utilities, Utf8ToGsm7) { |
142 using utilities::Utf8StringToGsm7; | 142 using utilities::Utf8StringToGsm7; |
143 std::vector<uint8_t> out; | 143 std::vector<uint8_t> out; |
144 | 144 |
145 for (int i = 0; gsm_test_data[i].packed_gsm7 != NULL; ++i) { | 145 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) { |
146 out = Utf8StringToGsm7(gsm_test_data[i].utf8_string); | 146 out = Utf8StringToGsm7(gsm7_test_data[i].utf8_string); |
147 EXPECT_EQ(gsm_test_data[i].packed_gsm7_size, out.size()); | 147 EXPECT_EQ(gsm7_test_data[i].packed_gsm7_size, out.size()); |
148 EXPECT_EQ(0, memcmp(&out[0], gsm_test_data[i].packed_gsm7, out.size())); | 148 EXPECT_EQ(0, memcmp(&out[0], gsm7_test_data[i].packed_gsm7, out.size())); |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 TEST(Utilities, Utf8Gsm7RoundTrip) { | 152 TEST(Utilities, Utf8Gsm7RoundTrip) { |
153 using utilities::Utf8StringToGsm7; | 153 using utilities::Utf8StringToGsm7; |
154 using utilities::Gsm7ToUtf8String; | 154 using utilities::Gsm7ToUtf8String; |
155 std::vector<uint8_t> gsm7_out; | 155 std::vector<uint8_t> gsm7_out; |
156 std::string utf8_out; | 156 std::string utf8_out; |
157 | 157 |
158 for (int i = 0; gsm_test_data[i].packed_gsm7 != NULL; ++i) { | 158 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) { |
159 gsm7_out = Utf8StringToGsm7(gsm_test_data[i].utf8_string); | 159 gsm7_out = Utf8StringToGsm7(gsm7_test_data[i].utf8_string); |
160 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]); | 160 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]); |
161 EXPECT_EQ(gsm_test_data[i].utf8_string, utf8_out); | 161 EXPECT_EQ(gsm7_test_data[i].utf8_string, utf8_out); |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 TEST(Utilities, Gsm7Utf8RoundTrip) { | 165 TEST(Utilities, Gsm7Utf8RoundTrip) { |
166 using utilities::Utf8StringToGsm7; | 166 using utilities::Utf8StringToGsm7; |
167 using utilities::Gsm7ToUtf8String; | 167 using utilities::Gsm7ToUtf8String; |
168 std::vector<uint8_t> gsm7_out; | 168 std::vector<uint8_t> gsm7_out; |
169 std::string utf8_out; | 169 std::string utf8_out; |
170 | 170 |
171 for (int i = 0; gsm_test_data[i].packed_gsm7 != NULL; ++i) { | 171 for (int i = 0; gsm7_test_data[i].packed_gsm7 != NULL; ++i) { |
172 utf8_out = Gsm7ToUtf8String(gsm_test_data[i].packed_gsm7); | 172 utf8_out = Gsm7ToUtf8String(gsm7_test_data[i].packed_gsm7); |
173 gsm7_out = Utf8StringToGsm7(utf8_out); | 173 gsm7_out = Utf8StringToGsm7(utf8_out); |
174 EXPECT_EQ(gsm_test_data[i].packed_gsm7_size, gsm7_out.size()); | 174 EXPECT_EQ(gsm7_test_data[i].packed_gsm7_size, gsm7_out.size()); |
175 EXPECT_EQ( | 175 EXPECT_EQ( |
176 0, memcmp(&gsm7_out[0], gsm_test_data[i].packed_gsm7, gsm7_out.size())); | 176 0, memcmp(&gsm7_out[0], gsm7_test_data[i].packed_gsm7, |
| 177 gsm7_out.size())); |
177 } | 178 } |
178 } | 179 } |
179 | 180 |
180 TEST(Utilities, Gsm7InvalidCharacter) { | 181 TEST(Utilities, Gsm7InvalidCharacter) { |
181 using utilities::Utf8StringToGsm7; | 182 using utilities::Utf8StringToGsm7; |
182 using utilities::Gsm7ToUtf8String; | 183 using utilities::Gsm7ToUtf8String; |
183 std::string utf8_input; | 184 std::string utf8_input; |
184 std::vector<uint8_t> gsm7_out; | 185 std::vector<uint8_t> gsm7_out; |
185 std::string utf8_out; | 186 std::string utf8_out; |
186 | 187 |
187 utf8_input = "This |±| text '©' has |½| non-GSM7 characters"; | 188 utf8_input = "This |±| text '©' has |½| non-GSM7 characters"; |
188 gsm7_out = Utf8StringToGsm7(utf8_input); | 189 gsm7_out = Utf8StringToGsm7(utf8_input); |
189 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]); | 190 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]); |
190 // Expect the text to have spaces where the invalid characters were. | 191 // Expect the text to have spaces where the invalid characters were. |
191 EXPECT_EQ("This | | text ' ' has | | non-GSM7 characters", utf8_out); | 192 EXPECT_EQ("This | | text ' ' has | | non-GSM7 characters", utf8_out); |
192 } | 193 } |
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 |
194 int main(int argc, char* argv[]) { | 272 int main(int argc, char* argv[]) { |
195 testing::InitGoogleTest(&argc, argv); | 273 testing::InitGoogleTest(&argc, argv); |
196 google::InitGoogleLogging(argv[0]); | 274 google::InitGoogleLogging(argv[0]); |
197 google::ParseCommandLineFlags(&argc, &argv, true); | 275 google::ParseCommandLineFlags(&argc, &argv, true); |
198 | 276 |
199 return RUN_ALL_TESTS(); | 277 return RUN_ALL_TESTS(); |
200 } | 278 } |
OLD | NEW |