Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [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 "util.h" |
| 18 | |
| 19 | #include <errno.h> |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 20 | |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame^] | 21 | #include <android-base/test_utils.h> |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | TEST(util, read_file_ENOENT) { |
| 25 | std::string s("hello"); |
| 26 | errno = 0; |
| 27 | EXPECT_FALSE(read_file("/proc/does-not-exist", &s)); |
| 28 | EXPECT_EQ(ENOENT, errno); |
| 29 | EXPECT_EQ("", s); // s was cleared. |
| 30 | } |
| 31 | |
| 32 | TEST(util, read_file_success) { |
| 33 | std::string s("hello"); |
| 34 | EXPECT_TRUE(read_file("/proc/version", &s)); |
| 35 | EXPECT_GT(s.length(), 6U); |
| 36 | EXPECT_EQ('\n', s[s.length() - 1]); |
| 37 | s[5] = 0; |
| 38 | EXPECT_STREQ("Linux", s.c_str()); |
| 39 | } |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 40 | |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame^] | 41 | TEST(util, write_file_binary) { |
| 42 | std::string contents("abcd"); |
| 43 | contents.push_back('\0'); |
| 44 | contents.push_back('\0'); |
| 45 | contents.append("dcba"); |
| 46 | ASSERT_EQ(10u, contents.size()); |
| 47 | |
| 48 | TemporaryFile tf; |
| 49 | ASSERT_TRUE(tf.fd != -1); |
| 50 | EXPECT_TRUE(write_file(tf.path, contents)) << strerror(errno); |
| 51 | |
| 52 | std::string read_back_contents; |
| 53 | EXPECT_TRUE(read_file(tf.path, &read_back_contents)) << strerror(errno); |
| 54 | EXPECT_EQ(contents, read_back_contents); |
| 55 | EXPECT_EQ(10u, read_back_contents.size()); |
| 56 | } |
| 57 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 58 | TEST(util, decode_uid) { |
| 59 | EXPECT_EQ(0U, decode_uid("root")); |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 60 | EXPECT_EQ(UINT_MAX, decode_uid("toot")); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 61 | EXPECT_EQ(123U, decode_uid("123")); |
| 62 | } |