1
0

test-lm-one.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <errno.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. static const luksmeta_uuid_t UUID = {
  25. 0xb0, 0xcc, 0xe0, 0x48, 0x89, 0x32, 0x90, 0x7e,
  26. 0x10, 0xd9, 0x62, 0xa2, 0x09, 0x3c, 0x05, 0x7d
  27. };
  28. int
  29. main(int argc, char *argv[])
  30. {
  31. uint8_t data[sizeof(UUID)] = {};
  32. struct crypt_device *cd = NULL;
  33. luksmeta_uuid_t uuid = {};
  34. uint32_t offset = 0;
  35. uint32_t length = 0;
  36. int r;
  37. crypt_free(test_format());
  38. cd = test_init();
  39. test_hole(cd, &offset, &length);
  40. r = luksmeta_save(cd, CRYPT_ANY_SLOT, UUID, UUID, sizeof(UUID));
  41. if (r < 0)
  42. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  43. /* Test the layout state. */
  44. assert(test_layout((range_t[]) {
  45. { 0, 1024 }, /* LUKS header */
  46. { 1024, offset - 1024, true }, /* Keyslot Area */
  47. { offset, 4096 }, /* luksmeta header */
  48. { offset + 4096, 4096 }, /* luksmeta slot 0 */
  49. END(offset + 8192), /* Rest of the file */
  50. }));
  51. assert(luksmeta_load(cd, r, uuid, data, sizeof(data)) == sizeof(data));
  52. assert(memcmp(uuid, UUID, sizeof(UUID)) == 0);
  53. assert(memcmp(data, UUID, sizeof(UUID)) == 0);
  54. assert(luksmeta_save(cd, r, UUID, UUID, sizeof(UUID)) == -EALREADY);
  55. assert(luksmeta_wipe(cd, r, (luksmeta_uuid_t) {}) == -EKEYREJECTED);
  56. assert(luksmeta_wipe(cd, r, (luksmeta_uuid_t) { 1 }) == -EKEYREJECTED);
  57. assert(luksmeta_wipe(cd, r, UUID) == 0);
  58. assert(luksmeta_wipe(cd, r, UUID) == -EALREADY);
  59. assert(luksmeta_wipe(cd, r, (luksmeta_uuid_t) {}) == -EALREADY);
  60. /* Test the layout state. */
  61. assert(test_layout((range_t[]) {
  62. { 0, 1024 }, /* LUKS header */
  63. { 1024, offset - 1024, true }, /* Keyslot Area */
  64. { offset, 4096 }, /* luksmeta header */
  65. END(offset + 4096), /* Rest of the file */
  66. }));
  67. crypt_free(cd);
  68. unlink(filename);
  69. return 0;
  70. }