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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mendersoftware/mender-artifact/areader"
"github.com/mendersoftware/mender-artifact/utils"
)
func testSetupTeardown(t *testing.T) (artifact, sdimg, fatsdimg, sparsesdimg string, f func()) {
tmp, err := ioutil.TempDir("", "mender-modify")
assert.NoError(t, err)
err = copyFile("mender_test.img", filepath.Join(tmp, "mender_test.img"))
assert.NoError(t, err)
err = WriteArtifact(tmp, LatestFormatVersion, filepath.Join(tmp, "mender_test.img"))
assert.NoError(t, err)
artifact = filepath.Join(tmp, "artifact.mender")
err = copyFile("mender_test.sdimg", filepath.Join(tmp, "mender_test.sdimg"))
assert.NoError(t, err)
sdimg = filepath.Join(tmp, "mender_test.sdimg")
err = copyFile("mender_test_fat.sdimg", filepath.Join(tmp, "mender_test_fat.sdimg"))
require.Nil(t, err)
fatsdimg = filepath.Join(tmp, "mender_test_fat.sdimg")
err = copyFile("mender_test.sparse.sdimg", filepath.Join(tmp, "mender_test.sparse.sdimg"))
require.Nil(t, err)
sparsesdimg = filepath.Join(tmp, "mender_test.sparse.sdimg")
return artifact, sdimg, fatsdimg, sparsesdimg, func() {
os.RemoveAll(tmp)
}
}
const testPrivateKey = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIMU7txA5VwTnkNIXQx+XjJE4HLSjvkVsckyFYhpjWXIioAoGCCqGSM49
AwEHoUQDQgAEzzRKBsM1lJ+z/ljS+9kCAJCiTB6+HbyD2TE2hLKGj9xnFkzOHnEj
7KybiE2PAx6skWvCPqBP5+H0d68jN9mOAw==
-----END EC PRIVATE KEY-----
`
const testPublicKey = `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzzRKBsM1lJ+z/ljS+9kCAJCiTB6+
HbyD2TE2hLKGj9xnFkzOHnEj7KybiE2PAx6skWvCPqBP5+H0d68jN9mOAw==
-----END PUBLIC KEY-----
`
func TestCLIErrors(t *testing.T) {
tests := []struct {
initfunc func(imgpath string)
name string
argv []string
expected string
err string
verifyTestFunc func(imgpath string)
}{
{
name: "error on no path given into the image file",
argv: []string{"mender-artifact", "cp", "artifact.mender", "output.txt"},
err: "failed to parse image path",
},
{
name: "no mender artifact or sdimg provided",
argv: []string{"mender-artifact", "cp", "foobar", "output.txt"},
err: "no artifact or sdimage provided\n",
},
{
name: "got 1 arguments, wants two",
argv: []string{"mender-artifact", "cp", "foobar"},
err: "got 1 arguments, wants two",
},
{
name: "too many arguments provided to cat",
argv: []string{"mender-artifact", "cat", "foo", "bar"},
err: "Got 2 arguments, wants one",
},
{
name: "input image has invalid extension",
argv: []string{"mender-artifact", "cat", "someimg.fooimb:bar"},
err: "The input image does not seem to be a valid image",
},
{
name: "error: please enter a path into the image",
argv: []string{"mender-artifact", "cat", "menderimg.mender:"},
err: "please enter a path into the image",
},
{
name: "Install: Error on parse error",
argv: []string{"mender-artifact", "install", "foo.txt", "nosdimg"},
err: "No artifact or sdimg provided",
},
{
name: "Install: Error on no file permissions given",
argv: []string{"mender-artifact", "install", "testkey", "artifact.mender:/etc/mender/testkey.key"},
err: "File permissions needs to be set, if you are simply copying, the cp command should fit your needs",
},
{
name: "Install: Error on wrong number of arguments given",
argv: []string{"mender-artifact", "install", "foo.txt", "bar.txt", ":/some/path/file.txt"},
err: "Wrong number of arguments, got 3",
},
{
name: "Install: Error on wrong number of arguments given to directory install",
argv: []string{"mender-artifact", "install", "-d", "foo.txt", ":/some/path/file.txt"},
err: "Wrong number of arguments, got 2",
},
{
name: "Install: Error on parse error for directory install",
argv: []string{"mender-artifact", "install", "-d", "foo.txt"},
err: "No artifact or sdimg provided",
},
}
for _, test := range tests {
err := Run(test.argv)
assert.Contains(t, err.Error(), test.err)
}
}
func TestCopyRootfsImage(t *testing.T) {
// build the mender-artifact binary
assert.Nil(t, exec.Command("go", "build", "..").Run())
defer os.Remove("mender-artifact")
dir, err := os.Getwd()
require.Nil(t, err)
dir = filepath.Join(dir, "")
tests := []struct {
initfunc func(imgpath string)
name string
argv []string
expected string
err string
verifyTestFunc func(imgpath string)
}{
{
name: "write artifact_info file to stdout",
argv: []string{"mender-artifact", "cat", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/etc/mender/artifact_info"},
expected: "artifact_name=release-1\n",
},
{
name: "write artifact_info file to stdout from signed artifact",
argv: []string{"mender-artifact", "cat", "<artifact>:/etc/mender/artifact_info"},
initfunc: func(imgpath string) {
// Write a private key to the same directory as the test artifact.
privateKeyFileName := filepath.Join(filepath.Dir(imgpath), "private.key")
err := ioutil.WriteFile(privateKeyFileName, []byte(testPrivateKey), 0600)
assert.Nil(t, err, "unexpected error writing private key file: %v", err)
// Write a public key to the same directory as the test artifact.
publicKeyFileName := filepath.Join(filepath.Dir(imgpath), "public.key")
err = ioutil.WriteFile(publicKeyFileName, []byte(testPublicKey), 0600)
assert.Nil(t, err, "unexpected error writing public key file: %v", err)
// Use the private key to sign the artifact in place.
executable := filepath.Join(dir, "mender-artifact")
cmd := exec.Command(executable, "sign", imgpath, "-k", privateKeyFileName)
err = cmd.Run()
assert.Nil(t, err, "unexpected error signing artifact: %v", err)
// Use the public key to verify the signature.
cmd = exec.Command(executable, "validate", imgpath, "-k", publicKeyFileName)
err = cmd.Run()
assert.Nil(t, err, "unexpected error verifying artifact signature: %v", err)
},
expected: "artifact_name=release-1\n",
},
{
initfunc: func(imgpath string) {
assert.Nil(t, ioutil.WriteFile("output.txt", []byte{}, 0755))
},
name: "write artifact_info file to output.txt",
argv: []string{"mender-artifact", "cp", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/etc/mender/artifact_info", "output.txt"},
verifyTestFunc: func(imgpath string) {
data, err := ioutil.ReadFile("output.txt")
assert.Nil(t, err)
assert.Equal(t, strings.TrimSpace(string(data)), "artifact_name=release-1")
os.Remove("output.txt")
},
},
{
name: "read from output.txt and write to img",
initfunc: func(imgpath string) {
// create some new data in the output.txt file,
// so that it does not shadow the previous test
assert.Nil(t, ioutil.WriteFile("output.txt", []byte("artifact_name=foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "output.txt", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:test.txt"},
verifyTestFunc: func(imgpath string) {
// as copy out of image is already verified to function, we
// will now use this functionality to confirm that the write
// was succesfull.
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":test.txt")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
assert.Nil(t, err, "got unexpected error: %s", err)
assert.Equal(t, "artifact_name=foobar", out.String())
os.Remove("output.txt")
},
},
{
initfunc: func(imgpath string) {
assert.Nil(t, ioutil.WriteFile("input.txt", []byte("dummy-text"), 0755))
},
name: "copy file into a non-existing directory",
argv: []string{"mender-artifact", "cp", "input.txt", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:/non/existing/path"},
err: "The directory: /non/existing does not exist in the image",
verifyTestFunc: func(string) {
os.Remove("input.txt")
},
},
{
initfunc: func(imgpath string) {
assert.Nil(t, ioutil.WriteFile("input.mender.txt", []byte("dummy-text"), 0755))
},
name: "copy file with containing '.mender' in its filename",
argv: []string{"mender-artifact", "cp", "input.mender.txt", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:/etc/mender/"},
verifyTestFunc: func(string) {
os.Remove("input.txt")
},
},
{
name: "write and read from the data partition",
initfunc: func(imgpath string) {
assert.Nil(t, ioutil.WriteFile("artifact_info", []byte("artifact_name=foobar"), 0755))
_, err := os.Open("artifact_info")
require.Nil(t, err)
},
argv: []string{"mender-artifact", "cp", "artifact_info", "<sdimg|sparse-sdimg|fat-sdimg>:/data/test.txt"},
verifyTestFunc: func(imgpath string) {
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":/data/test.txt")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
require.Nil(t, err, "catting the copied file does not function")
assert.Equal(t, "artifact_name=foobar", out.String())
os.Remove("artifact_info")
},
},
{
name: "error: artifact does not contain a data partition",
initfunc: func(string) {
assert.Nil(t, ioutil.WriteFile("foo.txt", nil, 0755))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<artifact>:/data/test.txt"},
err: "newArtifactExtFile: A mender artifact does not contain a data partition, only a rootfs",
verifyTestFunc: func(string) {
os.Remove("foo.txt")
},
},
{
name: "error: artifact does not contain a boot partition",
initfunc: func(string) {
assert.Nil(t, ioutil.WriteFile("foo.txt", nil, 0755))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<artifact>:/uboot/test.txt"},
err: "newArtifactExtFile: A mender artifact does not contain a boot partition, only a rootfs",
verifyTestFunc: func(string) {
os.Remove("foo.txt")
},
},
{
name: "Install file, standard permissions (0600)",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("testkey", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "install", "-m", "0600", "testkey", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:/etc/mender/testkey.key"},
verifyTestFunc: func(imgpath string) {
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":/etc/mender/testkey.key")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
assert.Nil(t, err, "got unexpected error: %v", err)
assert.Equal(t, "foobar", out.String())
// Cleanup the testkey
assert.Nil(t, os.Remove("testkey"))
// Check that the permission bits have been set correctly!
pf, err := virtualImage.OpenFile(nil, imgpath+":/etc/mender/testkey.key")
defer pf.Close()
require.Nil(t, err)
// Type switch on the artifact, or sdimg underlying
switch innerImg := pf.(*vImageAndFile).file.(type) {
case *extFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat /etc/mender/testkey.key", innerImg.imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
assert.True(t, strings.Contains(string(out), "Mode: 0600"))
case sdimgFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat /etc/mender/testkey.key", innerImg[0].(*extFile).imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
assert.True(t, strings.Contains(string(out), "Mode: 0600"))
default:
t.Fatal("Unexpected file type")
}
},
},
{
name: "Install file with permissions (0777)",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("testkey", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "install", "-m", "0777", "testkey", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:/etc/mender/testkey.key"},
verifyTestFunc: func(imgpath string) {
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":/etc/mender/testkey.key")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
assert.Nil(t, err, "got unexpected error: %v", err)
assert.Equal(t, "foobar", out.String())
// Cleanup the testkey
assert.Nil(t, os.Remove("testkey"))
// Check that the permission bits have been set correctly!
pf, err := virtualImage.OpenFile(nil, imgpath+":/etc/mender/testkey.key")
defer pf.Close()
require.Nil(t, err)
// Type switch on the artifact, or sdimg underlying
switch innerImg := pf.(*vImageAndFile).file.(type) {
case *extFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat /etc/mender/testkey.key", innerImg.imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
assert.True(t, strings.Contains(string(out), "Mode: 0777"))
case sdimgFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat /etc/mender/testkey.key", innerImg[0].(*extFile).imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
assert.True(t, strings.Contains(string(out), "Mode: 0777"))
default:
t.Fatal("Unexpected file type")
}
},
},
{
name: "Install file with permissions (0700)",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("testkey", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "install", "-m", "0700", "testkey", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:/etc/mender/testkey.key"},
verifyTestFunc: func(imgpath string) {
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":/etc/mender/testkey.key")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
assert.Nil(t, err, "got unexpected error: %v", err)
assert.Equal(t, "foobar", out.String())
// Cleanup the testkey
assert.Nil(t, os.Remove("testkey"))
// Check that the permission bits have been set correctly!
pf, err := virtualImage.OpenFile(nil, imgpath+":/etc/mender/testkey.key")
defer pf.Close()
require.Nil(t, err)
// Type switch on the artifact, or sdimg underlying
switch innerImg := pf.(*vImageAndFile).file.(type) {
case *extFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat /etc/mender/testkey.key", innerImg.imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
assert.True(t, strings.Contains(string(out), "Mode: 0700"))
case sdimgFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat /etc/mender/testkey.key", innerImg[0].(*extFile).imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
assert.True(t, strings.Contains(string(out), "Mode: 0700"))
default:
t.Fatal("Unexpected file type")
}
},
},
{
name: "write and read from the boot partition",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<sdimg|fat-sdimg|sparse-sdimg>:/uboot/test.txt"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact", "cp",
imgpath + ":/uboot/test.txt",
"test.res"})
assert.NoError(t, err)
data, err := ioutil.ReadFile("test.res")
assert.Nil(t, err, imgpath)
assert.Equal(t, "foobar", string(data), imgpath)
os.Remove("foo.txt")
os.Remove("test.res")
},
},
{
name: "write and read from the boot/efi partition",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<sdimg|fat-sdimg|sparse-sdimg>:/boot/efi/test.txt"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact", "cp",
imgpath + ":/boot/efi/test.txt",
"test.res"})
assert.NoError(t, err)
data, err := ioutil.ReadFile("test.res")
require.Nil(t, err)
assert.Equal(t, "foobar", string(data))
os.Remove("foo.txt")
os.Remove("test.res")
},
},
{
name: "cat from the boot partition",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
err := Run([]string{
"mender-artifact", "cp",
"foo.txt",
imgpath + ":/boot/efi/test.txt",
})
assert.NoError(t, err)
},
argv: []string{"mender-artifact", "cat", "<sdimg|fat-sdimg|sparse-sdimg>:/boot/efi/test.txt"},
expected: "foobar",
verifyTestFunc: func(string) {
os.Remove("foo.txt")
},
},
{
name: "write and read from the boot/grub partition",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<sdimg|fat-sdimg|sparse-sdimg>:/boot/grub/test.txt"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact", "cp",
imgpath + ":/boot/grub/test.txt",
"test.res"})
assert.NoError(t, err)
data, err := ioutil.ReadFile("test.res")
require.Nil(t, err)
assert.Equal(t, "foobar", string(data))
os.Remove("foo.txt")
os.Remove("test.res")
},
},
{
name: "error: read non-existing file from sdimg boot partition (ext)",
argv: []string{"mender-artifact", "cp", "<sdimg|sparse-sdimg>:/boot/grub/test.txt", "foo.txt"},
err: "The file: test.txt does not exist in the image",
},
{
name: "error: read non-existing file from sdimg boot partition (fat)",
argv: []string{"mender-artifact", "cp", "<fat-sdimg>:/boot/grub/test.txt", "foo.txt"},
err: "fatPartitionFile: Read: MTools mcopy failed: exit status 1",
},
{
name: "error: mender artifact does not contain a boot partition",
argv: []string{"mender-artifact", "cp", "<artifact>:/boot/grub/test.txt", "foo.txt"},
err: "newArtifactExtFile: A mender artifact does not contain a boot partition, only a rootfs",
},
{
name: "check if artifact does not change name",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<artifact>:/test.txt"},
verifyTestFunc: func(imgpath string) {
defer os.Remove("foo.txt")
// Read the artifact after cp.
readScripts := func(r io.Reader, info os.FileInfo) error {
return nil
}
f, err := os.Open(imgpath)
require.Nil(t, err)
defer f.Close()
ver := func(message, sig []byte) error {
return nil
}
ar := areader.NewReader(f)
ar.ScriptsReadCallback = readScripts
ar.VerifySignatureCallback = ver
err = ar.ReadArtifact()
require.Nil(t, err)
// Verify that the artifact-name has not changed.
assert.Equal(t, "test-artifact", ar.GetArtifactName())
// Cleanup
},
},
{
name: "Make sure that the update in a mender artifact does not change name",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<artifact>:/etc/test.txt"},
verifyTestFunc: func(imgpath string) {
defer os.Remove("foo.txt")
// Read the artifact after cp.
readScripts := func(r io.Reader, info os.FileInfo) error {
return nil
}
f, err := os.Open(imgpath)
require.Nil(t, err)
defer f.Close()
ver := func(message, sig []byte) error {
return nil
}
ar := areader.NewReader(f)
ar.ScriptsReadCallback = readScripts
ar.VerifySignatureCallback = ver
err = ar.ReadArtifact()
require.Nil(t, err)
inst := ar.GetHandlers()
// Verify that the update name has not changed.
assert.Equal(t, "mender_test.img", inst[0].GetUpdateFiles()[0].Name)
},
},
{
name: "Delete a file from an image or Artifact",
argv: []string{"mender-artifact", "rm", "<artifact|sdimg|sparse-sdimg|fat-sdimg>:/etc/mender/artifact_info"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact", "cat",
imgpath + ":/etc/mender/artifact_info"})
assert.Error(t, err)
},
},
{
name: "Error when deleting a non-empty directory from an image or Artifact",
argv: []string{"mender-artifact", "rm", "<artifact|sdimg|fat-sdimg>:/etc/mender/"},
err: "debugfsRemoveDir: debugfs: error running command:",
},
{
name: "Delete a directory from an image or Artifact recursively",
argv: []string{"mender-artifact", "rm", "-r", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/etc/mender/"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact", "cat",
imgpath + ":/etc/mender/artifact_info"})
assert.Error(t, err)
},
},
{
name: "Delete a file from a fat partition",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("test.txt", []byte("foobar"), 0644))
require.NoError(t, Run([]string{
"mender-artifact", "cp",
"test.txt",
imgpath + ":/boot/grub/test.txt"}))
},
argv: []string{"mender-artifact", "rm", "<fat-sdimg>:/boot/grub/test.txt"},
verifyTestFunc: func(imgpath string) {
defer os.Remove("test.txt")
err := Run([]string{
"mender-artifact", "cat",
imgpath + ":/boot/test.txt"})
assert.Error(t, err)
},
},
{
name: "Delete a directory from a fat partition",
argv: []string{"mender-artifact", "rm", "-r", "<fat-sdimg>:/uboot/testdir/"},
},
{
name: "Make sure that mender-artifact cp keeps the file permissions intact",
initfunc: func(imgpath string) {
f, err := os.OpenFile("foobar.txt", os.O_RDWR|os.O_CREATE, 0666)
require.Nil(t, err)
defer f.Close()
err = f.Chmod(0666)
},
argv: []string{"mender-artifact", "cp", "foobar.txt", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/etc/mender/foo.txt"},
verifyTestFunc: func(imgpath string) {
defer os.Remove("foobar.txt")
pf, err := virtualImage.OpenFile(nil, imgpath+":/etc/mender/foo.txt")
defer pf.Close()
require.Nil(t, err)
// Type switch on the artifact, or sdimg underlying
switch innerImg := pf.(*vImageAndFile).file.(type) {
case *extFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat etc/mender/foo.txt", innerImg.imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
require.True(t, strings.Contains(string(out), "Mode: 0666"))
case sdimgFile:
bin, err := utils.GetBinaryPath("debugfs")
require.Nil(t, err)
cmd := exec.Command(bin, "-R", "stat etc/mender/foo.txt", innerImg[0].(*extFile).imagePath)
out, err := cmd.CombinedOutput()
require.Nil(t, err)
require.True(t, strings.Contains(string(out), "Mode: 0666"))
}
},
},
{
name: "Make sure that rootfs-image.checksum is updated when repacking Artifact",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("foo.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "cp", "foo.txt", "<artifact>:/etc/test.txt"},
verifyTestFunc: func(imgpath string) {
defer os.Remove("foo.txt")
// Read the artifact after cp.
readScripts := func(r io.Reader, info os.FileInfo) error {
return nil
}
f, err := os.Open(imgpath)
require.Nil(t, err)
defer f.Close()
ver := func(message, sig []byte) error {
return nil
}
ar := areader.NewReader(f)
ar.ScriptsReadCallback = readScripts
ar.VerifySignatureCallback = ver
err = ar.ReadArtifact()
require.Nil(t, err)
inst := ar.GetHandlers()
provides, err := inst[0].GetUpdateProvides()
require.NoError(t, err)
assert.Equal(t, string(inst[0].GetUpdateFiles()[0].Checksum), provides["rootfs-image.checksum"])
},
},
{
name: "Create a directory",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("test.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "install", "-d", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/foo"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact",
"install",
"-m", "0644",
"test.txt",
imgpath + ":/foo/test.txt"})
assert.NoError(t, err)
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":/foo/test.txt")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
assert.Nil(t, err, "got unexpected error: %v", err)
assert.Equal(t, "foobar", out.String())
// Cleanup the testkey
assert.Nil(t, os.Remove("test.txt"))
},
},
{
name: "Create nested directories",
initfunc: func(imgpath string) {
require.Nil(t, ioutil.WriteFile("test.txt", []byte("foobar"), 0644))
},
argv: []string{"mender-artifact", "install", "-d", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/foo/bar"},
verifyTestFunc: func(imgpath string) {
err := Run([]string{
"mender-artifact",
"install",
"-m", "0644",
"test.txt",
imgpath + ":/foo/bar/test.txt"})
assert.NoError(t, err)
cmd := exec.Command(filepath.Join(dir, "mender-artifact"), "cat", imgpath+":/foo/bar/test.txt")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
assert.Nil(t, err, "got unexpected error: %v", err)
assert.Equal(t, "foobar", out.String())
// Cleanup the testkey
assert.Nil(t, os.Remove("test.txt"))
},
},
{
name: "Create a directory that already exists",
argv: []string{"mender-artifact", "install", "-d", "<artifact|sdimg|fat-sdimg|sparse-sdimg>:/"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// create a copy of the working images
artifact, sdimg, fatsdimg, sparsesdimg, closer := testSetupTeardown(t)
defer closer()
type testArgs struct {
validImg string
testArgVect []string
}
targs := []testArgs{}
// Add the images the test is valid for.
targv := make([][]string, 4)
targv[0] = make([]string, len(test.argv))
targv[1] = make([]string, len(test.argv))
targv[2] = make([]string, len(test.argv))
targv[3] = make([]string, len(test.argv))
copy(targv[0], test.argv)
copy(targv[1], test.argv)
copy(targv[2], test.argv)
copy(targv[3], test.argv)
for i, arg := range test.argv {
sarr := strings.Split(arg, ":")
if len(sarr) == 2 {
testimgs := strings.Split(strings.Trim(sarr[0], "<>"), "|")
for _, str := range testimgs {
switch str {
case "artifact":
targv[0][i] = artifact + ":" + sarr[1]
targs = append(targs, testArgs{
validImg: artifact,
testArgVect: targv[0],
})
case "sdimg":
targv[1][i] = sdimg + ":" + sarr[1]
targs = append(targs, testArgs{
validImg: sdimg,
testArgVect: targv[1],
})
case "fat-sdimg":
targv[2][i] = fatsdimg + ":" + sarr[1]
targs = append(targs, testArgs{
validImg: fatsdimg,
testArgVect: targv[2],
})
case "sparse-sdimg":
targv[3][i] = sparsesdimg + ":" + sarr[1]
targs = append(targs, testArgs{
validImg: sparsesdimg,
testArgVect: targv[3],
})
default:
t.Fatalf("Unrecognized image type: %s\n", str)
}
}
break
}
}
if len(targs) == 0 {
t.Fatalf("Test: %s - enabled no test cases\n", test.name)
}
for _, targ := range targs {
t.Run(filepath.Base(targ.validImg), func(t *testing.T) {
if strings.HasSuffix(targ.validImg, ".sdimg") {
skipPartedTestsOnMac(t)
}
if test.initfunc != nil {
test.initfunc(targ.validImg)
}
old := os.Stdout // keep backup of the real stdout
r, w, err := os.Pipe()
if err != nil {
log.Fatal(err)
}
os.Stdout = w
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
err = Run(targ.testArgVect)
// back to normal state
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC
if test.err != "" {
assert.Contains(t, err.Error(), test.err)
} else if err != nil {
t.Log(out)
t.Logf("Test argument vector: %v", targ)
t.Fatal(fmt.Sprintf("cmd: %s failed with err: %v", test.name, err))
}
if test.verifyTestFunc != nil {
test.verifyTestFunc(targ.validImg)
} else if test.err == "" {
assert.Equal(t, test.expected, out, test.name)
}
})
}
})
}
}
func TestCopyFromStdin(t *testing.T) {
type testArgs struct {
name string
argv []string
stdinPipe *os.File
testImg string
}
type testExpects struct {
name string
testImg string
err error
}
var testArgv []string
tests := []struct {
name string
argv []string
setupTestFunc func(testArgs)
verifyTestFunc func(testExpects)
}{
{
name: "Test boot partition file copy from stdin (ext4, fat)",
setupTestFunc: func(ta testArgs) {
ta.stdinPipe.Write([]byte("foobar"))
testArgv = []string{"mender-artifact", "cp",
"-", ta.testImg + ":/uboot/foo.txt"}
},
verifyTestFunc: func(te testExpects) {
assert.Nil(t, te.err)
// Copy back out and verify
err := Run([]string{
"mender-artifact", "cp",
te.testImg + ":/uboot/foo.txt",
"output.txt"})
defer os.Remove("output.txt")
assert.Nil(t, err)
of, err := ioutil.ReadFile("output.txt")
assert.Nil(t, err)
assert.Equal(t, "foobar", string(of), te.name, te.testImg)
},
},
{
name: "Test non existing dir from stdin (ext, fat)",
setupTestFunc: func(ta testArgs) {
ta.stdinPipe.Write([]byte("foobar"))
testArgv = []string{"mender-artifact", "cp", "-",
ta.testImg + ":/uboot/nonexisting/foo.txt"}
},
verifyTestFunc: func(te testExpects) {
assert.Error(t, te.err, te.name, te.testImg)
},
},
}
for _, test := range tests {
_, sdimg, fatsdimg, sparsesdimg, closer := testSetupTeardown(t)
defer closer()
for _, testimg := range []string{sdimg, fatsdimg, sparsesdimg} {
if strings.HasSuffix(testimg, ".sdimg") {
skipPartedTestsOnMac(t)
}
// Copy in from stdin
r, w, err := os.Pipe()
defer r.Close()
if err != nil {
t.Fatal(t, err)
}
orgStdin := os.Stdin
os.Stdin = r
test.setupTestFunc(testArgs{
name: test.name,
testImg: testimg,
stdinPipe: w,
})
go func(w *os.File) {
time.Sleep(1 * time.Second)
err := w.Close() // EOF
if err != nil {
t.Fatalf("Failed to close the pipe")
}
}(w)
err = Run(testArgv)
// Reset the original stdin
os.Stdin = orgStdin
test.verifyTestFunc(testExpects{
name: test.name,
testImg: testimg,
err: err,
})
}
}
}
func TestCopyModuleImage(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "mendertest")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
artfile := filepath.Join(tmpdir, "artifact.mender")
df, err := ioutil.TempFile(tmpdir, "CopyModuleImage")
require.Nil(t, err)
defer df.Close()
fd, err := os.OpenFile(filepath.Join(tmpdir, "updateFile"), os.O_WRONLY|os.O_CREATE, 0644)
require.NoError(t, err)
fd.Write([]byte("updateContent"))
fd.Close()
err = Run([]string{
"mender-artifact", "write", "module-image",
"-o", artfile,
"-n", "testName",
"-t", "testDevice",
"-T", "testType",
"-f", filepath.Join(tmpdir, "updateFile"),
})
assert.NoError(t, err)
err = Run([]string{
"mender-artifact", "cp",
df.Name(),
artfile + ":/dummy/path",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), errFsTypeUnsupported.Error())
err = Run([]string{
"mender-artifact", "cat",
artfile + ":/dummy/path",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), errFsTypeUnsupported.Error())
err = Run([]string{
"mender-artifact", "install",
"-m", "777",
df.Name(),
artfile + ":/dummy/path",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), errFsTypeUnsupported.Error())
err = Run([]string{
"mender-artifact", "rm",
artfile + ":/dummy/path",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), errFsTypeUnsupported.Error())
}
|