File: ios_password_manager_driver_unittest.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 11,726 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "components/password_manager/ios/ios_password_manager_driver.h"

#import "base/memory/raw_ptr.h"
#import "base/memory/scoped_refptr.h"
#import "base/strings/sys_string_conversions.h"
#import "components/autofill/ios/browser/autofill_java_script_feature.h"
#import "components/password_manager/core/browser/password_manager.h"
#import "components/password_manager/core/browser/password_manager_client.h"
#import "components/password_manager/core/browser/password_store/mock_password_store_interface.h"
#import "components/password_manager/core/browser/stub_password_manager_client.h"
#import "components/password_manager/ios/ios_password_manager_driver_factory.h"
#import "components/password_manager/ios/shared_password_controller.h"
#import "ios/web/public/test/fakes/fake_web_frame.h"
#import "ios/web/public/test/fakes/fake_web_frames_manager.h"
#import "ios/web/public/test/fakes/fake_web_state.h"
#import "testing/gtest_mac.h"
#import "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/gtest_support.h"

using autofill::AutofillJavaScriptFeature;
using base::SysNSStringToUTF8;
using password_manager::PasswordManager;
using testing::Return;

#define andCompareStringAtIndex(expected_string, index) \
  andDo(^(NSInvocation * invocation) {                  \
    const std::string* param;                           \
    [invocation getArgument:&param atIndex:index + 2];  \
    EXPECT_EQ(*param, expected_string);                 \
  })

// This is a workaround for returning const GURL&, for which .andReturn and
// .andReturnValue don’t work.
@interface URLGetter : NSObject

- (const GURL&)lastCommittedURL;

@end

@implementation URLGetter

- (const GURL&)lastCommittedURL {
  return GURL::EmptyGURL();
}

@end

class MockPasswordManagerClient
    : public password_manager::StubPasswordManagerClient {
 public:
  MOCK_METHOD(bool,
              IsSavingAndFillingEnabled,
              (const GURL&),
              (const, override));
  MOCK_METHOD(password_manager::PasswordStoreInterface*,
              GetProfilePasswordStore,
              (),
              (const, override));
};

class IOSPasswordManagerDriverTest : public PlatformTest {
 public:
  IOSPasswordManagerDriverTest() : PlatformTest() {
    auto web_frames_manager = std::make_unique<web::FakeWebFramesManager>();
    web_frames_manager_ = web_frames_manager.get();
    web::ContentWorld content_world =
        AutofillJavaScriptFeature::GetInstance()->GetSupportedContentWorld();
    web_state_.SetWebFramesManager(content_world,
                                   std::move(web_frames_manager));

    auto web_frame = web::FakeWebFrame::Create(SysNSStringToUTF8(@"main-frame"),
                                               /*is_main_frame=*/true);
    auto web_frame2 = web::FakeWebFrame::Create(SysNSStringToUTF8(@"frame"),
                                                /*is_main_frame=*/false);
    web::WebFrame* frame = web_frame.get();
    web::WebFrame* frame2 = web_frame2.get();
    web_frames_manager_->AddWebFrame(std::move(web_frame));
    web_frames_manager_->AddWebFrame(std::move(web_frame2));

    password_controller_ = OCMStrictClassMock([SharedPasswordController class]);

    IOSPasswordManagerDriverFactory::CreateForWebState(
        &web_state_, password_controller_, &password_manager_);

    driver_ = IOSPasswordManagerDriverFactory::FromWebStateAndWebFrame(
        &web_state_, frame);
    driver2_ = IOSPasswordManagerDriverFactory::FromWebStateAndWebFrame(
        &web_state_, frame2);
  }

 protected:
  raw_ptr<web::FakeWebFramesManager> web_frames_manager_;
  web::FakeWebState web_state_;
  raw_ptr<IOSPasswordManagerDriver> driver_;
  raw_ptr<IOSPasswordManagerDriver> driver2_;
  id password_controller_;
  testing::StrictMock<MockPasswordManagerClient> password_manager_client_;
  PasswordManager password_manager_ =
      PasswordManager(&password_manager_client_);
};

// Tests that the drivers have the correct ids.
TEST_F(IOSPasswordManagerDriverTest, GetId) {
  ASSERT_EQ(driver_->GetId(), 0);
  ASSERT_EQ(driver2_->GetId(), 1);
}

// Tests the IsInPrimaryMainFrame method.
TEST_F(IOSPasswordManagerDriverTest, IsInPrimaryMainFrame) {
  ASSERT_TRUE(driver_->IsInPrimaryMainFrame());
  ASSERT_FALSE(driver2_->IsInPrimaryMainFrame());
}

// Tests the PropagateFillDataOnParsingCompletion method.
TEST_F(IOSPasswordManagerDriverTest, PropagateFillDataOnParsingCompletion) {
  autofill::PasswordFormFillData form_data;

  OCMExpect([[password_controller_ ignoringNonObjectArgs]
                processPasswordFormFillData:form_data
                                 forFrameId:""
                                isMainFrame:driver_->IsInPrimaryMainFrame()
                          forSecurityOrigin:driver_->security_origin()])
      .andCompareStringAtIndex(driver_->web_frame_id(), 1);
  driver_->PropagateFillDataOnParsingCompletion(form_data);

  EXPECT_OCMOCK_VERIFY(password_controller_);
}

// Tests the InformNoSavedCredentials method.
TEST_F(IOSPasswordManagerDriverTest, InformNoSavedCredentials) {
  const std::string main_frame_id = SysNSStringToUTF8(@"main-frame");
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
                onNoSavedCredentialsWithFrameId:""])
      .andCompareStringAtIndex(main_frame_id, 0);
  driver_->InformNoSavedCredentials(
      /*should_show_popup_without_passwords=*/false);

  EXPECT_OCMOCK_VERIFY(password_controller_);
}

// Tests the driver when forms eligible for password generation are found.
// Verifies that the proactive generation can only be used once that it is known
// that there are no passwords for the site.
TEST_F(IOSPasswordManagerDriverTest, FormEligibleForGenerationFound) {
  // Set objects needed to handle 3 forms eligible for password generation.

  // Set store for the client that is able to save passwords.
  auto store =
      base::MakeRefCounted<password_manager::MockPasswordStoreInterface>();
  EXPECT_CALL(password_manager_client_, GetProfilePasswordStore)
      .WillRepeatedly(testing::Return(store.get()));
  EXPECT_CALL(*store, IsAbleToSavePasswords)
      .Times(3)
      .WillRepeatedly(Return(true));

  // Enable password saving and generation in the client.
  EXPECT_CALL(password_manager_client_, IsSavingAndFillingEnabled(GURL()))
      .Times(3)
      .WillRepeatedly(Return(true));
  EXPECT_CALL(*password_manager_client_.GetPasswordFeatureManager(),
              IsGenerationEnabled())
      .Times(3)
      .WillRepeatedly(Return(true));

  // Set a last commited URL eligible for generation.
  URLGetter* url_getter = [URLGetter alloc];
  for (int i = 0; i < 3; ++i) {
    OCMStub([password_controller_ lastCommittedURL])
        .andCall(url_getter, @selector(lastCommittedURL));
  }

  // Set other expected calls on the controller when an eligible form for
  // generation is found.
  autofill::PasswordFormGenerationData form;
  for (int i = 0; i < 3; ++i) {
    OCMExpect([password_controller_ formEligibleForGenerationFound:form]);
  }

  // Inform the driver that 2 forms eligible for generation were found. The
  // driver doesn't know yet at this point whether proactive generation can also
  // be used.
  driver_->FormEligibleForGenerationFound(form);
  driver_->FormEligibleForGenerationFound(form);

  // Inform that there are no saved credentials for the site so proactive
  // generation is set up on the forms eligible for generation.
  // Verify that the listeners for proactive generation are attached for the
  // 2 forms.
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
      attachListenersForPasswordGenerationFields:form
                                      forFrameId:""]);
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
      attachListenersForPasswordGenerationFields:form
                                      forFrameId:""]);
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
      onNoSavedCredentialsWithFrameId:""]);
  driver_->InformNoSavedCredentials(
      /*should_show_popup_without_passwords=*/false);

  // Inform the driver again that an eligible form for generation was found.
  // Verify that the listeners for proactive generation are immediately attached
  // since it is already known that there are no saved credentials for the site.
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
      attachListenersForPasswordGenerationFields:form
                                      forFrameId:""]);
  driver_->FormEligibleForGenerationFound(form);

  EXPECT_OCMOCK_VERIFY(password_controller_);
}

// Tests that the queue of pending forms for proactive generation
// doesn't grow more than its max capacity.
TEST_F(IOSPasswordManagerDriverTest,
       FormEligibleForGenerationFound_MaxProactiveQueueCapacity) {
  // Set objects needed to handle 3 forms eligible for password generation.

  // Set store for the client that is able to save passwords.
  auto store =
      base::MakeRefCounted<password_manager::MockPasswordStoreInterface>();
  EXPECT_CALL(password_manager_client_, GetProfilePasswordStore)
      .WillRepeatedly(testing::Return(store.get()));
  EXPECT_CALL(*store, IsAbleToSavePasswords)
      .Times(21)
      .WillRepeatedly(Return(true));

  // Enable password saving and generation in the client.
  EXPECT_CALL(password_manager_client_, IsSavingAndFillingEnabled(GURL()))
      .Times(21)
      .WillRepeatedly(Return(true));
  EXPECT_CALL(*password_manager_client_.GetPasswordFeatureManager(),
              IsGenerationEnabled())
      .Times(21)
      .WillRepeatedly(Return(true));

  // Set a last commited URL eligible for generation.
  URLGetter* url_getter = [URLGetter alloc];
  for (int i = 0; i < 21; ++i) {
    OCMStub([password_controller_ lastCommittedURL])
        .andCall(url_getter, @selector(lastCommittedURL));
  }
  // Set other expected calls on the controller when an eligible form for
  // generation is found.
  autofill::PasswordFormGenerationData form;
  for (int i = 0; i < 21; ++i) {
    OCMExpect([password_controller_ formEligibleForGenerationFound:form]);
  }

  // Inform the driver that 20 forms eligible for generation were found. The
  // driver doesn't know yet at this point whether proactive generation can also
  // be used.
  for (int i = 0; i < 20; ++i) {
    driver_->FormEligibleForGenerationFound(form);
  }

  // Inform that there are no saved credentials for the site so proactive
  // generation is set up on the forms eligible for generation.
  // Verify that the listeners for proactive generation are only attached for
  // the first 10 forms which corresponds to the max capacity of the queue.
  for (int i = 0; i < 10; ++i) {
    OCMExpect([[password_controller_ ignoringNonObjectArgs]
        attachListenersForPasswordGenerationFields:form
                                        forFrameId:""]);
  }
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
      onNoSavedCredentialsWithFrameId:""]);
  driver_->InformNoSavedCredentials(
      /*should_show_popup_without_passwords=*/false);

  // Inform the driver again that an eligible form for generation was found.
  // Since the queue is now cleared, verify that the listeners for proactive
  // generation are immediately attached since it is already known that there
  // are no saved credentials for the site.
  OCMExpect([[password_controller_ ignoringNonObjectArgs]
      attachListenersForPasswordGenerationFields:form
                                      forFrameId:""]);

  driver_->FormEligibleForGenerationFound(form);

  EXPECT_OCMOCK_VERIFY(password_controller_);
}