Showing posts with label rspec. Show all posts
Showing posts with label rspec. Show all posts

Monday, April 25, 2011

Making rspec working on my own computer

Added ethernet.rb  and made rspec work.

To run the rspec, eg:
sudo rspec socket_wrapper_spec.rb
...............

Finished in 7.61 seconds
15 examples, 0 failures

CANNOT be using Wireless connection for the rspec to work. MUST be using Ethernet.
commit c21eb650831d6921e33fa393e5f7d3cd3fdd2d1e

Boring details of how I got here
RawSocket mac should have 6 bytes
Failure/Error: let(:mac) { EtherShell::RawSocket.mac eth_device }
Errno::EPERM:
Operation not permitted - socket(2)

man 2 socket
SOCKET(2)                        Linux Programmer's Manual                       SOCKET(2)

NAME
       socket - create an endpoint for communication

SYNOPSIS
       #include           /* See NOTES */
       #include 

       int socket(int domain, int type, int protocol);

DESCRIPTION
       socket() creates an endpoint for communication and returns a descriptor.

       The  domain  argument  specifies  a communication domain; this selects the protocol
       family which will be  used  for  communication.   These  families  are  defined  in
       <sys/socket.h>.  The currently understood formats include:

       Name                Purpose                          Man page
       AF_UNIX, AF_LOCAL   Local communication              unix(7)
       AF_INET             IPv4 Internet protocols          ip(7)
       AF_INET6            IPv6 Internet protocols          ipv6(7)
       AF_IPX              IPX - Novell protocols
       AF_NETLINK          Kernel user interface device     netlink(7)
       AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
       AF_AX25             Amateur radio AX.25 protocol
       AF_ATMPVC           Access to raw ATM PVCs
       AF_APPLETALK        Appletalk                        ddp(7)
       AF_PACKET           Low level packet interface       packet(7)

..../ether_shell/spec/ether_shell$sudo rspec raw_socket_spec.rb
[sudo] password for haoqili:
.....

Finished in 0.06603 seconds
5 examples, 0 failures

/ethernet/spec$ sudo rspec raw_socket_spec.rb 
[sudo] password for haoqili: 
/usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in `const_missing': uninitialized constant ShellStub::EtherShell (NameError)
 from /usr/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
 from /home/haoqili/Desktop/rails/ethernet/spec/support/shell_stub.rb:2
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
 from /home/haoqili/Desktop/rails/ethernet/spec/spec_helper.rb:8
 from /home/haoqili/Desktop/rails/ethernet/spec/spec_helper.rb:8:in `each'
 from /home/haoqili/Desktop/rails/ethernet/spec/spec_helper.rb:8
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
 from /home/haoqili/Desktop/rails/ethernet/spec/raw_socket_spec.rb:3
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
 from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `autorun'
 from /usr/bin/rspec:19

In ./spec/raw_socket_spec.rb, ./spec/socket_wrapper_spec.rb
:%s/EtherShell/Ethernet/g
Didn't fix


After talking to Victor, he told me that I don't have ethernet.rb, the equivalent of ether_shell.rb. So modified ether_shell.rb into ethernet.rb.

Fixed.

Thursday, March 24, 2011

Read about Ruby Modules

RSpec
Read about Rspec Testing. Link 1. Link 2 gives a great introduction to RSpec, and good TDD example. let(). :colon_things

See how it's applied to Victor's ether_shell: git clone https://github.com/pwnall/ether_shell.git and navigate to its "spec" directory to run the rspec tests.

Install rspec: sudo gem install rspec.

Run an RSpec test to see it produces RSpec outputs! :)
ether_shell/spec/ether_shell$ rspec raw_socket_spec.rb
FFFFF

Failures:

1) RawSocket mac should have 6 bytes
Failure/Error: let(:mac) { EtherShell::RawSocket.mac eth_device }
Errno::EPERM:
Operation not permitted - socket(2)
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `initialize'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `new'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `socket'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:31:in `mac'
# ./raw_socket_spec.rb:5
# ./raw_socket_spec.rb:14

2) RawSocket mac should match ifconfig output
Failure/Error: let(:mac) { EtherShell::RawSocket.mac eth_device }
Errno::EPERM:
Operation not permitted - socket(2)
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `initialize'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `new'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `socket'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:31:in `mac'
# ./raw_socket_spec.rb:5
# ./raw_socket_spec.rb:18

3) RawSocket socket should be able to receive data
Failure/Error: before { @socket = EtherShell::RawSocket.socket eth_device }
Errno::EPERM:
Operation not permitted - socket(2)
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `initialize'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `new'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `socket'
# ./raw_socket_spec.rb:25

4) RawSocket socket should output a packet
Failure/Error: before { @socket = EtherShell::RawSocket.socket eth_device }
Errno::EPERM:
Operation not permitted - socket(2)
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `initialize'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `new'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `socket'
# ./raw_socket_spec.rb:25

5) RawSocket socket should receive some network noise
Failure/Error: before { @socket = EtherShell::RawSocket.socket eth_device }
Errno::EPERM:
Operation not permitted - socket(2)
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `initialize'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `new'
# /home/haoqili/Desktop/rails/secureSt/ether_shell/spec/../lib/ether_shell/raw_socket.rb:15:in `socket'
# ./raw_socket_spec.rb:25

Finished in 0.00203 seconds
5 examples, 5 failures