File: obfuscated_gaia_id_fetcher_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (107 lines) | stat: -rw-r--r-- 3,466 bytes parent folder | download
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"

#include <string>

#include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.h"
#include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h"
#include "net/url_request/test_url_fetcher_factory.h"

namespace {

static const char kGoodData[] = "{ \"id\" : \"My-channel-id\" }";
static const char kBadJsonData[] = "I am not a JSON string";
static const char kDictionaryMissingIdData[] = "{ \"foo\" : \"bar\" }";
static const char kNonDictionaryJsonData[] = "{ 0.5 }";

// Delegate class for catching notifications from the ObfuscatedGaiaIdFetcher.
class TestDelegate : public extensions::ObfuscatedGaiaIdFetcher::Delegate {
 public:
  void OnObfuscatedGaiaIdFetchSuccess(
      const std::string& obfuscated_id) override {
    succeeded_ = true;
  }
  void OnObfuscatedGaiaIdFetchFailure(
      const GoogleServiceAuthError& error) override {
    failed_ = true;
  }
  TestDelegate() : succeeded_(false), failed_(false) {}
  ~TestDelegate() override {}
  bool succeeded() const { return succeeded_; }
  bool failed() const { return failed_; }

 private:
  bool succeeded_;
  bool failed_;
  DISALLOW_COPY_AND_ASSIGN(TestDelegate);
};

}  // namespace

namespace extensions {

TEST(ObfuscatedGaiaIdFetcherTest, ParseResponse) {
  // Try a good response string.
  std::string channel_id_out1;
  bool ret1 = ObfuscatedGaiaIdFetcher::ParseResponse(
      kGoodData, &channel_id_out1);
  EXPECT_EQ("My-channel-id", channel_id_out1);
  EXPECT_TRUE(ret1);

  // Try badly formatted JSON.
  std::string channel_id_out2;
  bool ret2 = ObfuscatedGaiaIdFetcher::ParseResponse(
      kBadJsonData, &channel_id_out2);
  EXPECT_TRUE(channel_id_out2.empty());
  EXPECT_FALSE(ret2);

  // Try a JSON dictionary with no "id" value.
  std::string channel_id_out3;
  bool ret3 = ObfuscatedGaiaIdFetcher::ParseResponse(
      kDictionaryMissingIdData, &channel_id_out3);
  EXPECT_TRUE(channel_id_out3.empty());
  EXPECT_FALSE(ret3);

  // Try JSON, but not a dictionary.
  std::string channel_id_out4;
  bool ret4 = ObfuscatedGaiaIdFetcher::ParseResponse(
      kNonDictionaryJsonData, &channel_id_out4);
  EXPECT_TRUE(channel_id_out4.empty());
  EXPECT_FALSE(ret4);
}

TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) {
  TestDelegate delegate;
  ObfuscatedGaiaIdFetcher fetcher(&delegate);

  net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
  url_fetcher.set_status(
      net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
  url_fetcher.SetResponseString(kGoodData);

  // Test the happy path.
  fetcher.ProcessApiCallSuccess(&url_fetcher);
  EXPECT_TRUE(delegate.succeeded());
}

TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) {
  TestDelegate delegate;
  ObfuscatedGaiaIdFetcher fetcher(&delegate);

  net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
  url_fetcher.set_status(
      net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
  url_fetcher.SetResponseString(kDictionaryMissingIdData);

  // Test with bad data, ensure it fails.
  fetcher.ProcessApiCallSuccess(&url_fetcher);
  EXPECT_TRUE(delegate.failed());

  // TODO(petewil): add case for when the base class fails and calls
  // ProcessApiCallFailure
}

}  // namespace extensions