github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/siad/daemon_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // TestUnitProcessNetAddr probes the 'processNetAddr' function.
     8  func TestUnitProcessNetAddr(t *testing.T) {
     9  	testVals := struct {
    10  		inputs          []string
    11  		expectedOutputs []string
    12  	}{
    13  		inputs:          []string{"9980", ":9980", "localhost:9980", "test.com:9980", "192.168.14.92:9980"},
    14  		expectedOutputs: []string{":9980", ":9980", "localhost:9980", "test.com:9980", "192.168.14.92:9980"},
    15  	}
    16  	for i, input := range testVals.inputs {
    17  		output := processNetAddr(input)
    18  		if output != testVals.expectedOutputs[i] {
    19  			t.Error("unexpected result", i)
    20  		}
    21  	}
    22  }
    23  
    24  // TestUnitProcessModules tests that processModules correctly processes modules
    25  // passed to the -M / --modules flag.
    26  func TestUnitProcessModules(t *testing.T) {
    27  	// Test valid modules.
    28  	testVals := []struct {
    29  		in  string
    30  		out string
    31  	}{
    32  		{"cghmrtwe", "cghmrtwe"},
    33  		{"CGHMRTWE", "cghmrtwe"},
    34  		{"c", "c"},
    35  		{"g", "g"},
    36  		{"h", "h"},
    37  		{"m", "m"},
    38  		{"r", "r"},
    39  		{"t", "t"},
    40  		{"w", "w"},
    41  		{"e", "e"},
    42  		{"C", "c"},
    43  		{"G", "g"},
    44  		{"H", "h"},
    45  		{"M", "m"},
    46  		{"R", "r"},
    47  		{"T", "t"},
    48  		{"W", "w"},
    49  		{"E", "e"},
    50  	}
    51  	for _, testVal := range testVals {
    52  		out, err := processModules(testVal.in)
    53  		if err != nil {
    54  			t.Error("processModules failed with error:", err)
    55  		}
    56  		if out != testVal.out {
    57  			t.Errorf("processModules returned incorrect modules: expected %s, got %s\n", testVal.out, out)
    58  		}
    59  	}
    60  
    61  	// Test invalid modules.
    62  	invalidModules := []string{"abdfijklnopqsuvxyz", "cghmrtwez", "cz", "z", "cc", "ccz", "ccm", "cmm", "ccmm"}
    63  	for _, invalidModule := range invalidModules {
    64  		_, err := processModules(invalidModule)
    65  		if err == nil {
    66  			t.Error("processModules didn't error on invalid module:", invalidModule)
    67  		}
    68  	}
    69  }
    70  
    71  // TestUnitProcessConfig probes the 'processConfig' function.
    72  func TestUnitProcessConfig(t *testing.T) {
    73  	// Test valid configs.
    74  	testVals := struct {
    75  		inputs          [][]string
    76  		expectedOutputs [][]string
    77  	}{
    78  		inputs: [][]string{
    79  			{"9980", "9981", "9982", "cghmrtwe"},
    80  			{":9980", ":9981", ":9982", "CGHMRTWE"},
    81  		},
    82  		expectedOutputs: [][]string{
    83  			{":9980", ":9981", ":9982", "cghmrtwe"},
    84  			{":9980", ":9981", ":9982", "cghmrtwe"},
    85  		},
    86  	}
    87  	var config Config
    88  	for i := range testVals.inputs {
    89  		config.Siad.APIaddr = testVals.inputs[i][0]
    90  		config.Siad.RPCaddr = testVals.inputs[i][1]
    91  		config.Siad.HostAddr = testVals.inputs[i][2]
    92  		config, err := processConfig(config)
    93  		if err != nil {
    94  			t.Error("processConfig failed with error:", err)
    95  		}
    96  		if config.Siad.APIaddr != testVals.expectedOutputs[i][0] {
    97  			t.Error("processing failure at check", i, 0)
    98  		}
    99  		if config.Siad.RPCaddr != testVals.expectedOutputs[i][1] {
   100  			t.Error("processing failure at check", i, 1)
   101  		}
   102  		if config.Siad.HostAddr != testVals.expectedOutputs[i][2] {
   103  			t.Error("processing failure at check", i, 2)
   104  		}
   105  	}
   106  
   107  	// Test invalid configs.
   108  	invalidModule := "z"
   109  	config.Siad.Modules = invalidModule
   110  	_, err := processConfig(config)
   111  	if err == nil {
   112  		t.Error("processModules didn't error on invalid module:", invalidModule)
   113  	}
   114  }