Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 #include "util/mac/xattr.h" | |
| 16 | |
| 17 #include <fcntl.h> | |
| 18 #include <sys/stat.h> | |
| 19 #include <unistd.h> | |
| 20 | |
| 21 #include <limits> | |
| 22 | |
| 23 #include "base/files/scoped_file.h" | |
| 24 #include "base/posix/eintr_wrapper.h" | |
| 25 #include "base/strings/stringprintf.h" | |
| 26 #include "gtest/gtest.h" | |
| 27 #include "util/test/errors.h" | |
| 28 | |
| 29 namespace crashpad { | |
| 30 namespace test { | |
| 31 namespace { | |
| 32 | |
| 33 class Xattr : public testing::Test { | |
| 34 public: | |
| 35 // testing::Test: | |
| 36 | |
| 37 void SetUp() override { | |
| 38 path_ = base::FilePath( | |
| 39 base::StringPrintf("/tmp/com.googlecode.crashpad.test.xattr.%p", this)); | |
|
Mark Mentovai
2014/12/30 19:21:51
You didn’t use O_EXCL, but I can see why you might
Robert Sesek
2014/12/30 21:59:04
Done.
| |
| 40 // TODO(rsesek): This should use something like ScopedTempDir. | |
| 41 base::ScopedFD tmp(HANDLE_EINTR( | |
| 42 open(path_.value().c_str(), O_CREAT | O_TRUNC, 0644))); | |
| 43 EXPECT_GE(tmp.get(), 0) << ErrnoMessage("open"); | |
| 44 } | |
| 45 | |
| 46 void TearDown() override { | |
| 47 EXPECT_EQ(0, unlink(path_.value().c_str())) << ErrnoMessage("unlink"); | |
| 48 } | |
| 49 | |
| 50 base::FilePath path() { return path_; } | |
| 51 | |
| 52 private: | |
| 53 base::FilePath path_; | |
| 54 }; | |
| 55 | |
| 56 const char kKey[] = "com.google.crashpad.test"; | |
| 57 | |
| 58 TEST_F(Xattr, ReadNonExistentXattr) { | |
| 59 std::string value; | |
| 60 EXPECT_FALSE(ReadXattr(path(), kKey, &value)); | |
| 61 } | |
| 62 | |
| 63 TEST_F(Xattr, WriteAndReadString) { | |
| 64 std::string value = "hello world"; | |
| 65 EXPECT_TRUE(WriteXattr(path(), kKey, value)); | |
| 66 | |
| 67 std::string actual; | |
| 68 EXPECT_TRUE(ReadXattr(path(), kKey, &actual)); | |
| 69 EXPECT_EQ(value, actual); | |
| 70 } | |
| 71 | |
| 72 TEST_F(Xattr, WriteAndReadVeryLongString) { | |
| 73 std::string value(533, 'A'); | |
| 74 EXPECT_TRUE(WriteXattr(path(), kKey, value)); | |
| 75 | |
| 76 std::string actual; | |
| 77 EXPECT_TRUE(ReadXattr(path(), kKey, &actual)); | |
| 78 EXPECT_EQ(value, actual); | |
| 79 } | |
| 80 | |
| 81 TEST_F(Xattr, WriteAndReadBool) { | |
| 82 EXPECT_TRUE(WriteXattrBool(path(), kKey, true)); | |
| 83 bool actual = false; | |
| 84 EXPECT_TRUE(ReadXattrBool(path(), kKey, &actual)); | |
| 85 EXPECT_TRUE(actual); | |
| 86 | |
| 87 EXPECT_TRUE(WriteXattrBool(path(), kKey, false)); | |
| 88 EXPECT_TRUE(ReadXattrBool(path(), kKey, &actual)); | |
| 89 EXPECT_FALSE(actual); | |
| 90 } | |
| 91 | |
| 92 TEST_F(Xattr, WriteAndReadInt) { | |
| 93 int expected = 42; | |
| 94 int actual; | |
| 95 | |
| 96 EXPECT_TRUE(WriteXattrInt(path(), kKey, expected)); | |
| 97 EXPECT_TRUE(ReadXattrInt(path(), kKey, &actual)); | |
| 98 EXPECT_EQ(expected, actual); | |
| 99 | |
| 100 expected = std::numeric_limits<int>::max(); | |
| 101 EXPECT_TRUE(WriteXattrInt(path(), kKey, expected)); | |
| 102 EXPECT_TRUE(ReadXattrInt(path(), kKey, &actual)); | |
| 103 EXPECT_EQ(expected, actual); | |
| 104 } | |
| 105 | |
| 106 TEST_F(Xattr, WriteAndReadTimeT) { | |
| 107 time_t expected = time(nullptr); | |
| 108 time_t actual; | |
| 109 | |
| 110 EXPECT_TRUE(WriteXattrTimeT(path(), kKey, expected)); | |
| 111 EXPECT_TRUE(ReadXattrTimeT(path(), kKey, &actual)); | |
| 112 EXPECT_EQ(expected, actual); | |
| 113 | |
| 114 expected = std::numeric_limits<time_t>::max(); | |
| 115 EXPECT_TRUE(WriteXattrTimeT(path(), kKey, expected)); | |
| 116 EXPECT_TRUE(ReadXattrTimeT(path(), kKey, &actual)); | |
| 117 EXPECT_EQ(expected, actual); | |
| 118 } | |
| 119 | |
| 120 } // namespace | |
| 121 } // namespace test | |
| 122 } // namespace crashpad | |
| OLD | NEW |