awa_unit_2.4.0_59135a52/ada-keystore/regtests/keystore-tools-tests.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-----------------------------------------------------------------------
--  keystore-tools-tests -- Tests for files
--  Copyright (C) 2019, 2021, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Ada.Directories;
with Util.Test_Caller;
with Util.Strings;
with Keystore.Files;
package body Keystore.Tools.Tests is

   use Ada.Directories;
   use type Interfaces.Unsigned_64;

   package Caller is new Util.Test_Caller (Test, "Keystore.Tools");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test Keystore.Files.Create+Open",
                       Test_Store_Directory'Access);
   end Add_Tests;

   --  ------------------------------
   --  Test storing a directory tree
   --  ------------------------------
   procedure Test_Store_Directory (T : in out Test) is
      function Filter (Ent : in Directory_Entry_Type) return Boolean;

      function Filter (Ent : in Directory_Entry_Type) return Boolean is
         Name  : constant String := Ada.Directories.Simple_Name (Ent);
         Kind  : constant File_Kind := Ada.Directories.Kind (Ent);
      begin
         if Kind = Ada.Directories.Ordinary_File then
            return Util.Strings.Ends_With (Name, ".ads");
         else
            return Name /= ".git" and then Name /= "results";
         end if;
      end Filter;

      Path     : constant String := Util.Tests.Get_Test_Path ("test-store.akt");
      Password : Keystore.Secret_Key := Keystore.Create ("mypassword");
      Config   : Keystore.Wallet_Config := Unsecure_Config;
   begin
      declare
         W        : Keystore.Files.Wallet_File;
      begin
         Config.Overwrite := True;
         W.Create (Path => Path, Password => Password, Config => Config);
         Keystore.Tools.Store (Wallet  => W,
                               Path    => ".",
                               Prefix  => "store/",
                               Pattern => "*",
                               Filter  => Filter'Access);
      end;
      declare
         procedure Check (Name : in String);

         W        : Keystore.Files.Wallet_File;

         procedure Check (Name : in String) is
            Item     : Keystore.Entry_Info;
         begin
            Item := W.Find (Name);
            T.Assert (Item.Size > 8192, "Invalid item size for " & Name);
            T.Assert (Item.Size < 128 * 1024, "Invalid item size for " & Name);
            T.Assert (Item.Block_Count > 2, "Invalid item for " & Name);
            T.Assert (Item.Kind = T_FILE, "Invalid item type for " & Name);
         end Check;

      begin
         W.Open (Password => Password, Path => Path);
         Check ("store/src/keystore.ads");
         Check ("store/src/keystore-repository.ads");
         Check ("store/obj/b__akt-main.ads");
      end;
   end Test_Store_Directory;

end Keystore.Tools.Tests;