nvram_hal_test.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. //
  2. // Copyright (C) 2016 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include <algorithm>
  17. #include <string>
  18. #include <vector>
  19. #include <android-base/logging.h>
  20. #include <android-base/macros.h>
  21. #include <gtest/gtest.h>
  22. #include <hardware/nvram.h>
  23. #include <openssl/sha.h>
  24. #include "nvram/hal/tests/scoped_nvram_device.h"
  25. namespace {
  26. constexpr uint32_t kTestIndex1 = 0xDEAD0001;
  27. constexpr uint32_t kTestIndex2 = 0xDEAD0002;
  28. constexpr uint32_t kTestIndexNeverExists = 0xDEAD0003;
  29. // Once we run a test that locks writing, that space is burned until reboot.
  30. // This value is the base index from which to dynamically burn spaces.
  31. constexpr uint32_t kTestIndexBurnBase = 0xDEAD0010;
  32. constexpr uint32_t kTestIndexBurnMax = 0xDEAD00FF;
  33. constexpr nvram_control_t kDefaultControls[] = {NV_CONTROL_BOOT_WRITE_LOCK,
  34. NV_CONTROL_BOOT_READ_LOCK};
  35. constexpr char kNoAuth[] = "";
  36. // If using authorization with an index returned by GetNextBurnSpace use this
  37. // as the value so the space can be cleaned up later.
  38. constexpr char kBurnSpaceAuth[] = "hal_test_burn";
  39. // Returns true if |target| contains |value|.
  40. template <typename T>
  41. bool Contains(T value, const std::vector<T>& target) {
  42. return (std::find(target.begin(), target.end(), value) != target.end());
  43. }
  44. // Returns true if |target| contains all of |values|.
  45. template <typename T>
  46. bool ContainsAll(const std::vector<T>& values,
  47. const std::vector<T>& target) {
  48. return std::all_of(values.begin(), values.end(),
  49. [target](T value) { return Contains(value, target); });
  50. }
  51. // Adds a few safety checks so tests don't get hardware into a state where it
  52. // needs factory reset.
  53. class SafeScopedNvramDevice : public nvram::ScopedNvramDevice {
  54. public:
  55. nvram_result_t CreateSpace(uint32_t index,
  56. uint64_t size_in_bytes,
  57. const std::vector<nvram_control_t>& control_list,
  58. const std::string& authorization_value) override {
  59. CHECK(!Contains(NV_CONTROL_PERSISTENT_WRITE_LOCK, control_list))
  60. << "Do not use NV_CONTROL_PERSISTENT_WRITE_LOCK in tests.";
  61. CHECK(!Contains(NV_CONTROL_BOOT_WRITE_LOCK, control_list) ||
  62. !Contains(NV_CONTROL_WRITE_AUTHORIZATION, control_list) ||
  63. authorization_value == kNoAuth ||
  64. authorization_value == kBurnSpaceAuth)
  65. << "Do not lock spaces with unknown authorization values.";
  66. return nvram::ScopedNvramDevice::CreateSpace(
  67. index, size_in_bytes, control_list, authorization_value);
  68. }
  69. nvram_result_t DisableCreate() override {
  70. LOG(FATAL) << "Do not use DisableCreate in tests.";
  71. return NV_RESULT_OPERATION_DISABLED;
  72. }
  73. };
  74. class ScopedNvramSpace {
  75. public:
  76. ScopedNvramSpace(SafeScopedNvramDevice* device, uint32_t index, uint32_t size)
  77. : ScopedNvramSpace(device,
  78. index,
  79. size,
  80. std::vector<nvram_control_t>(
  81. &kDefaultControls[0],
  82. &kDefaultControls[arraysize(kDefaultControls)]),
  83. kNoAuth) {}
  84. ScopedNvramSpace(SafeScopedNvramDevice* device,
  85. uint32_t index,
  86. uint32_t size,
  87. const std::vector<nvram_control_t>& control_list)
  88. : ScopedNvramSpace(device,
  89. index,
  90. size,
  91. control_list,
  92. kNoAuth) {}
  93. ScopedNvramSpace(SafeScopedNvramDevice* device,
  94. uint32_t index,
  95. uint32_t size,
  96. const std::vector<nvram_control_t>& control_list,
  97. const std::string& authorization_value)
  98. : device_(device),
  99. index_(index),
  100. authorization_value_(authorization_value) {
  101. Create(size, control_list);
  102. }
  103. ~ScopedNvramSpace() { Delete(); }
  104. private:
  105. void Create(uint32_t size,
  106. const std::vector<nvram_control_t>& control_list) {
  107. ASSERT_EQ(
  108. NV_RESULT_SUCCESS,
  109. device_->CreateSpace(index_, size, control_list, authorization_value_));
  110. }
  111. void Delete() {
  112. ASSERT_EQ(NV_RESULT_SUCCESS,
  113. device_->DeleteSpace(index_, authorization_value_));
  114. }
  115. SafeScopedNvramDevice* device_;
  116. uint32_t index_;
  117. std::string authorization_value_;
  118. };
  119. // Remove all unlocked burn spaces. Returns false on failure.
  120. bool CleanBurnSpaces(SafeScopedNvramDevice* device) {
  121. // Burned spaces will only be available for cleanup after reboot so there's no
  122. // sense in attempting cleanup more than once.
  123. static bool cleaned = false;
  124. if (cleaned) {
  125. return true;
  126. }
  127. bool success = true;
  128. cleaned = true;
  129. std::vector<uint32_t> space_index_list;
  130. if (device->GetSpaceList(&space_index_list) != NV_RESULT_SUCCESS) {
  131. return false;
  132. }
  133. for (uint32_t index : space_index_list) {
  134. if (index >= kTestIndexBurnBase && index <= kTestIndexBurnMax) {
  135. int write_lock, read_lock;
  136. if (device->IsSpaceLocked(index, &write_lock, &read_lock) !=
  137. NV_RESULT_SUCCESS) {
  138. success = false;
  139. continue;
  140. }
  141. if (!write_lock) {
  142. nvram_result_t result = device->DeleteSpace(index, kNoAuth);
  143. if (result == NV_RESULT_ACCESS_DENIED) {
  144. result = device->DeleteSpace(index, kBurnSpaceAuth);
  145. }
  146. if (result != NV_RESULT_SUCCESS) {
  147. success = false;
  148. continue;
  149. }
  150. }
  151. }
  152. }
  153. return success;
  154. }
  155. // Returns the next available burn space index. If using authorization, the
  156. // value MUST be kBurnSpaceAuth.
  157. bool GetNextBurnSpace(SafeScopedNvramDevice* device, uint32_t* index) {
  158. if (!CleanBurnSpaces(device)) {
  159. return false;
  160. }
  161. std::vector<uint32_t> space_index_list;
  162. if (device->GetSpaceList(&space_index_list) != NV_RESULT_SUCCESS) {
  163. return false;
  164. }
  165. *index = kTestIndexBurnBase;
  166. while (Contains(*index, space_index_list)) {
  167. (*index)++;
  168. }
  169. if (*index >= kTestIndexBurnMax) {
  170. return false;
  171. }
  172. return true;
  173. }
  174. std::string SHA256HashString(const std::string& input) {
  175. uint8_t hash[SHA256_DIGEST_LENGTH];
  176. SHA256(reinterpret_cast<const uint8_t*>(input.data()), input.size(), hash);
  177. return std::string(reinterpret_cast<const char*>(hash), SHA256_DIGEST_LENGTH);
  178. }
  179. } // namespace
  180. namespace nvram {
  181. TEST(NVRAMModuleTest, TotalSize) {
  182. SafeScopedNvramDevice device;
  183. uint64_t total_size = 0;
  184. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetTotalSizeInBytes(&total_size));
  185. EXPECT_LE(2048u, total_size);
  186. };
  187. TEST(NVRAMModuleTest, AvailableSize) {
  188. SafeScopedNvramDevice device;
  189. uint64_t available_size = 0;
  190. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetAvailableSizeInBytes(&available_size));
  191. uint64_t total_size = 0;
  192. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetTotalSizeInBytes(&total_size));
  193. EXPECT_LE(available_size, total_size);
  194. }
  195. TEST(NVRAMModuleTest, MaxSpaceSize) {
  196. SafeScopedNvramDevice device;
  197. uint64_t max_space_size = 0;
  198. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaceSizeInBytes(&max_space_size));
  199. uint64_t total_size = 0;
  200. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetTotalSizeInBytes(&total_size));
  201. EXPECT_LE(max_space_size, total_size);
  202. EXPECT_GE(max_space_size, 32u);
  203. }
  204. TEST(NVRAMModuleTest, MaxSpaces) {
  205. SafeScopedNvramDevice device;
  206. uint32_t num_spaces = 0;
  207. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaces(&num_spaces));
  208. EXPECT_LE(8u, num_spaces);
  209. }
  210. TEST(NVRAMModuleTest, SpaceList) {
  211. SafeScopedNvramDevice device;
  212. uint32_t max_spaces = 0;
  213. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaces(&max_spaces));
  214. std::vector<uint32_t> space_index_list;
  215. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceList(&space_index_list));
  216. ASSERT_LE(space_index_list.size(), max_spaces);
  217. // Add a test space and check it gets reported.
  218. {
  219. ScopedNvramSpace space(&device, kTestIndex1, 32);
  220. std::vector<uint32_t> space_index_list2;
  221. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceList(&space_index_list2));
  222. ASSERT_EQ(space_index_list.size() + 1, space_index_list2.size());
  223. EXPECT_TRUE(ContainsAll(space_index_list, space_index_list2));
  224. EXPECT_TRUE(Contains(kTestIndex1, space_index_list2));
  225. }
  226. // Check we're back to the original list.
  227. std::vector<uint32_t> space_index_list3;
  228. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceList(&space_index_list3));
  229. ASSERT_EQ(space_index_list.size(), space_index_list3.size());
  230. EXPECT_TRUE(ContainsAll(space_index_list, space_index_list3));
  231. EXPECT_FALSE(Contains(kTestIndex1, space_index_list3));
  232. }
  233. TEST(NVRAMModuleTest, SpaceSize) {
  234. SafeScopedNvramDevice device;
  235. ScopedNvramSpace space(&device, kTestIndex1, 17);
  236. ScopedNvramSpace space2(&device, kTestIndex2, 32);
  237. uint64_t size = 0;
  238. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceSize(kTestIndex1, &size));
  239. EXPECT_EQ(17u, size);
  240. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceSize(kTestIndex2, &size));
  241. EXPECT_EQ(32u, size);
  242. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  243. device.GetSpaceSize(kTestIndexNeverExists, &size));
  244. }
  245. TEST(NVRAMModuleTest, SpaceControls) {
  246. SafeScopedNvramDevice device;
  247. ScopedNvramSpace space(&device, kTestIndex1, 32);
  248. std::vector<nvram_control_t> expected_control_list(
  249. &kDefaultControls[0], &kDefaultControls[arraysize(kDefaultControls)]);
  250. std::vector<nvram_control_t> control_list;
  251. ASSERT_EQ(NV_RESULT_SUCCESS,
  252. device.GetSpaceControls(kTestIndex1, &control_list));
  253. ASSERT_EQ(expected_control_list.size(), control_list.size());
  254. EXPECT_TRUE(ContainsAll(expected_control_list, control_list));
  255. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  256. device.GetSpaceControls(kTestIndexNeverExists, &control_list));
  257. }
  258. TEST(NVRAMModuleTest, IsLocked) {
  259. SafeScopedNvramDevice device;
  260. ScopedNvramSpace space(&device, kTestIndex1, 32);
  261. int write_lock, read_lock;
  262. ASSERT_EQ(NV_RESULT_SUCCESS,
  263. device.IsSpaceLocked(kTestIndex1, &write_lock, &read_lock));
  264. EXPECT_FALSE(read_lock);
  265. EXPECT_FALSE(write_lock);
  266. ASSERT_EQ(NV_RESULT_SUCCESS, device.EnableReadLock(kTestIndex1, kNoAuth));
  267. ASSERT_EQ(NV_RESULT_SUCCESS,
  268. device.IsSpaceLocked(kTestIndex1, &write_lock, &read_lock));
  269. EXPECT_TRUE(read_lock);
  270. EXPECT_FALSE(write_lock);
  271. EXPECT_EQ(
  272. NV_RESULT_SPACE_DOES_NOT_EXIST,
  273. device.IsSpaceLocked(kTestIndexNeverExists, &write_lock, &read_lock));
  274. }
  275. TEST(NVRAMModuleTest, CreateSmall) {
  276. SafeScopedNvramDevice device;
  277. ScopedNvramSpace space(&device, kTestIndex1, 1);
  278. }
  279. TEST(NVRAMModuleTest, CreateLarge) {
  280. SafeScopedNvramDevice device;
  281. uint64_t max_space_size = 0;
  282. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaceSizeInBytes(&max_space_size));
  283. uint64_t available_size = 0;
  284. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetAvailableSizeInBytes(&available_size));
  285. ScopedNvramSpace space(&device, kTestIndex1,
  286. std::min(max_space_size, available_size));
  287. }
  288. TEST(NVRAMModuleTest, CreateWithCustomControls) {
  289. const std::vector<nvram_control_t> kControlList{
  290. NV_CONTROL_BOOT_WRITE_LOCK, NV_CONTROL_READ_AUTHORIZATION,
  291. NV_CONTROL_WRITE_EXTEND};
  292. SafeScopedNvramDevice device;
  293. ScopedNvramSpace space(&device, kTestIndex1, 32, kControlList);
  294. std::vector<nvram_control_t> control_list;
  295. ASSERT_EQ(NV_RESULT_SUCCESS,
  296. device.GetSpaceControls(kTestIndex1, &control_list));
  297. ASSERT_EQ(kControlList.size(), control_list.size());
  298. EXPECT_TRUE(ContainsAll(control_list, kControlList));
  299. EXPECT_TRUE(ContainsAll(kControlList, control_list));
  300. }
  301. TEST(NVRAMModuleTest, CreateWithAuthorization) {
  302. SafeScopedNvramDevice device;
  303. std::string password = "hunter2";
  304. ScopedNvramSpace space(
  305. &device, kTestIndex1, 32,
  306. {NV_CONTROL_WRITE_AUTHORIZATION, NV_CONTROL_READ_AUTHORIZATION},
  307. password);
  308. std::string data = "test";
  309. std::string bad_password = "*******";
  310. EXPECT_EQ(NV_RESULT_ACCESS_DENIED,
  311. device.WriteSpace(kTestIndex1, data, bad_password));
  312. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(kTestIndex1, data, password));
  313. }
  314. TEST(NVRAMModuleTest, CreateAlreadyExists) {
  315. SafeScopedNvramDevice device;
  316. ScopedNvramSpace space(&device, kTestIndex1, 32);
  317. EXPECT_EQ(NV_RESULT_SPACE_ALREADY_EXISTS,
  318. device.CreateSpace(kTestIndex1, 32, {}, kNoAuth));
  319. }
  320. TEST(NVRAMModuleTest, Delete) {
  321. SafeScopedNvramDevice device;
  322. {
  323. ScopedNvramSpace space(&device, kTestIndex1, 32);
  324. uint64_t size = 0;
  325. EXPECT_EQ(NV_RESULT_SUCCESS, device.GetSpaceSize(kTestIndex1, &size));
  326. }
  327. // ScopedNvramSpace will call Delete when it falls out of scope. Now we can
  328. // make sure that worked.
  329. uint64_t size = 0;
  330. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  331. device.GetSpaceSize(kTestIndex1, &size));
  332. }
  333. TEST(NVRAMModuleTest, WriteLock) {
  334. SafeScopedNvramDevice device;
  335. uint32_t index;
  336. ASSERT_TRUE(GetNextBurnSpace(&device, &index));
  337. ASSERT_EQ(
  338. NV_RESULT_SUCCESS,
  339. device.CreateSpace(index, 32, {NV_CONTROL_BOOT_WRITE_LOCK}, kNoAuth));
  340. int write_lock, read_lock;
  341. EXPECT_EQ(NV_RESULT_SUCCESS,
  342. device.IsSpaceLocked(index, &write_lock, &read_lock));
  343. EXPECT_FALSE(write_lock);
  344. EXPECT_FALSE(read_lock);
  345. // It should be possible to delete if the space has not yet been locked.
  346. ASSERT_EQ(NV_RESULT_SUCCESS, device.DeleteSpace(index, kNoAuth));
  347. ASSERT_EQ(
  348. NV_RESULT_SUCCESS,
  349. device.CreateSpace(index, 32, {NV_CONTROL_BOOT_WRITE_LOCK}, kNoAuth));
  350. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test", kNoAuth));
  351. EXPECT_EQ(NV_RESULT_SUCCESS, device.EnableWriteLock(index, kNoAuth));
  352. EXPECT_EQ(NV_RESULT_SUCCESS,
  353. device.IsSpaceLocked(index, &write_lock, &read_lock));
  354. EXPECT_TRUE(write_lock);
  355. EXPECT_FALSE(read_lock);
  356. EXPECT_EQ(NV_RESULT_OPERATION_DISABLED,
  357. device.WriteSpace(index, "test2", kNoAuth));
  358. EXPECT_EQ(NV_RESULT_OPERATION_DISABLED, device.DeleteSpace(index, kNoAuth));
  359. std::string data;
  360. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 4, kNoAuth, &data));
  361. EXPECT_EQ("test", data);
  362. }
  363. TEST(NVRAMModuleTest, ReadLock) {
  364. uint32_t index = kTestIndex1;
  365. SafeScopedNvramDevice device;
  366. ScopedNvramSpace space(&device, index, 32, {NV_CONTROL_BOOT_READ_LOCK});
  367. int write_lock, read_lock;
  368. EXPECT_EQ(NV_RESULT_SUCCESS,
  369. device.IsSpaceLocked(index, &write_lock, &read_lock));
  370. EXPECT_FALSE(write_lock);
  371. EXPECT_FALSE(read_lock);
  372. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test", kNoAuth));
  373. std::string data;
  374. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 4, kNoAuth, &data));
  375. EXPECT_EQ("test", data);
  376. EXPECT_EQ(NV_RESULT_SUCCESS, device.EnableReadLock(index, kNoAuth));
  377. EXPECT_EQ(NV_RESULT_SUCCESS,
  378. device.IsSpaceLocked(index, &write_lock, &read_lock));
  379. EXPECT_FALSE(write_lock);
  380. EXPECT_TRUE(read_lock);
  381. EXPECT_EQ(NV_RESULT_OPERATION_DISABLED,
  382. device.ReadSpace(index, 4, kNoAuth, &data));
  383. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test2", kNoAuth));
  384. }
  385. TEST(NVRAMModuleTest, WriteAuthorization) {
  386. uint32_t index = kTestIndex1;
  387. std::string password = "hunter2";
  388. SafeScopedNvramDevice device;
  389. ScopedNvramSpace space(&device, index, 32, {NV_CONTROL_WRITE_AUTHORIZATION},
  390. password);
  391. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test", password));
  392. EXPECT_EQ(NV_RESULT_ACCESS_DENIED,
  393. device.WriteSpace(index, "test2", kNoAuth));
  394. EXPECT_EQ(NV_RESULT_ACCESS_DENIED,
  395. device.WriteSpace(index, "test3", "bad_password"));
  396. EXPECT_EQ(NV_RESULT_ACCESS_DENIED, device.DeleteSpace(index, kNoAuth));
  397. EXPECT_EQ(NV_RESULT_ACCESS_DENIED, device.DeleteSpace(index, "bad"));
  398. std::string data;
  399. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 4, kNoAuth, &data));
  400. EXPECT_EQ("test", data);
  401. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 4, password, &data));
  402. }
  403. TEST(NVRAMModuleTest, ReadAuthorization) {
  404. uint32_t index = kTestIndex1;
  405. std::string password = "hunter2";
  406. SafeScopedNvramDevice device;
  407. ScopedNvramSpace space(&device, index, 32, {NV_CONTROL_READ_AUTHORIZATION},
  408. password);
  409. ASSERT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test", password));
  410. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test2", kNoAuth));
  411. std::string data;
  412. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 4, password, &data));
  413. EXPECT_EQ("test", data);
  414. EXPECT_EQ(NV_RESULT_ACCESS_DENIED,
  415. device.ReadSpace(index, 4, kNoAuth, &data));
  416. EXPECT_EQ(NV_RESULT_ACCESS_DENIED,
  417. device.ReadSpace(index, 4, "bad_password", &data));
  418. }
  419. TEST(NVRAMModuleTest, WriteLockAuthorization) {
  420. SafeScopedNvramDevice device;
  421. uint32_t index;
  422. ASSERT_TRUE(GetNextBurnSpace(&device, &index));
  423. ASSERT_EQ(NV_RESULT_SUCCESS,
  424. device.CreateSpace(index, 32, {NV_CONTROL_BOOT_WRITE_LOCK,
  425. NV_CONTROL_BOOT_READ_LOCK,
  426. NV_CONTROL_WRITE_AUTHORIZATION},
  427. kBurnSpaceAuth));
  428. EXPECT_EQ(NV_RESULT_SUCCESS, device.EnableReadLock(index, kNoAuth));
  429. EXPECT_EQ(NV_RESULT_ACCESS_DENIED, device.EnableWriteLock(index, kNoAuth));
  430. EXPECT_EQ(NV_RESULT_ACCESS_DENIED, device.EnableWriteLock(index, "bad"));
  431. EXPECT_EQ(NV_RESULT_SUCCESS, device.EnableWriteLock(index, kBurnSpaceAuth));
  432. }
  433. TEST(NVRAMModuleTest, ReadLockAuthorization) {
  434. uint32_t index = kTestIndex1;
  435. std::string password = "hunter2";
  436. SafeScopedNvramDevice device;
  437. ScopedNvramSpace space(&device, index, 32,
  438. {NV_CONTROL_BOOT_WRITE_LOCK, NV_CONTROL_BOOT_READ_LOCK,
  439. NV_CONTROL_READ_AUTHORIZATION},
  440. password);
  441. EXPECT_EQ(NV_RESULT_ACCESS_DENIED, device.EnableReadLock(index, kNoAuth));
  442. EXPECT_EQ(NV_RESULT_ACCESS_DENIED, device.EnableReadLock(index, "bad"));
  443. EXPECT_EQ(NV_RESULT_SUCCESS, device.EnableReadLock(index, password));
  444. }
  445. TEST(NVRAMModuleTest, WriteExtend) {
  446. uint32_t index = kTestIndex1;
  447. SafeScopedNvramDevice device;
  448. ScopedNvramSpace space(&device, index, 32, {NV_CONTROL_WRITE_EXTEND});
  449. ASSERT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test", kNoAuth));
  450. std::string data;
  451. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 32, kNoAuth, &data));
  452. std::string hash1 = SHA256HashString(std::string(32, 0) + "test");
  453. EXPECT_EQ(hash1, data);
  454. EXPECT_EQ(NV_RESULT_SUCCESS, device.WriteSpace(index, "test2", kNoAuth));
  455. EXPECT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 32, kNoAuth, &data));
  456. std::string hash2 = SHA256HashString(hash1 + "test2");
  457. EXPECT_EQ(hash2, data);
  458. }
  459. TEST(NVRAMModuleTest, WriteExtendTooShort) {
  460. uint32_t index = kTestIndex1;
  461. SafeScopedNvramDevice device;
  462. // Only SHA-256 is supported. Try 20 which is SHA-1 output.
  463. EXPECT_EQ(
  464. NV_RESULT_INVALID_PARAMETER,
  465. device.CreateSpace(index, 20, {NV_CONTROL_WRITE_EXTEND}, kNoAuth));
  466. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  467. device.WriteSpace(index, "test", kNoAuth));
  468. }
  469. TEST(NVRAMModuleTest, WriteExtendTooLong) {
  470. uint32_t index = kTestIndex1;
  471. SafeScopedNvramDevice device;
  472. uint64_t max_space_size = 0;
  473. ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaceSizeInBytes(&max_space_size));
  474. if (max_space_size > 32) {
  475. // Only SHA-256 is supported. Try 64 which is SHA-512 output.
  476. EXPECT_EQ(NV_RESULT_INVALID_PARAMETER,
  477. device.CreateSpace(index, std::min<uint64_t>(max_space_size, 64),
  478. {NV_CONTROL_WRITE_EXTEND}, kNoAuth));
  479. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  480. device.WriteSpace(index, "test", kNoAuth));
  481. }
  482. }
  483. TEST(NVRAMModuleTest, InitialValue) {
  484. uint32_t index = kTestIndex1;
  485. SafeScopedNvramDevice device;
  486. ScopedNvramSpace space(&device, index, 32);
  487. std::string data;
  488. ASSERT_EQ(NV_RESULT_SUCCESS, device.ReadSpace(index, 32, kNoAuth, &data));
  489. EXPECT_EQ(std::string(32, 0), data);
  490. }
  491. TEST(NVRAMModuleTest, ReadWriteSpaceDoesNotExist) {
  492. uint32_t index = kTestIndexNeverExists;
  493. SafeScopedNvramDevice device;
  494. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  495. device.WriteSpace(index, "test", kNoAuth));
  496. std::string data;
  497. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  498. device.ReadSpace(index, 1, kNoAuth, &data));
  499. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  500. device.EnableWriteLock(index, kNoAuth));
  501. EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST,
  502. device.EnableReadLock(index, kNoAuth));
  503. }
  504. } // namespace nvram