yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie HermaszewskaCorrect include dir for libslang (#5539)7b570feed

master
6.1 KiB185 linesraw
1// unit-test-sha1.cpp
2#include "../../source/core/slang-crypto.h"
3#include "unit-test/slang-unit-test.h"
4
5using namespace Slang;
6
7SLANG_UNIT_TEST(crypto)
8{
9    // HashDigest
10    {
11        using Digest = HashDigest<8>;
12        Digest empty;
13        SLANG_CHECK(empty.data[0] == 0 && empty.data[1] == 0);
14        SLANG_CHECK(empty.toString() == "0000000000000000");
15
16        Digest all = Digest("ffffffffffffffff");
17        SLANG_CHECK(all.data[0] == 0xffffffff && all.data[1] == 0xffffffff);
18        SLANG_CHECK(all.toString() == "ffffffffffffffff");
19
20        SLANG_CHECK(empty == Digest("0000000000000000"));
21        SLANG_CHECK(all == Digest("ffffffffffffffff"));
22        SLANG_CHECK(empty != all);
23
24        SLANG_CHECK(Digest("invalid") == empty);
25        SLANG_CHECK(Digest("X000000000000000") == empty);
26        SLANG_CHECK(Digest(" 000000000000000") == empty);
27
28        SLANG_CHECK(Digest("0123456789abcdef").toString() == "0123456789abcdef");
29
30        Slang::ComPtr<ISlangBlob> blob = Digest("0123456789abcdef").toBlob();
31        const uint8_t check[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
32        SLANG_CHECK(blob->getBufferSize() == 8);
33        SLANG_CHECK(::memcmp(blob->getBufferPointer(), check, 8) == 0);
34
35        SLANG_CHECK(Digest(blob) == Digest("0123456789abcdef"));
36    }
37
38    // MD5
39
40    // Empty string
41    {
42        MD5 sha1;
43        auto digest = sha1.finalize();
44        SLANG_CHECK(digest.toString() == "d41d8cd98f00b204e9800998ecf8427e");
45    }
46
47    // One call to update()
48    {
49        MD5 sha1;
50        const String str("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
51                         "tempor incididunt ut labore et dolore magna aliqua.");
52        sha1.update(str.getBuffer(), str.getLength());
53        auto digest = sha1.finalize();
54        SLANG_CHECK(digest.toString() == "818c6e601a24f72750da0f6c9b8ebe28");
55    }
56
57    // Two calls to update()
58    {
59        MD5 sha1;
60        const String str1("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
61                          "tempor incididunt ut labore et dolore magna aliqua.");
62        const String str2("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi "
63                          "ut aliquip ex ea commodo consequat.");
64        sha1.update(str1.getBuffer(), str1.getLength());
65        sha1.update(str2.getBuffer(), str2.getLength());
66        auto digest = sha1.finalize();
67        SLANG_CHECK(digest.toString() == "87d3caecb0ab82faae84d60fde994aca");
68    }
69
70    // compute()
71    {
72        SLANG_CHECK(MD5::compute(nullptr, 0).toString() == "d41d8cd98f00b204e9800998ecf8427e");
73        const String str("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
74                         "tempor incididunt ut labore et dolore magna aliqua.");
75        SLANG_CHECK(
76            MD5::compute(str.getBuffer(), str.getLength()).toString() ==
77            "818c6e601a24f72750da0f6c9b8ebe28");
78    }
79
80    // SHA1
81
82    // Empty string
83    {
84        SHA1 sha1;
85        auto digest = sha1.finalize();
86        SLANG_CHECK(digest.toString() == "da39a3ee5e6b4b0d3255bfef95601890afd80709");
87    }
88
89    // One call to update()
90    {
91        SHA1 sha1;
92        const String str("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
93                         "tempor incididunt ut labore et dolore magna aliqua.");
94        sha1.update(str.getBuffer(), str.getLength());
95        auto digest = sha1.finalize();
96        SLANG_CHECK(digest.toString() == "cca0871ecbe200379f0a1e4b46de177e2d62e655");
97    }
98
99    // Two calls to update()
100    {
101        SHA1 sha1;
102        const String str1("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
103                          "tempor incididunt ut labore et dolore magna aliqua.");
104        const String str2("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi "
105                          "ut aliquip ex ea commodo consequat.");
106        sha1.update(str1.getBuffer(), str1.getLength());
107        sha1.update(str2.getBuffer(), str2.getLength());
108        auto digest = sha1.finalize();
109        SLANG_CHECK(digest.toString() == "7a8213edf9976d2e693f27bbc7dc41546bcfcc97");
110    }
111
112    // compute()
113    {
114        SLANG_CHECK(
115            SHA1::compute(nullptr, 0).toString() == "da39a3ee5e6b4b0d3255bfef95601890afd80709");
116        const String str("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
117                         "tempor incididunt ut labore et dolore magna aliqua.");
118        SLANG_CHECK(
119            SHA1::compute(str.getBuffer(), str.getLength()).toString() ==
120            "cca0871ecbe200379f0a1e4b46de177e2d62e655");
121    }
122
123    // DigestBuider
124
125    // Raw numerical values, etc.
126    {
127        DigestBuilder<MD5> builder;
128
129        int64_t valueA = -1;
130        uint64_t valueB = 1;
131        builder.append(valueA);
132        builder.append(valueB);
133
134        auto digest = builder.finalize();
135        SLANG_CHECK(digest.toString() == "5ba171e20898bdd205639013746f2679");
136    }
137
138    // List
139    {
140        DigestBuilder<MD5> builder;
141
142        List<int64_t> listA;
143        listA.add(1);
144        listA.add(2);
145        listA.add(3);
146        listA.add(4);
147        builder.append(listA);
148
149        auto digest = builder.finalize();
150        SLANG_CHECK(digest.toString() == "9f66c130786a1a05e4731f71a3c5f172");
151    }
152
153    // UnownedStringSlice
154    {
155        DigestBuilder<MD5> builder;
156
157        UnownedStringSlice stringSlice = UnownedStringSlice("String Slice Test");
158        builder.append(stringSlice);
159
160        auto digest = builder.finalize();
161        SLANG_CHECK(digest.toString() == "5d6cc58e1824a4dfd0cf57395b603316");
162    }
163
164    // String
165    {
166        DigestBuilder<MD5> builder;
167
168        String str = String("String Test");
169        builder.append(str);
170
171        auto digest = builder.finalize();
172        SLANG_CHECK(digest.toString() == "df5a79cc2170c7401cf0a506ceb0ce24");
173    }
174
175    // Digest
176    {
177        DigestBuilder<MD5> builder;
178
179        MD5::Digest hash;
180        builder.append(hash);
181
182        auto digest = builder.finalize();
183        SLANG_CHECK(digest.toString() == "4ae71336e44bf9bf79d2752e234818a5");
184    }
185}