Install toolchain into tools_bin rather than /usr/local
[nativeclient.git] / ncv / nacl_cpuid_test.c
blob6673794dec97ca96f536cfab2cdf277aa15ad4f9
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * nacl_cpuid_test.c
34 * test main and subroutines for nacl_cpuid
36 #include "native_client/include/portability.h"
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include "native_client/ncv/nacl_cpuid.h"
41 #include "native_client/ncv/nacl_cpuwhitelist.h"
43 #define dprint(args) printf args ;
45 #define sizeofA(array) (sizeof(array)/sizeof(array[0]))
46 static void CPUIDWhitelistUnitTests() {
47 extern bool xNaCl_CPUIsWhitelisted(const char *myid);
48 size_t i;
50 /* check lengths */
51 for (i = 0; i < sizeofA(nacl_cpuwhitelist); i++) {
52 if (strlen(nacl_cpuwhitelist[i]) != kCPUWhitelistIDLength - 1) {
53 fprintf(stderr, "ERROR: whitelist entry %"PRIdS" bad length\n", i);
54 exit(-1);
57 /* check order */
58 for (i = 1; i < sizeofA(nacl_cpuwhitelist); i++) {
59 if (strncmp(nacl_cpuwhitelist[i-1], nacl_cpuwhitelist[i],
60 kCPUWhitelistIDLength) >= 0) {
61 fprintf(stderr, "ERROR: whitelist order defect at index %"PRIdS"\n", i);
62 exit(-1);
65 if (!xNaCl_CPUIsWhitelisted(" FakeEntry0000000000")) {
66 fprintf(stderr, "ERROR: whitelist search 1 failed\n");
67 exit(-1);
69 if (!xNaCl_CPUIsWhitelisted("GenuineIntel00000f43")) {
70 fprintf(stderr, "ERROR: whitelist search 2 failed\n");
71 exit(-1);
73 if (!xNaCl_CPUIsWhitelisted("zFakeEntry0000000000")) {
74 fprintf(stderr, "ERROR: whitelist search 3 failed\n");
75 exit(-1);
77 if (xNaCl_CPUIsWhitelisted("a")) {
78 fprintf(stderr, "ERROR: whitelist search 4 didn't fail\n");
79 exit(-1);
81 if (xNaCl_CPUIsWhitelisted("")) {
82 fprintf(stderr, "ERROR: whitelist search 5 didn't fail\n");
83 exit(-1);
85 if (xNaCl_CPUIsWhitelisted("zFakeEntry0000000001")) {
86 fprintf(stderr, "ERROR: whitelist search 6 didn't fail\n");
87 exit(-1);
89 if (xNaCl_CPUIsWhitelisted("zFakeEntry00000000000")) {
90 fprintf(stderr, "ERROR: whitelist search 7 didn't fail\n");
91 exit(-1);
93 printf("All whitelist unit tests passed\n");
96 int main(int argc, char *argv[]) {
97 CPUFeatures fv;
98 extern char *GetCPUWhitelistID();
99 extern bool NaCl_CPUIsWhitelisted();
101 GetCPUFeatures(&fv);
102 if (fv.f_386) dprint(("This is a 386 compatible computer\n"));
103 printf("white list ID: %s\n", GetCPUWhitelistID());
104 if (NaCl_CPUIsWhitelisted()) {
105 printf("this CPU is on the whitelist\n");
106 } else {
107 printf("this CPU is NOT on the whitelist\n");
110 CPUIDWhitelistUnitTests();
111 return 0;