Example #1
0
func RoundTripTest(t *testing.T, codec mc.Codec, o1, o2 interface{}) bool {
	m1, err := mc.Marshal(codec, o1)
	if err != nil {
		t.Error(err)
		return false
	}

	if err := mc.Unmarshal(codec, m1, o2); err != nil {
		t.Log(m1)
		t.Error(err)
		return false
	}

	m2, err := mc.Marshal(codec, o2)
	if err != nil {
		t.Error(err)
		return false
	}

	if !bytes.Equal(m1, m2) {
		t.Error("marshalled values not equal")
		t.Log(m1)
		t.Log(m2)
		return false
	}

	return true
}
Example #2
0
func HeaderTest(t *testing.T, codec mc.Multicodec, o interface{}) bool {
	h := codec.Header()
	if len(h) < 4 {
		t.Error("header should be >4 bytes (varint, /, _, \\n):", h)
		return false
	}

	if h[1] != byte('/') || h[len(h)-1] != byte('\n') {
		t.Error("malformed header (no / or \\n)")
		return false
	}

	m1, err := mc.Marshal(codec, o)
	if err != nil {
		t.Error(err)
		return false
	}

	if !bytes.HasPrefix(m1, h) {
		t.Error("marshalled data does not have multicodec header")
		return false
	}

	return true
}
Example #3
0
// Test decoding and encoding a json and cbor file
func TestCodecsDecodeEncode(t *testing.T) {
	for fname, testfile := range codedFiles {
		var n ipld.Node
		codec := Multicodec()

		if err := mc.Unmarshal(codec, testfile, &n); err != nil {
			t.Log(testfile)
			t.Error(err)
			continue
		}

		linksExpected := map[string]ipld.Link{
			"abc": ipld.Link{
				"mlink": "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V",
			},
		}
		linksActual := ipld.Links(n)
		if !reflect.DeepEqual(linksExpected, linksActual) {
			t.Logf("Expected: %#v", linksExpected)
			t.Logf("Actual:   %#v", linksActual)
			t.Logf("node: %#v\n", n)
			t.Error("Links are not expected in " + fname)
			continue
		}

		encoded, err := mc.Marshal(codec, &n)
		if err != nil {
			t.Error(err)
			return
		}

		if !bytes.Equal(testfile, encoded) {
			t.Error("marshalled values not equal in " + fname)
			t.Log(string(testfile))
			t.Log(string(encoded))
			t.Log(testfile)
			t.Log(encoded)
		}
	}
}
Example #4
0
func main() {
	infile := flag.String("i", "", "Input file")
	outfile := flag.String("o", "", "Output file")
	codecid := flag.String("c", "", "Multicodec to use")
	flag.Parse()
	file, err := ioutil.ReadFile(*infile)
	if err != nil {
		panic(err)
	}

	var n ipld.Node
	codec := coding.Multicodec()

	if err := mc.Unmarshal(codec, file, &n); err != nil {
		panic(err)
	}

	codec = codecByName(*codecid)
	if codec == nil {
		panic("Could not find codec " + *codecid)
	}

	delete(n, ipld.CodecKey)

	encoded, err := mc.Marshal(codec, &n)
	if err != nil {
		panic(err)
	}

	f, err := os.Create(*outfile)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	_, err = f.Write(encoded)
	if err != nil {
		panic(err)
	}
}
Example #5
0
// Test decoding and encoding a json file
func TestJsonDecodeEncode(t *testing.T) {
	var n ipld.Node
	codec := Multicodec()

	if err := mc.Unmarshal(codec, json_testfile, &n); err != nil {
		t.Log(json_testfile)
		t.Error(err)
		return
	}

	linksExpected := map[string]ipld.Link{
		"abc": ipld.Link{
			"mlink": "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V",
		},
	}
	linksActual := ipld.Links(n)
	if !reflect.DeepEqual(linksExpected, linksActual) {
		t.Log(linksExpected)
		t.Log(linksActual)
		t.Logf("node: %#v\n", n)
		t.Fatalf("Links are not expected")
	}

	encoded, err := mc.Marshal(codec, &n)
	if err != nil {
		t.Error(err)
		return
	}

	if !bytes.Equal(json_testfile, encoded) {
		t.Error("marshalled values not equal")
		t.Log(string(json_testfile))
		t.Log(string(encoded))
		t.Log(json_testfile)
		t.Log(encoded)
	}
}