github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/restic/id_test.go (about)

     1  package restic
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  var TestStrings = []struct {
     9  	id   string
    10  	data string
    11  }{
    12  	{"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", "foobar"},
    13  	{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
    14  	{"cc5d46bdb4991c6eae3eb739c9c8a7a46fe9654fab79c47b4fe48383b5b25e1c", "foo/bar"},
    15  	{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", "foo/../../baz"},
    16  }
    17  
    18  func TestID(t *testing.T) {
    19  	for _, test := range TestStrings {
    20  		id, err := ParseID(test.id)
    21  		if err != nil {
    22  			t.Error(err)
    23  		}
    24  
    25  		id2, err := ParseID(test.id)
    26  		if err != nil {
    27  			t.Error(err)
    28  		}
    29  		if !id.Equal(id2) {
    30  			t.Errorf("ID.Equal() does not work as expected")
    31  		}
    32  
    33  		ret, err := id.EqualString(test.id)
    34  		if err != nil {
    35  			t.Error(err)
    36  		}
    37  		if !ret {
    38  			t.Error("ID.EqualString() returned wrong value")
    39  		}
    40  
    41  		// test json marshalling
    42  		buf, err := id.MarshalJSON()
    43  		if err != nil {
    44  			t.Error(err)
    45  		}
    46  		want := `"` + test.id + `"`
    47  		if string(buf) != want {
    48  			t.Errorf("string comparison failed, wanted %q, got %q", want, string(buf))
    49  		}
    50  
    51  		var id3 ID
    52  		err = id3.UnmarshalJSON(buf)
    53  		if err != nil {
    54  			t.Fatal(err)
    55  		}
    56  		if !reflect.DeepEqual(id, id3) {
    57  			t.Error("ids are not equal")
    58  		}
    59  	}
    60  }