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

Side by Side Diff: libraries/nacl-mounts/test.nacl/DupTest.cc

Issue 10832154: Fix dup and dup2 in nacl-mounts (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6 #include "gtest/gtest.h"
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11
12 TEST(DupTest, Simple) {
13 char buff[5];
14
15 int random = open("/dev/random", O_RDONLY);
16 EXPECT_GT(random, 0);
17 int null = open("/dev/null", O_RDWR);
18 EXPECT_GT(null, 0);
19
20 // dup should work and take a reference.
21 int null_copy = dup(null);
22 EXPECT_GT(null_copy, 0);
23 EXPECT_EQ(5, write(null, reinterpret_cast<void*>(buff), 5));
24 EXPECT_EQ(0, read(null, reinterpret_cast<void*>(buff), 5));
25
26 EXPECT_EQ(0, close(null));
27 EXPECT_EQ(5, write(null_copy, reinterpret_cast<void*>(buff), 5));
28 EXPECT_EQ(0, read(null_copy, reinterpret_cast<void*>(buff), 5));
29
30 // dup2 with oldfd == newfd is a no-op.
31 EXPECT_EQ(null_copy, dup2(null_copy, null_copy));
32
33 // dup2 should work on a high fd and take a reference.
34 EXPECT_EQ(99, dup2(null_copy, 99));
35 EXPECT_EQ(5, write(null_copy, reinterpret_cast<void*>(buff), 5));
36 EXPECT_EQ(0, read(null_copy, reinterpret_cast<void*>(buff), 5));
37
38 EXPECT_EQ(0, close(null_copy));
39 EXPECT_EQ(5, write(99, reinterpret_cast<void*>(buff), 5));
40 EXPECT_EQ(0, read(99, reinterpret_cast<void*>(buff), 5));
41
42 // dup2 should close newfd if needbe.
43 EXPECT_EQ(random, dup2(99, random));
44 EXPECT_EQ(5, write(99, reinterpret_cast<void*>(buff), 5));
45 EXPECT_EQ(0, read(99, reinterpret_cast<void*>(buff), 5));
46
47 EXPECT_EQ(0, close(99));
48 EXPECT_EQ(5, write(random, reinterpret_cast<void*>(buff), 5));
49 EXPECT_EQ(0, read(random, reinterpret_cast<void*>(buff), 5));
50 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698