File: test_functions

package info (click to toggle)
flash-kernel 3.35%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 600 kB
  • ctags: 8
  • sloc: sh: 297; makefile: 23
file content (697 lines) | stat: -rwxr-xr-x 22,935 bytes parent folder | download
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#!/bin/dash

# Copyright (C) 2011  Loïc Minier <lool@dooz.org>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
# USA.

# Let the function library know that it is called from the testsuite.
FK_TESTSUITE_RUNNING="yes"

. ./testlib

functions="${FK_CHECKOUT:-$FK_DIR}/functions"

test_syntax() {
    sh -n "$functions"
}
add_test test_syntax

test_mtdblock() {
    get_tempfile
    mock_proc_mtd="$last_tempfile"
    cat >"$mock_proc_mtd" <<EOF
dev:    size   erasesize  name
mtd0: 00580000 00020000 "root"
mtd1: 00100000 00020000 "kernel"
mtd2: 00160000 00020000 "initrd"
mtd3: 00020000 00020000 "reset"
mtd4: 00800000 00020000 "jffs2"
EOF
    (
        . "$functions"
        PROC_MTD="$mock_proc_mtd"
        root_mtd=$(mtdblock "root")
        if [ "$root_mtd" != "/dev/mtdblock0" ]; then
            echo "Expected root mtd to be /dev/mtdblock0 but got $root_mtd" >&2
            exit 1
        fi
        jffs2_mtd=$(mtdblock "jffs2")
        if [ "$jffs2_mtd" != "/dev/mtdblock4" ]; then
            echo "Expected jffs2 mtd to be /dev/mtdblock4 but got $jffs2_mtd" >&2
            exit 1
        fi
    )
}
add_test test_mtdblock

test_mtdchar() {
    get_tempfile
    mock_proc_mtd="$last_tempfile"
    cat >"$mock_proc_mtd" <<EOF
dev:    size   erasesize  name
mtd0: 00580000 00020000 "root"
mtd1: 00100000 00020000 "kernel"
mtd2: 00160000 00020000 "initrd"
mtd3: 00020000 00020000 "reset"
mtd4: 00800000 00020000 "jffs2"
EOF
    (
        . "$functions"
        PROC_MTD="$mock_proc_mtd"
        root_mtd=$(mtdchar "root")
        if [ "$root_mtd" != "/dev/mtd0" ]; then
            echo "Expected root mtd to be /dev/mtd0 but got $root_mtd" >&2
            exit 1
        fi
        jffs2_mtd=$(mtdchar "jffs2")
        if [ "$jffs2_mtd" != "/dev/mtd4" ]; then
            echo "Expected jffs2 mtd to be /dev/mtd4 but got $jffs2_mtd" >&2
            exit 1
        fi
    )
}
add_test test_mtdchar

test_mtdsize() {
    get_tempfile
    mock_proc_mtd="$last_tempfile"
    cat >"$mock_proc_mtd" <<EOF
dev:    size   erasesize  name
mtd0: 00580000 00020000 "root"
mtd1: 00100000 00020000 "kernel"
mtd2: 00160000 00020000 "initrd"
mtd3: 00020000 00020000 "reset"
mtd4: 00800000 00020000 "jffs2"
EOF
    (
        . "$functions"
        PROC_MTD="$mock_proc_mtd"
        root_size=$(mtdsize "root")
        if [ "$root_size" != 5767168 ]; then
            echo "Expected root size of 5767168 but got $root_size" >&2
            exit 1
        fi
        jffs2_size=$(mtdsize "jffs2")
        if [ "$jffs2_size" != 8388608 ]; then
            echo "Expected jffs2 size of 8388608 but got $jffs2_size" >&2
            exit 1
        fi
    )
}
add_test test_mtdsize

test_check_kflavors() {
    (
        . "$functions"
        if check_kflavors "ksuffix" "kflavor1" "kflavor2"; then
            echo "Expected check_kflavors to fail with kernel suffix not in expected flavors, but it succeeded" >&2
            exit 1
        fi
        if ! check_kflavors "foo" "kflavor1" "foo" "kflavor3"; then
            echo "Expected check_kflavors to succeed with kernel suffix in expected flavors, but it failed" >&2
            exit 1
        fi
        if ! check_kflavors "kflavor1-suffix" "klavor1" "kflavor1-suffix" "kflavor2"; then
            echo "Expected check_kflavours to succeed with double-barrelled kernel suffix in expected flavours, but it failed" >&2
            exit 1
        fi
        if check_kflavors "kflavor1-suffix" "klavor1" "kflavor2"; then
            echo "Expected check_kflavours to fail with double-barrelled kernel suffix not in expected flavours, but it succeeded" >&2
            exit 1
        fi
        if ! check_kflavors "" "kflavor1" "kflavor2" "kflavor3"; then
            echo "Expected check_kflavors to succeed with empty kernel suffix, but it failed" >&2
            exit 1
        fi
        if check_kflavors "ksuffix+" "kflavor1" "kflavor2"; then
            echo "Expected check_kflavors to fail with kernel suffix (with additional +) not in expected flavors, but it succeeded" >&2
            exit 1
        fi
        if ! check_kflavors "foo+" "kflavor1" "foo" "kflavor2"; then
            echo "Expected check_kflavours to succeed with kernel suffix (with additional +) in expected flavors, but it failed" >&2
            exit 1
        fi
    )
}
add_test test_check_kflavors

test_check_size() {
    (
        . "$functions"
        error() {
            return 1
        }
        if check_mtd_size "MTD" 2 1; then
            echo "Expected check_mtd_size to fail for too small size, but it succeeded" >&2
            exit 1
        fi
        if ! check_mtd_size "MTD" 1 1; then
            echo "Expected check_supported to succeed with large enough size, but it failed" >&2
            exit 1
        fi
    )
}
add_test test_check_size

test_check_supported() {
    (
        . "$functions"
        if check_supported "Dummy"; then
            echo "Expected check_supported to fail for non-existent board, but it succeeded" >&2
            exit 1
        fi
        machine="Marvell SheevaPlug Reference Board"
        if ! check_supported "$machine"; then
            echo "Expected check_supported to succeed with machine $machine, but it failed" >&2
            exit 1
        fi
    )
}
add_test test_check_supported

test_get_cpuinfo_hardware() {
    get_tempfile
    mock_proc_cpuinfo="$last_tempfile"
    cat >"$mock_proc_cpuinfo" <<EOF
Processor       : Feroceon 88FR131 rev 1 (v5l)
BogoMIPS        : 1192.75
Features        : swp half thumb fastmult edsp
CPU implementer : 0x56
CPU architecture: 5TE
CPU variant     : 0x2
CPU part        : 0x131
CPU revision    : 1

Hardware        : Marvell SheevaPlug Reference Board
Revision        : 0000
Serial          : 0000000000000000
EOF
    (
        . "$functions"
        PROC_CPUINFO="$mock_proc_cpuinfo"
        PROC_DTMODEL="/this/must/not/exist"
        get_machine
        if [ "$machine" != "Marvell SheevaPlug Reference Board" ]; then
            echo "Expected machine to be Marvell SheevaPlug Reference Board but got $machine" >&2
            exit 1
        fi
    )
}
add_test test_get_cpuinfo_hardware

test_get_dt_hardware() {
    get_tempfile
    mock_proc_cpuinfo="$last_tempfile"
    cat >"$mock_proc_cpuinfo" <<EOF
Processor       : Feroceon 88FR131 rev 1 (v5l)
BogoMIPS        : 1191.11
Features        : swp half thumb fastmult edsp 
CPU implementer : 0x56
CPU architecture: 5TE
CPU variant     : 0x2
CPU part        : 0x131
CPU revision    : 1

Hardware        : Marvell Kirkwood (Flattened Device Tree)
Revision        : 0000
Serial          : 0000000000000000
EOF

    get_tempfile
    mock_dt_model="$last_tempfile"
    echo -n "Globalscale Technologies Dreamplug" >"$mock_dt_model"

    (
        . "$functions"
        PROC_CPUINFO="$mock_proc_cpuinfo"
        PROC_DTMODEL="$mock_dt_model"
        get_machine
        if [ "$machine" != "Globalscale Technologies Dreamplug" ]; then
            echo "Expected machine to be Globalscale Technologies Dreamplug but got $machine" >&2
            exit 1
        fi
    )

}
add_test test_get_dt_hardware

test_get_machine() {
    get_tempfile
    mock_etc_machine="$last_tempfile"
    echo "Freescale MX53 LOCO Board" >"$mock_etc_machine"

    get_tempfile
    mock_proc_cpuinfo="$last_tempfile"
    cat >"$mock_proc_cpuinfo" <<EOF
Processor       : Feroceon 88FR131 rev 1 (v5l)
BogoMIPS        : 1192.75
Features        : swp half thumb fastmult edsp
CPU implementer : 0x56
CPU architecture: 5TE
CPU variant     : 0x2
CPU part        : 0x131
CPU revision    : 1

Hardware        : Marvell SheevaPlug Reference Board
Revision        : 0000
Serial          : 0000000000000000
EOF

    get_tempfile
    mock_dt_model="$last_tempfile"
    echo -n "Globalscale Technologies Dreamplug" >"$mock_dt_model"
    (
        . "$functions"
        FK_MACHINE=""
        PROC_DTMODEL="/this/must/not/exist"
        FK_ETC_MACHINE="/this/must/not/exist"
        PROC_CPUINFO="$mock_proc_cpuinfo"
        if get_machine; then
            if [ "$machine" != "Marvell SheevaPlug Reference Board" ]; then
                echo "Expected get_machine to return Marvell SheevaPlug Reference Board but got $machine" >&2
                exit 1
            fi
        else
            echo "Expected get_machine to succeed with mocked /proc/cpuinfo but it failed" >&2
            exit 1
        fi
        PROC_DTMODEL="$mock_dt_model"
        if get_machine; then
            if [ "$machine" != "Globalscale Technologies Dreamplug" ]; then
                echo "Expected get_machine to return Globalscale Technologies Dreamplug but got $machine" >&2
                exit 1
            fi
        else
            echo "Expected get_machine to succeed with mocked /proc/dtmodel but it failed" >&2
            exit 1
        fi
        FK_ETC_MACHINE="$mock_etc_machine"
        if get_machine; then
            if [ "$machine" != "Freescale MX53 LOCO Board" ]; then
                echo "Expected get_machine to return Freescale MX53 LOCO Board but got $machine" >&2
                exit 1
            fi
        else
            echo "Expected get_machine to succeed with mocked /etc/flash-kernel/machine but it failed" >&2
            exit 1
        fi
        FK_MACHINE="Genesi EfikaMX nettop"
        if get_machine; then
            if [ "$machine" != "Genesi EfikaMX nettop" ]; then
                echo "Expected get_machine to return Genesi EfikaMX nettop but got $machine" >&2
                exit 1
            fi
        else
            echo "Expected get_machine to succeed with valid FK_MACHINE but it failed" >&2
            exit 1
        fi
        FK_MACHINE="none"
        if get_machine; then
            if [ -n "$none_machine" ]; then
                echo "Expected get_machine to return an empty string but got $machine" >&2
                exit 1
            fi
        else
            echo "Expected get_machine to succeed with FK_MACHINE=\"none\" but it failed" >&2
            exit 1
        fi
    )
}
add_test test_get_machine

test_get_kfile_suffix() {
    (
        . "$functions"
        kfile_suffix=$(get_kfile_suffix "/boot/vmlinuz-2.6.32-5-kirkwood")
        if [ "$kfile_suffix" != "kirkwood" ]; then
            echo "Expected kernel file suffix to be kirkwood but got $kfile_suffix" >&2
            exit 1
        fi
        kfile_suffix=""
        kfile_suffix=$(get_kfile_suffix "/boot/vmlinuz-3.10-1-armmp-lpae" "$kfile_suffix")
        if [ "$kfile_suffix" != "lpae" ]; then
            echo "Expected kernel file suffix to be lpae but got $kfile_suffix" >&2
            exit 1
        fi
        kfile_suffix=$(get_kfile_suffix "/boot/vmlinuz-3.10-1-armmp-lpae" "$kfile_suffix")
        if [ "$kfile_suffix" != "armmp-lpae" ]; then
            echo "Expected kernel file suffix to be armmp-lpae but got $kfile_suffix" >&2
            exit 1
        fi
        kfile_suffix=$(get_kfile_suffix "/boot/vmlinuz-3.10-1-armmp")
        if [ "$kfile_suffix" != "armmp" ]; then
            echo "Expected kernel file suffix to be armmp but got $kfile_suffix" >&2
            exit 1
        fi
    )
}
add_test test_get_kfile_suffix

test_get_machine_field() {
    (
        . "$functions"
        machine="Marvell SheevaPlug Reference Board"
        if machine_field="$(get_machine_field "$machine" "Machine")"; then
            if [ "$machine_field" != "$machine" ]; then
                echo "Expected Machine field to be $machine but got $machine_field" >&2
                exit 1
            fi
        else
            echo "Expected get_machine_field to succeed on Machine field but it failed" >&2
            exit 1
        fi
        kflavors="$(get_machine_field "$machine" "Kernel-Flavors")"
        if [ "$kflavors" != "kirkwood" ]; then
            echo "Expected Kernel-Flavors field to be kirkwood but got $kflavors" >&2
            exit 1
        fi
        if dummy_field="$(get_machine_field "$machine" "Dummy")"; then
            echo "Expected get_machine_field to fail on unknown field Dummy but it succeeded" >&2
            exit 1
        fi
    )

    get_tempfile
    mock_etc_db="$last_tempfile"
    cat >"$mock_etc_db" <<EOF
Machine: Board File Machine A
Machine: DT Machine A
Machine: DT Machine A v2
Kernel-Flavors: machinea

Machine: Board File Machine B
Kernel-Flavors: machineb

Machine: DT Machine B
Kernel-Flavors: machineb-dt

Machine: Machine C
Machine: Machine C
Kernel-Flavors: machinec
Machine: Invalid
Dummy: Dummy
EOF
    (
        FK_ETC_DB="$mock_etc_db"
        . "$functions"
        machine="Board File Machine A"
        if machine_field="$(get_machine_field "$machine" "Machine")"; then
            if [ "$machine_field" != "$machine" ]; then
                echo "Expected Machine field to be $machine but got $machine_field" >&2
                exit 1
            fi
        else
            echo "Expected get_machine_field to succeed on Machine field but it failed" >&2
            exit 1
        fi
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "machinea" ]; then
            echo "Expected Kernel-Flavors field to be machinea but got $kflavors" >&2
            exit 1
        fi
        machine="DT Machine A"
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "machinea" ]; then
            echo "Expected Kernel-Flavors field to be machinea but got $kflavors" >&2
            exit 1
        fi
        if machine_field="$(get_machine_field "$machine" "Machine")"; then
            if [ "$machine_field" != "$machine" ]; then
                echo "Expected Machine field to be $machine but got $machine_field" >&2
                exit 1
            fi
        else
            echo "Expected get_machine_field to succeed on Machine field but it failed" >&2
            exit 1
        fi
        machine="DT Machine A v2"
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "machinea" ]; then
            echo "Expected Kernel-Flavors field to be machinea but got $kflavors" >&2
            exit 1
        fi
        if machine_field="$(get_machine_field "$machine" "Machine")"; then
            if [ "$machine_field" != "$machine" ]; then
                echo "Expected Machine field to be $machine but got $machine_field" >&2
                exit 1
            fi
        else
            echo "Expected get_machine_field to succeed on Machine field but it failed" >&2
            exit 1
        fi

        machine="Board File Machine B"
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "machineb" ]; then
            echo "Expected Kernel-Flavors field to be machineb but got $kflavors" >&2
            exit 1
        fi
        if machine_field="$(get_machine_field "$machine" "Machine")"; then
            if [ "$machine_field" != "$machine" ]; then
                echo "Expected Machine field to be $machine but got $machine_field" >&2
                exit 1
            fi
        else
            echo "Expected get_machine_field to succeed on Machine field but it failed" >&2
            exit 1
        fi

        machine="DT Machine B"
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "machineb-dt" ]; then
            echo "Expected Kernel-Flavors field to be machineb-dt but got $kflavors" >&2
            exit 1
        fi
        if machine_field="$(get_machine_field "$machine" "Machine")"; then
            if [ "$machine_field" != "$machine" ]; then
                echo "Expected Machine field to be $machine but got $machine_field" >&2
                exit 1
            fi
        else
            echo "Expected get_machine_field to succeed on Machine field but it failed" >&2
            exit 1
        fi

        machine="Machine C"
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "machinec" ]; then
            echo "Expected Kernel-Flavors field to be machinec but got $kflavors" >&2
            exit 1
        fi
        if field="$(get_machine_field "$machine" "Dummy" 2>/dev/null)"; then
            echo "Expected Dummy field to hit syntax error but got $field" >&2
            exit 1
        fi

    )
}
add_test test_get_machine_field

test_etc_db() {
    get_tempfile
    mock_etc_db="$last_tempfile"
    cat >"$mock_etc_db" <<EOF
Machine: Marvell SheevaPlug Reference Board
Kernel-Flavors: Dummy
EOF

    (
        FK_ETC_DB="$mock_etc_db"
        . "$functions"
        machine="Marvell SheevaPlug Reference Board"
        kflavors=$(get_machine_field "$machine" "Kernel-Flavors")
        if [ "$kflavors" != "Dummy" ]; then
            echo "Expected Kernel-Flavors field to be overridden to Dummy but got $kflavors" >&2
            exit 1
        fi
        kaddress=$(get_machine_field "$machine" "U-Boot-Kernel-Address")
        if [ "$kaddress" != "0x00008000" ]; then
            echo "Expected U-Boot-Kernel-Address to be 0x00008000 but got $kaddress" >&2
            exit 1
        fi
    )
}
add_test test_etc_db

test_machine_uses_flash() {
    (
        . "$functions"
        if ! machine_uses_flash "Dummy"; then
            echo "Expected machine_uses_flash to succeed for non-existent board, but it failed" >&2
            exit 1
        fi
        machine="Buffalo Linkstation Mini"
        if machine_uses_flash "$machine"; then
            echo "Expected machine_uses_flash to fail for $machine, but it succeeded" >&2
            exit 1
        fi
        machine="Lanner EM7210"
        if ! machine_uses_flash "$machine"; then
            echo "Expected machine_uses_flash to succeed for $machine, but it failed" >&2
            exit 1
        fi
    )
}
add_test test_machine_uses_flash

test_set_machine_id() {
    (
        . "$functions"
        arm_code="$(set_machine_id 2097 | od -x)"
        expected="0000000 1c08 e3a0 1031 e381
0000010"
        if [ "$arm_code" != "$expected" ]; then
            echo "Expected \"$expected\" for machine-id 2097 but got \"$arm_code\"" >&2
            exit 1
        fi
        arm_code="$(set_machine_id "" | od -x)"
        expected="0000000"
        if [ "$arm_code" != "$expected" ]; then
            echo "Expected \"$expected\" for empty machine-id but got \"$arm_code\"" >&2
            exit 1
        fi
    )
}
add_test test_set_machine_id

test_mkimage_kernel() {
    (
        mkimage() {
            saved_args="$@"
        }
        . "$functions"
        saved_args=""
        mkimage_kernel "0xdeadbeef" "0xbaddcafe" "desc" "input" "output" 2>/dev/null
        expected="-A arm -O linux -T kernel -C none -a 0xdeadbeef -e 0xbaddcafe -n desc -d input output"
        if [ "$expected" != "$saved_args" ]; then
            echo "Expected mkimage_kernel to be called with \"$expected\" but it was called with \"$saved_args\"" >&2
            exit 1
        fi
    )
}
add_test test_mkimage_kernel

test_mkimage_initrd() {
    (
        mkimage() {
            saved_args="$@"
        }
        . "$functions"
        saved_args=""
        mkimage_initrd "0xdeadbeef" "desc" "input" "output" 2>/dev/null
        expected="-A arm -O linux -T ramdisk -C none -a 0xdeadbeef -e 0xdeadbeef -n desc -d input output"
        if [ "$expected" != "$saved_args" ]; then
            echo "Expected mkimage_initrd to be called with \"$expected\" but it was called with \"$saved_args\"" >&2
            exit 1
        fi
    )
}
add_test test_mkimage_initrd

test_mkimage_multi() {
    (
        mkimage() {
            saved_args="$@"
        }
        . "$functions"
        saved_args=""
        mkimage_multi "0xdeadbeef" "desc" "kinput" "iinput" "output" 2>/dev/null
        expected="-A arm -O linux -T multi -C none -a 0xdeadbeef -e 0xdeadbeef -n desc -d kinput:iinput output"
        if [ "$expected" != "$saved_args" ]; then
            echo "Expected mkimage_multi to be called with \"$expected\" but it was called with \"$saved_args\"" >&2
            exit 1
        fi
    )
}
add_test test_mkimage_multi

test_gen_kernel() {
    get_tempfile
    kernel_input="$last_tempfile"
    echo "foo" >"$kernel_input"
    get_tempfile
    kernel_output="$last_tempfile"
    (
        . "$functions"
        gen_kernel "$kernel_input" "$kernel_output" 2097
        result="$(od -t x1 "$kernel_output")"
        expected="0000000 08 1c a0 e3 31 10 81 e3 66 6f 6f 0a
0000014"
        if [ "$result" != "$expected" ]; then
            echo "With machine id 2097, expected \"$expected\" but got \"$result\"" >&2
            exit 1
        fi
        gen_kernel "$kernel_input" "$kernel_output" ""
        result="$(od -t x1 "$kernel_output")"
        expected="0000000 66 6f 6f 0a
0000004"
        if [ "$result" != "$expected" ]; then
            echo "With no machine id, expected \"$expected\" but got \"$result\"" >&2
            exit 1
        fi
    )
}
add_test test_gen_kernel

test_flash_initrd() {
    get_tempfile
    initrd_input="$last_tempfile"
    echo "foo" >"$initrd_input"
    get_tempfile
    initrd_output="$last_tempfile"
    (
        . "$functions"
        flash_initrd "$initrd_input" "$initrd_output" 3 2>/dev/null
        result="$(od -t x1 "$initrd_output")"
        expected="0000000 66 6f 6f 0a 00 00 00
0000007"
        if [ "$result" != "$expected" ]; then
            echo "With 3 bytes of padding, expected \"$expected\" but got \"$result\"" >&2
            exit 1
        fi
        flash_initrd "$initrd_input" "$initrd_output" 0 2>/dev/null
        result="$(od -t x1 "$initrd_output")"
        expected="0000000 66 6f 6f 0a
0000004"
        if [ "$result" != "$expected" ]; then
            echo "With no padding, expected \"$expected\" but got \"$result\"" >&2
            exit 1
        fi
    )
}
add_test test_flash_initrd

test_dtb_append_from() {
    (
        . "$functions"
        dtb_append_from="3.12"
        kvers=3.11-1-kirkwood
        if dtb_append_required ; then
            echo "Kernel $kvers does not need appended DTB, but got true" >&2
            exit 1
        fi
        kvers=3.12-1-kirkwood
        if ! dtb_append_required ; then
            echo "Kernel $kvers does need appended DTB, but got false" >&2
            exit 1
        fi
    )
}
add_test test_dtb_append_from

test_main

# vim:syntax=sh