1 // Copyright 2014 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 #include "mojo/system/platform_handle_dispatcher.h" 6 7 #include <stdio.h> 8 9 #include "base/file_util.h" 10 #include "base/files/file_path.h" 11 #include "base/files/scoped_file.h" 12 #include "base/memory/ref_counted.h" 13 #include "mojo/common/test/test_utils.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace mojo { 17 namespace system { 18 namespace { 19 20 TEST(PlatformHandleDispatcherTest, Basic) { 21 static const char kHelloWorld[] = "hello world"; 22 23 base::FilePath unused; 24 base::ScopedFILE fp(CreateAndOpenTemporaryFile(&unused)); 25 ASSERT_TRUE(fp); 26 EXPECT_EQ(sizeof(kHelloWorld), 27 fwrite(kHelloWorld, 1, sizeof(kHelloWorld), fp.get())); 28 29 embedder::ScopedPlatformHandle 30 h(mojo::test::PlatformHandleFromFILE(fp.Pass())); 31 EXPECT_FALSE(fp); 32 ASSERT_TRUE(h.is_valid()); 33 34 scoped_refptr<PlatformHandleDispatcher> dispatcher( 35 new PlatformHandleDispatcher(h.Pass())); 36 EXPECT_FALSE(h.is_valid()); 37 EXPECT_EQ(Dispatcher::kTypePlatformHandle, dispatcher->GetType()); 38 39 h = dispatcher->PassPlatformHandle().Pass(); 40 EXPECT_TRUE(h.is_valid()); 41 42 fp = mojo::test::FILEFromPlatformHandle(h.Pass(), "rb").Pass(); 43 EXPECT_FALSE(h.is_valid()); 44 EXPECT_TRUE(fp); 45 46 rewind(fp.get()); 47 char read_buffer[1000] = {}; 48 EXPECT_EQ(sizeof(kHelloWorld), 49 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); 50 EXPECT_STREQ(kHelloWorld, read_buffer); 51 52 // Try getting the handle again. (It should fail cleanly.) 53 h = dispatcher->PassPlatformHandle().Pass(); 54 EXPECT_FALSE(h.is_valid()); 55 56 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); 57 } 58 59 TEST(PlatformHandleDispatcherTest, CreateEquivalentDispatcherAndClose) { 60 static const char kFooBar[] = "foo bar"; 61 62 base::FilePath unused; 63 base::ScopedFILE fp(CreateAndOpenTemporaryFile(&unused)); 64 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); 65 66 scoped_refptr<PlatformHandleDispatcher> dispatcher( 67 new PlatformHandleDispatcher( 68 mojo::test::PlatformHandleFromFILE(fp.Pass()))); 69 70 DispatcherTransport transport( 71 test::DispatcherTryStartTransport(dispatcher.get())); 72 EXPECT_TRUE(transport.is_valid()); 73 EXPECT_EQ(Dispatcher::kTypePlatformHandle, transport.GetType()); 74 EXPECT_FALSE(transport.IsBusy()); 75 76 scoped_refptr<Dispatcher> generic_dispatcher = 77 transport.CreateEquivalentDispatcherAndClose(); 78 ASSERT_TRUE(generic_dispatcher); 79 80 transport.End(); 81 EXPECT_TRUE(dispatcher->HasOneRef()); 82 dispatcher = NULL; 83 84 ASSERT_EQ(Dispatcher::kTypePlatformHandle, generic_dispatcher->GetType()); 85 dispatcher = static_cast<PlatformHandleDispatcher*>(generic_dispatcher.get()); 86 87 fp = mojo::test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), 88 "rb").Pass(); 89 EXPECT_TRUE(fp); 90 91 rewind(fp.get()); 92 char read_buffer[1000] = {}; 93 EXPECT_EQ(sizeof(kFooBar), 94 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); 95 EXPECT_STREQ(kFooBar, read_buffer); 96 97 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); 98 } 99 100 } // namespace 101 } // namespace system 102 } // namespace mojo 103