github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/mixnet/encoding_test.go (about)

     1  // Copyright (c) 2016, Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License0.
    14  
    15  package mixnet
    16  
    17  import "testing"
    18  
    19  func stringsCompare(strs1, strs2 []string) bool {
    20  	if len(strs1) != len(strs2) {
    21  		return false
    22  	}
    23  	for i := range strs1 {
    24  		if strs1[i] != strs2[i] {
    25  			return false
    26  		}
    27  	}
    28  	return true
    29  }
    30  
    31  func TestMarshalDirective(t *testing.T) {
    32  	id := uint64(7654321)
    33  
    34  	d := &Directive{
    35  		Type:             DirectiveType_CREATE.Enum(),
    36  		Addrs:            []string{"192,168.1.1:2002", "192,168.1.2:9001", "192,168.1.3:8002"},
    37  		Error:            nil,
    38  		XXX_unrecognized: nil,
    39  	}
    40  
    41  	dBytes, err := marshalDirective(id, d)
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  
    46  	res := new(Directive)
    47  	err = unmarshalDirective(dBytes, res)
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  
    52  	if *d.Type != *res.Type || !stringsCompare(d.Addrs, res.Addrs) || d.Error != res.Error {
    53  		t.Error("Marshalling failed")
    54  	}
    55  }