test-lm-two.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <error.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. static const luksmeta_uuid_t UUID0 = {
  24. 0x35, 0x08, 0x50, 0xc3, 0x25, 0xc9, 0x85, 0xea,
  25. 0x1b, 0x55, 0x93, 0x56, 0x36, 0x2a, 0xd9, 0x85
  26. };
  27. static const luksmeta_uuid_t UUID1 = {
  28. 0xb4, 0xcb, 0x8c, 0x1c, 0x34, 0xea, 0xcc, 0x21,
  29. 0x0b, 0x9c, 0xc3, 0x9c, 0x9a, 0x09, 0xc0, 0x0f
  30. };
  31. int
  32. main(int argc, char *argv[])
  33. {
  34. uint8_t data[sizeof(UUID0)] = {};
  35. struct crypt_device *cd = NULL;
  36. luksmeta_uuid_t uuid = {};
  37. uint32_t offset = 0;
  38. uint32_t length = 0;
  39. int r;
  40. crypt_free(test_format());
  41. cd = test_init();
  42. test_hole(cd, &offset, &length);
  43. /* Add one metadata. */
  44. r = luksmeta_save(cd, 0, UUID0, UUID0, sizeof(UUID0));
  45. if (r < 0)
  46. error(EXIT_FAILURE, -r, "luksmeta_save()");
  47. assert(test_layout((range_t[]) {
  48. { 0, 1024 }, /* LUKS header */
  49. { 1024, offset - 1024, true }, /* Keyslot Area */
  50. { offset, 4096 }, /* luksmeta header */
  51. { offset + 4096, 4096 }, /* luksmeta slot 0 */
  52. END(offset + 8192), /* Rest of the file */
  53. }));
  54. assert(luksmeta_load(cd, 0, uuid, data, sizeof(data)) == sizeof(data));
  55. assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  56. assert(memcmp(data, UUID0, sizeof(UUID0)) == 0);
  57. /* Add a second metadata. */
  58. r = luksmeta_save(cd, 1, UUID1, UUID1, sizeof(UUID1));
  59. if (r < 0)
  60. error(EXIT_FAILURE, -r, "luksmeta_save()");
  61. assert(test_layout((range_t[]) {
  62. { 0, 1024 }, /* LUKS header */
  63. { 1024, offset - 1024, true }, /* Keyslot Area */
  64. { offset, 4096 }, /* luksmeta header */
  65. { offset + 4096, 4096 }, /* luksmeta slot 0 */
  66. { offset + 8192, 4096 }, /* luksmeta slot 1 */
  67. END(offset + 12288), /* Rest of the file */
  68. }));
  69. assert(luksmeta_load(cd, 0, uuid, data, sizeof(data)) == sizeof(data));
  70. assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  71. assert(memcmp(data, UUID0, sizeof(UUID0)) == 0);
  72. assert(luksmeta_load(cd, 1, uuid, data, sizeof(data)) == sizeof(data));
  73. assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
  74. assert(memcmp(data, UUID1, sizeof(UUID1)) == 0);
  75. /* Delete the first metadata. */
  76. assert(luksmeta_wipe(cd, 0, UUID0) == 0);
  77. assert(test_layout((range_t[]) {
  78. { 0, 1024 }, /* LUKS header */
  79. { 1024, offset - 1024, true }, /* Keyslot Area */
  80. { offset, 4096 }, /* luksmeta header */
  81. { offset + 4096, 4096, true }, /* luksmeta slot 0 */
  82. { offset + 8192, 4096 }, /* luksmeta slot 1 */
  83. END(offset + 12288), /* Rest of the file */
  84. }));
  85. /* Delete the second metadata. */
  86. assert(luksmeta_wipe(cd, 1, UUID1) == 0);
  87. assert(test_layout((range_t[]) {
  88. { 0, 1024 }, /* LUKS header */
  89. { 1024, offset - 1024, true }, /* Keyslot Area */
  90. { offset, 4096 }, /* luksmeta header */
  91. END(offset + 4096), /* Rest of the file */
  92. }));
  93. crypt_free(cd);
  94. unlink(filename);
  95. return 0;
  96. }