Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2017 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import datetime |
| 18 | import StringIO |
| 19 | import unittest |
| 20 | import update_crypto_support |
| 21 | |
| 22 | |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 23 | def do_update_data(prev_data, current_data, name_dict={}): |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 24 | return update_crypto_support.update_data( |
| 25 | prev_data, |
| 26 | current_data, |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 27 | name_dict, |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 28 | 72, |
| 29 | datetime.datetime.utcfromtimestamp(1234567890)) |
| 30 | |
| 31 | |
| 32 | # The timestamp 1234567890 in our text format |
| 33 | LAST_UPDATED_TEXT = '2009-02-13 23:31:30 UTC' |
| 34 | |
| 35 | |
| 36 | class TestUpdateData(unittest.TestCase): |
| 37 | def test_find_by_name(self): |
| 38 | self.assertIsNone(update_crypto_support.find_by_name([], 'foo')) |
| 39 | self.assertIsNone( |
| 40 | update_crypto_support.find_by_name([{'name': 'foo'}], 'bar')) |
| 41 | self.assertEqual( |
| 42 | update_crypto_support.find_by_name( |
| 43 | [{'name': 'foo', 'value': 'foo_value'}], |
| 44 | 'foo'), |
| 45 | {'name': 'foo', 'value': 'foo_value'}) |
| 46 | self.assertEqual( |
| 47 | update_crypto_support.find_by_name( |
| 48 | [{'name': 'foo', 'value': 'foo_value'}, |
| 49 | {'name': 'bar', 'value': 'bar_value'}], |
| 50 | 'bar'), |
| 51 | {'name': 'bar', 'value': 'bar_value'}) |
| 52 | self.assertEqual( |
| 53 | update_crypto_support.find_by_name( |
| 54 | [{'name': 'foo', 'value': 'foo_value'}, |
| 55 | {'name': 'bar', 'value': 'bar_value'}, |
| 56 | {'name': 'foo', 'value': 'foo2_value'}], |
| 57 | 'foo'), |
| 58 | {'name': 'foo', 'value': 'foo_value'}) |
| 59 | self.assertEqual( |
| 60 | update_crypto_support.find_by_name( |
| 61 | [{'name': 'foot', 'value': 'foot_value'}, |
| 62 | {'name': 'bar', 'value': 'bar_value'}, |
| 63 | {'name': 'foo', 'value': 'foo_value'}], |
| 64 | 'foo'), |
| 65 | {'name': 'foo', 'value': 'foo_value'}) |
| 66 | |
| 67 | def test_sort_by_name(self): |
| 68 | self.assertEqual(update_crypto_support.sort_by_name([]), []) |
| 69 | self.assertEqual( |
| 70 | update_crypto_support.sort_by_name( |
| 71 | [{'name': 'foot', 'value': 'foot_value'}, |
| 72 | {'name': 'bar', 'value': 'bar_value'}, |
| 73 | {'name': 'foo', 'value': 'foo_value'}]), |
| 74 | [{'name': 'bar', 'value': 'bar_value'}, |
| 75 | {'name': 'foo', 'value': 'foo_value'}, |
| 76 | {'name': 'foot', 'value': 'foot_value'}]) |
| 77 | with self.assertRaises(KeyError): |
| 78 | update_crypto_support.sort_by_name([{'not_name': 'foo'}]) |
| 79 | |
| 80 | def test_get_current_data(self): |
| 81 | self.assertEqual(update_crypto_support.get_current_data( |
| 82 | StringIO.StringIO( |
| 83 | ''' |
| 84 | BEGIN ALGORITHM LIST |
| 85 | Mac Bob |
| 86 | Mac Jones |
| 87 | MessageDigest Jim |
| 88 | Mac Amy |
| 89 | OtherThing Mary |
| 90 | END ALGORITHM LIST |
| 91 | ''')), |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 92 | ({'Mac': ['BOB', 'JONES', 'AMY'], |
| 93 | 'MessageDigest': ['JIM']}, |
| 94 | {'AMY': 'Amy', |
| 95 | 'BOB': 'Bob', |
| 96 | 'JONES': 'Jones', |
| 97 | 'JIM': 'Jim'})) |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 98 | self.assertEqual(update_crypto_support.get_current_data( |
| 99 | StringIO.StringIO( |
| 100 | ''' |
| 101 | BEGIN ALGORITHM LIST |
| 102 | Mac Dupe |
| 103 | Mac Jones |
| 104 | MessageDigest Jim |
| 105 | Mac Amy |
| 106 | Mac Dupe |
| 107 | OtherThing Mary |
| 108 | END ALGORITHM LIST |
| 109 | ''')), |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 110 | ({'Mac': ['DUPE', 'JONES', 'AMY', 'DUPE'], |
| 111 | 'MessageDigest': ['JIM']}, |
| 112 | {'AMY': 'Amy', |
| 113 | 'DUPE': 'Dupe', |
| 114 | 'JONES': 'Jones', |
| 115 | 'JIM': 'Jim'})) |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 116 | self.assertEqual(update_crypto_support.get_current_data( |
| 117 | StringIO.StringIO( |
| 118 | ''' |
| 119 | Mac NotAValue |
| 120 | BEGIN ALGORITHM LIST |
| 121 | Mac Bob |
| 122 | Mac Jones |
| 123 | MessageDigest Jim |
| 124 | Mac Amy |
| 125 | OtherThing Mary |
| 126 | END ALGORITHM LIST |
| 127 | Mac AlsoNotAValue |
| 128 | ''')), |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 129 | ({'Mac': ['BOB', 'JONES', 'AMY'], |
| 130 | 'MessageDigest': ['JIM']}, |
| 131 | {'AMY': 'Amy', |
| 132 | 'BOB': 'Bob', |
| 133 | 'JONES': 'Jones', |
| 134 | 'JIM': 'Jim'})) |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 135 | self.assertEqual(update_crypto_support.get_current_data( |
| 136 | StringIO.StringIO( |
| 137 | ''' |
| 138 | BEGIN ALGORITHM LIST OF LISTS |
| 139 | Mac NotAValue |
| 140 | BEGIN ALGORITHM LIST |
| 141 | Mac Bob |
| 142 | Mac Jones |
| 143 | MessageDigest Jim |
| 144 | Mac Amy |
| 145 | OtherThing Mary |
| 146 | END ALGORITHM LIST |
| 147 | ''')), |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 148 | ({'Mac': ['BOB', 'JONES', 'AMY'], |
| 149 | 'MessageDigest': ['JIM']}, |
| 150 | {'AMY': 'Amy', |
| 151 | 'BOB': 'Bob', |
| 152 | 'JONES': 'Jones', |
| 153 | 'JIM': 'Jim'})) |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 154 | with self.assertRaises(EOFError): |
| 155 | update_crypto_support.get_current_data(StringIO.StringIO( |
| 156 | ''' |
| 157 | NOTBEGIN ALGORITHM LIST |
| 158 | Mac Bob |
| 159 | Mac Jones |
| 160 | MessageDigest Jim |
| 161 | Mac Amy |
| 162 | OtherThing Mary |
| 163 | END ALGORITHM LIST |
| 164 | ''')) |
| 165 | with self.assertRaises(EOFError): |
| 166 | update_crypto_support.get_current_data(StringIO.StringIO( |
| 167 | ''' |
| 168 | BEGIN ALGORITHM LIST |
| 169 | Mac Bob |
| 170 | Mac Jones |
| 171 | MessageDigest Jim |
| 172 | Mac Amy |
| 173 | OtherThing Mary''')) |
| 174 | with self.assertRaises(ValueError): |
| 175 | update_crypto_support.get_current_data(StringIO.StringIO( |
| 176 | ''' |
| 177 | BEGIN ALGORITHM LIST |
| 178 | Mac Bob |
| 179 | Mac Jones |
| 180 | MessageDigest Jim OneTooManyItems |
| 181 | Mac Amy |
| 182 | OtherThing Mary |
| 183 | END ALGORITHM LIST |
| 184 | ''')) |
| 185 | with self.assertRaises(ValueError): |
| 186 | update_crypto_support.get_current_data(StringIO.StringIO( |
| 187 | ''' |
| 188 | BEGIN ALGORITHM LIST |
| 189 | Mac Bob |
| 190 | Mac Jones |
| 191 | TooFewItems |
| 192 | MessageDigest Jim |
| 193 | Mac Amy |
| 194 | OtherThing Mary |
| 195 | END ALGORITHM LIST |
| 196 | ''')) |
| 197 | |
| 198 | def test_update_data_no_data(self): |
| 199 | self.assertEqual( |
| 200 | do_update_data( |
| 201 | {'categories': []}, |
| 202 | {}), |
| 203 | {'categories': [], |
| 204 | 'api_level': '72', |
| 205 | 'last_updated': LAST_UPDATED_TEXT}) |
| 206 | |
| 207 | self.assertEqual( |
| 208 | do_update_data( |
| 209 | {'categories': [ |
| 210 | {'name': 'MessageDigest', |
| 211 | 'algorithms': []}]}, |
| 212 | {}), |
| 213 | {'categories': [], |
| 214 | 'api_level': '72', |
| 215 | 'last_updated': LAST_UPDATED_TEXT}) |
| 216 | |
| 217 | self.assertEqual( |
| 218 | do_update_data( |
| 219 | {'categories': []}, |
| 220 | {'MessageDigest': []}), |
| 221 | {'categories': [], |
| 222 | 'api_level': '72', |
| 223 | 'last_updated': LAST_UPDATED_TEXT}) |
| 224 | |
| 225 | def test_update_data_no_updates(self): |
| 226 | self.assertEqual( |
| 227 | do_update_data( |
| 228 | {'categories': [ |
| 229 | {'name': 'MessageDigest', |
| 230 | 'algorithms': [ |
| 231 | {'name': 'SHA-1', |
| 232 | 'supported_api_levels': '1+'}, |
| 233 | {'name': 'SHA-2', |
| 234 | 'supported_api_levels': '1-22', |
| 235 | 'deprecated': 'true'}]}]}, |
| 236 | {'MessageDigest': ['SHA-1']}), |
| 237 | {'categories': [ |
| 238 | {'name': 'MessageDigest', |
| 239 | 'algorithms': [ |
| 240 | {'name': 'SHA-1', |
| 241 | 'supported_api_levels': '1+'}, |
| 242 | {'name': 'SHA-2', |
| 243 | 'supported_api_levels': '1-22', |
| 244 | 'deprecated': 'true'}]}], |
| 245 | 'api_level': '72', |
| 246 | 'last_updated': LAST_UPDATED_TEXT}) |
| 247 | |
| 248 | def test_update_data_new_item(self): |
| 249 | self.assertEqual( |
| 250 | do_update_data( |
| 251 | {'categories': [ |
| 252 | {'name': 'MessageDigest', |
| 253 | 'algorithms': [ |
| 254 | {'name': 'SHA-1', |
| 255 | 'supported_api_levels': '1+'}, |
| 256 | {'name': 'SHA-2', |
| 257 | 'supported_api_levels': '1-22', |
| 258 | 'deprecated': 'true'}, |
| 259 | {'name': 'SHA-384', |
| 260 | 'supported_api_levels': '17-32'}]}]}, |
| 261 | {'MessageDigest': ['SHA-1', 'SHA-256', 'SHA-384']}), |
| 262 | {'categories': [ |
| 263 | {'name': 'MessageDigest', |
| 264 | 'algorithms': [ |
| 265 | {'name': 'SHA-1', |
| 266 | 'supported_api_levels': '1+'}, |
| 267 | {'name': 'SHA-2', |
| 268 | 'supported_api_levels': '1-22', |
| 269 | 'deprecated': 'true'}, |
| 270 | {'name': 'SHA-256', |
| 271 | 'supported_api_levels': '72+'}, |
| 272 | {'name': 'SHA-384', |
| 273 | 'supported_api_levels': '17-32,72+'}]}], |
| 274 | 'api_level': '72', |
| 275 | 'last_updated': LAST_UPDATED_TEXT}) |
| 276 | |
| 277 | def test_update_data_removed_item(self): |
| 278 | self.assertEqual( |
| 279 | do_update_data( |
| 280 | {'categories': [ |
| 281 | {'name': 'MessageDigest', |
| 282 | 'algorithms': [ |
| 283 | {'name': 'SHA-1', |
| 284 | 'supported_api_levels': '1+'}, |
| 285 | {'name': 'SHA-2', |
| 286 | 'supported_api_levels': '1-22', |
| 287 | 'deprecated': 'true'}, |
| 288 | {'name': 'SHA-256', |
| 289 | 'supported_api_levels': '70+'}, |
| 290 | {'name': 'SHA-384', |
| 291 | 'supported_api_levels': '71+'}, |
| 292 | {'name': 'SHA-512', |
| 293 | 'supported_api_levels': '1-3,17-32,47+'}]}]}, |
| 294 | {'MessageDigest': ['SHA-1']}), |
| 295 | {'categories': [ |
| 296 | {'name': 'MessageDigest', |
| 297 | 'algorithms': [ |
| 298 | {'name': 'SHA-1', |
| 299 | 'supported_api_levels': '1+'}, |
| 300 | {'name': 'SHA-2', |
| 301 | 'supported_api_levels': '1-22', |
| 302 | 'deprecated': 'true'}, |
| 303 | {'name': 'SHA-256', |
| 304 | 'supported_api_levels': '70-71', |
| 305 | 'deprecated': 'true'}, |
| 306 | {'name': 'SHA-384', |
| 307 | 'supported_api_levels': '71', |
| 308 | 'deprecated': 'true'}, |
| 309 | {'name': 'SHA-512', |
| 310 | 'supported_api_levels': '1-3,17-32,47-71', |
| 311 | 'deprecated': 'true'}]}], |
| 312 | 'api_level': '72', |
| 313 | 'last_updated': LAST_UPDATED_TEXT}) |
| 314 | |
| 315 | def test_update_data_duplicates(self): |
| 316 | self.assertEqual( |
| 317 | do_update_data( |
| 318 | {'categories': [ |
| 319 | {'name': 'MessageDigest', |
| 320 | 'algorithms': [ |
| 321 | {'name': 'SHA-1', |
| 322 | 'supported_api_levels': '1+'}, |
| 323 | {'name': 'SHA-2', |
| 324 | 'supported_api_levels': '1-22', |
| 325 | 'deprecated': 'true'}, |
| 326 | {'name': 'SHA-1', |
| 327 | 'supported_api_levels': '7+'}]}]}, |
| 328 | {'MessageDigest': ['SHA-1']}), |
| 329 | {'categories': [ |
| 330 | {'name': 'MessageDigest', |
| 331 | 'algorithms': [ |
| 332 | {'name': 'SHA-1', |
| 333 | 'supported_api_levels': '1+'}, |
| 334 | {'name': 'SHA-2', |
| 335 | 'supported_api_levels': '1-22', |
| 336 | 'deprecated': 'true'}]}], |
| 337 | 'api_level': '72', |
| 338 | 'last_updated': LAST_UPDATED_TEXT}) |
| 339 | |
| 340 | self.assertEqual( |
| 341 | do_update_data( |
| 342 | {'categories': [ |
| 343 | {'name': 'MessageDigest', |
| 344 | 'algorithms': [ |
| 345 | {'name': 'SHA-1', |
| 346 | 'supported_api_levels': '1+'}, |
| 347 | {'name': 'SHA-2', |
| 348 | 'supported_api_levels': '1-22', |
| 349 | 'deprecated': 'true'}]}]}, |
| 350 | {'MessageDigest': ['SHA-1', 'SHA-1']}), |
| 351 | {'categories': [ |
| 352 | {'name': 'MessageDigest', |
| 353 | 'algorithms': [ |
| 354 | {'name': 'SHA-1', |
| 355 | 'supported_api_levels': '1+'}, |
| 356 | {'name': 'SHA-2', |
| 357 | 'supported_api_levels': '1-22', |
| 358 | 'deprecated': 'true'}]}], |
| 359 | 'api_level': '72', |
| 360 | 'last_updated': LAST_UPDATED_TEXT}) |
| 361 | |
Adam Vartanian | eb12138 | 2017-03-21 15:42:20 +0000 | [diff] [blame] | 362 | def test_update_name_matching(self): |
| 363 | self.assertEqual( |
| 364 | do_update_data( |
| 365 | {'categories': [ |
| 366 | {'name': 'MessageDigest', |
| 367 | 'algorithms': [ |
| 368 | {'name': 'sha-1', |
| 369 | 'supported_api_levels': '1+'}, |
| 370 | {'name': 'Sha-2', |
| 371 | 'supported_api_levels': '1-22', |
| 372 | 'deprecated': 'true'}, |
| 373 | {'name': 'SHA-3', |
| 374 | 'supported_api_levels': '7+'}]}]}, |
| 375 | {'MessageDigest': ['SHA-1', 'SHA-2', 'SHA-3']}, |
| 376 | {'SHA-1': 'Sha-1', 'SHA-2': 'Sha-2', 'SHA-3': 'Sha-3'}), |
| 377 | {'categories': [ |
| 378 | {'name': 'MessageDigest', |
| 379 | 'algorithms': [ |
| 380 | {'name': 'Sha-1', |
| 381 | 'supported_api_levels': '1+'}, |
| 382 | {'name': 'Sha-2', |
| 383 | 'supported_api_levels': '1-22,72+'}, |
| 384 | {'name': 'Sha-3', |
| 385 | 'supported_api_levels': '7+'}]}], |
| 386 | 'api_level': '72', |
| 387 | 'last_updated': LAST_UPDATED_TEXT}) |
| 388 | self.assertEqual( |
| 389 | do_update_data( |
| 390 | {'categories': [ |
| 391 | {'name': 'MessageDigest', |
| 392 | 'algorithms': [ |
| 393 | {'name': 'sha-1', |
| 394 | 'supported_api_levels': '1+'}, |
| 395 | {'name': 'Sha-2', |
| 396 | 'supported_api_levels': '1-22', |
| 397 | 'deprecated': 'true'}, |
| 398 | {'name': 'SHA-3', |
| 399 | 'supported_api_levels': '7+'}]}]}, |
| 400 | {'MessageDigest': ['SHA-1', 'SHA-3']}, |
| 401 | {'SHA-1': 'Sha-1', 'SHA-3': 'Sha-3'}), |
| 402 | {'categories': [ |
| 403 | {'name': 'MessageDigest', |
| 404 | 'algorithms': [ |
| 405 | {'name': 'Sha-1', |
| 406 | 'supported_api_levels': '1+'}, |
| 407 | {'name': 'Sha-2', |
| 408 | 'supported_api_levels': '1-22', |
| 409 | 'deprecated': 'true'}, |
| 410 | {'name': 'Sha-3', |
| 411 | 'supported_api_levels': '7+'}]}], |
| 412 | 'api_level': '72', |
| 413 | 'last_updated': LAST_UPDATED_TEXT}) |
| 414 | |
Adam Vartanian | 9ae0b40 | 2017-03-08 16:39:31 +0000 | [diff] [blame] | 415 | |
| 416 | if __name__ == '__main__': |
| 417 | unittest.main() |