Skip to content

Commit b21a5b3

Browse files
committed
Work on tests.
1 parent a64cc38 commit b21a5b3

File tree

8 files changed

+113
-50
lines changed

8 files changed

+113
-50
lines changed

Tests/Aws.IoTCore.Devices.UnitTests.nfproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
<HintPath>..\packages\nanoFramework.Json.2.2.99\lib\nanoFramework.Json.dll</HintPath>
5151
<Private>True</Private>
5252
</Reference>
53+
<Reference Include="nanoFramework.Runtime.Events">
54+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
55+
</Reference>
5356
<Reference Include="nanoFramework.System.Collections, Version=1.5.18.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
5457
<HintPath>..\packages\nanoFramework.System.Collections.1.5.18\lib\nanoFramework.System.Collections.dll</HintPath>
5558
<Private>True</Private>
@@ -70,6 +73,15 @@
7073
<HintPath>..\packages\nanoFramework.System.IO.Streams.1.1.38\lib\System.IO.Streams.dll</HintPath>
7174
<Private>True</Private>
7275
</Reference>
76+
<Reference Include="System.Net">
77+
<HintPath>..\packages\nanoFramework.System.Net.1.10.52\lib\System.Net.dll</HintPath>
78+
</Reference>
79+
<Reference Include="System.Net.Http">
80+
<HintPath>..\packages\nanoFramework.System.Net.Http.1.5.99\lib\System.Net.Http.dll</HintPath>
81+
</Reference>
82+
<Reference Include="System.Threading">
83+
<HintPath>..\packages\nanoFramework.System.Threading.1.1.19\lib\System.Threading.dll</HintPath>
84+
</Reference>
7385
</ItemGroup>
7486
<ItemGroup>
7587
<ProjectReference Include="..\nanoFramework.Aws.IoTCore.Devices\nanoFramework.Aws.IoTCore.Devices.nfproj" />

Tests/SignatureVersion4Tests.cs

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using nanoFramework.TestFramework;
77
using System;
88
using System.Collections;
9-
using nanoFramework.Aws.SignatureVersion4;
109

1110
namespace nanoFramework.Aws.SignatureVersion4.Tests
1211
{
@@ -16,47 +15,71 @@ namespace nanoFramework.Aws.SignatureVersion4.Tests
1615
[TestClass]
1716
public class SignatureVersion4Tests
1817
{
18+
1919
[TestMethod]
20-
public void check_CanonicalizeHeaderNames_when_order_is_canonical_already() // using an already ordered list
20+
public void check_SignerForQueryParameters_ComputeSignature_Get_iotGateway_generated_correctly()
2121
{
22-
//// Given that: SigV4 seems to require a header that is canonicalized
23-
//var headers = new Hashtable();
24-
//headers.Add("aTest", "atestval");
25-
//headers.Add("Atest2", "Atest2val");
26-
//headers.Add("btest", "btestval");
27-
//headers.Add("ctest", "ctestval");
22+
// See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
23+
// https://github.com/aws/aws-sdk-java-v2/blob/master/core/auth/src/test/java/software/amazon/awssdk/auth/signer/Aws4SignerTest.java
24+
// https://github.com/aws/aws-sdk-net/blob/master/sdk/test/Services/Signer/UnitTests/Generated/Endpoints/SignerEndpointProviderTests.cs
25+
26+
var v4signer = new SignerForQueryParameterAuth
27+
{
28+
EndpointUri = new System.Uri("https://test.us-east-1.amazonaws.com"),
29+
HttpMethod = "GET",
30+
Service = "iotdevicegateway",
31+
Region = "us-east-1"
32+
};
2833

29-
//Console.WriteLine(SignerBase.CanonicalizeHeaderNames(headers));
34+
Console.WriteLine(v4signer.ComputeSignature(new Hashtable(), "", "", "fakeAccessKey", "fakeSecret"));
3035

31-
// Expect "atest;atest2;btest;ctest"
36+
//http://demo.us-east-1.amazonaws.com
37+
//?X-Amz-Algorithm=AWS4-HMAC-SHA256
38+
//&X-Amz-Credential=<your-access-key-id>/20130721/us-east-1/iotdevicegateway/aws4_request
39+
//&X-Amz-Date=20130721T201207Z
40+
//&X-Amz-SignedHeaders=host
41+
//&X-Amz-Signature=<signature-value>
42+
43+
// replace '<your-access-key-id>' with "fakeAccessKey"
44+
// replace '<signature-value>'
45+
// replace '/' with '%2F' in url
46+
// ignore or inject datetime?
3247
}
3348

3449
[TestMethod]
35-
public void check_CanonicalizeHeaderNames_when_order_is_not_sorted()
50+
public void check_SignerForQueryParameters_ComputeSignature_Post_S3_generated_correctly()
3651
{
37-
//var headers = new Hashtable();
38-
//headers.Add("aTest", "atestval");
39-
//headers.Add("ctest", "ctestval");
40-
//headers.Add("btest", "btestval");
41-
//headers.Add("Atest2", "Atest2val");
52+
// See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
4253

43-
//Console.WriteLine(CanonicalizeHeaderNames(headers));
4454

45-
// Expect "atest;atest2;btest;ctest"
46-
}
55+
var v4signer = new SignerForQueryParameterAuth
56+
{
57+
EndpointUri = new System.Uri("https://test.us-east-1.amazonaws.com"),
58+
HttpMethod = "POST",
59+
Service = "s3",
60+
Region = "us-east-1"
61+
};
4762

63+
// FIXME: would fail!
64+
var sigparams = new Hashtable
65+
{
66+
{ "X-Amz-Expires", "86400" }
67+
};
4868

49-
[TestMethod]
50-
public void check_CanonicalizeHeaders_when_order_is_not_sorted()
51-
{
52-
//var headers = new Hashtable();
53-
//headers.Add("aTest", "atestval");
54-
//headers.Add("ctest", "ctestval");
55-
//headers.Add("btest", "btestval");
56-
//headers.Add("Atest2", "Atest2val");
57-
//Console.WriteLine(CanonicalizeHeaders(headers));
58-
59-
// Expect "atest:atestval\natest2:Atest2val\nbtest:btestval\nctest:ctestval\n"
69+
Console.WriteLine(v4signer.ComputeSignature(sigparams, "", "", "fakeAccessKey", "fakeSecret"));
70+
71+
//http://demo.us-east-1.amazonaws.com
72+
//?X-Amz-Algorithm=AWS4-HMAC-SHA256
73+
//&X-Amz-Credential=<your-access-key-id>/20130721/us-east-1/s3/aws4_request
74+
//&X-Amz-Date=20130721T201207Z
75+
//&X-Amz-Expires=86400
76+
//&X-Amz-SignedHeaders=host
77+
//&X-Amz-Signature=<signature-value>
78+
79+
// replace '<your-access-key-id>' with "fakeAccessKey"
80+
// replace '<signature-value>'
81+
// replace '/' with '%2F' in url
82+
// ignore or inject datetime?
6083
}
6184

6285
}

Tests/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.14.2" targetFramework="netnano1.0" />
44
<package id="nanoFramework.Json" version="2.2.99" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.Runtime.Events" version="1.11.6" targetFramework="netnano1.0" />
56
<package id="nanoFramework.System.Collections" version="1.5.18" targetFramework="netnano1.0" />
67
<package id="nanoFramework.System.IO.Streams" version="1.1.38" targetFramework="netnano1.0" />
8+
<package id="nanoFramework.System.Net" version="1.10.52" targetFramework="netnano1.0" />
9+
<package id="nanoFramework.System.Net.Http" version="1.5.99" targetFramework="netnano1.0" />
710
<package id="nanoFramework.System.Text" version="1.2.37" targetFramework="netnano1.0" />
11+
<package id="nanoFramework.System.Threading" version="1.1.19" targetFramework="netnano1.0" />
812
<package id="nanoFramework.TestFramework" version="2.1.85" targetFramework="netnano1.0" developmentDependency="true" />
913
</packages>

Tests/packages.lock.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"resolved": "2.2.99",
1515
"contentHash": "WWyvmtVDwrld8KYEeWAKGXcCTVMtkyuVaNtmrOB/WpRagCfE3i2IP776Ubrk5GXYCfI3Pta1uMqvV2N8WfaZBg=="
1616
},
17+
"nanoFramework.Runtime.Events": {
18+
"type": "Direct",
19+
"requested": "[1.11.6, 1.11.6]",
20+
"resolved": "1.11.6",
21+
"contentHash": "xkltRh/2xKaZ9zmPHbVr32s1k+e17AInUBhzxKKkUDicJKF39yzTShSklb1OL6DBER5z71SpkGLyl9IdMK9l6w=="
22+
},
1723
"nanoFramework.System.Collections": {
1824
"type": "Direct",
1925
"requested": "[1.5.18, 1.5.18]",
@@ -26,12 +32,30 @@
2632
"resolved": "1.1.38",
2733
"contentHash": "qEtu/lMDtr5kPKc939vO3uX8h+W0/+Qx2N3Zx005JxqGiL71e4ScecEyGPIp8v1MzRd9pkoxInUb6jOAh+eyXA=="
2834
},
35+
"nanoFramework.System.Net": {
36+
"type": "Direct",
37+
"requested": "[1.10.52, 1.10.52]",
38+
"resolved": "1.10.52",
39+
"contentHash": "XKxtB7DYUnuePJRgBi37kLLAOzVcGk4l3YU7MgHv5RlIdN0pECQiv/DJXVt1HiGBQc318Zn8Zrd4uO5Nelxrig=="
40+
},
41+
"nanoFramework.System.Net.Http": {
42+
"type": "Direct",
43+
"requested": "[1.5.99, 1.5.99]",
44+
"resolved": "1.5.99",
45+
"contentHash": "q611hrYfuZL+4n/JiFSguLK+0VYWiX34tQzgkC7mogpeNRobtAY9mgQe0urL++o1V1IKzit5W1bvUb2LN0ErWg=="
46+
},
2947
"nanoFramework.System.Text": {
3048
"type": "Direct",
3149
"requested": "[1.2.37, 1.2.37]",
3250
"resolved": "1.2.37",
3351
"contentHash": "ORgRq0HSynSBhlXRTHdhzZiOdq/nRhdnX+DeIGw56y9OSc8dvqUz6elm97Jz+4WQ6ikpvs5PFGINAa35kBebwQ=="
3452
},
53+
"nanoFramework.System.Threading": {
54+
"type": "Direct",
55+
"requested": "[1.1.19, 1.1.19]",
56+
"resolved": "1.1.19",
57+
"contentHash": "HjnY0DNoFnU+t1GiH8Wgf7pJCD9yMOcLVtAZXVAhstMKisVN/MDj9TvfXxZScRIz7ZDoZYUnb+Ixfl6rqyppvA=="
58+
},
3559
"nanoFramework.TestFramework": {
3660
"type": "Direct",
3761
"requested": "[2.1.85, 2.1.85]",

nanoFramework.Aws.IoTCore.Devices/AwsSignatureVersion4/SignerForAuthorizationHeader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public string ComputeSignature(IDictionary headers,
112112
canonicalizedHeaderNames,
113113
canonicalizedHeaders,
114114
bodyHash);
115-
Debug.WriteLine($"\nCanonicalRequest:\n{canonicalRequest}");
115+
Debug.WriteLine($"\nDEBUG-CanonicalRequest:\n{canonicalRequest}");
116116

117117
// generate a hash of the canonical request, to go into signature computation
118118
var canonicalRequestHashBytes
@@ -127,15 +127,15 @@ var canonicalRequestHashBytes
127127
stringToSign.Append($"{SCHEME}-{ALGORITHM}\n{dateTimeStamp}\n{scope}\n");
128128
stringToSign.Append(ToHexString(canonicalRequestHashBytes, true));
129129

130-
Debug.WriteLine($"\nStringToSign:\n{stringToSign}");
130+
Debug.WriteLine($"\nDEBUG-StringToSign:\n{stringToSign}");
131131

132132
// compute the signing key
133133
var kha = new HMACSHA256(DeriveSigningKey(awsSecretKey, Region, dateStamp, Service));
134134

135135
// compute the AWS4 signature and return it
136136
var signature = kha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
137137
var signatureString = ToHexString(signature, true);
138-
Debug.WriteLine($"\nSignature:\n{signatureString}");
138+
Debug.WriteLine($"\nDEBUG-Signature:\n{signatureString}");
139139

140140
var authString = new StringBuilder();
141141
authString.Append($"{SCHEME}-{ALGORITHM} ");
@@ -144,7 +144,7 @@ var canonicalRequestHashBytes
144144
authString.Append($"Signature={signatureString}");
145145

146146
var authorization = authString.ToString();
147-
Debug.WriteLine($"\nAuthorization:\n{authorization}");
147+
Debug.WriteLine($"\nDEBUG-Authorization:\n{authorization}");
148148

149149
return authorization;
150150
}

nanoFramework.Aws.IoTCore.Devices/AwsSignatureVersion4/SignerForChunkedUpload.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public string ComputeSignature(IDictionary headers,
147147
canonicalizedHeaderNames,
148148
canonicalizedHeaders,
149149
bodyHash);
150-
Debug.WriteLine($"\nCanonicalRequest:\n{canonicalRequest}");
150+
Debug.WriteLine($"\nDEBUG-CanonicalRequest:\n{canonicalRequest}");
151151

152152
// generate a hash of the canonical request, to go into signature computation
153153
var canonicalRequestHashBytes
@@ -162,7 +162,7 @@ var canonicalRequestHashBytes
162162
stringToSign.Append($"{SCHEME}-{ALGORITHM}\n{DateTimeStamp}\n{Scope}\n");
163163
stringToSign.Append(ToHexString(canonicalRequestHashBytes, true));
164164

165-
Debug.WriteLine($"\nStringToSign:\n{stringToSign}");
165+
Debug.WriteLine($"\nDEBUG-StringToSign:\n{stringToSign}");
166166

167167
// compute the signing key
168168
SigningKey = DeriveSigningKey(awsSecretKey, Region, dateStamp, Service);
@@ -172,7 +172,7 @@ var canonicalRequestHashBytes
172172
// compute the AWS4 signature and return it
173173
var signature = kha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
174174
var signatureString = ToHexString(signature, true);
175-
Debug.WriteLine($"\nSignature:\n{signatureString}");
175+
Debug.WriteLine($"\nDEBUG-Signature:\n{signatureString}");
176176

177177
// cache the computed signature ready for chunk 0 upload
178178
LastComputedSignature = signatureString;
@@ -184,7 +184,7 @@ var canonicalRequestHashBytes
184184
authString.Append($"Signature={signatureString}");
185185

186186
var authorization = authString.ToString();
187-
Debug.WriteLine($"\nAuthorization:\n{authorization}");
187+
Debug.WriteLine($"\nDEBUG-Authorization:\n{authorization}");
188188

189189
return authorization;
190190
}
@@ -217,7 +217,7 @@ public long CalculateChunkedContentLength(long originalLength, long chunkSize)
217217
+ (remainingBytes > 0 ? CalculateChunkHeaderLength(remainingBytes) : 0)
218218
+ CalculateChunkHeaderLength(0);
219219

220-
Debug.WriteLine($"\nComputed chunked content length for original length {originalLength} bytes, chunk size {chunkSize / 1024}KB is {chunkedContentLength} bytes");
220+
Debug.WriteLine($"\nDEBUG-Computed chunked content length for original length {originalLength} bytes, chunk size {chunkSize / 1024}KB is {chunkedContentLength} bytes");
221221
return chunkedContentLength;
222222
}
223223

@@ -300,15 +300,15 @@ public byte[] ConstructSignedChunk(long userDataLen, byte[] userData)
300300
ToHexString(CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(nonsigExtension)), true) + "\n" +
301301
ToHexString(CanonicalRequestHashAlgorithm.ComputeHash(dataToChunk), true);
302302

303-
Debug.WriteLine($"\nChunkStringToSign:\n{chunkStringToSign}");
303+
Debug.WriteLine($"\nDEBUG-ChunkStringToSign:\n{chunkStringToSign}");
304304

305305
// compute the V4 signature for the chunk
306306
var chunkSignature
307307
= ToHexString(ComputeKeyedHash(SigningKey,
308308
Encoding.UTF8.GetBytes(chunkStringToSign)),
309309
true);
310310

311-
Debug.WriteLine($"\nChunkSignature:\n{chunkSignature}");
311+
Debug.WriteLine($"\nDEBUG-ChunkSignature:\n{chunkSignature}");
312312

313313
// cache the signature to include with the next chunk's signature computation
314314
this.LastComputedSignature = chunkSignature;
@@ -319,7 +319,7 @@ var chunkSignature
319319
chunkHeader.Append(nonsigExtension + CHUNK_SIGNATURE_HEADER + chunkSignature);
320320
chunkHeader.Append(CLRF);
321321

322-
Debug.WriteLine($"\nChunkHeader:\n{chunkHeader}");
322+
Debug.WriteLine($"\nDEBUG-ChunkHeader:\n{chunkHeader}");
323323

324324
try
325325
{

nanoFramework.Aws.IoTCore.Devices/AwsSignatureVersion4/SignerForPOST.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public string ComputeSignature(IDictionary headers,
127127
canonicalizedHeaderNames,
128128
canonicalizedHeaders,
129129
bodyHash);
130-
Debug.WriteLine($"\nCanonicalRequest:\n{canonicalRequest}");
130+
//Debug.WriteLine($"\nDEBUG-CanonicalRequest:\n{canonicalRequest}");
131131

132132
// generate a hash of the canonical request, to go into signature computation
133133
var canonicalRequestHashBytes
@@ -142,15 +142,15 @@ var canonicalRequestHashBytes
142142
stringToSign.Append($"{SCHEME}-{ALGORITHM}\n{dateTimeStamp}\n{scope}\n");
143143
stringToSign.Append(ToHexString(canonicalRequestHashBytes, true));
144144

145-
Debug.WriteLine($"\nStringToSign:\n{stringToSign}");
145+
//Debug.WriteLine($"\nDEBUG-StringToSign:\n{stringToSign}");
146146

147147
// compute the signing key
148148
var kha = new HMACSHA256(DeriveSigningKey(awsSecretKey, Region, dateStamp, Service));
149149

150150
// compute the AWS4 signature and return it
151151
var signature = kha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
152152
var signatureString = ToHexString(signature, true);
153-
Debug.WriteLine($"\nSignature:\n{signatureString}");
153+
//Debug.WriteLine($"\nDEBUG-Signature:\n{signatureString}");
154154

155155
var authString = new StringBuilder();
156156
authString.Append($"{SCHEME}-{ALGORITHM} ");
@@ -159,7 +159,7 @@ var canonicalRequestHashBytes
159159
authString.Append($"Signature={signatureString}");
160160

161161
var authorization = authString.ToString();
162-
Debug.WriteLine($"\nAuthorization:\n{authorization}");
162+
//Debug.WriteLine($"\nDEBUG-Authorization:\n{authorization}");
163163

164164
return authorization;
165165
}

nanoFramework.Aws.IoTCore.Devices/AwsSignatureVersion4/SignerForQueryParameterAuth.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public string ComputeSignature(IDictionary headers,
125125
canonicalizedHeaderNames,
126126
canonicalizedHeaders,
127127
bodyHash);
128-
Debug.WriteLine($"\nCanonicalRequest:\n{canonicalRequest}");
128+
//Debug.WriteLine($"\nDEBUG-CanonicalRequest:\n{canonicalRequest}");
129129

130130
byte[] canonicalRequestHashBytes
131131
= CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(canonicalRequest));
@@ -136,7 +136,7 @@ byte[] canonicalRequestHashBytes
136136
stringToSign.Append($"{SCHEME}-{ALGORITHM}\n{dateTimeStamp}\n{scope}\n");
137137
stringToSign.Append(ToHexString(canonicalRequestHashBytes, true));
138138

139-
Debug.WriteLine($"\nStringToSign:\n{stringToSign}");
139+
//Debug.WriteLine($"\nDEBUG-StringToSign:\n{stringToSign}");
140140

141141
// compute the multi-stage signing key
142142
var kha = new HMACSHA256(DeriveSigningKey(awsSecretKey, Region, dateStamp, Service));
@@ -145,7 +145,7 @@ byte[] canonicalRequestHashBytes
145145
// user to be embedded in the request as needed
146146
var signature = kha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
147147
var signatureString = ToHexString(signature, true);
148-
Debug.WriteLine($"\nSignature:\n{signatureString}");
148+
//Debug.WriteLine($"\nDEBUG-Signature:\n{signatureString}");
149149

150150
// form up the authorization parameters for the caller to place in the query string
151151
var authString = new StringBuilder();
@@ -167,7 +167,7 @@ byte[] canonicalRequestHashBytes
167167
authString.Append($"&{X_Amz_Signature}={signatureString}");
168168

169169
var authorization = authString.ToString();
170-
Debug.WriteLine($"\nAuthorization:\n{authorization}");
170+
//Debug.WriteLine($"\nDEBUG-Authorization:\n{authorization}");
171171

172172
return authorization;
173173
}

0 commit comments

Comments
 (0)