| Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2018 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 |  | 
 | 17 | #include "fastboot.h" | 
 | 18 |  | 
 | 19 | #include <gtest/gtest.h> | 
 | 20 |  | 
 | 21 | TEST(FastBoot, ParseOsPatchLevel) { | 
 | 22 |     FastBoot fb; | 
 | 23 |     boot_img_hdr_v1 hdr; | 
 | 24 |  | 
 | 25 |     hdr = {}; | 
 | 26 |     fb.ParseOsPatchLevel(&hdr, "2018-01-05"); | 
 | 27 |     ASSERT_EQ(2018U, 2000U + ((hdr.os_version >> 4) & 0x7f)); | 
 | 28 |     ASSERT_EQ(1U, ((hdr.os_version >> 0) & 0xf)); | 
 | 29 |  | 
 | 30 |     EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018"), "should be YYYY-MM-DD"); | 
 | 31 |     EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-01"), "should be YYYY-MM-DD"); | 
 | 32 |     EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2128-01-05"), "year out of range"); | 
 | 33 |     EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-13-05"), "month out of range"); | 
 | 34 | } | 
 | 35 |  | 
 | 36 | TEST(FastBoot, ParseOsVersion) { | 
 | 37 |     FastBoot fb; | 
 | 38 |     boot_img_hdr_v1 hdr; | 
 | 39 |  | 
 | 40 |     hdr = {}; | 
 | 41 |     fb.ParseOsVersion(&hdr, "1.2.3"); | 
 | 42 |     ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f)); | 
 | 43 |     ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f)); | 
 | 44 |     ASSERT_EQ(3U, ((hdr.os_version >> 11) & 0x7f)); | 
 | 45 |  | 
 | 46 |     fb.ParseOsVersion(&hdr, "1.2"); | 
 | 47 |     ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f)); | 
 | 48 |     ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f)); | 
 | 49 |     ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f)); | 
 | 50 |  | 
 | 51 |     fb.ParseOsVersion(&hdr, "1"); | 
 | 52 |     ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f)); | 
 | 53 |     ASSERT_EQ(0U, ((hdr.os_version >> 18) & 0x7f)); | 
 | 54 |     ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f)); | 
 | 55 |  | 
 | 56 |     EXPECT_DEATH(fb.ParseOsVersion(&hdr, ""), "bad OS version"); | 
 | 57 |     EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.3.4"), "bad OS version"); | 
 | 58 |     EXPECT_DEATH(fb.ParseOsVersion(&hdr, "128.2.3"), "bad OS version"); | 
 | 59 |     EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.128.3"), "bad OS version"); | 
 | 60 |     EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.128"), "bad OS version"); | 
 | 61 | } |