1
0

test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <assert.h>
  20. #include <error.h>
  21. #include <errno.h>
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include "test.h"
  28. char filename[] = "/tmp/luksmetaXXXXXX";
  29. static size_t
  30. first_nonzero(FILE *file, size_t start, size_t length)
  31. {
  32. uint8_t *buffer;
  33. buffer = malloc(length);
  34. if (!buffer)
  35. return false;
  36. if (fseek(file, start, SEEK_SET) != 0) {
  37. free(buffer);
  38. return false;
  39. }
  40. if (fread(buffer, length, 1, file) != 1) {
  41. free(buffer);
  42. return false;
  43. }
  44. for (size_t i = 0; i < length; i++) {
  45. if (buffer[i] != 0) {
  46. free(buffer);
  47. return i;
  48. }
  49. }
  50. free(buffer);
  51. return length;
  52. }
  53. bool
  54. test_layout(range_t *ranges)
  55. {
  56. bool valid = false;
  57. FILE *file = NULL;
  58. file = fopen(filename, "r");
  59. if (!file)
  60. return false;
  61. for (size_t i = 0; ranges[i].length != 0; i++) {
  62. size_t nonzero;
  63. fprintf(stderr, "%08zu:%08zu (%c= 0)\n",
  64. ranges[i].start, ranges[i].start + ranges[i].length,
  65. ranges[i].zero ? '=' : '!');
  66. nonzero = first_nonzero(file, ranges[i].start, ranges[i].length);
  67. if (ranges[i].zero && nonzero < ranges[i].length) {
  68. fprintf(stderr, "unexpected nonzero: %zu\n", nonzero);
  69. goto egress;
  70. } else if (!ranges[i].zero && nonzero == ranges[i].length) {
  71. fprintf(stderr, "unexpected zero: %zu-%zu\n",
  72. ranges[i].start, ranges[i].start + ranges[i].length);
  73. goto egress;
  74. }
  75. }
  76. valid = true;
  77. egress:
  78. fclose(file);
  79. return valid;
  80. }
  81. void
  82. test_hole(struct crypt_device *cd, uint32_t *offset, uint32_t *length)
  83. {
  84. uint64_t payload_offset = 0;
  85. uint64_t keyarea_end = 0;
  86. int r = 0;
  87. payload_offset = crypt_get_data_offset(cd) * 512;
  88. if (payload_offset < ALIGN(1, true))
  89. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  90. for (int slot = 0; slot < crypt_keyslot_max(CRYPT_LUKS1); slot++) {
  91. uint64_t off = 0;
  92. uint64_t len = 0;
  93. r = crypt_keyslot_area(cd, slot, &off, &len);
  94. if (r < 0)
  95. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  96. if (off + len > keyarea_end)
  97. keyarea_end = off + len;
  98. }
  99. *offset = ALIGN(keyarea_end, true);
  100. *length = ALIGN(payload_offset, false) - *offset;
  101. }
  102. struct crypt_device *
  103. test_format(void)
  104. {
  105. struct crypt_device *cd = NULL;
  106. int fd;
  107. int r;
  108. fd = mkstemp(filename);
  109. if (fd < 0)
  110. error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  111. /* Create a 4MB sparse file. */
  112. if (lseek(fd, 4194303, SEEK_SET) == -1)
  113. error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  114. if (write(fd, "", 1) != 1)
  115. error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  116. close(fd);
  117. r = crypt_init(&cd, filename);
  118. if (r < 0)
  119. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  120. r = crypt_format(cd, CRYPT_LUKS1, "aes", "xts-plain64",
  121. NULL, NULL, 32, NULL);
  122. if (r < 0)
  123. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  124. return cd;
  125. }
  126. struct crypt_device *
  127. test_init(void)
  128. {
  129. struct crypt_device *cd = NULL;
  130. int r;
  131. r = crypt_init(&cd, filename);
  132. if (r < 0)
  133. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  134. r = crypt_load(cd, CRYPT_LUKS1, NULL);
  135. if (r < 0)
  136. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  137. r = luksmeta_init(cd);
  138. if (r < 0)
  139. error(EXIT_FAILURE, -r, "%s:%d", __FILE__, __LINE__);
  140. return cd;
  141. }