github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/wifi/iwl_test.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wifi
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/wpa/passphrase"
    15  )
    16  
    17  type GenerateConfigTestCase struct {
    18  	name string
    19  	args []string
    20  	exp  []byte
    21  	err  error
    22  }
    23  
    24  var (
    25  	EssidStub       = "stub"
    26  	IdStub          = "stub"
    27  	PassStub        = "123456789"
    28  	BadWpaPskPass   = "123"
    29  	expWpaPsk, _    = passphrase.Run(EssidStub, PassStub)
    30  	_, expWpaPskErr = passphrase.Run(EssidStub, BadWpaPskPass)
    31  
    32  	generateConfigTestcases = []GenerateConfigTestCase{
    33  		{
    34  			name: "No Pass Phrase",
    35  			args: []string{EssidStub},
    36  			exp:  []byte(fmt.Sprintf(nopassphrase, EssidStub)),
    37  			err:  nil,
    38  		},
    39  		{
    40  			name: "WPA-PSK",
    41  			args: []string{EssidStub, PassStub},
    42  			exp:  expWpaPsk,
    43  			err:  nil,
    44  		},
    45  		{
    46  			name: "WPA-EAP",
    47  			args: []string{EssidStub, PassStub, IdStub},
    48  			exp:  []byte(fmt.Sprintf(eap, EssidStub, IdStub, PassStub)),
    49  			err:  nil,
    50  		},
    51  		{
    52  			name: "WPA-PSK Error",
    53  			args: []string{EssidStub, BadWpaPskPass},
    54  			exp:  nil,
    55  			err:  fmt.Errorf("essid: %v, pass: %v : %v", EssidStub, BadWpaPskPass, expWpaPskErr),
    56  		},
    57  		{
    58  			name: "Invalid Args Length Error",
    59  			args: nil,
    60  			exp:  nil,
    61  			err:  fmt.Errorf("generateConfig needs 1, 2, or 3 args"),
    62  		},
    63  	}
    64  )
    65  
    66  func TestGenerateConfig(t *testing.T) {
    67  	for _, test := range generateConfigTestcases {
    68  		out, err := generateConfig(test.args...)
    69  		if !reflect.DeepEqual(err, test.err) || !bytes.Equal(out, test.exp) {
    70  			t.Logf("TEST %v", test.name)
    71  			fncCall := fmt.Sprintf("genrateConfig(%v)", test.args)
    72  			t.Errorf("%s\ngot:[%v, %v]\nwant:[%v, %v]", fncCall, string(out), err, string(test.exp), test.err)
    73  		}
    74  	}
    75  }
    76  
    77  func TestCellRE(t *testing.T) {
    78  	testcases := []struct {
    79  		s   string
    80  		exp bool
    81  	}{
    82  		{"blahblahblah\n   Cell 01:", true},
    83  		{"blahblahblah\n   Cell 01: blah blah", true},
    84  		{"\"Cell\"", false},
    85  		{"\"blah blah Cell blah blah\"", false},
    86  	}
    87  	for _, test := range testcases {
    88  		if out := cellRE.MatchString(test.s); out != test.exp {
    89  			t.Errorf("%s\ngot:%v\nwant:%v", test.s, out, test.exp)
    90  		}
    91  	}
    92  }
    93  
    94  func TestEssidRE(t *testing.T) {
    95  	testcases := []struct {
    96  		s   string
    97  		exp bool
    98  	}{
    99  		{"blahblahblah\n    ESSID:\"stub\"", true},
   100  		{"blahblahblah\n    ESSID:\"stub\"\n", true},
   101  		{"blahblahblah\n    ESSID:\"stub-stub\"", true},
   102  		{"blahblahblah\n    ESSID:\"stub-stub\"\n", true},
   103  		{"blah blah ESSID blah", false},
   104  	}
   105  	for _, test := range testcases {
   106  		if out := essidRE.MatchString(test.s); out != test.exp {
   107  			t.Errorf("%s\ngot:%v\nwant:%v", test.s, out, test.exp)
   108  		}
   109  	}
   110  }
   111  
   112  func TestEncKeyOptRE(t *testing.T) {
   113  	testcases := []struct {
   114  		s   string
   115  		exp bool
   116  	}{
   117  		{"blahblahblah\n      Encryption key:on\n", true},
   118  		{"blahblahblah\n      Encryption key:on", true},
   119  		{"blahblahblah\n      Encryption key:off\n", true},
   120  		{"blahblahblah\n      Encryption key:off", true},
   121  		{"blah blah Encryption key blah blah", false},
   122  		{"blah blah Encryption key:on  blah blah", false},
   123  		{"blah blah Encryption key:off blah blah", false},
   124  	}
   125  	for _, test := range testcases {
   126  		if out := encKeyOptRE.MatchString(test.s); out != test.exp {
   127  			t.Errorf("%s\ngot:%v\nwant:%v", test.s, out, test.exp)
   128  		}
   129  	}
   130  }
   131  
   132  func TestWpa2RE(t *testing.T) {
   133  	testcases := []struct {
   134  		s   string
   135  		exp bool
   136  	}{
   137  		{"blahblahblah\n            IE: IEEE 802.11i/WPA2 Version 1\n", true},
   138  		{"blahblahblah\n            IE: IEEE 802.11i/WPA2 Version 1", true},
   139  		{"blah blah IE: IEEE 802.11i/WPA2 Version 1", false},
   140  	}
   141  	for _, test := range testcases {
   142  		if out := wpa2RE.MatchString(test.s); out != test.exp {
   143  			t.Errorf("%s\ngot:%v\nwant:%v", test.s, out, test.exp)
   144  		}
   145  	}
   146  }
   147  
   148  func TestAuthSuitesRE(t *testing.T) {
   149  	testcases := []struct {
   150  		s   string
   151  		exp bool
   152  	}{
   153  		{"blahblahblah\n            Authentication Suites (1) : 802.1x\n", true},
   154  		{"blahblahblah\n            Authentication Suites (1) : 802.1x", true},
   155  		{"blahblahblah\n            Authentication Suites (1) : PSK\n", true},
   156  		{"blahblahblah\n            Authentication Suites (1) : PSK\n", true},
   157  		{"blahblahblah\n            Authentication Suites (2) : blah, blah\n", true},
   158  		{"blahblahblah\n            Authentication Suites (1) : other protocol\n", true},
   159  		{"blahblahblah\n            Authentication Suites (1) : other protocol", true},
   160  		{"blah blah Authentication Suites : blah blah", false},
   161  	}
   162  	for _, test := range testcases {
   163  		if out := authSuitesRE.MatchString(test.s); out != test.exp {
   164  			t.Errorf("%s\ngot:%v\nwant:%v", test.s, out, test.exp)
   165  		}
   166  	}
   167  }
   168  
   169  func TestParseIwlistOutput(t *testing.T) {
   170  	var (
   171  		o        []byte
   172  		exp, out []Option
   173  		err      error
   174  	)
   175  
   176  	// No WiFi present
   177  	o = nil
   178  	exp = nil
   179  	out = parseIwlistOut(o)
   180  	if !reflect.DeepEqual(out, exp) {
   181  		t.Errorf("\ngot:[%v]\nwant:[%v]", out, exp)
   182  	}
   183  
   184  	// Only 1 WiFi present
   185  	o = []byte(`
   186  wlan0    Scan completed :
   187            Cell 01 - Address: 00:00:00:00:00:01
   188                      Channel:001
   189                      Frequency:5.58 GHz (Channel 001)
   190                      Quality=1/2  Signal level=-23 dBm  
   191                      Encryption key:on
   192                      ESSID:"stub-wpa-eap-1"
   193                      Bit Rates:36 Mb/s; 48 Mb/s; 54 Mb/s
   194                      Mode:Master
   195                      Extra:tsf=000000000000000000
   196                      Extra: Last beacon: 1260ms ago
   197                      IE: Unknown: 000000000000000000
   198                      IE: Unknown: 000000000000000000
   199                      IE: Unknown: 000000000000000000
   200                      IE: IEEE 802.11i/WPA2 Version 1
   201                          Group Cipher : CCMP
   202                          Pairwise Ciphers (1) : CCMP
   203                          Authentication Suites (1) : 802.1x
   204                      IE: Unknown: 000000000000000000
   205                      IE: Unknown: 000000000000000000
   206                      IE: Unknown: 000000000000000000
   207                      IE: Unknown: 000000000000000000
   208                      IE: Unknown: 000000000000000000
   209  `)
   210  	exp = []Option{
   211  		{"stub-wpa-eap-1", WpaEap},
   212  	}
   213  	out = parseIwlistOut(o)
   214  	if !reflect.DeepEqual(out, exp) {
   215  		t.Errorf("\ngot:[%v]\nwant:[%v]", out, exp)
   216  	}
   217  
   218  	// Regular scenarios (many choices)
   219  	exp = []Option{
   220  		{"stub-wpa-eap-1", WpaEap},
   221  		{"stub-rsa-1", NoEnc},
   222  		{"stub-wpa-psk-1", WpaPsk},
   223  		{"stub-rsa-2", NoEnc},
   224  		{"stub-wpa-psk-2", WpaPsk},
   225  	}
   226  	o, err = ioutil.ReadFile("iwlistStubOutput.txt")
   227  	if err != nil {
   228  		t.Errorf("error reading iwlistStubOutput.txt: %v", err)
   229  	}
   230  	out = parseIwlistOut(o)
   231  	if !reflect.DeepEqual(out, exp) {
   232  		t.Errorf("\ngot:[%v]\nwant:[%v]", out, exp)
   233  	}
   234  }
   235  
   236  func BenchmarkParseIwlistOutput(b *testing.B) {
   237  	// Set Up
   238  	o, err := ioutil.ReadFile("iwlistStubOutput.txt")
   239  	if err != nil {
   240  		b.Errorf("error reading iwlistStubOutput.txt: %v", err)
   241  	}
   242  	for i := 0; i < b.N; i++ {
   243  		parseIwlistOut(o)
   244  	}
   245  }