blob: 6c6fd2ad22fb731c1940ef6486edbcc185b6a980 [file] [log] [blame]
Mark Salyzyn52bd37e2016-11-07 09:39:30 -08001/*
2 * Copyright (C) 2016 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 <ctype.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <time.h>
23
24#include <string>
25
26#include <android-base/stringprintf.h>
27#include <android-base/test_utils.h>
28#include <cutils/android_get_control_file.h>
29#include <gtest/gtest.h>
30
31TEST(FilesTest, android_get_control_file) {
32 TemporaryFile tf;
33 ASSERT_GE(tf.fd, 0);
34
35 std::string key(ANDROID_FILE_ENV_PREFIX);
36 key += tf.path;
37
38 std::for_each(key.begin(), key.end(), [] (char& c) { c = isalnum(c) ? c : '_'; });
39
40 EXPECT_EQ(unsetenv(key.c_str()), 0);
41 EXPECT_EQ(android_get_control_file(tf.path), -1);
42
43 EXPECT_EQ(setenv(key.c_str(), android::base::StringPrintf("%d", tf.fd).c_str(), true), 0);
44
45 EXPECT_EQ(android_get_control_file(tf.path), tf.fd);
46 close(tf.fd);
47 EXPECT_EQ(android_get_control_file(tf.path), -1);
48 EXPECT_EQ(unsetenv(key.c_str()), 0);
49 EXPECT_EQ(android_get_control_file(tf.path), -1);
50}