1 // Copyright 2015 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 <stddef.h> 6 #include <stdint.h> 7 8 #include "base/macros.h" 9 #include "mojo/public/interfaces/bindings/tests/versioning_test_client.mojom.h" 10 #include "services/service_manager/public/cpp/application_test_base.h" 11 #include "services/service_manager/public/cpp/connector.h" 12 13 namespace mojo { 14 namespace test { 15 namespace versioning { 16 17 class VersioningApplicationTest : public ApplicationTestBase { 18 public: 19 VersioningApplicationTest() : ApplicationTestBase() {} 20 ~VersioningApplicationTest() override {} 21 22 protected: 23 // ApplicationTestBase overrides. 24 void SetUp() override { 25 ApplicationTestBase::SetUp(); 26 27 connector()->BindInterface("versioning_test_service", &database_); 28 } 29 30 HumanResourceDatabasePtr database_; 31 32 private: 33 DISALLOW_COPY_AND_ASSIGN(VersioningApplicationTest); 34 }; 35 36 TEST_F(VersioningApplicationTest, Struct) { 37 // The service side uses a newer version of Employee defintion. 38 // The returned struct should be truncated. 39 EmployeePtr employee(Employee::New()); 40 employee->employee_id = 1; 41 employee->name = "Homer Simpson"; 42 employee->department = DEPARTMENT_DEV; 43 44 database_->QueryEmployee(1, true, 45 [&employee](EmployeePtr returned_employee, 46 Array<uint8_t> returned_finger_print) { 47 EXPECT_TRUE(employee->Equals(*returned_employee)); 48 EXPECT_FALSE(returned_finger_print.is_null()); 49 }); 50 database_.WaitForIncomingResponse(); 51 52 // Passing a struct of older version to the service side works. 53 EmployeePtr new_employee(Employee::New()); 54 new_employee->employee_id = 2; 55 new_employee->name = "Marge Simpson"; 56 new_employee->department = DEPARTMENT_SALES; 57 58 database_->AddEmployee(new_employee.Clone(), 59 [](bool success) { EXPECT_TRUE(success); }); 60 database_.WaitForIncomingResponse(); 61 62 database_->QueryEmployee( 63 2, false, [&new_employee](EmployeePtr returned_employee, 64 Array<uint8_t> returned_finger_print) { 65 EXPECT_TRUE(new_employee->Equals(*returned_employee)); 66 EXPECT_TRUE(returned_finger_print.is_null()); 67 }); 68 database_.WaitForIncomingResponse(); 69 } 70 71 TEST_F(VersioningApplicationTest, QueryVersion) { 72 EXPECT_EQ(0u, database_.version()); 73 database_.QueryVersion([](uint32_t version) { EXPECT_EQ(1u, version); }); 74 database_.WaitForIncomingResponse(); 75 EXPECT_EQ(1u, database_.version()); 76 } 77 78 TEST_F(VersioningApplicationTest, RequireVersion) { 79 EXPECT_EQ(0u, database_.version()); 80 81 database_.RequireVersion(1); 82 EXPECT_EQ(1u, database_.version()); 83 database_->QueryEmployee(3, false, 84 [](EmployeePtr returned_employee, 85 Array<uint8_t> returned_finger_print) {}); 86 database_.WaitForIncomingResponse(); 87 EXPECT_FALSE(database_.encountered_error()); 88 89 // Requiring a version higher than what the service side implements will close 90 // the pipe. 91 database_.RequireVersion(3); 92 EXPECT_EQ(3u, database_.version()); 93 database_->QueryEmployee(1, false, 94 [](EmployeePtr returned_employee, 95 Array<uint8_t> returned_finger_print) {}); 96 database_.WaitForIncomingResponse(); 97 EXPECT_TRUE(database_.encountered_error()); 98 } 99 100 TEST_F(VersioningApplicationTest, CallNonexistentMethod) { 101 EXPECT_EQ(0u, database_.version()); 102 103 Array<uint8_t> new_finger_print(128); 104 for (size_t i = 0; i < 128; ++i) 105 new_finger_print[i] = i + 13; 106 107 // Although the client side doesn't know whether the service side supports 108 // version 1, calling a version 1 method succeeds as long as the service side 109 // supports version 1. 110 database_->AttachFingerPrint(1, new_finger_print.Clone(), 111 [](bool success) { EXPECT_TRUE(success); }); 112 database_.WaitForIncomingResponse(); 113 114 // Calling a version 2 method (which the service side doesn't support) closes 115 // the pipe. 116 database_->ListEmployeeIds([](Array<uint64_t> ids) { EXPECT_TRUE(false); }); 117 database_.WaitForIncomingResponse(); 118 EXPECT_TRUE(database_.encountered_error()); 119 } 120 121 } // namespace versioning 122 } // namespace examples 123 } // namespace mojo 124