OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/common/bluetooth/bluetooth_device_id.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "crypto/random.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using content::BluetoothDeviceId; |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kValidDeviceId1[] = "1234567890123456789012=="; |
| 17 const char kValidDeviceId2[] = "AbCdEfGhIjKlMnOpQrSt+/=="; |
| 18 const char kInvalidLongDeviceId[] = "12345678901234567890123="; |
| 19 const char kInvalidShortDeviceId[] = "12345678901234567890"; |
| 20 const char kInvalidCharacterDeviceId[] = "123456789012345678901*=="; |
| 21 // A base64 string should have a length of a multiple of 4. |
| 22 const char kInvalidLengthDeviceId[] = "123456789012345678901"; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 TEST(BluetoothDeviceIdTest, DefaulConstructor) { |
| 27 BluetoothDeviceId default_id1; |
| 28 BluetoothDeviceId default_id2; |
| 29 BluetoothDeviceId valid_id(kValidDeviceId1); |
| 30 |
| 31 ASSERT_DEATH_IF_SUPPORTED(default_id1.str(), ""); |
| 32 ASSERT_DEATH_IF_SUPPORTED(default_id2.str(), ""); |
| 33 ASSERT_TRUE(BluetoothDeviceId::IsValid(valid_id.str())); |
| 34 |
| 35 EXPECT_DEATH_IF_SUPPORTED([&]() { return default_id1 == default_id2; }(), ""); |
| 36 EXPECT_DEATH_IF_SUPPORTED([&]() { return default_id1 != default_id2; }(), ""); |
| 37 |
| 38 EXPECT_DEATH_IF_SUPPORTED([&]() { return default_id1 == valid_id; }(), ""); |
| 39 EXPECT_DEATH_IF_SUPPORTED([&]() { return valid_id == default_id1; }(), ""); |
| 40 |
| 41 EXPECT_DEATH_IF_SUPPORTED([&]() { return default_id1 != valid_id; }(), ""); |
| 42 EXPECT_DEATH_IF_SUPPORTED([&]() { return valid_id != default_id1; }(), ""); |
| 43 } |
| 44 |
| 45 TEST(BluetoothDeviceIdTest, StrConstructor) { |
| 46 BluetoothDeviceId(kValidDeviceId1); |
| 47 BluetoothDeviceId(kValidDeviceId2); |
| 48 |
| 49 EXPECT_TRUE(kValidDeviceId1 == kValidDeviceId1); |
| 50 EXPECT_TRUE(kValidDeviceId2 == kValidDeviceId2); |
| 51 |
| 52 EXPECT_TRUE(kValidDeviceId1 != kValidDeviceId2); |
| 53 |
| 54 EXPECT_DEATH_IF_SUPPORTED(BluetoothDeviceId(""), ""); |
| 55 EXPECT_DEATH_IF_SUPPORTED( |
| 56 [&]() { return BluetoothDeviceId(kInvalidLongDeviceId); }(), ""); |
| 57 EXPECT_DEATH_IF_SUPPORTED( |
| 58 [&]() { return BluetoothDeviceId(kInvalidShortDeviceId); }(), ""); |
| 59 EXPECT_DEATH_IF_SUPPORTED( |
| 60 [&]() { return BluetoothDeviceId(kInvalidCharacterDeviceId); }(), ""); |
| 61 EXPECT_DEATH_IF_SUPPORTED( |
| 62 [&]() { return BluetoothDeviceId(kInvalidLengthDeviceId); }(), ""); |
| 63 } |
| 64 |
| 65 TEST(BluetoothDeviceIdTest, IsValid_Valid) { |
| 66 EXPECT_TRUE(BluetoothDeviceId::IsValid(kValidDeviceId1)); |
| 67 EXPECT_TRUE(BluetoothDeviceId::IsValid(kValidDeviceId2)); |
| 68 } |
| 69 |
| 70 TEST(BluetoothDeviceIdTest, IsValid_Invalid) { |
| 71 EXPECT_FALSE(BluetoothDeviceId::IsValid("")); |
| 72 EXPECT_FALSE(BluetoothDeviceId::IsValid(kInvalidLongDeviceId)); |
| 73 EXPECT_FALSE(BluetoothDeviceId::IsValid(kInvalidShortDeviceId)); |
| 74 EXPECT_FALSE(BluetoothDeviceId::IsValid(kInvalidCharacterDeviceId)); |
| 75 EXPECT_FALSE(BluetoothDeviceId::IsValid(kInvalidLengthDeviceId)); |
| 76 } |
| 77 |
| 78 TEST(BluetoothDeviceIdTest, Create) { |
| 79 // Tests that Create generates a valid Device Id. |
| 80 EXPECT_TRUE(BluetoothDeviceId::IsValid(BluetoothDeviceId::Create().str())) |
| 81 << "This should never fail."; |
| 82 } |
OLD | NEW |