OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "chrome/browser/local_discovery/service_discovery_client_mac.h" | 11 #include "chrome/browser/local_discovery/service_discovery_client_mac.h" |
12 #include "chrome/common/local_discovery/service_discovery_client.h" | 12 #include "chrome/common/local_discovery/service_discovery_client.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_util.h" |
14 #include "testing/gtest_mac.h" | 16 #include "testing/gtest_mac.h" |
15 | 17 |
16 @interface TestNSNetService : NSNetService { | 18 @interface TestNSNetService : NSNetService { |
17 @private | 19 @private |
18 NSData* data_; | 20 NSData* data_; |
| 21 NSArray* addresses_; |
19 } | 22 } |
20 - (id) initWithData:(NSData *)data; | 23 - (id)initWithData:(NSData*)data; |
| 24 - (void)setAddresses:(NSArray*)addresses; |
21 @end | 25 @end |
22 | 26 |
23 @implementation TestNSNetService | 27 @implementation TestNSNetService |
24 | 28 |
25 -(id) initWithData:(NSData *)data { | 29 - (id)initWithData:(NSData*)data { |
26 if ((self = [super init])) { | 30 if ((self = [super init])) { |
27 data_ = data; | 31 data_ = data; |
28 } | 32 } |
29 return self; | 33 return self; |
30 } | 34 } |
31 | 35 |
32 - (NSArray *)addresses { | 36 - (void)setAddresses:(NSArray*)addresses { |
33 return [NSMutableArray array]; | 37 addresses_ = addresses; |
34 } | 38 } |
35 | 39 |
36 - (NSData *)TXTRecordData { | 40 - (NSArray*)addresses { |
| 41 return addresses_; |
| 42 } |
| 43 |
| 44 - (NSData*)TXTRecordData { |
37 return data_; | 45 return data_; |
38 } | 46 } |
39 | 47 |
40 @end | 48 @end |
41 | 49 |
42 namespace local_discovery { | 50 namespace local_discovery { |
43 | 51 |
44 class ServiceDiscoveryClientMacTest : public CocoaTest { | 52 class ServiceDiscoveryClientMacTest : public CocoaTest { |
45 public: | 53 public: |
46 ServiceDiscoveryClientMacTest() : num_updates_(0), num_resolves_(0) { | 54 ServiceDiscoveryClientMacTest() : num_updates_(0), num_resolves_(0) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 112 } |
105 | 113 |
106 TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) { | 114 TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) { |
107 const std::string test_service_name = "Test.123._testing._tcp.local"; | 115 const std::string test_service_name = "Test.123._testing._tcp.local"; |
108 scoped_ptr<ServiceResolver> resolver = client_->CreateServiceResolver( | 116 scoped_ptr<ServiceResolver> resolver = client_->CreateServiceResolver( |
109 test_service_name, | 117 test_service_name, |
110 base::Bind(&ServiceDiscoveryClientMacTest::OnResolveComplete, | 118 base::Bind(&ServiceDiscoveryClientMacTest::OnResolveComplete, |
111 base::Unretained(this))); | 119 base::Unretained(this))); |
112 | 120 |
113 const uint8 record_bytes[] = { 2, 'a', 'b', 3, 'd', '=', 'e' }; | 121 const uint8 record_bytes[] = { 2, 'a', 'b', 3, 'd', '=', 'e' }; |
114 base::scoped_nsobject<NSNetService> test_service( | 122 base::scoped_nsobject<TestNSNetService> test_service([[TestNSNetService alloc] |
115 [[TestNSNetService alloc] initWithData: | 123 initWithData:[[NSData alloc] initWithBytes:record_bytes |
116 [[NSData alloc] initWithBytes:record_bytes | 124 length:arraysize(record_bytes)]]); |
117 length:arraysize(record_bytes)]]); | 125 |
| 126 const std::string kIp = "2001:4860:4860::8844"; |
| 127 const uint16_t kPort = 4321; |
| 128 net::IPAddressNumber ip_address; |
| 129 ASSERT_TRUE(net::ParseIPLiteralToNumber(kIp, &ip_address)); |
| 130 net::IPEndPoint endpoint(ip_address, kPort); |
| 131 net::SockaddrStorage storage; |
| 132 ASSERT_TRUE(endpoint.ToSockAddr(storage.addr, &storage.addr_len)); |
| 133 NSData* discoveryHost = |
| 134 [NSData dataWithBytes:storage.addr length:storage.addr_len]; |
| 135 NSArray* addresses = @[ discoveryHost ]; |
| 136 [test_service setAddresses:addresses]; |
118 | 137 |
119 ServiceResolverImplMac* resolver_impl = | 138 ServiceResolverImplMac* resolver_impl = |
120 static_cast<ServiceResolverImplMac*>(resolver.get()); | 139 static_cast<ServiceResolverImplMac*>(resolver.get()); |
121 resolver_impl->GetContainerForTesting()->SetServiceForTesting(test_service); | 140 resolver_impl->GetContainerForTesting()->SetServiceForTesting(test_service); |
122 resolver->StartResolving(); | 141 resolver->StartResolving(); |
123 | 142 |
124 resolver_impl->GetContainerForTesting()->OnResolveUpdate( | 143 resolver_impl->GetContainerForTesting()->OnResolveUpdate( |
125 ServiceResolver::STATUS_SUCCESS); | 144 ServiceResolver::STATUS_SUCCESS); |
126 | 145 |
127 base::MessageLoop::current()->RunUntilIdle(); | 146 base::MessageLoop::current()->RunUntilIdle(); |
128 | 147 |
129 EXPECT_EQ(1, num_resolves_); | 148 EXPECT_EQ(1, num_resolves_); |
130 EXPECT_EQ(2u, last_service_description_.metadata.size()); | 149 EXPECT_EQ(2u, last_service_description_.metadata.size()); |
| 150 EXPECT_EQ(kIp, last_service_description_.address.host()); |
| 151 EXPECT_EQ(kPort, last_service_description_.address.port()); |
131 } | 152 } |
132 | 153 |
133 } // namespace local_discovery | 154 } // namespace local_discovery |
OLD | NEW |