Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: mojo/public/c/tests/system/buffer_unittest.cc

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file tests the C buffer API (the functions declared in
6 // mojo/public/c/include/mojo/system/buffer.h).
7
8 #include <mojo/system/buffer.h>
9
10 #include <mojo/result.h>
11 #include <mojo/system/handle.h>
12
13 #include "gtest/gtest.h"
14
15 namespace {
16
17 const MojoHandleRights kDefaultSharedBufferHandleRights =
18 MOJO_HANDLE_RIGHT_DUPLICATE | MOJO_HANDLE_RIGHT_TRANSFER |
19 MOJO_HANDLE_RIGHT_GET_OPTIONS | MOJO_HANDLE_RIGHT_SET_OPTIONS |
20 MOJO_HANDLE_RIGHT_MAP_READABLE | MOJO_HANDLE_RIGHT_MAP_WRITABLE |
21 MOJO_HANDLE_RIGHT_MAP_EXECUTABLE;
22
23 // The only handle that's guaranteed to be invalid is |MOJO_HANDLE_INVALID|.
24 // Tests that everything that takes a handle properly recognizes it.
25 TEST(BufferTest, InvalidHandle) {
26 MojoHandle out_handle = MOJO_HANDLE_INVALID;
27 EXPECT_EQ(
28 MOJO_RESULT_INVALID_ARGUMENT,
29 MojoDuplicateBufferHandle(MOJO_HANDLE_INVALID, nullptr, &out_handle));
30 MojoBufferInformation buffer_info = {};
31 EXPECT_EQ(
32 MOJO_RESULT_INVALID_ARGUMENT,
33 MojoGetBufferInformation(MOJO_HANDLE_INVALID, &buffer_info,
34 static_cast<uint32_t>(sizeof(buffer_info))));
35 void* write_pointer = nullptr;
36 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
37 MojoMapBuffer(MOJO_HANDLE_INVALID, 0u, 1u, &write_pointer,
38 MOJO_MAP_BUFFER_FLAG_NONE));
39 // This isn't an "invalid handle" test, but we'll throw it in here anyway
40 // (since it involves a look-up).
41 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoUnmapBuffer(nullptr));
42 }
43
44 TEST(BufferTest, Basic) {
45 // Create a shared buffer (|h0|).
46 MojoHandle h0 = MOJO_HANDLE_INVALID;
47 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateSharedBuffer(nullptr, 100, &h0));
48 EXPECT_NE(h0, MOJO_HANDLE_INVALID);
49
50 // The handle should have the correct rights.
51 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE;
52 EXPECT_EQ(MOJO_RESULT_OK, MojoGetRights(h0, &rights));
53 EXPECT_EQ(kDefaultSharedBufferHandleRights, rights);
54
55 // Check information about the buffer from |h0|.
56 MojoBufferInformation info = {};
57 static const uint32_t kInfoSize = static_cast<uint32_t>(sizeof(info));
58 EXPECT_EQ(MOJO_RESULT_OK, MojoGetBufferInformation(h0, &info, kInfoSize));
59 EXPECT_EQ(kInfoSize, info.struct_size);
60 EXPECT_EQ(MOJO_BUFFER_INFORMATION_FLAG_NONE, info.flags);
61 EXPECT_EQ(100u, info.num_bytes);
62
63 // Map everything.
64 void* pointer = nullptr;
65 EXPECT_EQ(MOJO_RESULT_OK,
66 MojoMapBuffer(h0, 0, 100, &pointer, MOJO_MAP_BUFFER_FLAG_NONE));
67 ASSERT_TRUE(pointer);
68 static_cast<char*>(pointer)[50] = 'x';
69
70 // Duplicate |h0| to |h1|.
71 MojoHandle h1 = MOJO_HANDLE_INVALID;
72 EXPECT_EQ(MOJO_RESULT_OK, MojoDuplicateHandle(h0, &h1));
73 EXPECT_NE(h1, MOJO_HANDLE_INVALID);
74 EXPECT_NE(h1, h0);
75
76 // The new handle should have the correct rights.
77 rights = MOJO_HANDLE_RIGHT_NONE;
78 EXPECT_EQ(MOJO_RESULT_OK, MojoGetRights(h1, &rights));
79 EXPECT_EQ(kDefaultSharedBufferHandleRights, rights);
80
81 // Check information about the buffer from |h1|.
82 info = MojoBufferInformation();
83 EXPECT_EQ(MOJO_RESULT_OK, MojoGetBufferInformation(h1, &info, kInfoSize));
84 EXPECT_EQ(kInfoSize, info.struct_size);
85 EXPECT_EQ(MOJO_BUFFER_INFORMATION_FLAG_NONE, info.flags);
86 EXPECT_EQ(100u, info.num_bytes);
87
88 // Close |h0|.
89 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
90
91 // The mapping should still be good.
92 static_cast<char*>(pointer)[51] = 'y';
93
94 // Unmap it.
95 EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(pointer));
96
97 // Map half of |h1|.
98 pointer = nullptr;
99 EXPECT_EQ(MOJO_RESULT_OK,
100 MojoMapBuffer(h1, 50, 50, &pointer, MOJO_MAP_BUFFER_FLAG_NONE));
101 ASSERT_TRUE(pointer);
102
103 // It should have what we wrote.
104 EXPECT_EQ('x', static_cast<char*>(pointer)[0]);
105 EXPECT_EQ('y', static_cast<char*>(pointer)[1]);
106
107 // Unmap it.
108 EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(pointer));
109
110 // Test duplication with reduced rights.
111 MojoHandle h2 = MOJO_HANDLE_INVALID;
112 EXPECT_EQ(MOJO_RESULT_OK, MojoDuplicateHandleWithReducedRights(
113 h1, MOJO_HANDLE_RIGHT_DUPLICATE, &h2));
114 EXPECT_NE(h2, MOJO_HANDLE_INVALID);
115 EXPECT_NE(h2, h1);
116 // |h2| should have the correct rights.
117 rights = MOJO_HANDLE_RIGHT_NONE;
118 EXPECT_EQ(MOJO_RESULT_OK, MojoGetRights(h2, &rights));
119 EXPECT_EQ(kDefaultSharedBufferHandleRights & ~MOJO_HANDLE_RIGHT_DUPLICATE,
120 rights);
121 // Trying to duplicate |h2| should fail.
122 MojoHandle h3 = MOJO_HANDLE_INVALID;
123 EXPECT_EQ(MOJO_RESULT_PERMISSION_DENIED, MojoDuplicateHandle(h2, &h3));
124 EXPECT_EQ(MOJO_HANDLE_INVALID, h3);
125
126 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
127 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2));
128 }
129
130 // TODO(vtl): Add multi-threaded tests.
131
132 } // namespace
OLDNEW
« no previous file with comments | « mojo/public/c/tests/result_unittest.cc ('k') | mojo/public/c/tests/system/data_pipe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698