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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include "DisplayHardware/DisplayIdentification.h"
21 
22 namespace android {
23 namespace {
24 
25 const unsigned char kInternalEdid[] =
26         "\x00\xff\xff\xff\xff\xff\xff\x00\x4c\xa3\x42\x31\x00\x00\x00\x00"
27         "\x00\x15\x01\x03\x80\x1a\x10\x78\x0a\xd3\xe5\x95\x5c\x60\x90\x27"
28         "\x19\x50\x54\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
29         "\x01\x01\x01\x01\x01\x01\x9e\x1b\x00\xa0\x50\x20\x12\x30\x10\x30"
30         "\x13\x00\x05\xa3\x10\x00\x00\x19\x00\x00\x00\x0f\x00\x00\x00\x00"
31         "\x00\x00\x00\x00\x00\x23\x87\x02\x64\x00\x00\x00\x00\xfe\x00\x53"
32         "\x41\x4d\x53\x55\x4e\x47\x0a\x20\x20\x20\x20\x20\x00\x00\x00\xfe"
33         "\x00\x31\x32\x31\x41\x54\x31\x31\x2d\x38\x30\x31\x0a\x20\x00\x45";
34 
35 const unsigned char kExternalEdid[] =
36         "\x00\xff\xff\xff\xff\xff\xff\x00\x22\xf0\x6c\x28\x01\x01\x01\x01"
37         "\x02\x16\x01\x04\xb5\x40\x28\x78\xe2\x8d\x85\xad\x4f\x35\xb1\x25"
38         "\x0e\x50\x54\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
39         "\x01\x01\x01\x01\x01\x01\xe2\x68\x00\xa0\xa0\x40\x2e\x60\x30\x20"
40         "\x36\x00\x81\x90\x21\x00\x00\x1a\xbc\x1b\x00\xa0\x50\x20\x17\x30"
41         "\x30\x20\x36\x00\x81\x90\x21\x00\x00\x1a\x00\x00\x00\xfc\x00\x48"
42         "\x50\x20\x5a\x52\x33\x30\x77\x0a\x20\x20\x20\x20\x00\x00\x00\xff"
43         "\x00\x43\x4e\x34\x32\x30\x32\x31\x33\x37\x51\x0a\x20\x20\x00\x71";
44 
45 // Extended EDID with timing extension.
46 const unsigned char kExternalEedid[] =
47         "\x00\xff\xff\xff\xff\xff\xff\x00\x4c\x2d\xfe\x08\x00\x00\x00\x00"
48         "\x29\x15\x01\x03\x80\x10\x09\x78\x0a\xee\x91\xa3\x54\x4c\x99\x26"
49         "\x0f\x50\x54\xbd\xef\x80\x71\x4f\x81\xc0\x81\x00\x81\x80\x95\x00"
50         "\xa9\xc0\xb3\x00\x01\x01\x02\x3a\x80\x18\x71\x38\x2d\x40\x58\x2c"
51         "\x45\x00\xa0\x5a\x00\x00\x00\x1e\x66\x21\x56\xaa\x51\x00\x1e\x30"
52         "\x46\x8f\x33\x00\xa0\x5a\x00\x00\x00\x1e\x00\x00\x00\xfd\x00\x18"
53         "\x4b\x0f\x51\x17\x00\x0a\x20\x20\x20\x20\x20\x20\x00\x00\x00\xfc"
54         "\x00\x53\x41\x4d\x53\x55\x4e\x47\x0a\x20\x20\x20\x20\x20\x01\x1d"
55         "\x02\x03\x1f\xf1\x47\x90\x04\x05\x03\x20\x22\x07\x23\x09\x07\x07"
56         "\x83\x01\x00\x00\xe2\x00\x0f\x67\x03\x0c\x00\x20\x00\xb8\x2d\x01"
57         "\x1d\x80\x18\x71\x1c\x16\x20\x58\x2c\x25\x00\xa0\x5a\x00\x00\x00"
58         "\x9e\x01\x1d\x00\x72\x51\xd0\x1e\x20\x6e\x28\x55\x00\xa0\x5a\x00"
59         "\x00\x00\x1e\x8c\x0a\xd0\x8a\x20\xe0\x2d\x10\x10\x3e\x96\x00\xa0"
60         "\x5a\x00\x00\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
61         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
62         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6";
63 
64 template <size_t N>
asDisplayIdentificationData(const unsigned char (& bytes)[N])65 DisplayIdentificationData asDisplayIdentificationData(const unsigned char (&bytes)[N]) {
66     return DisplayIdentificationData(bytes, bytes + N - 1);
67 }
68 
69 } // namespace
70 
getInternalEdid()71 const DisplayIdentificationData& getInternalEdid() {
72     static const DisplayIdentificationData data = asDisplayIdentificationData(kInternalEdid);
73     return data;
74 }
75 
getExternalEdid()76 const DisplayIdentificationData& getExternalEdid() {
77     static const DisplayIdentificationData data = asDisplayIdentificationData(kExternalEdid);
78     return data;
79 }
80 
getExternalEedid()81 const DisplayIdentificationData& getExternalEedid() {
82     static const DisplayIdentificationData data = asDisplayIdentificationData(kExternalEedid);
83     return data;
84 }
85 
TEST(DisplayIdentificationTest,isEdid)86 TEST(DisplayIdentificationTest, isEdid) {
87     EXPECT_FALSE(isEdid({}));
88 
89     EXPECT_TRUE(isEdid(getInternalEdid()));
90     EXPECT_TRUE(isEdid(getExternalEdid()));
91     EXPECT_TRUE(isEdid(getExternalEedid()));
92 }
93 
TEST(DisplayIdentificationTest,parseEdid)94 TEST(DisplayIdentificationTest, parseEdid) {
95     auto edid = parseEdid(getInternalEdid());
96     ASSERT_TRUE(edid);
97     EXPECT_EQ(0x4ca3u, edid->manufacturerId);
98     EXPECT_STREQ("SEC", edid->pnpId.data());
99     // ASCII text should be used as fallback if display name and serial number are missing.
100     EXPECT_EQ("121AT11-801", edid->displayName);
101 
102     edid = parseEdid(getExternalEdid());
103     ASSERT_TRUE(edid);
104     EXPECT_EQ(0x22f0u, edid->manufacturerId);
105     EXPECT_STREQ("HWP", edid->pnpId.data());
106     EXPECT_EQ("HP ZR30w", edid->displayName);
107 
108     edid = parseEdid(getExternalEedid());
109     ASSERT_TRUE(edid);
110     EXPECT_EQ(0x4c2du, edid->manufacturerId);
111     EXPECT_STREQ("SAM", edid->pnpId.data());
112     EXPECT_EQ("SAMSUNG", edid->displayName);
113 }
114 
TEST(DisplayIdentificationTest,parseInvalidEdid)115 TEST(DisplayIdentificationTest, parseInvalidEdid) {
116     EXPECT_FALSE(isEdid({}));
117     EXPECT_FALSE(parseEdid({}));
118 
119     // Display name must be printable.
120     auto data = getExternalEdid();
121     data[97] = '\x1b';
122     auto edid = parseEdid(data);
123     ASSERT_TRUE(edid);
124     // Serial number should be used as fallback if display name is invalid.
125     EXPECT_EQ("CN4202137Q", edid->displayName);
126 
127     // Parsing should succeed even if EDID is truncated.
128     data.pop_back();
129     edid = parseEdid(data);
130     ASSERT_TRUE(edid);
131     EXPECT_EQ("CN4202137Q", edid->displayName);
132 }
133 
TEST(DisplayIdentificationTest,getPnpId)134 TEST(DisplayIdentificationTest, getPnpId) {
135     EXPECT_FALSE(getPnpId(0));
136     EXPECT_FALSE(getPnpId(static_cast<uint16_t>(-1)));
137 
138     EXPECT_STREQ("SEC", getPnpId(0x4ca3u).value_or(PnpId{}).data());
139     EXPECT_STREQ("HWP", getPnpId(0x22f0u).value_or(PnpId{}).data());
140     EXPECT_STREQ("SAM", getPnpId(0x4c2du).value_or(PnpId{}).data());
141 }
142 
TEST(DisplayIdentificationTest,parseDisplayIdentificationData)143 TEST(DisplayIdentificationTest, parseDisplayIdentificationData) {
144     const auto primaryInfo = parseDisplayIdentificationData(0, getInternalEdid());
145     ASSERT_TRUE(primaryInfo);
146 
147     const auto secondaryInfo = parseDisplayIdentificationData(1, getExternalEdid());
148     ASSERT_TRUE(secondaryInfo);
149 
150     const auto tertiaryInfo = parseDisplayIdentificationData(2, getExternalEedid());
151     ASSERT_TRUE(tertiaryInfo);
152 
153     // Display IDs should be unique.
154     EXPECT_NE(primaryInfo->id, secondaryInfo->id);
155     EXPECT_NE(primaryInfo->id, tertiaryInfo->id);
156     EXPECT_NE(secondaryInfo->id, tertiaryInfo->id);
157 }
158 
TEST(DisplayIdentificationTest,getFallbackDisplayId)159 TEST(DisplayIdentificationTest, getFallbackDisplayId) {
160     // Manufacturer ID should be invalid.
161     ASSERT_FALSE(getPnpId(getFallbackDisplayId(0)));
162     ASSERT_FALSE(getPnpId(getFallbackDisplayId(0xffu)));
163 }
164 
TEST(DisplayIdentificationTest,getVirtualDisplayId)165 TEST(DisplayIdentificationTest, getVirtualDisplayId) {
166     // Manufacturer ID should be invalid.
167     ASSERT_FALSE(getPnpId(getVirtualDisplayId(0)));
168     ASSERT_FALSE(getPnpId(getVirtualDisplayId(0xffff'ffffu)));
169 }
170 
171 } // namespace android
172