Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tianjie Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 20 | #include <android-base/strings.h> |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 21 | #include <android-base/test_utils.h> |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 22 | #include <bootloader_message/bootloader_message.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 25 | TEST(BootloaderMessageTest, read_and_write_bootloader_message) { |
| 26 | TemporaryFile temp_misc; |
Tianjie Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 27 | |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 28 | // Write the BCB. |
| 29 | bootloader_message boot = {}; |
| 30 | strlcpy(boot.command, "command", sizeof(boot.command)); |
| 31 | strlcpy(boot.recovery, "message1\nmessage2\n", sizeof(boot.recovery)); |
| 32 | strlcpy(boot.status, "status1", sizeof(boot.status)); |
| 33 | |
| 34 | std::string err; |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_misc.path, &err)) |
| 36 | << "Failed to write BCB: " << err; |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 37 | |
| 38 | // Read and verify. |
| 39 | bootloader_message boot_verify; |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 40 | ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_misc.path, &err)) |
| 41 | << "Failed to read BCB: " << err; |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 42 | |
| 43 | ASSERT_EQ(std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)), |
| 44 | std::string(reinterpret_cast<const char*>(&boot_verify), sizeof(boot_verify))); |
| 45 | } |
| 46 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 47 | TEST(BootloaderMessageTest, update_bootloader_message_in_struct) { |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 48 | // Write the options to BCB. |
| 49 | std::vector<std::string> options = { "option1", "option2" }; |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 50 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 51 | bootloader_message boot = {}; |
| 52 | // Inject some bytes into boot. |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 53 | strlcpy(boot.recovery, "random message", sizeof(boot.recovery)); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 54 | strlcpy(boot.status, "status bytes", sizeof(boot.status)); |
| 55 | strlcpy(boot.stage, "stage bytes", sizeof(boot.stage)); |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 56 | strlcpy(boot.reserved, "reserved bytes", sizeof(boot.reserved)); |
| 57 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 58 | ASSERT_TRUE(update_bootloader_message_in_struct(&boot, options)); |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 59 | |
| 60 | // Verify that command and recovery fields should be set. |
| 61 | ASSERT_EQ("boot-recovery", std::string(boot.command)); |
| 62 | std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n"; |
| 63 | ASSERT_EQ(expected, std::string(boot.recovery)); |
| 64 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 65 | // The rest should be intact. |
| 66 | ASSERT_EQ("status bytes", std::string(boot.status)); |
| 67 | ASSERT_EQ("stage bytes", std::string(boot.stage)); |
| 68 | ASSERT_EQ("reserved bytes", std::string(boot.reserved)); |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 71 | TEST(BootloaderMessageTest, update_bootloader_message_recovery_options_empty) { |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 72 | // Write empty vector. |
| 73 | std::vector<std::string> options; |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 74 | |
| 75 | // Read and verify. |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 76 | bootloader_message boot = {}; |
| 77 | ASSERT_TRUE(update_bootloader_message_in_struct(&boot, options)); |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 78 | |
| 79 | // command and recovery fields should be set. |
| 80 | ASSERT_EQ("boot-recovery", std::string(boot.command)); |
| 81 | ASSERT_EQ("recovery\n", std::string(boot.recovery)); |
| 82 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 83 | // The rest should be empty. |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 84 | ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status))); |
| 85 | ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage))); |
| 86 | ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'), |
| 87 | std::string(boot.reserved, sizeof(boot.reserved))); |
| 88 | } |
| 89 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 90 | TEST(BootloaderMessageTest, update_bootloader_message_recovery_options_long) { |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 91 | // Write super long message. |
| 92 | std::vector<std::string> options; |
| 93 | for (int i = 0; i < 100; i++) { |
| 94 | options.push_back("option: " + std::to_string(i)); |
| 95 | } |
| 96 | |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 97 | // Read and verify. |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 98 | bootloader_message boot = {}; |
| 99 | ASSERT_TRUE(update_bootloader_message_in_struct(&boot, options)); |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 100 | |
| 101 | // Make sure it's long enough. |
| 102 | std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n"; |
| 103 | ASSERT_GE(expected.size(), sizeof(boot.recovery)); |
| 104 | |
| 105 | // command and recovery fields should be set. |
| 106 | ASSERT_EQ("boot-recovery", std::string(boot.command)); |
| 107 | ASSERT_EQ(expected.substr(0, sizeof(boot.recovery) - 1), std::string(boot.recovery)); |
| 108 | ASSERT_EQ('\0', boot.recovery[sizeof(boot.recovery) - 1]); |
| 109 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 110 | // The rest should be empty. |
Tao Bao | 8b7301b | 2016-12-14 15:52:34 -0800 | [diff] [blame] | 111 | ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status))); |
| 112 | ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage))); |
| 113 | ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'), |
| 114 | std::string(boot.reserved, sizeof(boot.reserved))); |
| 115 | } |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 116 | |