blob: 24c75c42d9862ed4ae8fe7172e71fa9ee31bd6f1 [file] [log] [blame]
Elliott Hughesf682b472015-02-06 12:19:48 -08001/*
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 "util.h"
18
19#include <errno.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070020
Elliott Hughesf682b472015-02-06 12:19:48 -080021#include <gtest/gtest.h>
22
23TEST(util, read_file_ENOENT) {
24 std::string s("hello");
25 errno = 0;
26 EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
27 EXPECT_EQ(ENOENT, errno);
28 EXPECT_EQ("", s); // s was cleared.
29}
30
31TEST(util, read_file_success) {
32 std::string s("hello");
33 EXPECT_TRUE(read_file("/proc/version", &s));
34 EXPECT_GT(s.length(), 6U);
35 EXPECT_EQ('\n', s[s.length() - 1]);
36 s[5] = 0;
37 EXPECT_STREQ("Linux", s.c_str());
38}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080039
40TEST(util, decode_uid) {
41 EXPECT_EQ(0U, decode_uid("root"));
Nick Kralevichd2104df2015-06-18 17:46:54 -070042 EXPECT_EQ(UINT_MAX, decode_uid("toot"));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080043 EXPECT_EQ(123U, decode_uid("123"));
44}