| OLD | NEW |
| 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "nacl_io/kernel_handle.h" | 14 #include "nacl_io/kernel_handle.h" |
| 15 #include "nacl_io/kernel_object.h" | 15 #include "nacl_io/kernel_object.h" |
| 16 #include "nacl_io/mount.h" | 16 #include "nacl_io/mount.h" |
| 17 #include "nacl_io/path.h" | 17 #include "nacl_io/path.h" |
| 18 | 18 |
| 19 #include "gtest/gtest.h" | 19 #include "gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class MountRefMock : public Mount { | 23 class MountRefMock : public Mount { |
| 24 public: | 24 public: |
| 25 MountRefMock(int* mount_count, int* handle_count) | 25 MountRefMock(int* mount_count, int* handle_count) |
| 26 : mount_count(mount_count), | 26 : mount_count(mount_count), handle_count(handle_count) { |
| 27 handle_count(handle_count) { | |
| 28 (*mount_count)++; | 27 (*mount_count)++; |
| 29 } | 28 } |
| 30 | 29 |
| 31 ~MountRefMock() { | 30 ~MountRefMock() { (*mount_count)--; } |
| 32 (*mount_count)--; | |
| 33 } | |
| 34 | 31 |
| 35 public: | 32 public: |
| 36 MountNode* Open(const Path& path, int mode) { return NULL; } | 33 Error Open(const Path& path, int mode, MountNode** out_node) { |
| 37 int Close(MountNode* node) { return 0; } | 34 *out_node = NULL; |
| 38 int Unlink(const Path& path) { return 0; } | 35 return ENOSYS; |
| 39 int Mkdir(const Path& path, int permissions) { return 0; } | 36 } |
| 40 int Rmdir(const Path& path) { return 0; } | 37 Error Unlink(const Path& path) { return 0; } |
| 41 int Remove(const Path& path) { return 0; } | 38 Error Mkdir(const Path& path, int permissions) { return 0; } |
| 39 Error Rmdir(const Path& path) { return 0; } |
| 40 Error Remove(const Path& path) { return 0; } |
| 42 | 41 |
| 43 public: | 42 public: |
| 44 int* mount_count; | 43 int* mount_count; |
| 45 int* handle_count; | 44 int* handle_count; |
| 46 }; | 45 }; |
| 47 | 46 |
| 48 class KernelHandleRefMock : public KernelHandle { | 47 class KernelHandleRefMock : public KernelHandle { |
| 49 public: | 48 public: |
| 50 KernelHandleRefMock(Mount* mnt, MountNode* node, int flags) | 49 KernelHandleRefMock(Mount* mnt, MountNode* node) |
| 51 : KernelHandle(mnt, node, flags) { | 50 : KernelHandle(mnt, node) { |
| 52 MountRefMock* mock_mount = static_cast<MountRefMock*>(mnt); | 51 MountRefMock* mock_mount = static_cast<MountRefMock*>(mnt); |
| 53 (*mock_mount->handle_count)++; | 52 (*mock_mount->handle_count)++; |
| 54 } | 53 } |
| 55 | 54 |
| 56 ~KernelHandleRefMock() { | 55 ~KernelHandleRefMock() { |
| 57 MountRefMock* mock_mount = static_cast<MountRefMock*>(mount_); | 56 MountRefMock* mock_mount = static_cast<MountRefMock*>(mount_); |
| 58 (*mock_mount->handle_count)--; | 57 (*mock_mount->handle_count)--; |
| 59 } | 58 } |
| 60 }; | 59 }; |
| 61 | 60 |
| 62 class KernelObjectTest : public ::testing::Test { | 61 class KernelObjectTest : public ::testing::Test { |
| 63 public: | 62 public: |
| 64 KernelObjectTest() | 63 KernelObjectTest() : mount_count(0), handle_count(0) { |
| 65 : mount_count(0), | |
| 66 handle_count(0) { | |
| 67 proxy = new KernelObject; | 64 proxy = new KernelObject; |
| 68 mnt = new MountRefMock(&mount_count, &handle_count); | 65 mnt = new MountRefMock(&mount_count, &handle_count); |
| 69 } | 66 } |
| 70 | 67 |
| 71 ~KernelObjectTest() { | 68 ~KernelObjectTest() { |
| 72 // mnt is ref-counted, it doesn't need to be explicitly deleted. | 69 // mnt is ref-counted, it doesn't need to be explicitly deleted. |
| 73 delete proxy; | 70 delete proxy; |
| 74 } | 71 } |
| 75 | 72 |
| 76 KernelObject* proxy; | 73 KernelObject* proxy; |
| 77 MountRefMock* mnt; | 74 MountRefMock* mnt; |
| 78 int mount_count; | 75 int mount_count; |
| 79 int handle_count; | 76 int handle_count; |
| 80 }; | 77 }; |
| 81 | 78 |
| 82 } // namespace | 79 } // namespace |
| 83 | 80 |
| 84 | |
| 85 TEST_F(KernelObjectTest, Referencing) { | 81 TEST_F(KernelObjectTest, Referencing) { |
| 86 KernelHandle* handle = new KernelHandleRefMock(mnt, NULL, 0); | 82 KernelHandle* handle = new KernelHandleRefMock(mnt, NULL); |
| 87 KernelHandle* handle2 = new KernelHandleRefMock(mnt, NULL, 0); | 83 KernelHandle* handle2 = new KernelHandleRefMock(mnt, NULL); |
| 84 KernelHandle* result_handle = NULL; |
| 88 | 85 |
| 89 // Objects should have one ref when we start | 86 // Objects should have one ref when we start |
| 90 EXPECT_EQ(1, mnt->RefCount()); | 87 EXPECT_EQ(1, mnt->RefCount()); |
| 91 EXPECT_EQ(1, handle->RefCount()); | 88 EXPECT_EQ(1, handle->RefCount()); |
| 92 | 89 |
| 93 // Objects should have two refs when we get here | 90 // Objects should have two refs when we get here |
| 94 int fd1 = proxy->AllocateFD(handle); | 91 int fd1 = proxy->AllocateFD(handle); |
| 95 EXPECT_EQ(2, mnt->RefCount()); | 92 EXPECT_EQ(2, mnt->RefCount()); |
| 96 EXPECT_EQ(2, handle->RefCount()); | 93 EXPECT_EQ(2, handle->RefCount()); |
| 97 | 94 |
| 98 // If we "dup" the handle, we should bump the refs | 95 // If we "dup" the handle, we should bump the refs |
| 99 int fd2 = proxy->AllocateFD(handle); | 96 int fd2 = proxy->AllocateFD(handle); |
| 100 EXPECT_EQ(3, mnt->RefCount()); | 97 EXPECT_EQ(3, mnt->RefCount()); |
| 101 EXPECT_EQ(3, handle->RefCount()); | 98 EXPECT_EQ(3, handle->RefCount()); |
| 102 | 99 |
| 103 // If use a new handle with the same values... bump the refs | 100 // If use a new handle with the same values... bump the refs |
| 104 int fd3 = proxy->AllocateFD(handle2); | 101 int fd3 = proxy->AllocateFD(handle2); |
| 105 EXPECT_EQ(4, mnt->RefCount()); | 102 EXPECT_EQ(4, mnt->RefCount()); |
| 106 EXPECT_EQ(2, handle2->RefCount()); | 103 EXPECT_EQ(2, handle2->RefCount()); |
| 107 | 104 |
| 108 // Handles are expected to come out in order | 105 // Handles are expected to come out in order |
| 109 EXPECT_EQ(0, fd1); | 106 EXPECT_EQ(0, fd1); |
| 110 EXPECT_EQ(1, fd2); | 107 EXPECT_EQ(1, fd2); |
| 111 EXPECT_EQ(2, fd3); | 108 EXPECT_EQ(2, fd3); |
| 112 | 109 |
| 113 // We should find the handle by either fd | 110 // We should find the handle by either fd |
| 114 EXPECT_EQ(handle, proxy->AcquireHandle(fd1)); | 111 EXPECT_EQ(0, proxy->AcquireHandle(fd1, &result_handle)); |
| 115 EXPECT_EQ(handle, proxy->AcquireHandle(fd2)); | 112 EXPECT_EQ(handle, result_handle); |
| 113 EXPECT_EQ(0, proxy->AcquireHandle(fd2, &result_handle)); |
| 114 EXPECT_EQ(handle, result_handle); |
| 116 | 115 |
| 117 // A non existent fd should fail | 116 // A non existent fd should fail |
| 118 EXPECT_EQ(NULL, proxy->AcquireHandle(-1)); | 117 EXPECT_EQ(EBADF, proxy->AcquireHandle(-1, &result_handle)); |
| 119 EXPECT_EQ(EBADF, errno); | 118 EXPECT_EQ(NULL, result_handle); |
| 120 EXPECT_EQ(NULL, proxy->AcquireHandle(100)); | 119 EXPECT_EQ(EBADF, proxy->AcquireHandle(100, &result_handle)); |
| 121 EXPECT_EQ(EBADF, errno); | 120 EXPECT_EQ(NULL, result_handle); |
| 122 | 121 |
| 123 // Acquiring the handle, should have ref'd it | 122 // Acquiring the handle, should have ref'd it |
| 124 EXPECT_EQ(4, mnt->RefCount()); | 123 EXPECT_EQ(4, mnt->RefCount()); |
| 125 EXPECT_EQ(5, handle->RefCount()); | 124 EXPECT_EQ(5, handle->RefCount()); |
| 126 | 125 |
| 127 // Release the handle for each call to acquire | 126 // Release the handle for each call to acquire |
| 128 proxy->ReleaseHandle(handle); | 127 proxy->ReleaseHandle(handle); |
| 129 proxy->ReleaseHandle(handle); | 128 proxy->ReleaseHandle(handle); |
| 130 | 129 |
| 131 // Release the handle for each time we constructed something | 130 // Release the handle for each time we constructed something |
| (...skipping 16 matching lines...) Expand all Loading... |
| 148 proxy->FreeFD(fd3); | 147 proxy->FreeFD(fd3); |
| 149 EXPECT_EQ(1, handle_count); | 148 EXPECT_EQ(1, handle_count); |
| 150 EXPECT_EQ(1, mount_count); | 149 EXPECT_EQ(1, mount_count); |
| 151 | 150 |
| 152 proxy->FreeFD(fd2); | 151 proxy->FreeFD(fd2); |
| 153 EXPECT_EQ(0, handle_count); | 152 EXPECT_EQ(0, handle_count); |
| 154 EXPECT_EQ(0, mount_count); | 153 EXPECT_EQ(0, mount_count); |
| 155 } | 154 } |
| 156 | 155 |
| 157 TEST_F(KernelObjectTest, FreeAndReassignFD) { | 156 TEST_F(KernelObjectTest, FreeAndReassignFD) { |
| 157 KernelHandle* result_handle = NULL; |
| 158 |
| 158 EXPECT_EQ(0, handle_count); | 159 EXPECT_EQ(0, handle_count); |
| 159 | 160 |
| 160 KernelHandle* handle = new KernelHandleRefMock(mnt, NULL, 0); | 161 KernelHandle* handle = new KernelHandleRefMock(mnt, NULL); |
| 161 | 162 |
| 162 EXPECT_EQ(1, handle_count); | 163 EXPECT_EQ(1, handle_count); |
| 163 EXPECT_EQ(1, handle->RefCount()); | 164 EXPECT_EQ(1, handle->RefCount()); |
| 164 | 165 |
| 165 // Assign to a non-existent FD | 166 // Assign to a non-existent FD |
| 166 proxy->FreeAndReassignFD(2, handle); | 167 proxy->FreeAndReassignFD(2, handle); |
| 167 EXPECT_EQ((KernelHandle*)NULL, proxy->AcquireHandle(0)); | 168 EXPECT_EQ(EBADF, proxy->AcquireHandle(0, &result_handle)); |
| 168 EXPECT_EQ((KernelHandle*)NULL, proxy->AcquireHandle(1)); | 169 EXPECT_EQ(NULL, result_handle); |
| 169 EXPECT_EQ(handle, proxy->AcquireHandle(2)); | 170 EXPECT_EQ(EBADF, proxy->AcquireHandle(1, &result_handle)); |
| 171 EXPECT_EQ(NULL, result_handle); |
| 172 EXPECT_EQ(0, proxy->AcquireHandle(2, &result_handle)); |
| 173 EXPECT_EQ(handle, result_handle); |
| 170 proxy->ReleaseHandle(handle); | 174 proxy->ReleaseHandle(handle); |
| 171 | 175 |
| 172 EXPECT_EQ(1, handle_count); | 176 EXPECT_EQ(1, handle_count); |
| 173 EXPECT_EQ(2, handle->RefCount()); | 177 EXPECT_EQ(2, handle->RefCount()); |
| 174 | 178 |
| 175 proxy->FreeAndReassignFD(0, handle); | 179 proxy->FreeAndReassignFD(0, handle); |
| 176 EXPECT_EQ(handle, proxy->AcquireHandle(0)); | 180 EXPECT_EQ(0, proxy->AcquireHandle(0, &result_handle)); |
| 177 EXPECT_EQ((KernelHandle*)NULL, proxy->AcquireHandle(1)); | 181 EXPECT_EQ(handle, result_handle); |
| 178 EXPECT_EQ(handle, proxy->AcquireHandle(2)); | 182 EXPECT_EQ(EBADF, proxy->AcquireHandle(1, &result_handle)); |
| 183 EXPECT_EQ(NULL, result_handle); |
| 184 EXPECT_EQ(0, proxy->AcquireHandle(2, &result_handle)); |
| 185 EXPECT_EQ(handle, result_handle); |
| 179 proxy->ReleaseHandle(handle); | 186 proxy->ReleaseHandle(handle); |
| 180 proxy->ReleaseHandle(handle); | 187 proxy->ReleaseHandle(handle); |
| 181 | 188 |
| 182 EXPECT_EQ(1, handle_count); | 189 EXPECT_EQ(1, handle_count); |
| 183 EXPECT_EQ(3, handle->RefCount()); | 190 EXPECT_EQ(3, handle->RefCount()); |
| 184 | 191 |
| 185 proxy->FreeFD(0); | 192 proxy->FreeFD(0); |
| 186 proxy->FreeFD(2); | 193 proxy->FreeFD(2); |
| 187 proxy->ReleaseHandle(handle); // handle is constructed with a refcount of 1. | 194 proxy->ReleaseHandle(handle); // handle is constructed with a refcount of 1. |
| 188 | 195 |
| 189 EXPECT_EQ(0, handle_count); | 196 EXPECT_EQ(0, handle_count); |
| 190 } | 197 } |
| OLD | NEW |