TEST(Tokenizer, CheckExpectedTokenValue) { Tokenizer::Token t; // Check expected token value test Tokenizer p(NS_LITERAL_CSTRING("blue velvet")); EXPECT_FALSE(p.Check(Tokenizer::TOKEN_INTEGER, t)); EXPECT_TRUE(p.Check(Tokenizer::TOKEN_WORD, t)); EXPECT_TRUE(t.AsString() == "blue"); EXPECT_FALSE(p.Check(Tokenizer::TOKEN_WORD, t)); EXPECT_TRUE(p.CheckWhite()); EXPECT_TRUE(p.Check(Tokenizer::TOKEN_WORD, t)); EXPECT_TRUE(t.AsString() == "velvet"); EXPECT_TRUE(p.CheckEOF()); EXPECT_FALSE(p.Next(t)); }
TEST(Tokenizer, Main) { Tokenizer::Token t; // Synthetic code-specific test Tokenizer p(NS_LITERAL_CSTRING("test123 ,15 \t*\r\n%xx,-15\r\r")); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_WORD); EXPECT_TRUE(t.AsString() == "test123"); Tokenizer::Token u; EXPECT_FALSE(p.Check(u)); EXPECT_FALSE(p.CheckChar('!')); EXPECT_FALSE(p.Check(Tokenizer::Token::Number(123))); EXPECT_TRUE(p.CheckWhite()); EXPECT_TRUE(p.CheckChar(',')); EXPECT_TRUE(p.Check(Tokenizer::Token::Number(15))); p.Rollback(); EXPECT_TRUE(p.Check(Tokenizer::Token::Number(15))); p.Rollback(); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_INTEGER); EXPECT_TRUE(t.AsInteger() == 15); EXPECT_FALSE(p.CheckChar(IsOperator)); EXPECT_TRUE(p.CheckWhite()); p.SkipWhites(); EXPECT_FALSE(p.CheckWhite()); p.Rollback(); EXPECT_TRUE(p.CheckWhite()); EXPECT_TRUE(p.CheckWhite()); p.Record(Tokenizer::EXCLUDE_LAST); EXPECT_TRUE(p.CheckChar(IsOperator)); p.Rollback(); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_CHAR); EXPECT_TRUE(t.AsChar() == '*'); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_EOL); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_CHAR); EXPECT_TRUE(t.AsChar() == '%'); nsAutoCString claim; p.Claim(claim, Tokenizer::EXCLUDE_LAST); EXPECT_TRUE(claim == "*\r\n"); p.Claim(claim, Tokenizer::INCLUDE_LAST); EXPECT_TRUE(claim == "*\r\n%"); p.Rollback(); EXPECT_TRUE(p.CheckChar('%')); p.Record(Tokenizer::INCLUDE_LAST); EXPECT_FALSE(p.CheckWord("xy")); EXPECT_TRUE(p.CheckWord("xx")); p.Claim(claim, Tokenizer::INCLUDE_LAST); EXPECT_TRUE(claim == "%xx"); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_CHAR); EXPECT_TRUE(t.AsChar() == ','); EXPECT_TRUE(p.CheckChar('-')); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_INTEGER); EXPECT_TRUE(t.AsInteger() == 15); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_EOL); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_EOL); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_EOF); EXPECT_FALSE(p.Next(t)); p.Rollback(); EXPECT_TRUE(p.Next(t)); EXPECT_TRUE(t.Type() == Tokenizer::TOKEN_EOF); EXPECT_FALSE(p.Next(t)); p.Rollback(); EXPECT_TRUE(p.CheckEOF()); EXPECT_FALSE(p.CheckEOF()); }
nsresult nsSystemInfo::Init() { nsresult rv; static const struct { PRSysInfo cmd; const char* name; } items[] = { { PR_SI_SYSNAME, "name" }, { PR_SI_HOSTNAME, "host" }, { PR_SI_ARCHITECTURE, "arch" }, { PR_SI_RELEASE, "version" } }; for (uint32_t i = 0; i < (sizeof(items) / sizeof(items[0])); i++) { char buf[SYS_INFO_BUFFER_LENGTH]; if (PR_GetSystemInfo(items[i].cmd, buf, sizeof(buf)) == PR_SUCCESS) { rv = SetPropertyAsACString(NS_ConvertASCIItoUTF16(items[i].name), nsDependentCString(buf)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } else { NS_WARNING("PR_GetSystemInfo failed"); } } rv = SetPropertyAsBool(NS_ConvertASCIItoUTF16("hasWindowsTouchInterface"), false); NS_ENSURE_SUCCESS(rv, rv); // Additional informations not available through PR_GetSystemInfo. SetInt32Property(NS_LITERAL_STRING("pagesize"), PR_GetPageSize()); SetInt32Property(NS_LITERAL_STRING("pageshift"), PR_GetPageShift()); SetInt32Property(NS_LITERAL_STRING("memmapalign"), PR_GetMemMapAlignment()); SetUint64Property(NS_LITERAL_STRING("memsize"), PR_GetPhysicalMemorySize()); SetUint32Property(NS_LITERAL_STRING("umask"), nsSystemInfo::gUserUmask); uint64_t virtualMem = 0; nsAutoCString cpuVendor; int cpuSpeed = -1; int cpuFamily = -1; int cpuModel = -1; int cpuStepping = -1; int logicalCPUs = -1; int physicalCPUs = -1; int cacheSizeL2 = -1; int cacheSizeL3 = -1; #if defined (XP_WIN) // Virtual memory: MEMORYSTATUSEX memStat; memStat.dwLength = sizeof(memStat); if (GlobalMemoryStatusEx(&memStat)) { virtualMem = memStat.ullTotalVirtual; } // CPU speed HKEY key; static const WCHAR keyName[] = L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName , 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { DWORD data, len, vtype; len = sizeof(data); if (RegQueryValueEx(key, L"~Mhz", 0, 0, reinterpret_cast<LPBYTE>(&data), &len) == ERROR_SUCCESS) { cpuSpeed = static_cast<int>(data); } // Limit to 64 double byte characters, should be plenty, but create // a buffer one larger as the result may not be null terminated. If // it is more than 64, we will not get the value. wchar_t cpuVendorStr[64+1]; len = sizeof(cpuVendorStr)-2; if (RegQueryValueExW(key, L"VendorIdentifier", 0, &vtype, reinterpret_cast<LPBYTE>(cpuVendorStr), &len) == ERROR_SUCCESS && vtype == REG_SZ && len % 2 == 0 && len > 1) { cpuVendorStr[len/2] = 0; // In case it isn't null terminated CopyUTF16toUTF8(nsDependentString(cpuVendorStr), cpuVendor); } RegCloseKey(key); } // Other CPU attributes: SYSTEM_INFO si; GetNativeSystemInfo(&si); logicalCPUs = si.dwNumberOfProcessors; GetProcessorInformation(&physicalCPUs, &cacheSizeL2, &cacheSizeL3); if (physicalCPUs <= 0) { physicalCPUs = logicalCPUs; } cpuFamily = si.wProcessorLevel; cpuModel = si.wProcessorRevision >> 8; cpuStepping = si.wProcessorRevision & 0xFF; #elif defined (XP_MACOSX) // CPU speed uint64_t sysctlValue64 = 0; uint32_t sysctlValue32 = 0; size_t len = 0; len = sizeof(sysctlValue64); if (!sysctlbyname("hw.cpufrequency_max", &sysctlValue64, &len, NULL, 0)) { cpuSpeed = static_cast<int>(sysctlValue64/1000000); } MOZ_ASSERT(sizeof(sysctlValue64) == len); len = sizeof(sysctlValue32); if (!sysctlbyname("hw.physicalcpu_max", &sysctlValue32, &len, NULL, 0)) { physicalCPUs = static_cast<int>(sysctlValue32); } MOZ_ASSERT(sizeof(sysctlValue32) == len); len = sizeof(sysctlValue32); if (!sysctlbyname("hw.logicalcpu_max", &sysctlValue32, &len, NULL, 0)) { logicalCPUs = static_cast<int>(sysctlValue32); } MOZ_ASSERT(sizeof(sysctlValue32) == len); len = sizeof(sysctlValue64); if (!sysctlbyname("hw.l2cachesize", &sysctlValue64, &len, NULL, 0)) { cacheSizeL2 = static_cast<int>(sysctlValue64/1024); } MOZ_ASSERT(sizeof(sysctlValue64) == len); len = sizeof(sysctlValue64); if (!sysctlbyname("hw.l3cachesize", &sysctlValue64, &len, NULL, 0)) { cacheSizeL3 = static_cast<int>(sysctlValue64/1024); } MOZ_ASSERT(sizeof(sysctlValue64) == len); if (!sysctlbyname("machdep.cpu.vendor", NULL, &len, NULL, 0)) { char* cpuVendorStr = new char[len]; if (!sysctlbyname("machdep.cpu.vendor", cpuVendorStr, &len, NULL, 0)) { cpuVendor = cpuVendorStr; } delete [] cpuVendorStr; } len = sizeof(sysctlValue32); if (!sysctlbyname("machdep.cpu.family", &sysctlValue32, &len, NULL, 0)) { cpuFamily = static_cast<int>(sysctlValue32); } MOZ_ASSERT(sizeof(sysctlValue32) == len); len = sizeof(sysctlValue32); if (!sysctlbyname("machdep.cpu.model", &sysctlValue32, &len, NULL, 0)) { cpuModel = static_cast<int>(sysctlValue32); } MOZ_ASSERT(sizeof(sysctlValue32) == len); len = sizeof(sysctlValue32); if (!sysctlbyname("machdep.cpu.stepping", &sysctlValue32, &len, NULL, 0)) { cpuStepping = static_cast<int>(sysctlValue32); } MOZ_ASSERT(sizeof(sysctlValue32) == len); #elif defined (MOZ_WIDGET_GTK) // Get vendor, family, model, stepping, physical cores, L3 cache size // from /proc/cpuinfo file { std::map<nsCString, nsCString> keyValuePairs; SimpleParseKeyValuePairs("/proc/cpuinfo", keyValuePairs); // cpuVendor from "vendor_id" cpuVendor.Assign(keyValuePairs[NS_LITERAL_CSTRING("vendor_id")]); { // cpuFamily from "cpu family" Tokenizer::Token t; Tokenizer p(keyValuePairs[NS_LITERAL_CSTRING("cpu family")]); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { cpuFamily = static_cast<int>(t.AsInteger()); } } { // cpuModel from "model" Tokenizer::Token t; Tokenizer p(keyValuePairs[NS_LITERAL_CSTRING("model")]); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { cpuModel = static_cast<int>(t.AsInteger()); } } { // cpuStepping from "stepping" Tokenizer::Token t; Tokenizer p(keyValuePairs[NS_LITERAL_CSTRING("stepping")]); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { cpuStepping = static_cast<int>(t.AsInteger()); } } { // physicalCPUs from "cpu cores" Tokenizer::Token t; Tokenizer p(keyValuePairs[NS_LITERAL_CSTRING("cpu cores")]); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { physicalCPUs = static_cast<int>(t.AsInteger()); } } { // cacheSizeL3 from "cache size" Tokenizer::Token t; Tokenizer p(keyValuePairs[NS_LITERAL_CSTRING("cache size")]); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { cacheSizeL3 = static_cast<int>(t.AsInteger()); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_WORD && t.AsString() != NS_LITERAL_CSTRING("KB")) { // If we get here, there was some text after the cache size value // and that text was not KB. For now, just don't report the // L3 cache. cacheSizeL3 = -1; } } } } { // Get cpuSpeed from another file. std::ifstream input("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"); std::string line; if (getline(input, line)) { Tokenizer::Token t; Tokenizer p(line.c_str()); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { cpuSpeed = static_cast<int>(t.AsInteger()/1000); } } } { // Get cacheSizeL2 from yet another file std::ifstream input("/sys/devices/system/cpu/cpu0/cache/index2/size"); std::string line; if (getline(input, line)) { Tokenizer::Token t; Tokenizer p(line.c_str(), nullptr, "K"); if (p.Next(t) && t.Type() == Tokenizer::TOKEN_INTEGER && t.AsInteger() <= INT32_MAX) { cacheSizeL2 = static_cast<int>(t.AsInteger()); } } } SetInt32Property(NS_LITERAL_STRING("cpucount"), PR_GetNumberOfProcessors()); #else SetInt32Property(NS_LITERAL_STRING("cpucount"), PR_GetNumberOfProcessors()); #endif if (virtualMem) SetUint64Property(NS_LITERAL_STRING("virtualmemsize"), virtualMem); if (cpuSpeed >= 0) SetInt32Property(NS_LITERAL_STRING("cpuspeed"), cpuSpeed); if (!cpuVendor.IsEmpty()) SetPropertyAsACString(NS_LITERAL_STRING("cpuvendor"), cpuVendor); if (cpuFamily >= 0) SetInt32Property(NS_LITERAL_STRING("cpufamily"), cpuFamily); if (cpuModel >= 0) SetInt32Property(NS_LITERAL_STRING("cpumodel"), cpuModel); if (cpuStepping >= 0) SetInt32Property(NS_LITERAL_STRING("cpustepping"), cpuStepping); if (logicalCPUs >= 0) SetInt32Property(NS_LITERAL_STRING("cpucount"), logicalCPUs); if (physicalCPUs >= 0) SetInt32Property(NS_LITERAL_STRING("cpucores"), physicalCPUs); if (cacheSizeL2 >= 0) SetInt32Property(NS_LITERAL_STRING("cpucachel2"), cacheSizeL2); if (cacheSizeL3 >= 0) SetInt32Property(NS_LITERAL_STRING("cpucachel3"), cacheSizeL3); for (uint32_t i = 0; i < ArrayLength(cpuPropItems); i++) { rv = SetPropertyAsBool(NS_ConvertASCIItoUTF16(cpuPropItems[i].name), cpuPropItems[i].propfun()); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } #ifdef XP_WIN BOOL isWow64; BOOL gotWow64Value = IsWow64Process(GetCurrentProcess(), &isWow64); NS_WARN_IF_FALSE(gotWow64Value, "IsWow64Process failed"); if (gotWow64Value) { rv = SetPropertyAsBool(NS_LITERAL_STRING("isWow64"), !!isWow64); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } if (NS_FAILED(GetProfileHDDInfo())) { // We might have been called before profile-do-change. We'll observe that // event so that we can fill this in later. nsCOMPtr<nsIObserverService> obsService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } rv = obsService->AddObserver(this, "profile-do-change", false); if (NS_FAILED(rv)) { return rv; } } nsAutoCString hddModel, hddRevision; if (NS_SUCCEEDED(GetHDDInfo(NS_GRE_DIR, hddModel, hddRevision))) { rv = SetPropertyAsACString(NS_LITERAL_STRING("binHDDModel"), hddModel); NS_ENSURE_SUCCESS(rv, rv); rv = SetPropertyAsACString(NS_LITERAL_STRING("binHDDRevision"), hddRevision); NS_ENSURE_SUCCESS(rv, rv); } if (NS_SUCCEEDED(GetHDDInfo(NS_WIN_WINDOWS_DIR, hddModel, hddRevision))) { rv = SetPropertyAsACString(NS_LITERAL_STRING("winHDDModel"), hddModel); NS_ENSURE_SUCCESS(rv, rv); rv = SetPropertyAsACString(NS_LITERAL_STRING("winHDDRevision"), hddRevision); NS_ENSURE_SUCCESS(rv, rv); } nsAutoString countryCode; if (NS_SUCCEEDED(GetCountryCode(countryCode))) { rv = SetPropertyAsAString(NS_LITERAL_STRING("countryCode"), countryCode); NS_ENSURE_SUCCESS(rv, rv); } uint32_t installYear = 0; if (NS_SUCCEEDED(GetInstallYear(installYear))) { rv = SetPropertyAsUint32(NS_LITERAL_STRING("installYear"), installYear); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } #endif #if defined(XP_MACOSX) nsAutoString countryCode; if (NS_SUCCEEDED(GetSelectedCityInfo(countryCode))) { rv = SetPropertyAsAString(NS_LITERAL_STRING("countryCode"), countryCode); NS_ENSURE_SUCCESS(rv, rv); } #endif #if defined(MOZ_WIDGET_GTK) // This must be done here because NSPR can only separate OS's when compiled, not libraries. // 64 bytes is going to be well enough for "GTK " followed by 3 integers // separated with dots. char gtkver[64]; ssize_t gtkver_len = 0; #if MOZ_WIDGET_GTK == 2 extern int gtk_read_end_of_the_pipe; if (gtk_read_end_of_the_pipe != -1) { do { gtkver_len = read(gtk_read_end_of_the_pipe, >kver, sizeof(gtkver)); } while (gtkver_len < 0 && errno == EINTR); close(gtk_read_end_of_the_pipe); } #endif if (gtkver_len <= 0) { gtkver_len = snprintf(gtkver, sizeof(gtkver), "GTK %u.%u.%u", gtk_major_version, gtk_minor_version, gtk_micro_version); } nsAutoCString secondaryLibrary; if (gtkver_len > 0) { secondaryLibrary.Append(nsDependentCSubstring(gtkver, gtkver_len)); } void* libpulse = dlopen("libpulse.so.0", RTLD_LAZY); const char* libpulseVersion = "not-available"; if (libpulse) { auto pa_get_library_version = reinterpret_cast<const char* (*)()> (dlsym(libpulse, "pa_get_library_version")); if (pa_get_library_version) { libpulseVersion = pa_get_library_version(); } } secondaryLibrary.AppendPrintf(",libpulse %s", libpulseVersion); if (libpulse) { dlclose(libpulse); } rv = SetPropertyAsACString(NS_LITERAL_STRING("secondaryLibrary"), secondaryLibrary); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } #endif #ifdef MOZ_WIDGET_ANDROID AndroidSystemInfo info; if (XRE_IsContentProcess()) { dom::ContentChild* child = dom::ContentChild::GetSingleton(); if (child) { child->SendGetAndroidSystemInfo(&info); SetupAndroidInfo(info); } } else { GetAndroidSystemInfo(&info); SetupAndroidInfo(info); } #endif #ifdef MOZ_WIDGET_GONK char sdk[PROP_VALUE_MAX]; if (__system_property_get("ro.build.version.sdk", sdk)) { android_sdk_version = atoi(sdk); SetPropertyAsInt32(NS_LITERAL_STRING("sdk_version"), android_sdk_version); SetPropertyAsACString(NS_LITERAL_STRING("secondaryLibrary"), nsPrintfCString("SDK %u", android_sdk_version)); } char characteristics[PROP_VALUE_MAX]; if (__system_property_get("ro.build.characteristics", characteristics)) { if (!strcmp(characteristics, "tablet")) { SetPropertyAsBool(NS_LITERAL_STRING("tablet"), true); } else if (!strcmp(characteristics, "tv")) { SetPropertyAsBool(NS_LITERAL_STRING("tv"), true); } } nsAutoString str; rv = GetPropertyAsAString(NS_LITERAL_STRING("version"), str); if (NS_SUCCEEDED(rv)) { SetPropertyAsAString(NS_LITERAL_STRING("kernel_version"), str); } const nsAdoptingString& b2g_os_name = mozilla::Preferences::GetString("b2g.osName"); if (b2g_os_name) { SetPropertyAsAString(NS_LITERAL_STRING("name"), b2g_os_name); } const nsAdoptingString& b2g_version = mozilla::Preferences::GetString("b2g.version"); if (b2g_version) { SetPropertyAsAString(NS_LITERAL_STRING("version"), b2g_version); } #endif #if defined(XP_LINUX) && defined(MOZ_SANDBOX) SandboxInfo sandInfo = SandboxInfo::Get(); SetPropertyAsBool(NS_LITERAL_STRING("hasSeccompBPF"), sandInfo.Test(SandboxInfo::kHasSeccompBPF)); SetPropertyAsBool(NS_LITERAL_STRING("hasSeccompTSync"), sandInfo.Test(SandboxInfo::kHasSeccompTSync)); SetPropertyAsBool(NS_LITERAL_STRING("hasUserNamespaces"), sandInfo.Test(SandboxInfo::kHasUserNamespaces)); SetPropertyAsBool(NS_LITERAL_STRING("hasPrivilegedUserNamespaces"), sandInfo.Test(SandboxInfo::kHasPrivilegedUserNamespaces)); if (sandInfo.Test(SandboxInfo::kEnabledForContent)) { SetPropertyAsBool(NS_LITERAL_STRING("canSandboxContent"), sandInfo.CanSandboxContent()); } if (sandInfo.Test(SandboxInfo::kEnabledForMedia)) { SetPropertyAsBool(NS_LITERAL_STRING("canSandboxMedia"), sandInfo.CanSandboxMedia()); } #endif // XP_LINUX && MOZ_SANDBOX return NS_OK; }