OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 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 Loading... |
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 } gsm_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; gsm_test_data[i].packed_gsm7 != NULL; ++i) { |
| 135 out = Gsm7ToUtf8String(gsm_test_data[i].packed_gsm7); |
| 136 EXPECT_EQ(gsm_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; gsm_test_data[i].packed_gsm7 != NULL; ++i) { |
| 146 out = Utf8StringToGsm7(gsm_test_data[i].utf8_string); |
| 147 EXPECT_EQ(gsm_test_data[i].packed_gsm7_size, out.size()); |
| 148 EXPECT_EQ(0, memcmp(&out[0], gsm_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; gsm_test_data[i].packed_gsm7 != NULL; ++i) { |
| 159 gsm7_out = Utf8StringToGsm7(gsm_test_data[i].utf8_string); |
| 160 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]); |
| 161 EXPECT_EQ(gsm_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; gsm_test_data[i].packed_gsm7 != NULL; ++i) { |
| 172 utf8_out = Gsm7ToUtf8String(gsm_test_data[i].packed_gsm7); |
| 173 gsm7_out = Utf8StringToGsm7(utf8_out); |
| 174 EXPECT_EQ(gsm_test_data[i].packed_gsm7_size, gsm7_out.size()); |
| 175 EXPECT_EQ( |
| 176 0, memcmp(&gsm7_out[0], gsm_test_data[i].packed_gsm7, gsm7_out.size())); |
| 177 } |
| 178 } |
| 179 |
| 180 TEST(Utilities, Gsm7InvalidCharacter) { |
| 181 using utilities::Utf8StringToGsm7; |
| 182 using utilities::Gsm7ToUtf8String; |
| 183 std::string utf8_input; |
| 184 std::vector<uint8_t> gsm7_out; |
| 185 std::string utf8_out; |
| 186 |
| 187 utf8_input = "This |±| text '©' has |½| non-GSM7 characters"; |
| 188 gsm7_out = Utf8StringToGsm7(utf8_input); |
| 189 utf8_out = Gsm7ToUtf8String(&gsm7_out[0]); |
| 190 // Expect the text to have spaces where the invalid characters were. |
| 191 EXPECT_EQ("This | | text ' ' has | | non-GSM7 characters", utf8_out); |
| 192 } |
| 193 |
| 194 int main(int argc, char* argv[]) { |
64 testing::InitGoogleTest(&argc, argv); | 195 testing::InitGoogleTest(&argc, argv); |
65 google::InitGoogleLogging(argv[0]); | 196 google::InitGoogleLogging(argv[0]); |
66 google::ParseCommandLineFlags(&argc, &argv, true); | 197 google::ParseCommandLineFlags(&argc, &argv, true); |
67 | 198 |
68 return RUN_ALL_TESTS(); | 199 return RUN_ALL_TESTS(); |
69 } | 200 } |
OLD | NEW |