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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
package codec_test
import (
"bytes"
"encoding/binary"
"fmt"
"testing"
"github.com/nixos/lix/gonix/codec"
)
// roundtrip encodes v then decodes it and checks the result equals v.
func roundtrip(t *testing.T, v codec.Value) codec.Value {
t.Helper()
enc := codec.Encode(v)
got, err := codec.Decode(enc)
if err != nil {
t.Fatalf("Decode error: %v\nencoded: %q", err, enc)
}
return got
}
func TestScalarString(t *testing.T) {
v := codec.String("hello")
got := roundtrip(t, v)
s, err := got.AsString()
if err != nil {
t.Fatal(err)
}
if s != "hello" {
t.Fatalf("want %q, got %q", "hello", s)
}
}
func TestScalarEmpty(t *testing.T) {
v := codec.String("")
got := roundtrip(t, v)
s, _ := got.AsString()
if s != "" {
t.Fatalf("want empty string, got %q", s)
}
}
func TestScalarBool(t *testing.T) {
for _, b := range []bool{true, false} {
got := roundtrip(t, codec.Bool(b))
result, err := got.AsBool()
if err != nil {
t.Fatal(err)
}
if result != b {
t.Fatalf("bool roundtrip: want %v got %v", b, result)
}
}
}
func TestScalarUint64(t *testing.T) {
cases := []uint64{0, 1, 255, 65536, 1<<63 - 1, ^uint64(0)}
for _, n := range cases {
got := roundtrip(t, codec.Uint64(n))
result, err := got.AsUint64()
if err != nil {
t.Fatal(err)
}
if result != n {
t.Fatalf("uint64 roundtrip: want %d got %d", n, result)
}
}
}
func TestScalarBinaryData(t *testing.T) {
// raw bytes including colons, braces, semicolons — must not confuse parser
raw := []byte{0x00, 0xff, ':', '{', ';', ',', '<', '}', 0x01}
got := roundtrip(t, codec.Scalar(raw))
if !bytes.Equal(got.Bytes, raw) {
t.Fatalf("binary scalar roundtrip failed: want %v got %v", raw, got.Bytes)
}
}
func TestScalarHash(t *testing.T) {
// 32-byte SHA256 hash
hash := make([]byte, 32)
for i := range hash {
hash[i] = byte(i)
}
got := roundtrip(t, codec.Scalar(hash))
if !bytes.Equal(got.Bytes, hash) {
t.Fatalf("hash roundtrip failed")
}
}
func TestRecordSingleField(t *testing.T) {
v := codec.Record("bool", codec.Bool(true))
got := roundtrip(t, v)
if got.Kind != codec.KindRecord {
t.Fatalf("want record, got kind %d", got.Kind)
}
f := got.MustField("bool")
b, _ := f.AsBool()
if !b {
t.Fatal("want true")
}
}
func TestRecordMultipleFields(t *testing.T) {
v := codec.Record(
"name", codec.String("hello"),
"size", codec.Uint64(42),
"ok", codec.Bool(true),
)
got := roundtrip(t, v)
name, _ := got.MustField("name").AsString()
size, _ := got.MustField("size").AsUint64()
ok, _ := got.MustField("ok").AsBool()
if name != "hello" {
t.Fatalf("name: want hello got %q", name)
}
if size != 42 {
t.Fatalf("size: want 42 got %d", size)
}
if !ok {
t.Fatal("ok: want true")
}
}
func TestRecordFieldOrder(t *testing.T) {
v := codec.Record(
"z", codec.String("last"),
"a", codec.String("first"),
"m", codec.String("middle"),
)
got := roundtrip(t, v)
if len(got.Order) != 3 || got.Order[0] != "z" || got.Order[1] != "a" || got.Order[2] != "m" {
t.Fatalf("field order not preserved: %v", got.Order)
}
}
func TestRecordNestedRecord(t *testing.T) {
inner := codec.Record("x", codec.String("inner"))
outer := codec.Record("nested", inner, "name", codec.String("outer"))
got := roundtrip(t, outer)
nestedField := got.MustField("nested")
x, _ := nestedField.MustField("x").AsString()
if x != "inner" {
t.Fatalf("nested field: want inner got %q", x)
}
}
func TestListEmpty(t *testing.T) {
v := codec.List()
got := roundtrip(t, v)
if got.Kind != codec.KindList {
t.Fatalf("want list, got kind %d", got.Kind)
}
if len(got.Items) != 0 {
t.Fatalf("want empty list, got %d items", len(got.Items))
}
}
func TestListStrings(t *testing.T) {
paths := []string{
"/nix/store/aaa-glibc",
"/nix/store/bbb-gcc",
"/nix/store/ccc-hello",
}
items := make([]codec.Value, len(paths))
for i, p := range paths {
items[i] = codec.String(p)
}
got := roundtrip(t, codec.List(items...))
if len(got.Items) != len(paths) {
t.Fatalf("want %d items, got %d", len(paths), len(got.Items))
}
for i, p := range paths {
s, _ := got.Items[i].AsString()
if s != p {
t.Fatalf("item %d: want %q got %q", i, p, s)
}
}
}
func TestListOfRecords(t *testing.T) {
items := []codec.Value{
codec.Record("k", codec.String("v1")),
codec.Record("k", codec.String("v2")),
}
got := roundtrip(t, codec.List(items...))
if len(got.Items) != 2 {
t.Fatalf("want 2 items, got %d", len(got.Items))
}
v1, _ := got.Items[0].MustField("k").AsString()
v2, _ := got.Items[1].MustField("k").AsString()
if v1 != "v1" || v2 != "v2" {
t.Fatalf("list of records: got %q %q", v1, v2)
}
}
func TestVariantSimple(t *testing.T) {
// <2:ia43:/nix/store/abc-hello
v := codec.Variant("ia", codec.String("/nix/store/abc-hello"))
got := roundtrip(t, v)
if got.Kind != codec.KindVariant {
t.Fatalf("want variant, got kind %d", got.Kind)
}
if got.Tag != "ia" {
t.Fatalf("tag: want ia got %q", got.Tag)
}
path, _ := got.Payload.AsString()
if path != "/nix/store/abc-hello" {
t.Fatalf("payload: want path got %q", path)
}
}
func TestVariantWithRecord(t *testing.T) {
// <8:ca-fixed{6:method,8:r:sha256;4:hash,32:<bytes>;8:hashType,1:3;}
hash := make([]byte, 32)
for i := range hash {
hash[i] = byte(i * 3)
}
v := codec.Variant("ca-fixed", codec.Record(
"method", codec.String("r:sha256"),
"hash", codec.Scalar(hash),
"hashType", codec.Scalar([]byte{3}),
))
got := roundtrip(t, v)
if got.Tag != "ca-fixed" {
t.Fatalf("tag: want ca-fixed got %q", got.Tag)
}
method, _ := got.Payload.MustField("method").AsString()
if method != "r:sha256" {
t.Fatalf("method: want r:sha256 got %q", method)
}
gotHash := got.Payload.MustField("hash").Bytes
if !bytes.Equal(gotHash, hash) {
t.Fatal("hash bytes mismatch")
}
}
func TestIsValidPathMessage(t *testing.T) {
// Full isValidPath request as it will appear on the wire
req := codec.Record(
"op", codec.String("isValidPath"),
"path", codec.String("/nix/store/abc123aaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello-1.0"),
)
enc := codec.Encode(req)
// verify envelope length is accurate
colon := bytes.IndexByte(enc, ':')
if colon < 0 {
t.Fatal("no colon in envelope")
}
declaredLen := 0
for _, c := range enc[:colon] {
declaredLen = declaredLen*10 + int(c-'0')
}
actualInner := len(enc) - colon - 1
if declaredLen != actualInner {
t.Fatalf("envelope length mismatch: declared %d actual %d", declaredLen, actualInner)
}
// decode and check
got, err := codec.Decode(enc)
if err != nil {
t.Fatal(err)
}
op, _ := got.MustField("op").AsString()
if op != "isValidPath" {
t.Fatalf("op: want isValidPath got %q", op)
}
}
func TestQueryPathInfoResponse(t *testing.T) {
// Simulate a queryPathInfo response
narHash := make([]byte, 32)
for i := range narHash {
narHash[i] = byte(i)
}
var narSize uint64 = 1234567
nsSz := make([]byte, 8)
binary.LittleEndian.PutUint64(nsSz, narSize)
refs := codec.List(
codec.String("/nix/store/aaa-glibc"),
codec.String("/nix/store/bbb-gcc"),
)
resp := codec.Record(
"narHash", codec.Scalar(narHash),
"hashType", codec.Scalar([]byte{3}), // sha256
"narSize", codec.Uint64(narSize),
"refs", refs,
// deriver omitted (absent optional)
)
got := roundtrip(t, resp)
gotHash := got.MustField("narHash").Bytes
if !bytes.Equal(gotHash, narHash) {
t.Fatal("narHash mismatch")
}
gotSize, _ := got.MustField("narSize").AsUint64()
if gotSize != narSize {
t.Fatalf("narSize: want %d got %d", narSize, gotSize)
}
gotRefs := got.MustField("refs")
if len(gotRefs.Items) != 2 {
t.Fatalf("refs: want 2 got %d", len(gotRefs.Items))
}
if _, ok := got.Field("deriver"); ok {
t.Fatal("deriver should be absent")
}
}
func TestErrorEncoding(t *testing.T) {
enc := codec.EncodeError(fmt.Errorf("store is read-only"))
got, err := codec.Decode(enc)
if err != nil {
t.Fatal(err)
}
msg, isErr := codec.IsError(got)
if !isErr {
t.Fatal("expected error record")
}
if msg != "store is read-only" {
t.Fatalf("error message: want %q got %q", "store is read-only", msg)
}
}
func TestDerivationOutputVariants(t *testing.T) {
// InputAddressed: <2:ia<storepath>
ia := codec.Variant("ia", codec.String("/nix/store/out-hello"))
gotIA := roundtrip(t, ia)
if gotIA.Tag != "ia" {
t.Fatalf("ia tag: got %q", gotIA.Tag)
}
path, _ := gotIA.Payload.AsString()
if path != "/nix/store/out-hello" {
t.Fatalf("ia path: got %q", path)
}
// CAFixed: <8:ca-fixed{...}
hash := make([]byte, 32)
caFixed := codec.Variant("ca-fixed", codec.Record(
"method", codec.String("r:sha256"),
"hash", codec.Scalar(hash),
"hashType", codec.Scalar([]byte{3}),
))
gotCA := roundtrip(t, caFixed)
if gotCA.Tag != "ca-fixed" {
t.Fatalf("ca-fixed tag: got %q", gotCA.Tag)
}
method, _ := gotCA.Payload.MustField("method").AsString()
if method != "r:sha256" {
t.Fatalf("method: got %q", method)
}
}
|