~moody/ninep

ref: 7b4c0484154111591a670ce6807bfbda3e864f36 ninep/dir_test.go -rw-r--r-- 1.4 KiB
7b4c0484 — Jacob Moody More docs touchup 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package ninep

import (
	"bytes"
	"testing"
)

func TestDir(t *testing.T) {

	tab := []struct {
		d   *Dir
		exp []byte
	}{
		{&Dir{Qid{3, 4, 5}, 6, 7, 8, 9, "hello.txt", "moody", "moody", "moody"},
			[]byte{0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x05, 0x00, 0x6d, 0x6f, 0x6f, 0x64, 0x79, 0x05, 0x00, 0x6d, 0x6f, 0x6f, 0x64, 0x79, 0x05, 0x00, 0x6d, 0x6f, 0x6f, 0x64, 0x79}},
		{&Dir{Qid: Qid{0, 0, 0x80}, Name: "/", Mode: 0x80000000 | 0777}, []byte{0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
	}

	for _, tb := range tab {
		b := make([]byte, tb.d.size())

		err := tb.d.encode(b)
		if err != nil {
			t.Fatal(err)
		}

		if bytes.Compare(b, tb.exp) != 0 {
			t.Log("actual:", b)
			t.Log("expected:", tb.exp)
			t.Fatal("marshal mismatch")
		}
		d2 := &Dir{}
		err = d2.decode(b)
		if err != nil {
			t.Fatal(err)
		}
		if *tb.d != *d2 {
			t.Log(tb.d)
			t.Log(b)
			t.Log(d2)
			t.Fatal("mismatch for unmarshaling what me marshal")
		}
	}
}