1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "catch.hpp"
15 #include <algorithm>
16 #include <cstring>
17 #include "nvs_test_api.h"
18 #include "nvs_handle_simple.hpp"
19 #include "nvs_partition.hpp"
20 #include "spi_flash_emulation.h"
21 
22 #include "test_fixtures.hpp"
23 
24 #include <iostream>
25 
26 using namespace std;
27 using namespace nvs;
28 
29 TEST_CASE("encrypted partition read size must be item size", "[nvs]")
30 {
31     char foo [32] = { };
32     nvs_sec_cfg_t xts_cfg;
33     for(int count = 0; count < NVS_KEY_SIZE; count++) {
34         xts_cfg.eky[count] = 0x11;
35         xts_cfg.tky[count] = 0x22;
36     }
37     EncryptedPartitionFixture fix(&xts_cfg);
38 
39     CHECK(fix.part.read(0, foo, sizeof (foo) -1) == ESP_ERR_INVALID_SIZE);
40 }
41 
42 TEST_CASE("encrypted partition write size must be mod item size", "[nvs]")
43 {
44     char foo [64] = { };
45     nvs_sec_cfg_t xts_cfg;
46     for(int count = 0; count < NVS_KEY_SIZE; count++) {
47         xts_cfg.eky[count] = 0x11;
48         xts_cfg.tky[count] = 0x22;
49     }
50     EncryptedPartitionFixture fix(&xts_cfg);
51 
52     CHECK(fix.part.write(0, foo, sizeof (foo) -1) == ESP_ERR_INVALID_SIZE);
53     CHECK(fix.part.write(0, foo, sizeof (foo)) == ESP_OK);
54     CHECK(fix.part.write(0, foo, sizeof (foo) * 2) == ESP_OK);
55 }
56