xref: /aosp_15_r20/external/boringssl/src/crypto/cpu_arm_linux_test.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "cpu_arm_linux.h"
16 
17 #include <string.h>
18 
19 #include <gtest/gtest.h>
20 
21 
TEST(ARMLinuxTest,CPUInfo)22 TEST(ARMLinuxTest, CPUInfo) {
23   struct CPUInfoTest {
24     const char *cpuinfo;
25     unsigned long hwcap2;
26   } kTests[] = {
27       // Nexus 4 from https://crbug.com/341598#c43
28       {
29           "Processor       : ARMv7 Processor rev 2 (v7l)\n"
30           "processor       : 0\n"
31           "BogoMIPS        : 13.53\n"
32           "\n"
33           "processor       : 1\n"
34           "BogoMIPS        : 13.53\n"
35           "\n"
36           "processor       : 2\n"
37           "BogoMIPS        : 13.53\n"
38           "\n"
39           "processor       : 3\n"
40           "BogoMIPS        : 13.53\n"
41           "\n"
42           "Features        : swp half thumb fastmult vfp edsp neon vfpv3 tls "
43           "vfpv4 \n"
44           "CPU implementer : 0x51\n"
45           "CPU architecture: 7\n"
46           "CPU variant     : 0x0\n"
47           "CPU part        : 0x06f\n"
48           "CPU revision    : 2\n"
49           "\n"
50           "Hardware        : QCT APQ8064 MAKO\n"
51           "Revision        : 000b\n"
52           "Serial          : 0000000000000000\n",
53           0,
54       },
55       // Pixel 2 (truncated slightly)
56       {
57           "Processor       : AArch64 Processor rev 1 (aarch64)\n"
58           "processor       : 0\n"
59           "BogoMIPS        : 38.00\n"
60           "Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
61           "CPU implementer : 0x51\n"
62           "CPU architecture: 8\n"
63           "CPU variant     : 0xa\n"
64           "CPU part        : 0x801\n"
65           "CPU revision    : 4\n"
66           "\n"
67           "processor       : 1\n"
68           "BogoMIPS        : 38.00\n"
69           "Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
70           "CPU implementer : 0x51\n"
71           "CPU architecture: 8\n"
72           "CPU variant     : 0xa\n"
73           "CPU part        : 0x801\n"
74           "CPU revision    : 4\n"
75           "\n"
76           "processor       : 2\n"
77           "BogoMIPS        : 38.00\n"
78           "Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
79           "CPU implementer : 0x51\n"
80           "CPU architecture: 8\n"
81           "CPU variant     : 0xa\n"
82           "CPU part        : 0x801\n"
83           "CPU revision    : 4\n"
84           "\n"
85           "processor       : 3\n"
86           "BogoMIPS        : 38.00\n"
87           "Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
88           "CPU implementer : 0x51\n"
89           "CPU architecture: 8\n"
90           "CPU variant     : 0xa\n"
91           "CPU part        : 0x801\n"
92           "CPU revision    : 4\n"
93           // (Extra processors omitted.)
94           "\n"
95           "Hardware        : Qualcomm Technologies, Inc MSM8998\n",
96           HWCAP2_AES | HWCAP2_PMULL | HWCAP2_SHA1 | HWCAP2_SHA2,
97       },
98       // Garbage should be tolerated.
99       {
100           "Blah blah blah this is definitely an ARM CPU",
101           0,
102       },
103       // A hypothetical ARMv8 CPU without crc32 (and thus no trailing space
104       // after the last crypto entry).
105       {
106           "Features        : aes pmull sha1 sha2\n"
107           "CPU architecture: 8\n",
108           HWCAP2_AES | HWCAP2_PMULL | HWCAP2_SHA1 | HWCAP2_SHA2,
109       },
110       // Various combinations of ARMv8 flags.
111       {
112           "Features        : aes sha1 sha2\n"
113           "CPU architecture: 8\n",
114           HWCAP2_AES | HWCAP2_SHA1 | HWCAP2_SHA2,
115       },
116       {
117           "Features        : pmull sha2\n"
118           "CPU architecture: 8\n",
119           HWCAP2_PMULL | HWCAP2_SHA2,
120       },
121       {
122           "Features        : aes aes   aes not_aes aes aes \n"
123           "CPU architecture: 8\n",
124           HWCAP2_AES,
125       },
126       {
127           "Features        : \n"
128           "CPU architecture: 8\n",
129           0,
130       },
131       {
132           "Features        : nothing\n"
133           "CPU architecture: 8\n",
134           0,
135       },
136       // If opening /proc/cpuinfo fails, we process the empty string.
137       {
138           "",
139           0,
140       },
141   };
142 
143   for (const auto &t : kTests) {
144     SCOPED_TRACE(t.cpuinfo);
145     STRING_PIECE sp = {t.cpuinfo, strlen(t.cpuinfo)};
146     EXPECT_EQ(t.hwcap2, crypto_get_arm_hwcap2_from_cpuinfo(&sp));
147   }
148 }
149