1 /*
2  * Copyright (C) 2020 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 <libvts_vintf_test_common/common.h>
18 
19 namespace android::vintf::testing {
20 
21 // kApiLevel2FcmMap is associated with API level. There can be multiple
22 // Framework Compatibility Matrix Version (FCM Version) per API level, or
23 // multiple API levels per FCM version.
24 // kApiLevel2FcmMap is defined apart from android::vintf::Level. Level is an
25 // integer designed to be irrelevant with API level; the O / O_MR1 values are
26 // historic values for convenience, and should be removed (b/70628538). Hence
27 // these values are not used here.
28 // For example:
29 //    ...
30 //    // Assume devices launch with Android X must implement FCM version >= 9
31 //    X = 9,
32 //    // Assume devices launch with Android Y and Android Z must implement
33 //    // FCM version >= 11
34 //    Y = 11,
35 //    Z = 11
36 static const std::map<uint64_t /* Shipping API Level */,
37                       Level /* FCM Version */>
38     kApiLevel2FcmMap{{
39         // N. The test runs on devices that launch with N and
40         // become a Treble device when upgrading to O.
41         {25, Level::O},
42 
43         {26, Level::O},
44         {27, Level::O_MR1},
45         {28, Level::P},
46         {29, Level::Q},
47         {30, Level::R},
48         {31, Level::S},
49         {32, Level::S},  // subject to change, placeholder value
50     }};
51 
TestTargetFcmVersion(Level shipping_fcm_version,uint64_t shipping_api_level)52 android::base::Result<void> TestTargetFcmVersion(Level shipping_fcm_version,
53                                                  uint64_t shipping_api_level) {
54   if (shipping_api_level == 0u) {
55     return android::base::Error()
56            << "Device's shipping API level cannot be determined.";
57   }
58 
59   if (shipping_fcm_version == Level::UNSPECIFIED) {
60     // O / O-MR1 vendor image doesn't have shipping FCM version declared and
61     // shipping FCM version is inferred from Shipping API level, hence it always
62     // meets the requirement.
63     return {};
64   }
65 
66   if (shipping_api_level < kApiLevel2FcmMap.begin()->first /* 25 */) {
67     return android::base::Error() << "Pre-N devices should not run this test.";
68   }
69 
70   auto it = kApiLevel2FcmMap.find(shipping_api_level);
71   if (it == kApiLevel2FcmMap.end()) {
72     return android::base::Error()
73            << "No launch requirement is set yet for Shipping API level "
74            << shipping_api_level << ". Please update the test.";
75   }
76 
77   Level required_fcm_version = it->second;
78   if (shipping_fcm_version < required_fcm_version) {
79     return android::base::Error()
80            << "Shipping API level == " << shipping_api_level
81            << " requires Shipping FCM Version >= " << required_fcm_version
82            << " (but is " << shipping_fcm_version << ")";
83   }
84 
85   return {};
86 }
87 
88 }  // namespace android::vintf::testing
89