Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
| 21 | TEST(sysdeps_win32, adb_getenv) { |
| 22 | // Insert all test env vars before first call to adb_getenv() which will |
| 23 | // read the env var block only once. |
| 24 | ASSERT_EQ(0, _putenv("SYSDEPS_WIN32_TEST_UPPERCASE=1")); |
| 25 | ASSERT_EQ(0, _putenv("sysdeps_win32_test_lowercase=2")); |
| 26 | ASSERT_EQ(0, _putenv("Sysdeps_Win32_Test_MixedCase=3")); |
| 27 | |
| 28 | // UTF-16 value |
| 29 | ASSERT_EQ(0, _wputenv(L"SYSDEPS_WIN32_TEST_UNICODE=\u00a1\u0048\u006f\u006c" |
| 30 | L"\u0061\u0021\u03b1\u03b2\u03b3\u0061\u006d\u0062" |
| 31 | L"\u0075\u006c\u014d\u043f\u0440\u0438\u0432\u0435" |
| 32 | L"\u0442")); |
| 33 | |
| 34 | // Search for non-existant env vars. |
| 35 | EXPECT_STREQ(nullptr, adb_getenv("SYSDEPS_WIN32_TEST_NONEXISTANT")); |
| 36 | |
| 37 | // Search for existing env vars. |
| 38 | |
| 39 | // There is no test for an env var with a value of a zero-length string |
| 40 | // because _putenv() does not support inserting such an env var. |
| 41 | |
| 42 | // Search for env var that is uppercase. |
| 43 | EXPECT_STREQ("1", adb_getenv("SYSDEPS_WIN32_TEST_UPPERCASE")); |
| 44 | EXPECT_STREQ("1", adb_getenv("sysdeps_win32_test_uppercase")); |
| 45 | EXPECT_STREQ("1", adb_getenv("Sysdeps_Win32_Test_Uppercase")); |
| 46 | |
| 47 | // Search for env var that is lowercase. |
| 48 | EXPECT_STREQ("2", adb_getenv("SYSDEPS_WIN32_TEST_LOWERCASE")); |
| 49 | EXPECT_STREQ("2", adb_getenv("sysdeps_win32_test_lowercase")); |
| 50 | EXPECT_STREQ("2", adb_getenv("Sysdeps_Win32_Test_Lowercase")); |
| 51 | |
| 52 | // Search for env var that is mixed-case. |
| 53 | EXPECT_STREQ("3", adb_getenv("SYSDEPS_WIN32_TEST_MIXEDCASE")); |
| 54 | EXPECT_STREQ("3", adb_getenv("sysdeps_win32_test_mixedcase")); |
| 55 | EXPECT_STREQ("3", adb_getenv("Sysdeps_Win32_Test_MixedCase")); |
| 56 | |
| 57 | // Check that UTF-16 was converted to UTF-8. |
| 58 | EXPECT_STREQ("\xc2\xa1\x48\x6f\x6c\x61\x21\xce\xb1\xce\xb2\xce\xb3\x61\x6d" |
| 59 | "\x62\x75\x6c\xc5\x8d\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5" |
| 60 | "\xd1\x82", |
| 61 | adb_getenv("SYSDEPS_WIN32_TEST_UNICODE")); |
| 62 | |
| 63 | // Check an env var that should always be set. |
| 64 | const char* path_val = adb_getenv("PATH"); |
| 65 | EXPECT_NE(nullptr, path_val); |
| 66 | if (path_val != nullptr) { |
| 67 | EXPECT_GT(strlen(path_val), 0); |
| 68 | } |
| 69 | } |