1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "AssembleVintfTest"
18 
19 #include <android-base/logging.h>
20 #include <gtest/gtest.h>
21 
22 #include <vintf/AssembleVintf.h>
23 #include <vintf/parse_string.h>
24 #include "test_constants.h"
25 
26 namespace android {
27 namespace vintf {
28 
29 static bool In(const std::string& sub, const std::string& str) {
30     return str.find(sub) != std::string::npos;
31 }
32 #define EXPECT_IN(sub, str) EXPECT_TRUE(In((sub), (str))) << (str);
33 
34 class AssembleVintfTest : public ::testing::Test {
35    public:
36     virtual void SetUp() override {
37         mInstance = AssembleVintf::newInstance();
38         auto s = makeStream("");
39         mOutputStream = s.get();
40         mInstance->setOutputStream(std::move(s));
41 
42         getInstance()->setFakeEnv("PRODUCT_ENFORCE_VINTF_MANIFEST", "true");
43     }
44     virtual void TearDown() override { mInstance = nullptr; }
45 
46     const std::unique_ptr<AssembleVintf>& getInstance() { return mInstance; }
47 
48     std::string getOutput() { return mOutputStream->str(); }
49 
50     void resetOutput() { mOutputStream->str(""); }
51 
52     void setFakeEnvs(const std::map<std::string, std::string>& envs) {
53         for (const auto& pair : envs) {
54             getInstance()->setFakeEnv(pair.first, pair.second);
55         }
56     }
57 
58     void addInput(const std::string& name, const std::string& s) {
59         getInstance()->addInputStream(name, makeStream(s));
60     }
61 
62     std::unique_ptr<std::stringstream> makeStream(const std::string& s) {
63         return std::make_unique<std::stringstream>(s);
64     }
65 
66     std::unique_ptr<AssembleVintf> mInstance;
67     // do not own this object.
68     std::stringstream* mOutputStream;
69 };
70 
71 // clang-format off
72 
73 TEST_F(AssembleVintfTest, FrameworkMatrixEmpty) {
74     std::string xmlEmpty = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" />";
75     std::string kernel318 = "CONFIG_FOO=y\n";
76     std::string kernel318_64 = "CONFIG_BAR=y\n";
77     std::string kernel44 = "# CONFIG_FOO is not set\n";
78     std::string kernel44_64 = "CONFIG_BAR=y\n";
79 
80     addInput("compatibility_matrix.empty.xml", xmlEmpty);
81     setFakeEnvs({
82         {"POLICYVERS", "30"},
83         {"PLATFORM_SEPOLICY_VERSION", "10000.0"},
84         {"FRAMEWORK_VBMETA_VERSION", "1.0"},
85     });
86     getInstance()->addKernelConfigInputStream({3, 18, 0}, "android-base.config",
87                                               makeStream(kernel318));
88     getInstance()->addKernelConfigInputStream({3, 18, 0}, "android-base-arm64.config",
89                                               makeStream(kernel318_64));
90     getInstance()->addKernelConfigInputStream({4, 4, 0}, "android-base.config", makeStream(kernel44));
91     getInstance()->addKernelConfigInputStream({4, 4, 0}, "android-base-arm64.config",
92                                               makeStream(kernel44_64));
93 
94     EXPECT_TRUE(getInstance()->assemble());
95 
96     EXPECT_IN(
97         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
98         "    <kernel version=\"3.18.0\">\n"
99         "        <config>\n"
100         "            <key>CONFIG_FOO</key>\n"
101         "            <value type=\"tristate\">y</value>\n"
102         "        </config>\n"
103         "    </kernel>\n"
104         "    <kernel version=\"3.18.0\">\n"
105         "        <conditions>\n"
106         "            <config>\n"
107         "                <key>CONFIG_ARM64</key>\n"
108         "                <value type=\"tristate\">y</value>\n"
109         "            </config>\n"
110         "        </conditions>\n"
111         "        <config>\n"
112         "            <key>CONFIG_BAR</key>\n"
113         "            <value type=\"tristate\">y</value>\n"
114         "        </config>\n"
115         "    </kernel>\n"
116         "    <kernel version=\"4.4.0\">\n"
117         "        <config>\n"
118         "            <key>CONFIG_FOO</key>\n"
119         "            <value type=\"tristate\">n</value>\n"
120         "        </config>\n"
121         "    </kernel>\n"
122         "    <kernel version=\"4.4.0\">\n"
123         "        <conditions>\n"
124         "            <config>\n"
125         "                <key>CONFIG_ARM64</key>\n"
126         "                <value type=\"tristate\">y</value>\n"
127         "            </config>\n"
128         "        </conditions>\n"
129         "        <config>\n"
130         "            <key>CONFIG_BAR</key>\n"
131         "            <value type=\"tristate\">y</value>\n"
132         "        </config>\n"
133         "    </kernel>\n"
134         "    <sepolicy>\n"
135         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
136         "        <sepolicy-version>10000.0</sepolicy-version>\n"
137         "    </sepolicy>\n"
138         "    <avb>\n"
139         "        <vbmeta-version>1.0</vbmeta-version>\n"
140         "    </avb>\n"
141         "</compatibility-matrix>\n",
142         getOutput());
143 }
144 
145 TEST_F(AssembleVintfTest, FrameworkMatrix) {
146     std::string tail =
147         "        <config>\n"
148         "            <key>CONFIG_FOO</key>\n"
149         "            <value type=\"tristate\">y</value>\n"
150         "        </config>\n"
151         "    </kernel>\n"
152         "    <sepolicy>\n"
153         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
154         "        <sepolicy-version>10000.0</sepolicy-version>\n"
155         "    </sepolicy>\n"
156         "    <avb>\n"
157         "        <vbmeta-version>1.0</vbmeta-version>\n"
158         "    </avb>\n"
159         "</compatibility-matrix>\n";
160 
161     std::string xmlEmpty =
162         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
163         "    <kernel version=\"3.18.0\">\n" +
164         tail;
165 
166     std::string xml1 =
167         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
168         "    <hal format=\"hidl\" optional=\"true\">\n"
169         "        <name>android.hardware.foo</name>\n"
170         "        <version>1.0</version>\n"
171         "        <interface>\n"
172         "            <name>IFoo</name>\n"
173         "            <instance>default</instance>\n"
174         "        </interface>\n"
175         "    </hal>\n"
176         "</compatibility-matrix>\n";
177 
178     std::string xml2 =
179         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
180         "    <hal format=\"hidl\" optional=\"true\">\n"
181         "        <name>android.hardware.foo</name>\n"
182         "        <version>1.0-1</version>\n"
183         "        <interface>\n"
184         "            <name>IFoo</name>\n"
185         "            <instance>default</instance>\n"
186         "        </interface>\n"
187         "    </hal>\n"
188         "</compatibility-matrix>\n";
189 
190     std::string xml3 =
191         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"3\">\n"
192         "    <hal format=\"hidl\" optional=\"false\">\n"
193         "        <name>android.hardware.foo</name>\n"
194         "        <version>2.0</version>\n"
195         "        <interface>\n"
196         "            <name>IFoo</name>\n"
197         "            <instance>default</instance>\n"
198         "        </interface>\n"
199         "    </hal>\n"
200         "</compatibility-matrix>\n";
201 
202     auto manifest = [](size_t level) {
203         return "<manifest " +
204                     kMetaVersionStr +
205                     " type=\"device\"" +
206                     " target-level=\"" + std::to_string(level) + "\">\n" +
207                "    <hal format=\"hidl\">\n"
208                "        <name>android.hardware.foo</name>\n"
209                "        <version>1.1</version>\n"
210                "        <transport>hwbinder</transport>\n"
211                "        <interface>\n"
212                "            <name>IFoo</name>\n"
213                "            <instance>default</instance>\n"
214                "        </interface>\n"
215                "    </hal>\n"
216                "    <hal format=\"hidl\">\n"
217                "        <name>android.hardware.foo</name>\n"
218                "        <version>2.0</version>\n"
219                "        <transport>hwbinder</transport>\n"
220                "        <interface>\n"
221                "            <name>IFoo</name>\n"
222                "            <instance>default</instance>\n"
223                "        </interface>\n"
224                "    </hal>\n"
225                "    <sepolicy>\n"
226                "        <version>10000.0</version>\n"
227                "    </sepolicy>\n"
228                "</manifest>\n";
229     };
230 
231     addInput("compatibility_matrix.1.xml", xml1);
232     addInput("compatibility_matrix.2.xml", xml2);
233     addInput("compatibility_matrix.3.xml", xml3);
234     addInput("compatibility_matrix.empty.xml", xmlEmpty);
235     getInstance()->setFakeEnv("PRODUCT_ENFORCE_VINTF_MANIFEST", "true");
236 
237     resetOutput();
238     getInstance()->setCheckInputStream(makeStream(manifest(1)));
239     EXPECT_TRUE(getInstance()->assemble());
240     EXPECT_IN(
241         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
242         "    <hal format=\"hidl\" optional=\"true\">\n"
243         "        <name>android.hardware.foo</name>\n"
244         "        <version>1.0-1</version>\n"
245         "        <version>2.0</version>\n"
246         "        <interface>\n"
247         "            <name>IFoo</name>\n"
248         "            <instance>default</instance>\n"
249         "        </interface>\n"
250         "    </hal>\n"
251         "    <kernel version=\"3.18.0\" level=\"1\">\n" +
252             tail,
253         getOutput());
254 
255     resetOutput();
256     getInstance()->setCheckInputStream(makeStream(manifest(2)));
257     EXPECT_TRUE(getInstance()->assemble());
258     EXPECT_IN(
259         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
260         "    <hal format=\"hidl\" optional=\"true\">\n"
261         "        <name>android.hardware.foo</name>\n"
262         "        <version>1.0-1</version>\n"
263         "        <version>2.0</version>\n"
264         "        <interface>\n"
265         "            <name>IFoo</name>\n"
266         "            <instance>default</instance>\n"
267         "        </interface>\n"
268         "    </hal>\n"
269         "    <kernel version=\"3.18.0\" level=\"2\">\n" +
270             tail,
271         getOutput());
272 
273     resetOutput();
274     getInstance()->setCheckInputStream(makeStream(manifest(3)));
275     EXPECT_TRUE(getInstance()->assemble());
276     EXPECT_IN(
277         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"3\">\n"
278         "    <hal format=\"hidl\" optional=\"false\">\n"
279         "        <name>android.hardware.foo</name>\n"
280         "        <version>2.0</version>\n"
281         "        <interface>\n"
282         "            <name>IFoo</name>\n"
283         "            <instance>default</instance>\n"
284         "        </interface>\n"
285         "    </hal>\n"
286         "    <kernel version=\"3.18.0\" level=\"3\">\n" +
287             tail,
288         getOutput());
289 }
290 
291 TEST_F(AssembleVintfTest, MatrixVendorNdk) {
292     addInput("compatibility_matrix.xml",
293              "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n");
294     getInstance()->setFakeEnv("REQUIRED_VNDK_VERSION", "P");
295     EXPECT_TRUE(getInstance()->assemble());
296     EXPECT_IN(
297         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
298         "    <vendor-ndk>\n"
299         "        <version>P</version>\n"
300         "    </vendor-ndk>\n"
301         "</compatibility-matrix>\n",
302         getOutput());
303 }
304 
305 TEST_F(AssembleVintfTest, ManifestVendorNdk) {
306     addInput("manifest.xml", "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n");
307     getInstance()->setFakeEnv("PROVIDED_VNDK_VERSIONS", "P  26 27   ");
308     EXPECT_TRUE(getInstance()->assemble());
309     EXPECT_IN(
310         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
311         "    <vendor-ndk>\n"
312         "        <version>P</version>\n"
313         "    </vendor-ndk>\n"
314         "    <vendor-ndk>\n"
315         "        <version>26</version>\n"
316         "    </vendor-ndk>\n"
317         "    <vendor-ndk>\n"
318         "        <version>27</version>\n"
319         "    </vendor-ndk>\n"
320         "</manifest>\n",
321         getOutput());
322 }
323 
324 TEST_F(AssembleVintfTest, VendorNdkCheckEmpty) {
325     addInput("manifest.xml", "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n");
326     getInstance()->setFakeEnv("PROVIDED_VNDK_VERSIONS", "P 26 27 ");
327 
328     std::string matrix = "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n";
329     getInstance()->setCheckInputStream(makeStream(matrix));
330     EXPECT_TRUE(getInstance()->assemble());
331 }
332 
333 TEST_F(AssembleVintfTest, VendorNdkCheckIncompat) {
334     addInput("manifest.xml", "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n");
335     getInstance()->setFakeEnv("PROVIDED_VNDK_VERSIONS", "P 26 27 ");
336     std::string matrix =
337         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
338         "    <vendor-ndk>\n"
339         "        <version>O</version>\n"
340         "    </vendor-ndk>\n"
341         "</compatibility-matrix>\n";
342     getInstance()->setCheckInputStream(makeStream(matrix));
343     EXPECT_FALSE(getInstance()->assemble());
344 }
345 
346 TEST_F(AssembleVintfTest, VendorNdkCheckCompat) {
347     addInput("manifest.xml", "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n");
348     getInstance()->setFakeEnv("PROVIDED_VNDK_VERSIONS", "P 26 27 ");
349     std::string matrix =
350         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
351         "    <vendor-ndk>\n"
352         "        <version>27</version>\n"
353         "    </vendor-ndk>\n"
354         "</compatibility-matrix>\n";
355     getInstance()->setCheckInputStream(makeStream(matrix));
356     EXPECT_TRUE(getInstance()->assemble());
357 }
358 
359 TEST_F(AssembleVintfTest, MatrixSystemSdk) {
360     addInput("compatibility_matrix.xml",
361              "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n");
362     getInstance()->setFakeEnv("BOARD_SYSTEMSDK_VERSIONS", "P 1 2 ");
363     EXPECT_TRUE(getInstance()->assemble());
364     EXPECT_IN(
365         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
366         "    <system-sdk>\n"
367         "        <version>1</version>\n"
368         "        <version>2</version>\n"
369         "        <version>P</version>\n"
370         "    </system-sdk>\n"
371         "</compatibility-matrix>\n",
372         getOutput());
373 }
374 
375 TEST_F(AssembleVintfTest, ManifestSystemSdk) {
376     addInput("manifest.xml", "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n");
377     getInstance()->setFakeEnv("PLATFORM_SYSTEMSDK_VERSIONS", "P 1 2 ");
378     EXPECT_TRUE(getInstance()->assemble());
379     EXPECT_IN(
380         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
381         "    <system-sdk>\n"
382         "        <version>1</version>\n"
383         "        <version>2</version>\n"
384         "        <version>P</version>\n"
385         "    </system-sdk>\n"
386         "</manifest>\n",
387         getOutput());
388 }
389 
390 const std::string gEmptyOutManifest =
391     "<manifest " + kMetaVersionStr + " type=\"device\">\n"
392     "    <sepolicy>\n"
393     "        <version>10000.0</version>\n"
394     "    </sepolicy>\n"
395     "</manifest>\n";
396 
397 TEST_F(AssembleVintfTest, EmptyManifest) {
398     const std::string emptyManifest = "<manifest " + kMetaVersionStr + " type=\"device\" />";
399     setFakeEnvs({{"BOARD_SEPOLICY_VERS", "10000.0"}, {"IGNORE_TARGET_FCM_VERSION", "true"}});
400     addInput("manifest.empty.xml", emptyManifest);
401     EXPECT_TRUE(getInstance()->assemble());
402     EXPECT_IN(gEmptyOutManifest, getOutput());
403 }
404 
405 TEST_F(AssembleVintfTest, DeviceFrameworkMatrixOptional) {
406     setFakeEnvs({{"POLICYVERS", "30"},
407                  {"PLATFORM_SEPOLICY_VERSION", "10000.0"},
408                  {"PLATFORM_SEPOLICY_COMPAT_VERSIONS", "26.0 27.0"},
409                  {"FRAMEWORK_VBMETA_VERSION", "1.0"},
410                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
411     getInstance()->setCheckInputStream(makeStream(gEmptyOutManifest));
412 
413     addInput("compatibility_matrix.empty.xml",
414              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
415              "    <hal format=\"hidl\" optional=\"true\">\n"
416              "        <name>vendor.foo.bar</name>\n"
417              "        <version>1.0</version>\n"
418              "        <interface>\n"
419              "            <name>IFoo</name>\n"
420              "            <instance>default</instance>\n"
421              "        </interface>\n"
422              "    </hal>\n"
423              "</compatibility-matrix>");
424 
425     EXPECT_TRUE(getInstance()->assemble());
426     EXPECT_IN(
427         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
428         "    <hal format=\"hidl\" optional=\"true\">\n"
429         "        <name>vendor.foo.bar</name>\n"
430         "        <version>1.0</version>\n"
431         "        <interface>\n"
432         "            <name>IFoo</name>\n"
433         "            <instance>default</instance>\n"
434         "        </interface>\n"
435         "    </hal>\n"
436         "    <sepolicy>\n"
437         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
438         "        <sepolicy-version>26.0</sepolicy-version>\n"
439         "        <sepolicy-version>27.0</sepolicy-version>\n"
440         "        <sepolicy-version>10000.0</sepolicy-version>\n"
441         "    </sepolicy>\n"
442         "    <avb>\n"
443         "        <vbmeta-version>1.0</vbmeta-version>\n"
444         "    </avb>\n"
445         "</compatibility-matrix>",
446         getOutput());
447 }
448 
449 TEST_F(AssembleVintfTest, DeviceFrameworkMatrixRequired) {
450     setFakeEnvs({{"POLICYVERS", "30"},
451                  {"PLATFORM_SEPOLICY_VERSION", "10000.0"},
452                  {"PLATFORM_SEPOLICY_COMPAT_VERSIONS", "26.0 27.0"},
453                  {"FRAMEWORK_VBMETA_VERSION", "1.0"},
454                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
455     getInstance()->setCheckInputStream(makeStream(gEmptyOutManifest));
456 
457     addInput("compatibility_matrix.empty.xml",
458              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
459              "    <hal format=\"hidl\" optional=\"false\">\n"
460              "        <name>vendor.foo.bar</name>\n"
461              "        <version>1.0</version>\n"
462              "        <interface>\n"
463              "            <name>IFoo</name>\n"
464              "            <instance>default</instance>\n"
465              "        </interface>\n"
466              "    </hal>\n"
467              "</compatibility-matrix>");
468 
469     EXPECT_FALSE(getInstance()->assemble());
470 }
471 
472 TEST_F(AssembleVintfTest, DeviceFrameworkMatrixMultiple) {
473     setFakeEnvs({{"POLICYVERS", "30"},
474                  {"PLATFORM_SEPOLICY_VERSION", "10000.0"},
475                  {"PLATFORM_SEPOLICY_COMPAT_VERSIONS", "26.0 27.0"},
476                  {"FRAMEWORK_VBMETA_VERSION", "1.0"},
477                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
478     getInstance()->setCheckInputStream(makeStream(gEmptyOutManifest));
479 
480     addInput("compatibility_matrix.foobar.xml",
481              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
482              "    <hal format=\"hidl\" optional=\"true\">\n"
483              "        <name>vendor.foo.bar</name>\n"
484              "        <version>1.0</version>\n"
485              "        <interface>\n"
486              "            <name>IFoo</name>\n"
487              "            <instance>default</instance>\n"
488              "        </interface>\n"
489              "    </hal>\n"
490              "</compatibility-matrix>");
491 
492     addInput("compatibility_matrix.bazquux.xml",
493              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
494              "    <hal format=\"hidl\" optional=\"true\">\n"
495              "        <name>vendor.baz.quux</name>\n"
496              "        <version>1.0</version>\n"
497              "        <interface>\n"
498              "            <name>IBaz</name>\n"
499              "            <instance>default</instance>\n"
500              "        </interface>\n"
501              "    </hal>\n"
502              "</compatibility-matrix>");
503 
504     EXPECT_TRUE(getInstance()->assemble());
505     EXPECT_IN(
506         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
507         "    <hal format=\"hidl\" optional=\"true\">\n"
508         "        <name>vendor.baz.quux</name>\n"
509         "        <version>1.0</version>\n"
510         "        <interface>\n"
511         "            <name>IBaz</name>\n"
512         "            <instance>default</instance>\n"
513         "        </interface>\n"
514         "    </hal>\n"
515         "    <hal format=\"hidl\" optional=\"true\">\n"
516         "        <name>vendor.foo.bar</name>\n"
517         "        <version>1.0</version>\n"
518         "        <interface>\n"
519         "            <name>IFoo</name>\n"
520         "            <instance>default</instance>\n"
521         "        </interface>\n"
522         "    </hal>\n"
523         "    <sepolicy>\n"
524         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
525         "        <sepolicy-version>26.0</sepolicy-version>\n"
526         "        <sepolicy-version>27.0</sepolicy-version>\n"
527         "        <sepolicy-version>10000.0</sepolicy-version>\n"
528         "    </sepolicy>\n"
529         "    <avb>\n"
530         "        <vbmeta-version>1.0</vbmeta-version>\n"
531         "    </avb>\n"
532         "</compatibility-matrix>",
533         getOutput());
534 }
535 
536 TEST_F(AssembleVintfTest, OutputFileMatrixTest) {
537     const std::string kFile = "file_name_1.xml";
538     const std::string kMatrix =
539         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\"/>";
540     addInput(kFile, kMatrix);
541     EXPECT_TRUE(getInstance()->assemble());
542     EXPECT_IN(kFile, getOutput());
543 }
544 
545 TEST_F(AssembleVintfTest, OutputFileManifestTest) {
546     const std::string kFile = "file_name_1.xml";
547     std::string kManifest = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\"/>";
548     addInput(kFile, kManifest);
549     EXPECT_TRUE(getInstance()->assemble());
550     EXPECT_IN(kFile, getOutput());
551 }
552 
553 TEST_F(AssembleVintfTest, AidlAndHidlNames) {
554     addInput("manifest1.xml",
555         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
556         "    <hal format=\"aidl\">\n"
557         "        <name>android.system.foo</name>\n"
558         "        <fqname>IFoo/default</fqname>\n"
559         "    </hal>\n"
560         "</manifest>\n");
561     addInput("manifest2.xml",
562         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
563         "    <hal format=\"hidl\">\n"
564         "        <name>android.system.foo</name>\n"
565         "        <transport>hwbinder</transport>\n"
566         "        <fqname>@1.0::IFoo/default</fqname>\n"
567         "    </hal>\n"
568         "</manifest>\n");
569     EXPECT_TRUE(getInstance()->assemble());
570     EXPECT_IN(
571         "    <hal format=\"aidl\">\n"
572         "        <name>android.system.foo</name>\n"
573         "        <fqname>IFoo/default</fqname>\n"
574         "    </hal>\n",
575         getOutput());
576     EXPECT_IN(
577         "    <hal format=\"hidl\">\n"
578         "        <name>android.system.foo</name>\n"
579         "        <transport>hwbinder</transport>\n"
580         "        <fqname>@1.0::IFoo/default</fqname>\n"
581         "    </hal>\n",
582         getOutput());
583 }
584 
585 // Merge kernel FCM from manually written device manifest and <config> from
586 // parsing kernel prebuilt.
587 TEST_F(AssembleVintfTest, MergeKernelFcmAndConfigs) {
588     addInput("manifest.xml",
589         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n"
590         "    <kernel target-level=\"2\"/>\n"
591         "</manifest>\n");
592     getInstance()->addKernelConfigInputStream({3, 18, 10}, "android-base.config",
593                                               makeStream("CONFIG_FOO=y"));
594     EXPECT_TRUE(getInstance()->assemble());
595     EXPECT_IN("<kernel version=\"3.18.10\" target-level=\"2\">", getOutput());
596 }
597 
598 TEST_F(AssembleVintfTest, NoAutoSetKernelFcm) {
599     addInput("manifest.xml",
600         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n"
601         "    <kernel version=\"3.18.10\"/>\n"
602         "</manifest>\n");
603     EXPECT_TRUE(getInstance()->assemble());
604     EXPECT_IN("<kernel version=\"3.18.10\"/>", getOutput());
605 }
606 
607 TEST_F(AssembleVintfTest, AutoSetKernelFcm) {
608     addInput("manifest.xml",
609         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"5\">\n"
610         "    <kernel version=\"5.4.10\"/>\n"
611         "</manifest>\n");
612     EXPECT_TRUE(getInstance()->assemble());
613     EXPECT_IN("<kernel version=\"5.4.10\" target-level=\"5\"/>", getOutput());
614 }
615 
616 TEST_F(AssembleVintfTest, NoAutoSetKernelFcmWithConfig) {
617     addInput("manifest.xml",
618         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\" />\n");
619     getInstance()->addKernelConfigInputStream({3, 18, 10}, "android-base.config",
620                                               makeStream("CONFIG_FOO=y"));
621     EXPECT_TRUE(getInstance()->assemble());
622     EXPECT_IN("<kernel version=\"3.18.10\">", getOutput());
623 }
624 
625 TEST_F(AssembleVintfTest, AutoSetKernelFcmWithConfig) {
626     addInput("manifest.xml",
627         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"5\" />\n");
628     getInstance()->addKernelConfigInputStream({5, 4, 10}, "android-base.config",
629                                               makeStream("CONFIG_FOO=y"));
630     EXPECT_TRUE(getInstance()->assemble());
631     EXPECT_IN("<kernel version=\"5.4.10\" target-level=\"5\">", getOutput());
632 }
633 
634 // Automatically add kernel FCM when parsing framework matrix for a single FCM version.
635 TEST_F(AssembleVintfTest, AutoSetMatrixKernelFcm) {
636     addInput("compatibility_matrix.xml",
637         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\"/>\n"
638     );
639     getInstance()->addKernelConfigInputStream({3, 18, 10}, "android-base.config",
640                                               makeStream(""));
641     EXPECT_TRUE(getInstance()->assemble());
642     EXPECT_IN("<kernel version=\"3.18.10\" level=\"1\"/>", getOutput());
643 }
644 
645 
646 TEST_F(AssembleVintfTest, WithKernelRequirements) {
647     setFakeEnvs({{"POLICYVERS", "30"},
648                  {"PLATFORM_SEPOLICY_VERSION", "10000.0"},
649                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
650     addInput("compatibility_matrix.xml",
651         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
652         "    <kernel version=\"3.18.1\" level=\"1\">\n"
653         "        <config>\n"
654         "            <key>CONFIG_FOO</key>\n"
655         "            <value type=\"tristate\">y</value>\n"
656         "        </config>\n"
657         "    </kernel>\n"
658         "</compatibility-matrix>\n");
659     getInstance()->setCheckInputStream(makeStream(
660         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n"
661         "    <kernel target-level=\"1\" version=\"3.18.0\"/>\n"
662         "    <sepolicy>\n"
663         "        <version>10000.0</version>\n"
664         "    </sepolicy>\n"
665         "</manifest>\n"));
666 
667     EXPECT_FALSE(getInstance()->assemble());
668 }
669 
670 TEST_F(AssembleVintfTest, NoKernelRequirements) {
671     setFakeEnvs({{"POLICYVERS", "30"},
672                  {"PLATFORM_SEPOLICY_VERSION", "10000.0"},
673                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
674     addInput("compatibility_matrix.xml",
675         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
676         "    <kernel version=\"3.18.0\" level=\"1\"/>\n"
677         "</compatibility-matrix>\n");
678     getInstance()->setCheckInputStream(makeStream(
679         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n"
680         "    <kernel target-level=\"1\"/>\n"
681         "    <sepolicy>\n"
682         "        <version>10000.0</version>\n"
683         "    </sepolicy>\n"
684         "</manifest>\n"));
685 
686     EXPECT_TRUE(getInstance()->setNoKernelRequirements());
687     EXPECT_TRUE(getInstance()->assemble());
688 }
689 
690 // clang-format on
691 
692 }  // namespace vintf
693 }  // namespace android
694