test-lm-init.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2016 Red Hat, Inc.
  4. * Author: Nathaniel McCallum <npmccallum@redhat.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 2.1 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "test.h"
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <error.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. static const luksmeta_uuid_t UUID = {
  28. 0xfb, 0x06, 0x8c, 0x1f, 0x68, 0x04, 0x87, 0x9f,
  29. 0xf4, 0xcd, 0x32, 0x25, 0xb2, 0x1a, 0x7e, 0xf8
  30. };
  31. int
  32. main(int argc, char *argv[])
  33. {
  34. uint8_t data[sizeof(UUID)] = {};
  35. struct crypt_device *cd = NULL;
  36. luksmeta_uuid_t uuid = {};
  37. uint32_t offset = 0;
  38. uint32_t length = 0;
  39. int fd;
  40. /* Test for -ENOENT when there is no luksmeta header. */
  41. cd = test_format();
  42. for (int slot = 0; slot < crypt_keyslot_max(CRYPT_LUKS1); slot++) {
  43. assert(luksmeta_save(cd, slot, UUID, UUID, sizeof(UUID)) == -ENOENT);
  44. assert(luksmeta_load(cd, slot, uuid, data, sizeof(data)) == -ENOENT);
  45. assert(luksmeta_wipe(cd, slot, UUID) == -ENOENT);
  46. assert(luksmeta_test(cd) == -ENOENT);
  47. }
  48. crypt_free(cd);
  49. cd = test_init();
  50. test_hole(cd, &offset, &length);
  51. /* Test the layout state. */
  52. assert(test_layout((range_t[]) {
  53. { 0, 1024 }, /* LUKS header */
  54. { 1024, offset - 1024, true }, /* Keyslot Area */
  55. { offset, 4096 }, /* luksmeta header */
  56. END(offset + 4096), /* Rest of the file */
  57. }));
  58. /* Test error codes for a valid luksmeta header but no slot. */
  59. for (int slot = 0; slot < crypt_keyslot_max(CRYPT_LUKS1); slot++) {
  60. assert(luksmeta_load(cd, slot, uuid, data, sizeof(data)) == -ENODATA);
  61. assert(luksmeta_wipe(cd, slot, UUID) == -EALREADY);
  62. }
  63. /* Test for -EALREADY when a valid header is present. */
  64. assert(luksmeta_init(cd) == -EALREADY);
  65. assert(luksmeta_test(cd) == 0);
  66. /* Test for -EBADSLT when an invalid slot is used. */
  67. assert(luksmeta_save(cd, 10, UUID, UUID, sizeof(UUID)) == -EBADSLT);
  68. assert(luksmeta_load(cd, 10, uuid, data, sizeof(data)) == -EBADSLT);
  69. assert(luksmeta_wipe(cd, 10, UUID) == -EBADSLT);
  70. assert(luksmeta_save(cd, -10, UUID, UUID, sizeof(UUID)) == -EBADSLT);
  71. assert(luksmeta_load(cd, -10, uuid, data, sizeof(data)) == -EBADSLT);
  72. assert(luksmeta_wipe(cd, -10, UUID) == -EBADSLT);
  73. assert(luksmeta_load(cd, -1, uuid, data, sizeof(data)) == -EBADSLT);
  74. assert(luksmeta_wipe(cd, -1, UUID) == -EBADSLT);
  75. /* Test for -EKEYREJECTED when a reserved UUID is used. */
  76. assert(luksmeta_save(cd, CRYPT_ANY_SLOT, (luksmeta_uuid_t) {},
  77. UUID, sizeof(UUID)) == -EKEYREJECTED);
  78. /* Test to make sure that data corruption is picked up correctly. */
  79. fd = open(filename, O_RDWR | O_SYNC);
  80. if (fd < 0)
  81. error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  82. if (lseek(fd, offset + 16, SEEK_SET) == -1)
  83. error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  84. if (write(fd, &(char) { 17 }, 1) != 1)
  85. error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  86. close(fd);
  87. assert(luksmeta_save(cd, 2, UUID, UUID, sizeof(UUID)) == -EINVAL);
  88. assert(luksmeta_load(cd, 2, uuid, data, sizeof(data)) == -EINVAL);
  89. assert(luksmeta_wipe(cd, 2, UUID) == -EINVAL);
  90. /* Test nuking */
  91. assert(luksmeta_init(cd) == 0);
  92. assert(luksmeta_test(cd) == 0);
  93. assert(luksmeta_nuke(cd) == 0);
  94. assert(luksmeta_test(cd) == -ENOENT);
  95. assert(test_layout((range_t[]) {
  96. { 0, 1024 }, /* LUKS header */
  97. { 1024, offset - 1024, true }, /* Keyslot Area */
  98. END(offset), /* Rest of the file */
  99. }));
  100. crypt_free(cd);
  101. unlink(filename);
  102. return 0;
  103. }