wpa_cli_ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzdlMDNkNGI4NzczYmEwZTVmNjc4ODgxYjVkMGVlOTYyZTFkNGNkMA==
4
+ YmFhNTkxZjFiNzlhYTAzNDgxYzE3MzJjZTA5YjM3ZTljZWU4NjAzYQ==
5
5
  data.tar.gz: !binary |-
6
- YTI2ZWE0ZWJmMzRlMjYyYWU4NTlmYjU3ZWM5NDUzMDg4NDAxYmZiOA==
6
+ OWZmMDQ2OTQ3Y2JlNzI5MTE2MGZhMTA1MTcyZGEzMzU3MTZkOWY5NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDYxM2Y1ZTcxYWJmMzM1ZDVlZjRkZDY1NTgzYmZhZTE0NDJkM2ZjMGNjZTM1
10
- MjY2NTkxMTFlMGU5NTI5MGU5YWRlZGM2MmU1MDhkOWM1YmU4NmJlMGU2Zjk3
11
- MDEyZTg4ZGI4YzA5N2ZjNDVjNzljZDZiMmMxODhhMzIzMGViYzM=
9
+ MDhmMDljOGFkNmRmNjM1NDA1YWYwYWEyMzE3OWY4YWE0NjhlMGIxNzMwNDQz
10
+ NmI5NDNjYTIyMzkyMmM1YjEwM2VhZDk0YTY0N2RmY2Y0NzRmYTQ2YjgyODdk
11
+ NDJlYTk4NzllMzMzMjQyYzIxOGZlNDg3MTIyMWQ1NjAyOTc1N2Y=
12
12
  data.tar.gz: !binary |-
13
- MzI0NzNlOTVkMTZkMjk3MTRkMDViNGY5MGI5N2I1NGJiNzg0OTNlMjE4MzJh
14
- MmQ1ZGQwMmM1ZjI3YzU5NDgzZmVmMmUxZWJjMmJkOGQ5ZGFlMTdhYWJmMWIx
15
- NjUzZDc4MWJiMDZlNjhjNzdkZDg0ZGJmZjFkZDNiNWYyZTQxODc=
13
+ MzNkZWVhZGFmNzU1YjUyNTk0YWM2Y2IyMTA3ODFjNWEzZmVhYTVlODUwZmYw
14
+ OGY4Yjc1ZjFiNGYzNDZhYmI5ZDA2OGUzZDY4ZWU5MjZkN2MxZGJiZTU5Y2Ri
15
+ NWJmMzlmYzVhMDg2M2I3MmU5N2VmZTk1ZTdkMGVhZmU4YmVjN2M=
data/AUTHORS CHANGED
@@ -1,3 +1,7 @@
1
1
  AUTHORS
2
2
 
3
3
  - Chris Lowis <chris.lowis at bbc.co.uk>
4
+
5
+ Contributors
6
+
7
+ - Jordan S <https://github.com/JSchaenzle>
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in wpa_cli_ruby.gemspec
4
4
  gemspec
5
+ gem 'mocha'
@@ -42,5 +42,14 @@ eos
42
42
  def save_config
43
43
  "Selected interface 'wlan0'\nOK\n"
44
44
  end
45
+
46
+ def list_networks
47
+ response = <<-eos
48
+ Selected interface 'wlan0'
49
+ network id / ssid / bssid / flags
50
+ 0 My Awesome Network any [CURRENT]
51
+ eos
52
+ end
53
+
45
54
  end
46
55
  end
@@ -1,3 +1,3 @@
1
1
  module WpaCliRuby
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  module WpaCliRuby
2
2
  class EnableNetworkFailure < Exception; end
3
+ class SelectNetworkFailure < Exception; end
3
4
  class SetNetworkFailure < Exception; end
4
5
  class NetworkNotFound < Exception; end
5
6
  class SaveConfigFailure < Exception; end
@@ -20,12 +21,45 @@ module WpaCliRuby
20
21
  end
21
22
  end
22
23
 
24
+ class ListNetworkResult < Struct.new(:network_id, :ssid, :bssid, :flags)
25
+ def initialize(*args)
26
+ super(*args)
27
+ end
28
+
29
+ # Instantiate from a tab delimited string (as given by `wpa_cli list_networks`)
30
+ #
31
+ # @param [String] tab delimited string in the form network_id\tssid\tbssid\tflags
32
+ def self.from_string(string)
33
+ new(*string.split("\t"))
34
+ end
35
+ end
36
+
23
37
  class Response < Struct.new(:interface, :status)
24
38
  def ok?
25
39
  status == "OK"
26
40
  end
27
41
  end
28
42
 
43
+ class StatusResponse
44
+ attr_reader :interface
45
+
46
+ def initialize(iface, items)
47
+ @interface = iface
48
+ @items = items
49
+ end
50
+
51
+ def method_missing(meth, *args, &block)
52
+ if not @items[meth.to_s].nil?
53
+ # run_find_by_method($1, *args, &block)
54
+ @items[meth.to_s]
55
+ else
56
+ super # You *must* call super if you don't handle the
57
+ # method, otherwise you'll mess up Ruby's method
58
+ # lookup.
59
+ end
60
+ end
61
+ end
62
+
29
63
  def initialize(wrapper = WpaCliWrapper.new)
30
64
  @wrapper = wrapper
31
65
  end
@@ -41,6 +75,12 @@ module WpaCliRuby
41
75
  results.map { |result| ScanResult.from_string(result) }
42
76
  end
43
77
 
78
+ def list_networks
79
+ response = @wrapper.list_networks
80
+ interface, header, *results = response.split("\n")
81
+ results.map { |result| ListNetworkResult.from_string(result) }
82
+ end
83
+
44
84
  def add_network
45
85
  response = @wrapper.add_network
46
86
  _, status = response.split("\n")
@@ -80,11 +120,35 @@ module WpaCliRuby
80
120
  response
81
121
  end
82
122
 
123
+ def select_network(network_id)
124
+ response = @wrapper.select_network(network_id)
125
+ response = parse_interface_status_response(response)
126
+ raise SelectNetworkFailure unless response.ok?
127
+
128
+ response
129
+ end
130
+
131
+ def get_status
132
+ response = @wrapper.get_status
133
+ parse_status_response(response)
134
+ end
135
+
136
+ def set_ap_scan(val)
137
+ @wrapper.set_ap_scan(val)
138
+ end
139
+
83
140
  private
84
141
  def parse_interface_status_response(response)
85
142
  interface_response, status = response.split("\n")
86
143
  interface = interface_response.scan(/'(.*)'/).flatten.first
87
144
  Response.new(interface, status)
88
145
  end
146
+
147
+ def parse_status_response(response)
148
+ interface_response, *status = response.split("\n")
149
+ interface = interface_response.scan(/'(.*)'/).flatten.first
150
+ status_items = Hash[status.map{|s| s.split("=")}]
151
+ StatusResponse.new(interface, status_items)
152
+ end
89
153
  end
90
154
  end
@@ -1,47 +1,60 @@
1
1
  module WpaCliRuby
2
2
  class WpaCliWrapper
3
- def execute(cmd)
4
- %x[#{cmd}]
3
+
4
+ def execute(*args)
5
+ IO.popen(["wpa_cli"] + args) do |io|
6
+ io.read
7
+ end
5
8
  end
6
-
9
+
7
10
  def self.available?
8
11
  system("which wpa_cli > /dev/null 2>&1")
9
12
  end
10
13
 
11
14
  def scan
12
- cmd = "wpa_cli scan"
13
- execute(cmd)
15
+ execute("scan")
14
16
  end
15
17
 
16
18
  def scan_results
17
- cmd = "wpa_cli scan_results"
18
- execute(cmd)
19
+ execute("scan_results")
19
20
  end
20
21
 
21
22
  def add_network
22
- cmd = "wpa_cli add_network"
23
- execute(cmd)
23
+ execute("add_network")
24
24
  end
25
25
 
26
26
  def set_network(network_id, key, value)
27
- value = "\"#{value}\"" unless value.is_a? Symbol
28
- cmd = "wpa_cli set_network #{network_id} #{key} '#{value}'"
29
- execute(cmd)
27
+ value = "\"#{value}\"" unless value.is_a?(Symbol)
28
+ execute("set_network", "#{network_id}", key, value.to_s)
30
29
  end
31
30
 
32
31
  def get_network(network_id, key)
33
- cmd = "wpa_cli get_network #{network_id} #{key}"
34
- execute(cmd)
32
+ execute("get_network", "#{network_id}", key)
33
+ end
34
+
35
+ def list_networks
36
+ execute("list_networks")
35
37
  end
36
38
 
37
39
  def enable_network(network_id)
38
- cmd = "wpa_cli enable_network #{network_id}"
39
- execute(cmd)
40
+ execute("enable_network", "#{network_id}")
41
+ end
42
+
43
+ def select_network(network_id)
44
+ execute("select_network", "#{network_id}")
40
45
  end
41
46
 
42
47
  def save_config
43
- cmd = "wpa_cli save_config"
44
- execute(cmd)
48
+ execute("save_config")
45
49
  end
50
+
51
+ def get_status
52
+ execute("status")
53
+ end
54
+
55
+ def set_ap_scan(val)
56
+ execute("ap_scan", val.to_s)
57
+ end
58
+
46
59
  end
47
60
  end
@@ -90,6 +90,10 @@ describe WpaCliRuby do
90
90
  end
91
91
  end
92
92
 
93
+ describe "list_networks" do
94
+
95
+ end
96
+
93
97
  describe "scan_results" do
94
98
  before do
95
99
  response = <<-eos
@@ -110,6 +114,90 @@ eos
110
114
  assert_equal 2437, scan_results[0].frequency
111
115
 
112
116
  assert_equal 'ssid2', scan_results[1].ssid
117
+ assert_equal -57, scan_results[1].signal_level
118
+ assert_equal 2412, scan_results[1].frequency
113
119
  end
114
120
  end
121
+
122
+ describe "get_status" do
123
+
124
+ describe "when disconnected" do
125
+ before do
126
+ response = <<-eos
127
+ Selected interface 'wlan0'
128
+ wpa_state=DISCONNECTED
129
+ address=80:1f:02:94:4c:f3
130
+ eos
131
+ @wrapper.expects(:get_status).returns(response)
132
+ end
133
+
134
+ it "returns interface value of wlan0" do
135
+ status = @wpa_cli.get_status
136
+ assert_equal "wlan0", status.interface
137
+ end
138
+
139
+ it "returns wpa_state value of DISCONNECTED" do
140
+ # status_results = @wpa_cli.status
141
+ status = @wpa_cli.get_status
142
+ assert_equal "DISCONNECTED", status.wpa_state
143
+ end
144
+
145
+ it "returns address value of 80:1f:02:94:4c:f3" do
146
+ status = @wpa_cli.get_status
147
+ assert_equal "80:1f:02:94:4c:f3", status.address
148
+ end
149
+
150
+ end
151
+
152
+ describe "when connected to a WPA2 network" do
153
+ before do
154
+ response = <<-eos
155
+ Selected interface 'wlan1'
156
+ bssid=64:0f:29:09:6d:d9
157
+ ssid=A Great Network
158
+ id=0
159
+ mode=station
160
+ pairwise_cipher=CCMP
161
+ group_cipher=TKIP
162
+ key_mgmt=WPA2-PSK
163
+ wpa_state=COMPLETED
164
+ ip_address=192.168.11.105
165
+ address=80:1f:02:94:4c:f3
166
+ eos
167
+ @wrapper.expects(:get_status).returns(response)
168
+ end
169
+
170
+ it "returns interface value of wlan1" do
171
+ status = @wpa_cli.get_status
172
+ assert_equal "wlan1", status.interface
173
+ end
174
+
175
+ it "returns id value of 0" do
176
+ status = @wpa_cli.get_status
177
+ assert_equal "0", status.id
178
+ end
179
+
180
+ it "returns wpa_state value of COMPLETED" do
181
+ status = @wpa_cli.get_status
182
+ assert_equal "COMPLETED", status.wpa_state
183
+ end
184
+
185
+ it "returns ssid value of A Great Network" do
186
+ status = @wpa_cli.get_status
187
+ assert_equal "A Great Network", status.ssid
188
+ end
189
+
190
+ it "returns group_cipher value of TKIP" do
191
+ status = @wpa_cli.get_status
192
+ assert_equal "TKIP", status.group_cipher
193
+ end
194
+
195
+ it "returns key_mgmt value of WPA2-PSK" do
196
+ status = @wpa_cli.get_status
197
+ assert_equal "WPA2-PSK", status.key_mgmt
198
+ end
199
+
200
+ end
201
+
202
+ end
115
203
  end
@@ -7,54 +7,75 @@ describe WpaCliRuby do
7
7
 
8
8
  describe "scan" do
9
9
  it "calls execute with the correct string" do
10
- @wrapper.expects(:execute).with("wpa_cli scan")
10
+ @wrapper.expects(:execute).with("scan")
11
11
  @wrapper.scan
12
12
  end
13
13
  end
14
14
 
15
15
  describe "scan_results" do
16
16
  it "calls execute with the correct string" do
17
- @wrapper.expects(:execute).with("wpa_cli scan_results")
17
+ @wrapper.expects(:execute).with("scan_results")
18
18
  @wrapper.scan_results
19
19
  end
20
20
  end
21
21
 
22
22
  describe "add_network" do
23
23
  it "calls execute with the correct string" do
24
- @wrapper.expects(:execute).with("wpa_cli add_network")
24
+ @wrapper.expects(:execute).with("add_network")
25
25
  @wrapper.add_network
26
26
  end
27
27
  end
28
28
 
29
29
  describe "set_network" do
30
30
  it "calls execute with the correct string" do
31
- @wrapper.expects(:execute).with("wpa_cli set_network 0 ssid '\"network_ssid\"'")
31
+ @wrapper.expects(:execute).with("set_network", "0", "ssid", "\"network_ssid\"")
32
32
  @wrapper.set_network(0, 'ssid', 'network_ssid')
33
33
  end
34
34
  it "treats symbols as unquoted values" do
35
- @wrapper.expects(:execute).with("wpa_cli set_network 0 key_mgmt 'NONE'")
35
+ @wrapper.expects(:execute).with("set_network", "0", "key_mgmt", "NONE")
36
36
  @wrapper.set_network(0, 'key_mgmt', :NONE)
37
37
  end
38
38
  end
39
39
 
40
40
  describe "get_network" do
41
41
  it "calls execute with the correct string" do
42
- @wrapper.expects(:execute).with("wpa_cli get_network 0 ssid")
42
+ @wrapper.expects(:execute).with("get_network", "0", "ssid")
43
43
  @wrapper.get_network(0, 'ssid')
44
44
  end
45
45
  end
46
46
 
47
47
  describe "enable_network" do
48
48
  it "calls execute with the correct string" do
49
- @wrapper.expects(:execute).with("wpa_cli enable_network 0")
49
+ @wrapper.expects(:execute).with("enable_network", "0")
50
50
  @wrapper.enable_network(0)
51
51
  end
52
52
  end
53
53
 
54
54
  describe "save_config" do
55
55
  it "calls execute with the correct string" do
56
- @wrapper.expects(:execute).with("wpa_cli save_config")
56
+ @wrapper.expects(:execute).with("save_config")
57
57
  @wrapper.save_config
58
58
  end
59
59
  end
60
+
61
+ describe "list_networks" do
62
+ it "calls execute with correct string" do
63
+ @wrapper.expects(:execute).with("list_networks")
64
+ @wrapper.list_networks
65
+ end
66
+ end
67
+
68
+ describe "get_status" do
69
+ it "calls execute with correct string" do
70
+ @wrapper.expects(:execute).with("status")
71
+ @wrapper.get_status
72
+ end
73
+ end
74
+
75
+ describe "set_ap_scan" do
76
+ it "calls execute with correct value" do
77
+ @wrapper.expects(:execute).with("ap_scan", "1")
78
+ @wrapper.set_ap_scan(1)
79
+ end
80
+ end
60
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wpa_cli_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Lowis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-20 00:00:00.000000000 Z
11
+ date: 2014-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler