Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / wifi / wifi_test.cc
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
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <string>
7
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/cancelable_callback.h"
11 #include "base/command_line.h"
12 #include "base/file_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/time/time.h"
21 #include "components/wifi/wifi_service.h"
22
23 #if defined(OS_MACOSX)
24 #include "base/mac/scoped_nsautorelease_pool.h"
25 #endif
26
27 namespace wifi {
28
29 class WiFiTest {
30  public:
31   WiFiTest() {}
32   ~WiFiTest() {}
33
34   enum Result {
35     RESULT_ERROR = -2,
36     RESULT_WRONG_USAGE = -1,
37     RESULT_OK = 0,
38     RESULT_PENDING = 1,
39   };
40
41   Result Main(int argc, const char* argv[]);
42
43  private:
44   bool ParseCommandLine(int argc, const char* argv[]);
45
46   void Start() {}
47   void Finish(Result result) {
48     DCHECK_NE(RESULT_PENDING, result);
49     result_ = result;
50     if (base::MessageLoop::current())
51       base::MessageLoop::current()->Quit();
52   }
53
54 #if defined(OS_MACOSX)
55   // Without this there will be a mem leak on osx.
56   base::mac::ScopedNSAutoreleasePool scoped_pool_;
57 #endif
58
59   // Need AtExitManager to support AsWeakPtr (in NetLog).
60   base::AtExitManager exit_manager_;
61
62   Result result_;
63 };
64
65 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) {
66   if (!ParseCommandLine(argc, argv)) {
67     VLOG(0) <<  "Usage: " << argv[0] <<
68                 " [--list]"
69                 " [--get_key]"
70                 " [--get_properties]"
71                 " [--create]"
72                 " [--connect]"
73                 " [--disconnect]"
74                 " [--network_guid=<network_guid>]"
75                 " [--frequency=0|2400|5000]"
76                 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
77                 " [--password=<wifi_password>]"
78                 " [<network_guid>]\n";
79     return RESULT_WRONG_USAGE;
80   }
81
82   base::MessageLoopForIO loop;
83   result_ = RESULT_PENDING;
84
85   return result_;
86 }
87
88 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) {
89   CommandLine::Init(argc, argv);
90   const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
91   std::string network_guid =
92       parsed_command_line.GetSwitchValueASCII("network_guid");
93   std::string frequency =
94       parsed_command_line.GetSwitchValueASCII("frequency");
95   std::string password =
96       parsed_command_line.GetSwitchValueASCII("password");
97   std::string security =
98       parsed_command_line.GetSwitchValueASCII("security");
99
100   if (parsed_command_line.GetArgs().size() == 1) {
101 #if defined(OS_WIN)
102     network_guid = WideToASCII(parsed_command_line.GetArgs()[0]);
103 #else
104     network_guid = parsed_command_line.GetArgs()[0];
105 #endif
106   }
107
108 #if defined(OS_WIN)
109   if (parsed_command_line.HasSwitch("debug"))
110     MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK);
111 #endif
112
113 #if defined(OS_WIN) || defined(OS_MACOSX)
114   scoped_ptr<WiFiService> wifi_service(WiFiService::Create());
115 #else
116   scoped_ptr<WiFiService> wifi_service(WiFiService::CreateForTest());
117 #endif
118
119   wifi_service->Initialize(NULL);
120
121   if (parsed_command_line.HasSwitch("list")) {
122     base::ListValue network_list;
123     wifi_service->GetVisibleNetworks(std::string(), &network_list);
124     VLOG(0) << network_list;
125     return true;
126   }
127
128   if (parsed_command_line.HasSwitch("get_properties")) {
129     if (network_guid.length() > 0) {
130       base::DictionaryValue properties;
131       std::string error;
132       wifi_service->GetProperties(network_guid, &properties, &error);
133       VLOG(0) << error << ":\n" << properties;
134       return true;
135     }
136   }
137
138   // Optional properties (frequency, password) to use for connect or create.
139   scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue());
140
141   if (!frequency.empty()) {
142     int value = 0;
143     if (base::StringToInt(frequency, &value)) {
144       properties->SetInteger("WiFi.Frequency", value);
145       // fall through to connect.
146     }
147   }
148
149   if (!password.empty())
150     properties->SetString("WiFi.Passphrase", password);
151
152   if (!security.empty())
153     properties->SetString("WiFi.Security", security);
154
155   if (parsed_command_line.HasSwitch("create")) {
156     if (!network_guid.empty()) {
157       std::string error;
158       std::string new_network_guid;
159       properties->SetString("WiFi.SSID", network_guid);
160       VLOG(0) << "Creating Network: " << *properties;
161       wifi_service->CreateNetwork(false,
162                                   properties.Pass(),
163                                   &new_network_guid,
164                                   &error);
165       VLOG(0) << error << ":\n" << new_network_guid;
166       return true;
167     }
168   }
169
170   if (parsed_command_line.HasSwitch("connect")) {
171     if (!network_guid.empty()) {
172       std::string error;
173       if (!properties->empty()) {
174         VLOG(0) << "Using connect properties: " << *properties;
175         wifi_service->SetProperties(network_guid,
176                                     properties.Pass(),
177                                     &error);
178       }
179       wifi_service->StartConnect(network_guid, &error);
180       VLOG(0) << error;
181       return true;
182     }
183   }
184
185   if (parsed_command_line.HasSwitch("disconnect")) {
186     if (network_guid.length() > 0) {
187       std::string error;
188       wifi_service->StartDisconnect(network_guid, &error);
189       VLOG(0) << error;
190       return true;
191     }
192   }
193
194   if (parsed_command_line.HasSwitch("get_key")) {
195     if (network_guid.length() > 0) {
196       std::string error;
197       std::string key_data;
198       wifi_service->GetKeyFromSystem(network_guid,
199                                      &key_data,
200                                      &error);
201       VLOG(0) << key_data << error;
202       return true;
203     }
204   }
205
206   return false;
207 }
208
209 }  // namespace wifi
210
211 int main(int argc, const char* argv[]) {
212   CommandLine::Init(argc, argv);
213   logging::LoggingSettings settings;
214   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
215   logging::InitLogging(settings);
216
217   wifi::WiFiTest wifi_test;
218   return wifi_test.Main(argc, argv);
219 }